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