Commit | Line | Data |
---|---|---|
24c8dbbb DH |
1 | /* 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 | ||
24c8dbbb DH |
13 | #include <linux/module.h> |
14 | #include <linux/init.h> | |
e8edc6e0 | 15 | #include <linux/sched.h> |
24c8dbbb DH |
16 | #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> | |
0896a725 | 26 | #include <linux/sunrpc/xprtsock.h> |
2cf7ff7a | 27 | #include <linux/sunrpc/xprtrdma.h> |
24c8dbbb DH |
28 | #include <linux/nfs_fs.h> |
29 | #include <linux/nfs_mount.h> | |
30 | #include <linux/nfs4_mount.h> | |
31 | #include <linux/lockd/bind.h> | |
24c8dbbb DH |
32 | #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> | |
3b0d3f93 TM |
37 | #include <linux/in6.h> |
38 | #include <net/ipv6.h> | |
24c8dbbb | 39 | #include <linux/nfs_xdr.h> |
0b5b7ae0 | 40 | #include <linux/sunrpc/bc_xprt.h> |
24c8dbbb DH |
41 | |
42 | #include <asm/system.h> | |
43 | ||
44 | #include "nfs4_fs.h" | |
45 | #include "callback.h" | |
46 | #include "delegation.h" | |
47 | #include "iostat.h" | |
48 | #include "internal.h" | |
14727281 | 49 | #include "fscache.h" |
24c8dbbb DH |
50 | |
51 | #define NFSDBG_FACILITY NFSDBG_CLIENT | |
52 | ||
53 | static DEFINE_SPINLOCK(nfs_client_lock); | |
54 | static LIST_HEAD(nfs_client_list); | |
54ceac45 | 55 | static LIST_HEAD(nfs_volume_list); |
24c8dbbb DH |
56 | static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq); |
57 | ||
5006a76c DH |
58 | /* |
59 | * RPC cruft for NFS | |
60 | */ | |
61 | static struct rpc_version *nfs_version[5] = { | |
62 | [2] = &nfs_version2, | |
63 | #ifdef CONFIG_NFS_V3 | |
64 | [3] = &nfs_version3, | |
65 | #endif | |
66 | #ifdef CONFIG_NFS_V4 | |
67 | [4] = &nfs_version4, | |
68 | #endif | |
69 | }; | |
70 | ||
71 | struct rpc_program nfs_program = { | |
72 | .name = "nfs", | |
73 | .number = NFS_PROGRAM, | |
74 | .nrvers = ARRAY_SIZE(nfs_version), | |
75 | .version = nfs_version, | |
76 | .stats = &nfs_rpcstat, | |
77 | .pipe_dir_name = "/nfs", | |
78 | }; | |
79 | ||
80 | struct rpc_stat nfs_rpcstat = { | |
81 | .program = &nfs_program | |
82 | }; | |
83 | ||
84 | ||
85 | #ifdef CONFIG_NFS_V3_ACL | |
86 | static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program }; | |
87 | static struct rpc_version * nfsacl_version[] = { | |
88 | [3] = &nfsacl_version3, | |
89 | }; | |
90 | ||
91 | struct rpc_program nfsacl_program = { | |
92 | .name = "nfsacl", | |
93 | .number = NFS_ACL_PROGRAM, | |
94 | .nrvers = ARRAY_SIZE(nfsacl_version), | |
95 | .version = nfsacl_version, | |
96 | .stats = &nfsacl_rpcstat, | |
97 | }; | |
98 | #endif /* CONFIG_NFS_V3_ACL */ | |
99 | ||
3a498026 TM |
100 | struct nfs_client_initdata { |
101 | const char *hostname; | |
d7422c47 | 102 | const struct sockaddr *addr; |
6e4cffd7 | 103 | size_t addrlen; |
40c55319 | 104 | const struct nfs_rpc_ops *rpc_ops; |
59dca3b2 | 105 | int proto; |
5aae4a9a | 106 | u32 minorversion; |
3a498026 TM |
107 | }; |
108 | ||
24c8dbbb DH |
109 | /* |
110 | * Allocate a shared client record | |
111 | * | |
112 | * Since these are allocated/deallocated very rarely, we don't | |
113 | * bother putting them in a slab cache... | |
114 | */ | |
3a498026 | 115 | static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init) |
24c8dbbb DH |
116 | { |
117 | struct nfs_client *clp; | |
7c67db3a | 118 | struct rpc_cred *cred; |
a21bdd9b | 119 | int err = -ENOMEM; |
24c8dbbb DH |
120 | |
121 | if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL) | |
122 | goto error_0; | |
123 | ||
40c55319 TM |
124 | clp->rpc_ops = cl_init->rpc_ops; |
125 | ||
24c8dbbb DH |
126 | atomic_set(&clp->cl_count, 1); |
127 | clp->cl_cons_state = NFS_CS_INITING; | |
128 | ||
6e4cffd7 CL |
129 | memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen); |
130 | clp->cl_addrlen = cl_init->addrlen; | |
24c8dbbb | 131 | |
3a498026 | 132 | if (cl_init->hostname) { |
a21bdd9b | 133 | err = -ENOMEM; |
3a498026 | 134 | clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL); |
24c8dbbb | 135 | if (!clp->cl_hostname) |
71468513 | 136 | goto error_cleanup; |
24c8dbbb DH |
137 | } |
138 | ||
139 | INIT_LIST_HEAD(&clp->cl_superblocks); | |
140 | clp->cl_rpcclient = ERR_PTR(-EINVAL); | |
141 | ||
59dca3b2 TM |
142 | clp->cl_proto = cl_init->proto; |
143 | ||
24c8dbbb | 144 | #ifdef CONFIG_NFS_V4 |
24c8dbbb | 145 | INIT_LIST_HEAD(&clp->cl_delegations); |
24c8dbbb | 146 | spin_lock_init(&clp->cl_lock); |
65f27f38 | 147 | INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state); |
24c8dbbb DH |
148 | rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client"); |
149 | clp->cl_boot_time = CURRENT_TIME; | |
150 | clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED; | |
5aae4a9a | 151 | clp->cl_minorversion = cl_init->minorversion; |
24c8dbbb | 152 | #endif |
7c67db3a TM |
153 | cred = rpc_lookup_machine_cred(); |
154 | if (!IS_ERR(cred)) | |
155 | clp->cl_machine_cred = cred; | |
24c8dbbb | 156 | |
14727281 DH |
157 | nfs_fscache_get_client_cookie(clp); |
158 | ||
24c8dbbb DH |
159 | return clp; |
160 | ||
71468513 | 161 | error_cleanup: |
24c8dbbb DH |
162 | kfree(clp); |
163 | error_0: | |
a21bdd9b | 164 | return ERR_PTR(err); |
24c8dbbb DH |
165 | } |
166 | ||
5dd3177a TM |
167 | static void nfs4_shutdown_client(struct nfs_client *clp) |
168 | { | |
169 | #ifdef CONFIG_NFS_V4 | |
170 | if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state)) | |
171 | nfs4_kill_renewd(clp); | |
9f958ab8 | 172 | BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners)); |
5dd3177a TM |
173 | if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state)) |
174 | nfs_idmap_delete(clp); | |
f6a1cc89 TM |
175 | |
176 | rpc_destroy_wait_queue(&clp->cl_rpcwaitq); | |
5dd3177a TM |
177 | #endif |
178 | } | |
179 | ||
9bdaa86d BH |
180 | /* |
181 | * Destroy the NFS4 callback service | |
182 | */ | |
183 | static void nfs4_destroy_callback(struct nfs_client *clp) | |
184 | { | |
185 | #ifdef CONFIG_NFS_V4 | |
186 | if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state)) | |
e82dc22d | 187 | nfs_callback_down(clp->cl_minorversion); |
9bdaa86d BH |
188 | #endif /* CONFIG_NFS_V4 */ |
189 | } | |
190 | ||
557134a3 AA |
191 | /* |
192 | * Clears/puts all minor version specific parts from an nfs_client struct | |
193 | * reverting it to minorversion 0. | |
194 | */ | |
195 | static void nfs4_clear_client_minor_version(struct nfs_client *clp) | |
196 | { | |
197 | #ifdef CONFIG_NFS_V4_1 | |
198 | if (nfs4_has_session(clp)) { | |
199 | nfs4_destroy_session(clp->cl_session); | |
200 | clp->cl_session = NULL; | |
201 | } | |
cccef3b9 AA |
202 | |
203 | clp->cl_call_sync = _nfs4_call_sync; | |
557134a3 | 204 | #endif /* CONFIG_NFS_V4_1 */ |
71468513 BH |
205 | |
206 | nfs4_destroy_callback(clp); | |
557134a3 AA |
207 | } |
208 | ||
24c8dbbb DH |
209 | /* |
210 | * Destroy a shared client record | |
211 | */ | |
212 | static void nfs_free_client(struct nfs_client *clp) | |
213 | { | |
40c55319 | 214 | dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version); |
24c8dbbb | 215 | |
557134a3 | 216 | nfs4_clear_client_minor_version(clp); |
5dd3177a | 217 | nfs4_shutdown_client(clp); |
24c8dbbb | 218 | |
14727281 DH |
219 | nfs_fscache_release_client_cookie(clp); |
220 | ||
24c8dbbb DH |
221 | /* -EIO all pending I/O */ |
222 | if (!IS_ERR(clp->cl_rpcclient)) | |
223 | rpc_shutdown_client(clp->cl_rpcclient); | |
224 | ||
7c67db3a TM |
225 | if (clp->cl_machine_cred != NULL) |
226 | put_rpccred(clp->cl_machine_cred); | |
227 | ||
24c8dbbb DH |
228 | kfree(clp->cl_hostname); |
229 | kfree(clp); | |
230 | ||
231 | dprintk("<-- nfs_free_client()\n"); | |
232 | } | |
233 | ||
234 | /* | |
235 | * Release a reference to a shared client record | |
236 | */ | |
237 | void nfs_put_client(struct nfs_client *clp) | |
238 | { | |
27ba8512 DH |
239 | if (!clp) |
240 | return; | |
241 | ||
24c8dbbb DH |
242 | dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count)); |
243 | ||
244 | if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) { | |
245 | list_del(&clp->cl_share_link); | |
246 | spin_unlock(&nfs_client_lock); | |
247 | ||
248 | BUG_ON(!list_empty(&clp->cl_superblocks)); | |
249 | ||
250 | nfs_free_client(clp); | |
251 | } | |
252 | } | |
253 | ||
9082a5cc | 254 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
9f4c899c TM |
255 | /* |
256 | * Test if two ip6 socket addresses refer to the same socket by | |
257 | * comparing relevant fields. The padding bytes specifically, are not | |
258 | * compared. sin6_flowinfo is not compared because it only affects QoS | |
259 | * and sin6_scope_id is only compared if the address is "link local" | |
260 | * because "link local" addresses need only be unique to a specific | |
261 | * link. Conversely, ordinary unicast addresses might have different | |
262 | * sin6_scope_id. | |
263 | * | |
264 | * The caller should ensure both socket addresses are AF_INET6. | |
265 | */ | |
3c8c45df CL |
266 | static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1, |
267 | const struct sockaddr *sa2) | |
9f4c899c | 268 | { |
3c8c45df CL |
269 | const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1; |
270 | const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2; | |
9f4c899c | 271 | |
3c8c45df CL |
272 | if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL && |
273 | sin1->sin6_scope_id != sin2->sin6_scope_id) | |
9f4c899c | 274 | return 0; |
3b0d3f93 | 275 | |
3c8c45df | 276 | return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr); |
3b0d3f93 | 277 | } |
3c8c45df CL |
278 | #else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */ |
279 | static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1, | |
280 | const struct sockaddr *sa2) | |
9f4c899c TM |
281 | { |
282 | return 0; | |
283 | } | |
9082a5cc | 284 | #endif |
3b0d3f93 | 285 | |
d7371c41 ID |
286 | /* |
287 | * Test if two ip4 socket addresses refer to the same socket, by | |
288 | * comparing relevant fields. The padding bytes specifically, are | |
289 | * not compared. | |
290 | * | |
291 | * The caller should ensure both socket addresses are AF_INET. | |
292 | */ | |
3c8c45df CL |
293 | static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1, |
294 | const struct sockaddr *sa2) | |
295 | { | |
296 | const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1; | |
297 | const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2; | |
298 | ||
299 | return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr; | |
300 | } | |
301 | ||
302 | static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1, | |
303 | const struct sockaddr *sa2) | |
304 | { | |
305 | const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1; | |
306 | const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2; | |
307 | ||
308 | return nfs_sockaddr_match_ipaddr6(sa1, sa2) && | |
309 | (sin1->sin6_port == sin2->sin6_port); | |
310 | } | |
311 | ||
9f4c899c TM |
312 | static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1, |
313 | const struct sockaddr *sa2) | |
d7371c41 | 314 | { |
3c8c45df CL |
315 | const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1; |
316 | const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2; | |
9f4c899c | 317 | |
3c8c45df CL |
318 | return nfs_sockaddr_match_ipaddr4(sa1, sa2) && |
319 | (sin1->sin_port == sin2->sin_port); | |
320 | } | |
321 | ||
322 | /* | |
323 | * Test if two socket addresses represent the same actual socket, | |
324 | * by comparing (only) relevant fields, excluding the port number. | |
325 | */ | |
326 | static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1, | |
327 | const struct sockaddr *sa2) | |
328 | { | |
329 | if (sa1->sa_family != sa2->sa_family) | |
d7371c41 | 330 | return 0; |
3c8c45df CL |
331 | |
332 | switch (sa1->sa_family) { | |
333 | case AF_INET: | |
334 | return nfs_sockaddr_match_ipaddr4(sa1, sa2); | |
335 | case AF_INET6: | |
336 | return nfs_sockaddr_match_ipaddr6(sa1, sa2); | |
337 | } | |
338 | return 0; | |
d7371c41 ID |
339 | } |
340 | ||
d7371c41 ID |
341 | /* |
342 | * Test if two socket addresses represent the same actual socket, | |
3c8c45df | 343 | * by comparing (only) relevant fields, including the port number. |
d7371c41 ID |
344 | */ |
345 | static int nfs_sockaddr_cmp(const struct sockaddr *sa1, | |
346 | const struct sockaddr *sa2) | |
347 | { | |
348 | if (sa1->sa_family != sa2->sa_family) | |
349 | return 0; | |
350 | ||
351 | switch (sa1->sa_family) { | |
352 | case AF_INET: | |
9f4c899c | 353 | return nfs_sockaddr_cmp_ip4(sa1, sa2); |
d7371c41 | 354 | case AF_INET6: |
9f4c899c | 355 | return nfs_sockaddr_cmp_ip6(sa1, sa2); |
d7371c41 ID |
356 | } |
357 | return 0; | |
358 | } | |
359 | ||
24c8dbbb | 360 | /* |
c81468a1 TM |
361 | * Find a client by IP address and protocol version |
362 | * - returns NULL if no such client | |
24c8dbbb | 363 | */ |
ff052645 | 364 | struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion) |
24c8dbbb DH |
365 | { |
366 | struct nfs_client *clp; | |
367 | ||
c81468a1 | 368 | spin_lock(&nfs_client_lock); |
24c8dbbb | 369 | list_for_each_entry(clp, &nfs_client_list, cl_share_link) { |
3b0d3f93 TM |
370 | struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; |
371 | ||
13bbc06a | 372 | /* Don't match clients that failed to initialise properly */ |
76db6d95 AA |
373 | if (!(clp->cl_cons_state == NFS_CS_READY || |
374 | clp->cl_cons_state == NFS_CS_SESSION_INITING)) | |
13bbc06a TM |
375 | continue; |
376 | ||
24c8dbbb | 377 | /* Different NFS versions cannot share the same nfs_client */ |
40c55319 | 378 | if (clp->rpc_ops->version != nfsversion) |
24c8dbbb DH |
379 | continue; |
380 | ||
c81468a1 | 381 | /* Match only the IP address, not the port number */ |
3b0d3f93 | 382 | if (!nfs_sockaddr_match_ipaddr(addr, clap)) |
24c8dbbb DH |
383 | continue; |
384 | ||
c81468a1 TM |
385 | atomic_inc(&clp->cl_count); |
386 | spin_unlock(&nfs_client_lock); | |
387 | return clp; | |
24c8dbbb | 388 | } |
c81468a1 | 389 | spin_unlock(&nfs_client_lock); |
24c8dbbb | 390 | return NULL; |
24c8dbbb DH |
391 | } |
392 | ||
3fbd67ad TM |
393 | /* |
394 | * Find a client by IP address and protocol version | |
395 | * - returns NULL if no such client | |
396 | */ | |
397 | struct nfs_client *nfs_find_client_next(struct nfs_client *clp) | |
398 | { | |
399 | struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr; | |
400 | u32 nfsvers = clp->rpc_ops->version; | |
401 | ||
402 | spin_lock(&nfs_client_lock); | |
403 | list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) { | |
404 | struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; | |
405 | ||
406 | /* Don't match clients that failed to initialise properly */ | |
407 | if (clp->cl_cons_state != NFS_CS_READY) | |
408 | continue; | |
409 | ||
410 | /* Different NFS versions cannot share the same nfs_client */ | |
411 | if (clp->rpc_ops->version != nfsvers) | |
412 | continue; | |
413 | ||
3fbd67ad TM |
414 | /* Match only the IP address, not the port number */ |
415 | if (!nfs_sockaddr_match_ipaddr(sap, clap)) | |
416 | continue; | |
417 | ||
418 | atomic_inc(&clp->cl_count); | |
419 | spin_unlock(&nfs_client_lock); | |
420 | return clp; | |
421 | } | |
422 | spin_unlock(&nfs_client_lock); | |
423 | return NULL; | |
424 | } | |
425 | ||
24c8dbbb | 426 | /* |
c81468a1 TM |
427 | * Find an nfs_client on the list that matches the initialisation data |
428 | * that is supplied. | |
24c8dbbb | 429 | */ |
c81468a1 | 430 | static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data) |
24c8dbbb DH |
431 | { |
432 | struct nfs_client *clp; | |
d7371c41 | 433 | const struct sockaddr *sap = data->addr; |
24c8dbbb | 434 | |
c81468a1 | 435 | list_for_each_entry(clp, &nfs_client_list, cl_share_link) { |
d7371c41 | 436 | const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; |
c81468a1 TM |
437 | /* Don't match clients that failed to initialise properly */ |
438 | if (clp->cl_cons_state < 0) | |
439 | continue; | |
440 | ||
441 | /* Different NFS versions cannot share the same nfs_client */ | |
40c55319 | 442 | if (clp->rpc_ops != data->rpc_ops) |
c81468a1 TM |
443 | continue; |
444 | ||
59dca3b2 TM |
445 | if (clp->cl_proto != data->proto) |
446 | continue; | |
5aae4a9a BH |
447 | /* Match nfsv4 minorversion */ |
448 | if (clp->cl_minorversion != data->minorversion) | |
449 | continue; | |
c81468a1 | 450 | /* Match the full socket address */ |
d7371c41 | 451 | if (!nfs_sockaddr_cmp(sap, clap)) |
c81468a1 TM |
452 | continue; |
453 | ||
454 | atomic_inc(&clp->cl_count); | |
455 | return clp; | |
13bbc06a | 456 | } |
c81468a1 | 457 | return NULL; |
24c8dbbb DH |
458 | } |
459 | ||
460 | /* | |
461 | * Look up a client by IP address and protocol version | |
462 | * - creates a new record if one doesn't yet exist | |
463 | */ | |
3a498026 | 464 | static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init) |
24c8dbbb DH |
465 | { |
466 | struct nfs_client *clp, *new = NULL; | |
467 | int error; | |
468 | ||
d7422c47 CL |
469 | dprintk("--> nfs_get_client(%s,v%u)\n", |
470 | cl_init->hostname ?: "", cl_init->rpc_ops->version); | |
24c8dbbb DH |
471 | |
472 | /* see if the client already exists */ | |
473 | do { | |
474 | spin_lock(&nfs_client_lock); | |
475 | ||
c81468a1 | 476 | clp = nfs_match_client(cl_init); |
24c8dbbb DH |
477 | if (clp) |
478 | goto found_client; | |
479 | if (new) | |
480 | goto install_client; | |
481 | ||
482 | spin_unlock(&nfs_client_lock); | |
483 | ||
3a498026 | 484 | new = nfs_alloc_client(cl_init); |
a21bdd9b | 485 | } while (!IS_ERR(new)); |
24c8dbbb | 486 | |
a21bdd9b CL |
487 | dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new)); |
488 | return new; | |
24c8dbbb DH |
489 | |
490 | /* install a new client and return with it unready */ | |
491 | install_client: | |
492 | clp = new; | |
493 | list_add(&clp->cl_share_link, &nfs_client_list); | |
494 | spin_unlock(&nfs_client_lock); | |
495 | dprintk("--> nfs_get_client() = %p [new]\n", clp); | |
496 | return clp; | |
497 | ||
498 | /* found an existing client | |
499 | * - make sure it's ready before returning | |
500 | */ | |
501 | found_client: | |
502 | spin_unlock(&nfs_client_lock); | |
503 | ||
504 | if (new) | |
505 | nfs_free_client(new); | |
506 | ||
150030b7 | 507 | error = wait_event_killable(nfs_client_active_wq, |
76db6d95 | 508 | clp->cl_cons_state < NFS_CS_INITING); |
0bae89ec TM |
509 | if (error < 0) { |
510 | nfs_put_client(clp); | |
511 | return ERR_PTR(-ERESTARTSYS); | |
24c8dbbb DH |
512 | } |
513 | ||
514 | if (clp->cl_cons_state < NFS_CS_READY) { | |
515 | error = clp->cl_cons_state; | |
516 | nfs_put_client(clp); | |
517 | return ERR_PTR(error); | |
518 | } | |
519 | ||
54ceac45 DH |
520 | BUG_ON(clp->cl_cons_state != NFS_CS_READY); |
521 | ||
24c8dbbb DH |
522 | dprintk("--> nfs_get_client() = %p [share]\n", clp); |
523 | return clp; | |
524 | } | |
525 | ||
526 | /* | |
527 | * Mark a server as ready or failed | |
528 | */ | |
76db6d95 | 529 | void nfs_mark_client_ready(struct nfs_client *clp, int state) |
24c8dbbb DH |
530 | { |
531 | clp->cl_cons_state = state; | |
532 | wake_up_all(&nfs_client_active_wq); | |
533 | } | |
5006a76c | 534 | |
008f55d0 BH |
535 | /* |
536 | * With sessions, the client is not marked ready until after a | |
537 | * successful EXCHANGE_ID and CREATE_SESSION. | |
538 | * | |
539 | * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate | |
540 | * other versions of NFS can be tried. | |
541 | */ | |
542 | int nfs4_check_client_ready(struct nfs_client *clp) | |
543 | { | |
544 | if (!nfs4_has_session(clp)) | |
545 | return 0; | |
546 | if (clp->cl_cons_state < NFS_CS_READY) | |
547 | return -EPROTONOSUPPORT; | |
548 | return 0; | |
549 | } | |
550 | ||
5006a76c DH |
551 | /* |
552 | * Initialise the timeout values for a connection | |
553 | */ | |
554 | static void nfs_init_timeout_values(struct rpc_timeout *to, int proto, | |
555 | unsigned int timeo, unsigned int retrans) | |
556 | { | |
557 | to->to_initval = timeo * HZ / 10; | |
558 | to->to_retries = retrans; | |
5006a76c DH |
559 | |
560 | switch (proto) { | |
0896a725 | 561 | case XPRT_TRANSPORT_TCP: |
2cf7ff7a | 562 | case XPRT_TRANSPORT_RDMA: |
259875ef TM |
563 | if (to->to_retries == 0) |
564 | to->to_retries = NFS_DEF_TCP_RETRANS; | |
7a3e3e18 | 565 | if (to->to_initval == 0) |
259875ef | 566 | to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10; |
5006a76c DH |
567 | if (to->to_initval > NFS_MAX_TCP_TIMEOUT) |
568 | to->to_initval = NFS_MAX_TCP_TIMEOUT; | |
569 | to->to_increment = to->to_initval; | |
570 | to->to_maxval = to->to_initval + (to->to_increment * to->to_retries); | |
7a3e3e18 TM |
571 | if (to->to_maxval > NFS_MAX_TCP_TIMEOUT) |
572 | to->to_maxval = NFS_MAX_TCP_TIMEOUT; | |
573 | if (to->to_maxval < to->to_initval) | |
574 | to->to_maxval = to->to_initval; | |
5006a76c DH |
575 | to->to_exponential = 0; |
576 | break; | |
0896a725 | 577 | case XPRT_TRANSPORT_UDP: |
259875ef TM |
578 | if (to->to_retries == 0) |
579 | to->to_retries = NFS_DEF_UDP_RETRANS; | |
5006a76c | 580 | if (!to->to_initval) |
259875ef | 581 | to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10; |
5006a76c DH |
582 | if (to->to_initval > NFS_MAX_UDP_TIMEOUT) |
583 | to->to_initval = NFS_MAX_UDP_TIMEOUT; | |
584 | to->to_maxval = NFS_MAX_UDP_TIMEOUT; | |
585 | to->to_exponential = 1; | |
586 | break; | |
259875ef TM |
587 | default: |
588 | BUG(); | |
5006a76c DH |
589 | } |
590 | } | |
591 | ||
592 | /* | |
593 | * Create an RPC client handle | |
594 | */ | |
59dca3b2 | 595 | static int nfs_create_rpc_client(struct nfs_client *clp, |
33170233 TM |
596 | const struct rpc_timeout *timeparms, |
597 | rpc_authflavor_t flavor, | |
4a01b8a4 | 598 | int discrtry, int noresvport) |
5006a76c | 599 | { |
5006a76c | 600 | struct rpc_clnt *clnt = NULL; |
41877d20 | 601 | struct rpc_create_args args = { |
59dca3b2 | 602 | .protocol = clp->cl_proto, |
41877d20 | 603 | .address = (struct sockaddr *)&clp->cl_addr, |
6e4cffd7 | 604 | .addrsize = clp->cl_addrlen, |
33170233 | 605 | .timeout = timeparms, |
41877d20 CL |
606 | .servername = clp->cl_hostname, |
607 | .program = &nfs_program, | |
608 | .version = clp->rpc_ops->version, | |
609 | .authflavor = flavor, | |
610 | }; | |
5006a76c | 611 | |
4a01b8a4 CL |
612 | if (discrtry) |
613 | args.flags |= RPC_CLNT_CREATE_DISCRTRY; | |
614 | if (noresvport) | |
615 | args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; | |
616 | ||
5006a76c DH |
617 | if (!IS_ERR(clp->cl_rpcclient)) |
618 | return 0; | |
619 | ||
41877d20 | 620 | clnt = rpc_create(&args); |
5006a76c DH |
621 | if (IS_ERR(clnt)) { |
622 | dprintk("%s: cannot create RPC client. Error = %ld\n", | |
3110ff80 | 623 | __func__, PTR_ERR(clnt)); |
5006a76c DH |
624 | return PTR_ERR(clnt); |
625 | } | |
626 | ||
5006a76c DH |
627 | clp->cl_rpcclient = clnt; |
628 | return 0; | |
629 | } | |
54ceac45 DH |
630 | |
631 | /* | |
632 | * Version 2 or 3 client destruction | |
633 | */ | |
634 | static void nfs_destroy_server(struct nfs_server *server) | |
635 | { | |
54ceac45 | 636 | if (!(server->flags & NFS_MOUNT_NONLM)) |
9289e7f9 | 637 | nlmclnt_done(server->nlm_host); |
54ceac45 DH |
638 | } |
639 | ||
640 | /* | |
641 | * Version 2 or 3 lockd setup | |
642 | */ | |
643 | static int nfs_start_lockd(struct nfs_server *server) | |
644 | { | |
9289e7f9 CL |
645 | struct nlm_host *host; |
646 | struct nfs_client *clp = server->nfs_client; | |
883bb163 CL |
647 | struct nlmclnt_initdata nlm_init = { |
648 | .hostname = clp->cl_hostname, | |
649 | .address = (struct sockaddr *)&clp->cl_addr, | |
650 | .addrlen = clp->cl_addrlen, | |
651 | .protocol = server->flags & NFS_MOUNT_TCP ? | |
652 | IPPROTO_TCP : IPPROTO_UDP, | |
653 | .nfs_version = clp->rpc_ops->version, | |
0cb2659b CL |
654 | .noresvport = server->flags & NFS_MOUNT_NORESVPORT ? |
655 | 1 : 0, | |
883bb163 | 656 | }; |
54ceac45 | 657 | |
883bb163 | 658 | if (nlm_init.nfs_version > 3) |
9289e7f9 | 659 | return 0; |
54ceac45 | 660 | if (server->flags & NFS_MOUNT_NONLM) |
9289e7f9 CL |
661 | return 0; |
662 | ||
883bb163 | 663 | host = nlmclnt_init(&nlm_init); |
9289e7f9 CL |
664 | if (IS_ERR(host)) |
665 | return PTR_ERR(host); | |
666 | ||
667 | server->nlm_host = host; | |
668 | server->destroy = nfs_destroy_server; | |
669 | return 0; | |
54ceac45 DH |
670 | } |
671 | ||
672 | /* | |
673 | * Initialise an NFSv3 ACL client connection | |
674 | */ | |
675 | #ifdef CONFIG_NFS_V3_ACL | |
676 | static void nfs_init_server_aclclient(struct nfs_server *server) | |
677 | { | |
40c55319 | 678 | if (server->nfs_client->rpc_ops->version != 3) |
54ceac45 DH |
679 | goto out_noacl; |
680 | if (server->flags & NFS_MOUNT_NOACL) | |
681 | goto out_noacl; | |
682 | ||
683 | server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3); | |
684 | if (IS_ERR(server->client_acl)) | |
685 | goto out_noacl; | |
686 | ||
687 | /* No errors! Assume that Sun nfsacls are supported */ | |
688 | server->caps |= NFS_CAP_ACLS; | |
689 | return; | |
690 | ||
691 | out_noacl: | |
692 | server->caps &= ~NFS_CAP_ACLS; | |
693 | } | |
694 | #else | |
695 | static inline void nfs_init_server_aclclient(struct nfs_server *server) | |
696 | { | |
697 | server->flags &= ~NFS_MOUNT_NOACL; | |
698 | server->caps &= ~NFS_CAP_ACLS; | |
699 | } | |
700 | #endif | |
701 | ||
702 | /* | |
703 | * Create a general RPC client | |
704 | */ | |
33170233 TM |
705 | static int nfs_init_server_rpcclient(struct nfs_server *server, |
706 | const struct rpc_timeout *timeo, | |
707 | rpc_authflavor_t pseudoflavour) | |
54ceac45 DH |
708 | { |
709 | struct nfs_client *clp = server->nfs_client; | |
710 | ||
711 | server->client = rpc_clone_client(clp->cl_rpcclient); | |
712 | if (IS_ERR(server->client)) { | |
3110ff80 | 713 | dprintk("%s: couldn't create rpc_client!\n", __func__); |
54ceac45 DH |
714 | return PTR_ERR(server->client); |
715 | } | |
716 | ||
33170233 TM |
717 | memcpy(&server->client->cl_timeout_default, |
718 | timeo, | |
719 | sizeof(server->client->cl_timeout_default)); | |
720 | server->client->cl_timeout = &server->client->cl_timeout_default; | |
721 | ||
54ceac45 DH |
722 | if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) { |
723 | struct rpc_auth *auth; | |
724 | ||
725 | auth = rpcauth_create(pseudoflavour, server->client); | |
726 | if (IS_ERR(auth)) { | |
3110ff80 | 727 | dprintk("%s: couldn't create credcache!\n", __func__); |
54ceac45 DH |
728 | return PTR_ERR(auth); |
729 | } | |
730 | } | |
731 | server->client->cl_softrtry = 0; | |
732 | if (server->flags & NFS_MOUNT_SOFT) | |
733 | server->client->cl_softrtry = 1; | |
734 | ||
54ceac45 DH |
735 | return 0; |
736 | } | |
737 | ||
738 | /* | |
739 | * Initialise an NFS2 or NFS3 client | |
740 | */ | |
2283f8d6 | 741 | static int nfs_init_client(struct nfs_client *clp, |
33170233 | 742 | const struct rpc_timeout *timeparms, |
2283f8d6 | 743 | const struct nfs_parsed_mount_data *data) |
54ceac45 | 744 | { |
54ceac45 DH |
745 | int error; |
746 | ||
747 | if (clp->cl_cons_state == NFS_CS_READY) { | |
748 | /* the client is already initialised */ | |
749 | dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp); | |
750 | return 0; | |
751 | } | |
752 | ||
54ceac45 DH |
753 | /* |
754 | * Create a client RPC handle for doing FSSTAT with UNIX auth only | |
755 | * - RFC 2623, sec 2.3.2 | |
756 | */ | |
d740351b CL |
757 | error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX, |
758 | 0, data->flags & NFS_MOUNT_NORESVPORT); | |
54ceac45 DH |
759 | if (error < 0) |
760 | goto error; | |
761 | nfs_mark_client_ready(clp, NFS_CS_READY); | |
762 | return 0; | |
763 | ||
764 | error: | |
765 | nfs_mark_client_ready(clp, error); | |
766 | dprintk("<-- nfs_init_client() = xerror %d\n", error); | |
767 | return error; | |
768 | } | |
769 | ||
770 | /* | |
771 | * Create a version 2 or 3 client | |
772 | */ | |
2283f8d6 TT |
773 | static int nfs_init_server(struct nfs_server *server, |
774 | const struct nfs_parsed_mount_data *data) | |
54ceac45 | 775 | { |
3a498026 TM |
776 | struct nfs_client_initdata cl_init = { |
777 | .hostname = data->nfs_server.hostname, | |
d7422c47 | 778 | .addr = (const struct sockaddr *)&data->nfs_server.address, |
4c568017 | 779 | .addrlen = data->nfs_server.addrlen, |
40c55319 | 780 | .rpc_ops = &nfs_v2_clientops, |
59dca3b2 | 781 | .proto = data->nfs_server.protocol, |
3a498026 | 782 | }; |
33170233 | 783 | struct rpc_timeout timeparms; |
54ceac45 | 784 | struct nfs_client *clp; |
3a498026 | 785 | int error; |
54ceac45 DH |
786 | |
787 | dprintk("--> nfs_init_server()\n"); | |
788 | ||
789 | #ifdef CONFIG_NFS_V3 | |
790 | if (data->flags & NFS_MOUNT_VER3) | |
40c55319 | 791 | cl_init.rpc_ops = &nfs_v3_clientops; |
54ceac45 DH |
792 | #endif |
793 | ||
794 | /* Allocate or find a client reference we can use */ | |
3a498026 | 795 | clp = nfs_get_client(&cl_init); |
54ceac45 DH |
796 | if (IS_ERR(clp)) { |
797 | dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp)); | |
798 | return PTR_ERR(clp); | |
799 | } | |
800 | ||
33170233 TM |
801 | nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, |
802 | data->timeo, data->retrans); | |
803 | error = nfs_init_client(clp, &timeparms, data); | |
54ceac45 DH |
804 | if (error < 0) |
805 | goto error; | |
806 | ||
807 | server->nfs_client = clp; | |
808 | ||
809 | /* Initialise the client representation from the mount data */ | |
ff3525a5 | 810 | server->flags = data->flags; |
b797cac7 | 811 | server->options = data->options; |
62ab460c TM |
812 | server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID| |
813 | NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP| | |
814 | NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME; | |
54ceac45 DH |
815 | |
816 | if (data->rsize) | |
817 | server->rsize = nfs_block_size(data->rsize, NULL); | |
818 | if (data->wsize) | |
819 | server->wsize = nfs_block_size(data->wsize, NULL); | |
820 | ||
821 | server->acregmin = data->acregmin * HZ; | |
822 | server->acregmax = data->acregmax * HZ; | |
823 | server->acdirmin = data->acdirmin * HZ; | |
824 | server->acdirmax = data->acdirmax * HZ; | |
825 | ||
826 | /* Start lockd here, before we might error out */ | |
827 | error = nfs_start_lockd(server); | |
828 | if (error < 0) | |
829 | goto error; | |
830 | ||
f22d6d79 CL |
831 | server->port = data->nfs_server.port; |
832 | ||
33170233 | 833 | error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]); |
54ceac45 DH |
834 | if (error < 0) |
835 | goto error; | |
836 | ||
3f8400d1 CL |
837 | /* Preserve the values of mount_server-related mount options */ |
838 | if (data->mount_server.addrlen) { | |
839 | memcpy(&server->mountd_address, &data->mount_server.address, | |
840 | data->mount_server.addrlen); | |
841 | server->mountd_addrlen = data->mount_server.addrlen; | |
842 | } | |
843 | server->mountd_version = data->mount_server.version; | |
844 | server->mountd_port = data->mount_server.port; | |
845 | server->mountd_protocol = data->mount_server.protocol; | |
846 | ||
54ceac45 DH |
847 | server->namelen = data->namlen; |
848 | /* Create a client RPC handle for the NFSv3 ACL management interface */ | |
849 | nfs_init_server_aclclient(server); | |
54ceac45 DH |
850 | dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp); |
851 | return 0; | |
852 | ||
853 | error: | |
854 | server->nfs_client = NULL; | |
855 | nfs_put_client(clp); | |
856 | dprintk("<-- nfs_init_server() = xerror %d\n", error); | |
857 | return error; | |
858 | } | |
859 | ||
860 | /* | |
861 | * Load up the server record from information gained in an fsinfo record | |
862 | */ | |
863 | static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo) | |
864 | { | |
865 | unsigned long max_rpc_payload; | |
866 | ||
867 | /* Work out a lot of parameters */ | |
868 | if (server->rsize == 0) | |
869 | server->rsize = nfs_block_size(fsinfo->rtpref, NULL); | |
870 | if (server->wsize == 0) | |
871 | server->wsize = nfs_block_size(fsinfo->wtpref, NULL); | |
872 | ||
873 | if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax) | |
874 | server->rsize = nfs_block_size(fsinfo->rtmax, NULL); | |
875 | if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax) | |
876 | server->wsize = nfs_block_size(fsinfo->wtmax, NULL); | |
877 | ||
878 | max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL); | |
879 | if (server->rsize > max_rpc_payload) | |
880 | server->rsize = max_rpc_payload; | |
881 | if (server->rsize > NFS_MAX_FILE_IO_SIZE) | |
882 | server->rsize = NFS_MAX_FILE_IO_SIZE; | |
883 | server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
e0bf68dd | 884 | |
d993831f | 885 | server->backing_dev_info.name = "nfs"; |
54ceac45 DH |
886 | server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD; |
887 | ||
888 | if (server->wsize > max_rpc_payload) | |
889 | server->wsize = max_rpc_payload; | |
890 | if (server->wsize > NFS_MAX_FILE_IO_SIZE) | |
891 | server->wsize = NFS_MAX_FILE_IO_SIZE; | |
892 | server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
893 | server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL); | |
894 | ||
895 | server->dtsize = nfs_block_size(fsinfo->dtpref, NULL); | |
896 | if (server->dtsize > PAGE_CACHE_SIZE) | |
897 | server->dtsize = PAGE_CACHE_SIZE; | |
898 | if (server->dtsize > server->rsize) | |
899 | server->dtsize = server->rsize; | |
900 | ||
901 | if (server->flags & NFS_MOUNT_NOAC) { | |
902 | server->acregmin = server->acregmax = 0; | |
903 | server->acdirmin = server->acdirmax = 0; | |
904 | } | |
905 | ||
906 | server->maxfilesize = fsinfo->maxfilesize; | |
907 | ||
908 | /* We're airborne Set socket buffersize */ | |
909 | rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100); | |
910 | } | |
911 | ||
912 | /* | |
913 | * Probe filesystem information, including the FSID on v2/v3 | |
914 | */ | |
915 | static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr) | |
916 | { | |
917 | struct nfs_fsinfo fsinfo; | |
918 | struct nfs_client *clp = server->nfs_client; | |
919 | int error; | |
920 | ||
921 | dprintk("--> nfs_probe_fsinfo()\n"); | |
922 | ||
923 | if (clp->rpc_ops->set_capabilities != NULL) { | |
924 | error = clp->rpc_ops->set_capabilities(server, mntfh); | |
925 | if (error < 0) | |
926 | goto out_error; | |
927 | } | |
928 | ||
929 | fsinfo.fattr = fattr; | |
930 | nfs_fattr_init(fattr); | |
931 | error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo); | |
932 | if (error < 0) | |
933 | goto out_error; | |
934 | ||
935 | nfs_server_set_fsinfo(server, &fsinfo); | |
e0bf68dd PZ |
936 | error = bdi_init(&server->backing_dev_info); |
937 | if (error) | |
938 | goto out_error; | |
939 | ||
54ceac45 DH |
940 | |
941 | /* Get some general file system info */ | |
942 | if (server->namelen == 0) { | |
943 | struct nfs_pathconf pathinfo; | |
944 | ||
945 | pathinfo.fattr = fattr; | |
946 | nfs_fattr_init(fattr); | |
947 | ||
948 | if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0) | |
949 | server->namelen = pathinfo.max_namelen; | |
950 | } | |
951 | ||
952 | dprintk("<-- nfs_probe_fsinfo() = 0\n"); | |
953 | return 0; | |
954 | ||
955 | out_error: | |
956 | dprintk("nfs_probe_fsinfo: error = %d\n", -error); | |
957 | return error; | |
958 | } | |
959 | ||
960 | /* | |
961 | * Copy useful information when duplicating a server record | |
962 | */ | |
963 | static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source) | |
964 | { | |
965 | target->flags = source->flags; | |
966 | target->acregmin = source->acregmin; | |
967 | target->acregmax = source->acregmax; | |
968 | target->acdirmin = source->acdirmin; | |
969 | target->acdirmax = source->acdirmax; | |
970 | target->caps = source->caps; | |
971 | } | |
972 | ||
973 | /* | |
974 | * Allocate and initialise a server record | |
975 | */ | |
976 | static struct nfs_server *nfs_alloc_server(void) | |
977 | { | |
978 | struct nfs_server *server; | |
979 | ||
980 | server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL); | |
981 | if (!server) | |
982 | return NULL; | |
983 | ||
984 | server->client = server->client_acl = ERR_PTR(-EINVAL); | |
985 | ||
986 | /* Zero out the NFS state stuff */ | |
987 | INIT_LIST_HEAD(&server->client_link); | |
988 | INIT_LIST_HEAD(&server->master_link); | |
989 | ||
ef818a28 SD |
990 | atomic_set(&server->active, 0); |
991 | ||
54ceac45 DH |
992 | server->io_stats = nfs_alloc_iostats(); |
993 | if (!server->io_stats) { | |
994 | kfree(server); | |
995 | return NULL; | |
996 | } | |
997 | ||
998 | return server; | |
999 | } | |
1000 | ||
1001 | /* | |
1002 | * Free up a server record | |
1003 | */ | |
1004 | void nfs_free_server(struct nfs_server *server) | |
1005 | { | |
1006 | dprintk("--> nfs_free_server()\n"); | |
1007 | ||
1008 | spin_lock(&nfs_client_lock); | |
1009 | list_del(&server->client_link); | |
1010 | list_del(&server->master_link); | |
1011 | spin_unlock(&nfs_client_lock); | |
1012 | ||
1013 | if (server->destroy != NULL) | |
1014 | server->destroy(server); | |
5cef338b TM |
1015 | |
1016 | if (!IS_ERR(server->client_acl)) | |
1017 | rpc_shutdown_client(server->client_acl); | |
54ceac45 DH |
1018 | if (!IS_ERR(server->client)) |
1019 | rpc_shutdown_client(server->client); | |
1020 | ||
1021 | nfs_put_client(server->nfs_client); | |
1022 | ||
1023 | nfs_free_iostats(server->io_stats); | |
e0bf68dd | 1024 | bdi_destroy(&server->backing_dev_info); |
54ceac45 DH |
1025 | kfree(server); |
1026 | nfs_release_automount_timer(); | |
1027 | dprintk("<-- nfs_free_server()\n"); | |
1028 | } | |
1029 | ||
1030 | /* | |
1031 | * Create a version 2 or 3 volume record | |
1032 | * - keyed on server and FSID | |
1033 | */ | |
2283f8d6 | 1034 | struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data, |
54ceac45 DH |
1035 | struct nfs_fh *mntfh) |
1036 | { | |
1037 | struct nfs_server *server; | |
1038 | struct nfs_fattr fattr; | |
1039 | int error; | |
1040 | ||
1041 | server = nfs_alloc_server(); | |
1042 | if (!server) | |
1043 | return ERR_PTR(-ENOMEM); | |
1044 | ||
1045 | /* Get a client representation */ | |
1046 | error = nfs_init_server(server, data); | |
1047 | if (error < 0) | |
1048 | goto error; | |
1049 | ||
1050 | BUG_ON(!server->nfs_client); | |
1051 | BUG_ON(!server->nfs_client->rpc_ops); | |
1052 | BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); | |
1053 | ||
1054 | /* Probe the root fh to retrieve its FSID */ | |
1055 | error = nfs_probe_fsinfo(server, mntfh, &fattr); | |
1056 | if (error < 0) | |
1057 | goto error; | |
54af3bb5 TM |
1058 | if (server->nfs_client->rpc_ops->version == 3) { |
1059 | if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN) | |
1060 | server->namelen = NFS3_MAXNAMLEN; | |
1061 | if (!(data->flags & NFS_MOUNT_NORDIRPLUS)) | |
1062 | server->caps |= NFS_CAP_READDIRPLUS; | |
1063 | } else { | |
1064 | if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN) | |
1065 | server->namelen = NFS2_MAXNAMLEN; | |
1066 | } | |
1067 | ||
54ceac45 DH |
1068 | if (!(fattr.valid & NFS_ATTR_FATTR)) { |
1069 | error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr); | |
1070 | if (error < 0) { | |
1071 | dprintk("nfs_create_server: getattr error = %d\n", -error); | |
1072 | goto error; | |
1073 | } | |
1074 | } | |
1075 | memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid)); | |
1076 | ||
6daabf1b DH |
1077 | dprintk("Server FSID: %llx:%llx\n", |
1078 | (unsigned long long) server->fsid.major, | |
1079 | (unsigned long long) server->fsid.minor); | |
54ceac45 | 1080 | |
54ceac45 DH |
1081 | spin_lock(&nfs_client_lock); |
1082 | list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks); | |
1083 | list_add_tail(&server->master_link, &nfs_volume_list); | |
1084 | spin_unlock(&nfs_client_lock); | |
1085 | ||
1086 | server->mount_time = jiffies; | |
1087 | return server; | |
1088 | ||
1089 | error: | |
1090 | nfs_free_server(server); | |
1091 | return ERR_PTR(error); | |
1092 | } | |
1093 | ||
1094 | #ifdef CONFIG_NFS_V4 | |
9bdaa86d BH |
1095 | /* |
1096 | * Initialize the NFS4 callback service | |
1097 | */ | |
1098 | static int nfs4_init_callback(struct nfs_client *clp) | |
1099 | { | |
1100 | int error; | |
1101 | ||
1102 | if (clp->rpc_ops->version == 4) { | |
0b5b7ae0 AA |
1103 | if (nfs4_has_session(clp)) { |
1104 | error = xprt_setup_backchannel( | |
1105 | clp->cl_rpcclient->cl_xprt, | |
1106 | NFS41_BC_MIN_CALLBACKS); | |
1107 | if (error < 0) | |
1108 | return error; | |
1109 | } | |
1110 | ||
71468513 BH |
1111 | error = nfs_callback_up(clp->cl_minorversion, |
1112 | clp->cl_rpcclient->cl_xprt); | |
9bdaa86d BH |
1113 | if (error < 0) { |
1114 | dprintk("%s: failed to start callback. Error = %d\n", | |
1115 | __func__, error); | |
1116 | return error; | |
1117 | } | |
1118 | __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state); | |
1119 | } | |
1120 | return 0; | |
1121 | } | |
1122 | ||
557134a3 AA |
1123 | /* |
1124 | * Initialize the minor version specific parts of an NFS4 client record | |
1125 | */ | |
1126 | static int nfs4_init_client_minor_version(struct nfs_client *clp) | |
1127 | { | |
cccef3b9 AA |
1128 | clp->cl_call_sync = _nfs4_call_sync; |
1129 | ||
557134a3 AA |
1130 | #if defined(CONFIG_NFS_V4_1) |
1131 | if (clp->cl_minorversion) { | |
1132 | struct nfs4_session *session = NULL; | |
1133 | /* | |
1134 | * Create the session and mark it expired. | |
1135 | * When a SEQUENCE operation encounters the expired session | |
1136 | * it will do session recovery to initialize it. | |
1137 | */ | |
1138 | session = nfs4_alloc_session(clp); | |
1139 | if (!session) | |
1140 | return -ENOMEM; | |
1141 | ||
1142 | clp->cl_session = session; | |
cccef3b9 | 1143 | clp->cl_call_sync = _nfs4_call_sync_session; |
557134a3 AA |
1144 | } |
1145 | #endif /* CONFIG_NFS_V4_1 */ | |
1146 | ||
71468513 | 1147 | return nfs4_init_callback(clp); |
557134a3 AA |
1148 | } |
1149 | ||
54ceac45 DH |
1150 | /* |
1151 | * Initialise an NFS4 client record | |
1152 | */ | |
1153 | static int nfs4_init_client(struct nfs_client *clp, | |
33170233 | 1154 | const struct rpc_timeout *timeparms, |
7d9ac06f | 1155 | const char *ip_addr, |
d740351b CL |
1156 | rpc_authflavor_t authflavour, |
1157 | int flags) | |
54ceac45 DH |
1158 | { |
1159 | int error; | |
1160 | ||
1161 | if (clp->cl_cons_state == NFS_CS_READY) { | |
1162 | /* the client is initialised already */ | |
1163 | dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp); | |
1164 | return 0; | |
1165 | } | |
1166 | ||
1167 | /* Check NFS protocol revision and initialize RPC op vector */ | |
1168 | clp->rpc_ops = &nfs_v4_clientops; | |
1169 | ||
59dca3b2 | 1170 | error = nfs_create_rpc_client(clp, timeparms, authflavour, |
d740351b | 1171 | 1, flags & NFS_MOUNT_NORESVPORT); |
54ceac45 DH |
1172 | if (error < 0) |
1173 | goto error; | |
7d9ac06f | 1174 | memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr)); |
54ceac45 DH |
1175 | |
1176 | error = nfs_idmap_new(clp); | |
1177 | if (error < 0) { | |
1178 | dprintk("%s: failed to create idmapper. Error = %d\n", | |
3110ff80 | 1179 | __func__, error); |
54ceac45 DH |
1180 | goto error; |
1181 | } | |
9c5bf38d | 1182 | __set_bit(NFS_CS_IDMAP, &clp->cl_res_state); |
54ceac45 | 1183 | |
557134a3 AA |
1184 | error = nfs4_init_client_minor_version(clp); |
1185 | if (error < 0) | |
1186 | goto error; | |
1187 | ||
76db6d95 AA |
1188 | if (!nfs4_has_session(clp)) |
1189 | nfs_mark_client_ready(clp, NFS_CS_READY); | |
54ceac45 DH |
1190 | return 0; |
1191 | ||
1192 | error: | |
1193 | nfs_mark_client_ready(clp, error); | |
1194 | dprintk("<-- nfs4_init_client() = xerror %d\n", error); | |
1195 | return error; | |
1196 | } | |
1197 | ||
1198 | /* | |
1199 | * Set up an NFS4 client | |
1200 | */ | |
1201 | static int nfs4_set_client(struct nfs_server *server, | |
dcecae0f CL |
1202 | const char *hostname, |
1203 | const struct sockaddr *addr, | |
1204 | const size_t addrlen, | |
7d9ac06f | 1205 | const char *ip_addr, |
54ceac45 | 1206 | rpc_authflavor_t authflavour, |
94a417f3 BH |
1207 | int proto, const struct rpc_timeout *timeparms, |
1208 | u32 minorversion) | |
54ceac45 | 1209 | { |
3a498026 TM |
1210 | struct nfs_client_initdata cl_init = { |
1211 | .hostname = hostname, | |
dcecae0f CL |
1212 | .addr = addr, |
1213 | .addrlen = addrlen, | |
40c55319 | 1214 | .rpc_ops = &nfs_v4_clientops, |
59dca3b2 | 1215 | .proto = proto, |
5aae4a9a | 1216 | .minorversion = minorversion, |
3a498026 | 1217 | }; |
54ceac45 DH |
1218 | struct nfs_client *clp; |
1219 | int error; | |
1220 | ||
1221 | dprintk("--> nfs4_set_client()\n"); | |
1222 | ||
1223 | /* Allocate or find a client reference we can use */ | |
3a498026 | 1224 | clp = nfs_get_client(&cl_init); |
54ceac45 DH |
1225 | if (IS_ERR(clp)) { |
1226 | error = PTR_ERR(clp); | |
1227 | goto error; | |
1228 | } | |
d740351b CL |
1229 | error = nfs4_init_client(clp, timeparms, ip_addr, authflavour, |
1230 | server->flags); | |
54ceac45 DH |
1231 | if (error < 0) |
1232 | goto error_put; | |
1233 | ||
1234 | server->nfs_client = clp; | |
1235 | dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp); | |
1236 | return 0; | |
1237 | ||
1238 | error_put: | |
1239 | nfs_put_client(clp); | |
1240 | error: | |
1241 | dprintk("<-- nfs4_set_client() = xerror %d\n", error); | |
1242 | return error; | |
1243 | } | |
1244 | ||
557134a3 | 1245 | |
96b09e02 AA |
1246 | /* |
1247 | * Session has been established, and the client marked ready. | |
1248 | * Set the mount rsize and wsize with negotiated fore channel | |
1249 | * attributes which will be bound checked in nfs_server_set_fsinfo. | |
1250 | */ | |
1251 | static void nfs4_session_set_rwsize(struct nfs_server *server) | |
1252 | { | |
1253 | #ifdef CONFIG_NFS_V4_1 | |
1254 | if (!nfs4_has_session(server->nfs_client)) | |
1255 | return; | |
1256 | server->rsize = server->nfs_client->cl_session->fc_attrs.max_resp_sz; | |
1257 | server->wsize = server->nfs_client->cl_session->fc_attrs.max_rqst_sz; | |
1258 | #endif /* CONFIG_NFS_V4_1 */ | |
1259 | } | |
1260 | ||
54ceac45 DH |
1261 | /* |
1262 | * Create a version 4 volume record | |
1263 | */ | |
1264 | static int nfs4_init_server(struct nfs_server *server, | |
91ea40b9 | 1265 | const struct nfs_parsed_mount_data *data) |
54ceac45 | 1266 | { |
33170233 | 1267 | struct rpc_timeout timeparms; |
54ceac45 DH |
1268 | int error; |
1269 | ||
1270 | dprintk("--> nfs4_init_server()\n"); | |
1271 | ||
33170233 TM |
1272 | nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, |
1273 | data->timeo, data->retrans); | |
1274 | ||
542fcc33 CL |
1275 | /* Initialise the client representation from the mount data */ |
1276 | server->flags = data->flags; | |
62ab460c | 1277 | server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR; |
b797cac7 | 1278 | server->options = data->options; |
542fcc33 | 1279 | |
33170233 TM |
1280 | /* Get a client record */ |
1281 | error = nfs4_set_client(server, | |
1282 | data->nfs_server.hostname, | |
1283 | (const struct sockaddr *)&data->nfs_server.address, | |
1284 | data->nfs_server.addrlen, | |
1285 | data->client_address, | |
1286 | data->auth_flavors[0], | |
1287 | data->nfs_server.protocol, | |
94a417f3 BH |
1288 | &timeparms, |
1289 | data->minorversion); | |
33170233 TM |
1290 | if (error < 0) |
1291 | goto error; | |
1292 | ||
54ceac45 DH |
1293 | if (data->rsize) |
1294 | server->rsize = nfs_block_size(data->rsize, NULL); | |
1295 | if (data->wsize) | |
1296 | server->wsize = nfs_block_size(data->wsize, NULL); | |
1297 | ||
1298 | server->acregmin = data->acregmin * HZ; | |
1299 | server->acregmax = data->acregmax * HZ; | |
1300 | server->acdirmin = data->acdirmin * HZ; | |
1301 | server->acdirmax = data->acdirmax * HZ; | |
1302 | ||
f22d6d79 CL |
1303 | server->port = data->nfs_server.port; |
1304 | ||
33170233 | 1305 | error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]); |
54ceac45 | 1306 | |
33170233 | 1307 | error: |
54ceac45 DH |
1308 | /* Done */ |
1309 | dprintk("<-- nfs4_init_server() = %d\n", error); | |
1310 | return error; | |
1311 | } | |
1312 | ||
1313 | /* | |
1314 | * Create a version 4 volume record | |
1315 | * - keyed on server and FSID | |
1316 | */ | |
91ea40b9 | 1317 | struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data, |
54ceac45 DH |
1318 | struct nfs_fh *mntfh) |
1319 | { | |
1320 | struct nfs_fattr fattr; | |
1321 | struct nfs_server *server; | |
1322 | int error; | |
1323 | ||
1324 | dprintk("--> nfs4_create_server()\n"); | |
1325 | ||
1326 | server = nfs_alloc_server(); | |
1327 | if (!server) | |
1328 | return ERR_PTR(-ENOMEM); | |
1329 | ||
54ceac45 | 1330 | /* set up the general RPC client */ |
91ea40b9 | 1331 | error = nfs4_init_server(server, data); |
54ceac45 DH |
1332 | if (error < 0) |
1333 | goto error; | |
1334 | ||
1335 | BUG_ON(!server->nfs_client); | |
1336 | BUG_ON(!server->nfs_client->rpc_ops); | |
1337 | BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); | |
1338 | ||
fccba804 TM |
1339 | error = nfs4_init_session(server); |
1340 | if (error < 0) | |
1341 | goto error; | |
557134a3 | 1342 | |
54ceac45 | 1343 | /* Probe the root fh to retrieve its FSID */ |
91ea40b9 | 1344 | error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path); |
54ceac45 DH |
1345 | if (error < 0) |
1346 | goto error; | |
1347 | ||
6daabf1b DH |
1348 | dprintk("Server FSID: %llx:%llx\n", |
1349 | (unsigned long long) server->fsid.major, | |
1350 | (unsigned long long) server->fsid.minor); | |
54ceac45 DH |
1351 | dprintk("Mount FH: %d\n", mntfh->size); |
1352 | ||
96b09e02 AA |
1353 | nfs4_session_set_rwsize(server); |
1354 | ||
54ceac45 DH |
1355 | error = nfs_probe_fsinfo(server, mntfh, &fattr); |
1356 | if (error < 0) | |
1357 | goto error; | |
1358 | ||
54af3bb5 TM |
1359 | if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) |
1360 | server->namelen = NFS4_MAXNAMLEN; | |
1361 | ||
54ceac45 DH |
1362 | spin_lock(&nfs_client_lock); |
1363 | list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks); | |
1364 | list_add_tail(&server->master_link, &nfs_volume_list); | |
1365 | spin_unlock(&nfs_client_lock); | |
1366 | ||
1367 | server->mount_time = jiffies; | |
1368 | dprintk("<-- nfs4_create_server() = %p\n", server); | |
1369 | return server; | |
1370 | ||
1371 | error: | |
1372 | nfs_free_server(server); | |
1373 | dprintk("<-- nfs4_create_server() = error %d\n", error); | |
1374 | return ERR_PTR(error); | |
1375 | } | |
1376 | ||
1377 | /* | |
1378 | * Create an NFS4 referral server record | |
1379 | */ | |
1380 | struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data, | |
f2d0d85e | 1381 | struct nfs_fh *mntfh) |
54ceac45 DH |
1382 | { |
1383 | struct nfs_client *parent_client; | |
1384 | struct nfs_server *server, *parent_server; | |
1385 | struct nfs_fattr fattr; | |
1386 | int error; | |
1387 | ||
1388 | dprintk("--> nfs4_create_referral_server()\n"); | |
1389 | ||
1390 | server = nfs_alloc_server(); | |
1391 | if (!server) | |
1392 | return ERR_PTR(-ENOMEM); | |
1393 | ||
1394 | parent_server = NFS_SB(data->sb); | |
1395 | parent_client = parent_server->nfs_client; | |
1396 | ||
542fcc33 CL |
1397 | /* Initialise the client representation from the parent server */ |
1398 | nfs_server_copy_userdata(server, parent_server); | |
62ab460c | 1399 | server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR; |
542fcc33 | 1400 | |
54ceac45 DH |
1401 | /* Get a client representation. |
1402 | * Note: NFSv4 always uses TCP, */ | |
dcecae0f | 1403 | error = nfs4_set_client(server, data->hostname, |
6677d095 CL |
1404 | data->addr, |
1405 | data->addrlen, | |
dcecae0f CL |
1406 | parent_client->cl_ipaddr, |
1407 | data->authflavor, | |
1408 | parent_server->client->cl_xprt->prot, | |
94a417f3 BH |
1409 | parent_server->client->cl_timeout, |
1410 | parent_client->cl_minorversion); | |
297de4f6 | 1411 | if (error < 0) |
1412 | goto error; | |
54ceac45 | 1413 | |
33170233 | 1414 | error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor); |
54ceac45 DH |
1415 | if (error < 0) |
1416 | goto error; | |
1417 | ||
1418 | BUG_ON(!server->nfs_client); | |
1419 | BUG_ON(!server->nfs_client->rpc_ops); | |
1420 | BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); | |
1421 | ||
f2d0d85e TM |
1422 | /* Probe the root fh to retrieve its FSID and filehandle */ |
1423 | error = nfs4_path_walk(server, mntfh, data->mnt_path); | |
1424 | if (error < 0) | |
1425 | goto error; | |
1426 | ||
54ceac45 | 1427 | /* probe the filesystem info for this server filesystem */ |
f2d0d85e | 1428 | error = nfs_probe_fsinfo(server, mntfh, &fattr); |
54ceac45 DH |
1429 | if (error < 0) |
1430 | goto error; | |
1431 | ||
54af3bb5 TM |
1432 | if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) |
1433 | server->namelen = NFS4_MAXNAMLEN; | |
1434 | ||
54ceac45 | 1435 | dprintk("Referral FSID: %llx:%llx\n", |
6daabf1b DH |
1436 | (unsigned long long) server->fsid.major, |
1437 | (unsigned long long) server->fsid.minor); | |
54ceac45 DH |
1438 | |
1439 | spin_lock(&nfs_client_lock); | |
1440 | list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks); | |
1441 | list_add_tail(&server->master_link, &nfs_volume_list); | |
1442 | spin_unlock(&nfs_client_lock); | |
1443 | ||
1444 | server->mount_time = jiffies; | |
1445 | ||
1446 | dprintk("<-- nfs_create_referral_server() = %p\n", server); | |
1447 | return server; | |
1448 | ||
1449 | error: | |
1450 | nfs_free_server(server); | |
1451 | dprintk("<-- nfs4_create_referral_server() = error %d\n", error); | |
1452 | return ERR_PTR(error); | |
1453 | } | |
1454 | ||
1455 | #endif /* CONFIG_NFS_V4 */ | |
1456 | ||
1457 | /* | |
1458 | * Clone an NFS2, NFS3 or NFS4 server record | |
1459 | */ | |
1460 | struct nfs_server *nfs_clone_server(struct nfs_server *source, | |
1461 | struct nfs_fh *fh, | |
1462 | struct nfs_fattr *fattr) | |
1463 | { | |
1464 | struct nfs_server *server; | |
1465 | struct nfs_fattr fattr_fsinfo; | |
1466 | int error; | |
1467 | ||
1468 | dprintk("--> nfs_clone_server(,%llx:%llx,)\n", | |
6daabf1b DH |
1469 | (unsigned long long) fattr->fsid.major, |
1470 | (unsigned long long) fattr->fsid.minor); | |
54ceac45 DH |
1471 | |
1472 | server = nfs_alloc_server(); | |
1473 | if (!server) | |
1474 | return ERR_PTR(-ENOMEM); | |
1475 | ||
1476 | /* Copy data from the source */ | |
1477 | server->nfs_client = source->nfs_client; | |
1478 | atomic_inc(&server->nfs_client->cl_count); | |
1479 | nfs_server_copy_userdata(server, source); | |
1480 | ||
1481 | server->fsid = fattr->fsid; | |
1482 | ||
33170233 TM |
1483 | error = nfs_init_server_rpcclient(server, |
1484 | source->client->cl_timeout, | |
1485 | source->client->cl_auth->au_flavor); | |
54ceac45 DH |
1486 | if (error < 0) |
1487 | goto out_free_server; | |
1488 | if (!IS_ERR(source->client_acl)) | |
1489 | nfs_init_server_aclclient(server); | |
1490 | ||
1491 | /* probe the filesystem info for this server filesystem */ | |
1492 | error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo); | |
1493 | if (error < 0) | |
1494 | goto out_free_server; | |
1495 | ||
54af3bb5 TM |
1496 | if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) |
1497 | server->namelen = NFS4_MAXNAMLEN; | |
1498 | ||
54ceac45 | 1499 | dprintk("Cloned FSID: %llx:%llx\n", |
6daabf1b DH |
1500 | (unsigned long long) server->fsid.major, |
1501 | (unsigned long long) server->fsid.minor); | |
54ceac45 DH |
1502 | |
1503 | error = nfs_start_lockd(server); | |
1504 | if (error < 0) | |
1505 | goto out_free_server; | |
1506 | ||
1507 | spin_lock(&nfs_client_lock); | |
1508 | list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks); | |
1509 | list_add_tail(&server->master_link, &nfs_volume_list); | |
1510 | spin_unlock(&nfs_client_lock); | |
1511 | ||
1512 | server->mount_time = jiffies; | |
1513 | ||
1514 | dprintk("<-- nfs_clone_server() = %p\n", server); | |
1515 | return server; | |
1516 | ||
1517 | out_free_server: | |
1518 | nfs_free_server(server); | |
1519 | dprintk("<-- nfs_clone_server() = error %d\n", error); | |
1520 | return ERR_PTR(error); | |
1521 | } | |
6aaca566 DH |
1522 | |
1523 | #ifdef CONFIG_PROC_FS | |
1524 | static struct proc_dir_entry *proc_fs_nfs; | |
1525 | ||
1526 | static int nfs_server_list_open(struct inode *inode, struct file *file); | |
1527 | static void *nfs_server_list_start(struct seq_file *p, loff_t *pos); | |
1528 | static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos); | |
1529 | static void nfs_server_list_stop(struct seq_file *p, void *v); | |
1530 | static int nfs_server_list_show(struct seq_file *m, void *v); | |
1531 | ||
1532 | static struct seq_operations nfs_server_list_ops = { | |
1533 | .start = nfs_server_list_start, | |
1534 | .next = nfs_server_list_next, | |
1535 | .stop = nfs_server_list_stop, | |
1536 | .show = nfs_server_list_show, | |
1537 | }; | |
1538 | ||
00977a59 | 1539 | static const struct file_operations nfs_server_list_fops = { |
6aaca566 DH |
1540 | .open = nfs_server_list_open, |
1541 | .read = seq_read, | |
1542 | .llseek = seq_lseek, | |
1543 | .release = seq_release, | |
34b37235 | 1544 | .owner = THIS_MODULE, |
6aaca566 DH |
1545 | }; |
1546 | ||
1547 | static int nfs_volume_list_open(struct inode *inode, struct file *file); | |
1548 | static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos); | |
1549 | static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos); | |
1550 | static void nfs_volume_list_stop(struct seq_file *p, void *v); | |
1551 | static int nfs_volume_list_show(struct seq_file *m, void *v); | |
1552 | ||
1553 | static struct seq_operations nfs_volume_list_ops = { | |
1554 | .start = nfs_volume_list_start, | |
1555 | .next = nfs_volume_list_next, | |
1556 | .stop = nfs_volume_list_stop, | |
1557 | .show = nfs_volume_list_show, | |
1558 | }; | |
1559 | ||
00977a59 | 1560 | static const struct file_operations nfs_volume_list_fops = { |
6aaca566 DH |
1561 | .open = nfs_volume_list_open, |
1562 | .read = seq_read, | |
1563 | .llseek = seq_lseek, | |
1564 | .release = seq_release, | |
34b37235 | 1565 | .owner = THIS_MODULE, |
6aaca566 DH |
1566 | }; |
1567 | ||
1568 | /* | |
1569 | * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which | |
1570 | * we're dealing | |
1571 | */ | |
1572 | static int nfs_server_list_open(struct inode *inode, struct file *file) | |
1573 | { | |
1574 | struct seq_file *m; | |
1575 | int ret; | |
1576 | ||
1577 | ret = seq_open(file, &nfs_server_list_ops); | |
1578 | if (ret < 0) | |
1579 | return ret; | |
1580 | ||
1581 | m = file->private_data; | |
1582 | m->private = PDE(inode)->data; | |
1583 | ||
1584 | return 0; | |
1585 | } | |
1586 | ||
1587 | /* | |
1588 | * set up the iterator to start reading from the server list and return the first item | |
1589 | */ | |
1590 | static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos) | |
1591 | { | |
6aaca566 DH |
1592 | /* lock the list against modification */ |
1593 | spin_lock(&nfs_client_lock); | |
259902ea | 1594 | return seq_list_start_head(&nfs_client_list, *_pos); |
6aaca566 DH |
1595 | } |
1596 | ||
1597 | /* | |
1598 | * move to next server | |
1599 | */ | |
1600 | static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos) | |
1601 | { | |
259902ea | 1602 | return seq_list_next(v, &nfs_client_list, pos); |
6aaca566 DH |
1603 | } |
1604 | ||
1605 | /* | |
1606 | * clean up after reading from the transports list | |
1607 | */ | |
1608 | static void nfs_server_list_stop(struct seq_file *p, void *v) | |
1609 | { | |
1610 | spin_unlock(&nfs_client_lock); | |
1611 | } | |
1612 | ||
1613 | /* | |
1614 | * display a header line followed by a load of call lines | |
1615 | */ | |
1616 | static int nfs_server_list_show(struct seq_file *m, void *v) | |
1617 | { | |
1618 | struct nfs_client *clp; | |
1619 | ||
1620 | /* display header on line 1 */ | |
259902ea | 1621 | if (v == &nfs_client_list) { |
6aaca566 DH |
1622 | seq_puts(m, "NV SERVER PORT USE HOSTNAME\n"); |
1623 | return 0; | |
1624 | } | |
1625 | ||
1626 | /* display one transport per line on subsequent lines */ | |
1627 | clp = list_entry(v, struct nfs_client, cl_share_link); | |
1628 | ||
5d8515ca | 1629 | seq_printf(m, "v%u %s %s %3d %s\n", |
40c55319 | 1630 | clp->rpc_ops->version, |
5d8515ca CL |
1631 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), |
1632 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), | |
6aaca566 DH |
1633 | atomic_read(&clp->cl_count), |
1634 | clp->cl_hostname); | |
1635 | ||
1636 | return 0; | |
1637 | } | |
1638 | ||
1639 | /* | |
1640 | * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes | |
1641 | */ | |
1642 | static int nfs_volume_list_open(struct inode *inode, struct file *file) | |
1643 | { | |
1644 | struct seq_file *m; | |
1645 | int ret; | |
1646 | ||
1647 | ret = seq_open(file, &nfs_volume_list_ops); | |
1648 | if (ret < 0) | |
1649 | return ret; | |
1650 | ||
1651 | m = file->private_data; | |
1652 | m->private = PDE(inode)->data; | |
1653 | ||
1654 | return 0; | |
1655 | } | |
1656 | ||
1657 | /* | |
1658 | * set up the iterator to start reading from the volume list and return the first item | |
1659 | */ | |
1660 | static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos) | |
1661 | { | |
6aaca566 DH |
1662 | /* lock the list against modification */ |
1663 | spin_lock(&nfs_client_lock); | |
259902ea | 1664 | return seq_list_start_head(&nfs_volume_list, *_pos); |
6aaca566 DH |
1665 | } |
1666 | ||
1667 | /* | |
1668 | * move to next volume | |
1669 | */ | |
1670 | static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos) | |
1671 | { | |
259902ea | 1672 | return seq_list_next(v, &nfs_volume_list, pos); |
6aaca566 DH |
1673 | } |
1674 | ||
1675 | /* | |
1676 | * clean up after reading from the transports list | |
1677 | */ | |
1678 | static void nfs_volume_list_stop(struct seq_file *p, void *v) | |
1679 | { | |
1680 | spin_unlock(&nfs_client_lock); | |
1681 | } | |
1682 | ||
1683 | /* | |
1684 | * display a header line followed by a load of call lines | |
1685 | */ | |
1686 | static int nfs_volume_list_show(struct seq_file *m, void *v) | |
1687 | { | |
1688 | struct nfs_server *server; | |
1689 | struct nfs_client *clp; | |
1690 | char dev[8], fsid[17]; | |
1691 | ||
1692 | /* display header on line 1 */ | |
259902ea | 1693 | if (v == &nfs_volume_list) { |
5d1acff1 | 1694 | seq_puts(m, "NV SERVER PORT DEV FSID FSC\n"); |
6aaca566 DH |
1695 | return 0; |
1696 | } | |
1697 | /* display one transport per line on subsequent lines */ | |
1698 | server = list_entry(v, struct nfs_server, master_link); | |
1699 | clp = server->nfs_client; | |
1700 | ||
1701 | snprintf(dev, 8, "%u:%u", | |
1702 | MAJOR(server->s_dev), MINOR(server->s_dev)); | |
1703 | ||
1704 | snprintf(fsid, 17, "%llx:%llx", | |
6daabf1b DH |
1705 | (unsigned long long) server->fsid.major, |
1706 | (unsigned long long) server->fsid.minor); | |
6aaca566 | 1707 | |
5d1acff1 | 1708 | seq_printf(m, "v%u %s %s %-7s %-17s %s\n", |
40c55319 | 1709 | clp->rpc_ops->version, |
5d8515ca CL |
1710 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), |
1711 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), | |
6aaca566 | 1712 | dev, |
5d1acff1 DH |
1713 | fsid, |
1714 | nfs_server_fscache_state(server)); | |
6aaca566 DH |
1715 | |
1716 | return 0; | |
1717 | } | |
1718 | ||
1719 | /* | |
1720 | * initialise the /proc/fs/nfsfs/ directory | |
1721 | */ | |
1722 | int __init nfs_fs_proc_init(void) | |
1723 | { | |
1724 | struct proc_dir_entry *p; | |
1725 | ||
36a5aeb8 | 1726 | proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL); |
6aaca566 DH |
1727 | if (!proc_fs_nfs) |
1728 | goto error_0; | |
1729 | ||
6aaca566 | 1730 | /* a file of servers with which we're dealing */ |
34b37235 DL |
1731 | p = proc_create("servers", S_IFREG|S_IRUGO, |
1732 | proc_fs_nfs, &nfs_server_list_fops); | |
6aaca566 DH |
1733 | if (!p) |
1734 | goto error_1; | |
1735 | ||
6aaca566 | 1736 | /* a file of volumes that we have mounted */ |
34b37235 DL |
1737 | p = proc_create("volumes", S_IFREG|S_IRUGO, |
1738 | proc_fs_nfs, &nfs_volume_list_fops); | |
6aaca566 DH |
1739 | if (!p) |
1740 | goto error_2; | |
6aaca566 DH |
1741 | return 0; |
1742 | ||
1743 | error_2: | |
1744 | remove_proc_entry("servers", proc_fs_nfs); | |
1745 | error_1: | |
36a5aeb8 | 1746 | remove_proc_entry("fs/nfsfs", NULL); |
6aaca566 DH |
1747 | error_0: |
1748 | return -ENOMEM; | |
1749 | } | |
1750 | ||
1751 | /* | |
1752 | * clean up the /proc/fs/nfsfs/ directory | |
1753 | */ | |
1754 | void nfs_fs_proc_exit(void) | |
1755 | { | |
1756 | remove_proc_entry("volumes", proc_fs_nfs); | |
1757 | remove_proc_entry("servers", proc_fs_nfs); | |
36a5aeb8 | 1758 | remove_proc_entry("fs/nfsfs", NULL); |
6aaca566 DH |
1759 | } |
1760 | ||
1761 | #endif /* CONFIG_PROC_FS */ |