Commit | Line | Data |
---|---|---|
428360d7 BS |
1 | /* |
2 | * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. | |
3 | * Written by David Howells (dhowells@redhat.com) | |
4 | */ | |
fcf10398 | 5 | #include <linux/module.h> |
428360d7 BS |
6 | #include <linux/nfs_fs.h> |
7 | #include <linux/nfs_idmap.h> | |
fcf10398 | 8 | #include <linux/nfs_mount.h> |
428360d7 BS |
9 | #include <linux/sunrpc/auth.h> |
10 | #include <linux/sunrpc/xprt.h> | |
11 | #include <linux/sunrpc/bc_xprt.h> | |
12 | #include "internal.h" | |
13 | #include "callback.h" | |
fcf10398 BS |
14 | #include "delegation.h" |
15 | #include "pnfs.h" | |
16 | #include "netns.h" | |
428360d7 BS |
17 | |
18 | #define NFSDBG_FACILITY NFSDBG_CLIENT | |
19 | ||
ec409897 BS |
20 | /* |
21 | * Get a unique NFSv4.0 callback identifier which will be used | |
22 | * by the V4.0 callback service to lookup the nfs_client struct | |
23 | */ | |
24 | static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion) | |
25 | { | |
26 | int ret = 0; | |
27 | struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); | |
28 | ||
29 | if (clp->rpc_ops->version != 4 || minorversion != 0) | |
30 | return ret; | |
31 | retry: | |
32 | if (!idr_pre_get(&nn->cb_ident_idr, GFP_KERNEL)) | |
33 | return -ENOMEM; | |
34 | spin_lock(&nn->nfs_client_lock); | |
35 | ret = idr_get_new(&nn->cb_ident_idr, clp, &clp->cl_cb_ident); | |
36 | spin_unlock(&nn->nfs_client_lock); | |
37 | if (ret == -EAGAIN) | |
38 | goto retry; | |
39 | return ret; | |
40 | } | |
41 | ||
42 | #ifdef CONFIG_NFS_V4_1 | |
43 | static void nfs4_shutdown_session(struct nfs_client *clp) | |
44 | { | |
45 | if (nfs4_has_session(clp)) { | |
46 | nfs4_destroy_session(clp->cl_session); | |
47 | nfs4_destroy_clientid(clp); | |
48 | } | |
49 | ||
50 | } | |
51 | #else /* CONFIG_NFS_V4_1 */ | |
52 | static void nfs4_shutdown_session(struct nfs_client *clp) | |
53 | { | |
54 | } | |
55 | #endif /* CONFIG_NFS_V4_1 */ | |
56 | ||
57 | struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init) | |
58 | { | |
59 | int err; | |
60 | struct nfs_client *clp = nfs_alloc_client(cl_init); | |
61 | if (IS_ERR(clp)) | |
62 | return clp; | |
63 | ||
64 | err = nfs_get_cb_ident_idr(clp, cl_init->minorversion); | |
65 | if (err) | |
66 | goto error; | |
67 | ||
68 | spin_lock_init(&clp->cl_lock); | |
69 | INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state); | |
70 | rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client"); | |
71 | clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED; | |
72 | clp->cl_minorversion = cl_init->minorversion; | |
73 | clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion]; | |
74 | return clp; | |
75 | ||
76 | error: | |
7653f6ff | 77 | nfs_free_client(clp); |
ec409897 BS |
78 | return ERR_PTR(err); |
79 | } | |
80 | ||
81 | /* | |
82 | * Destroy the NFS4 callback service | |
83 | */ | |
84 | static void nfs4_destroy_callback(struct nfs_client *clp) | |
85 | { | |
86 | if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state)) | |
c946556b | 87 | nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net); |
ec409897 BS |
88 | } |
89 | ||
90 | static void nfs4_shutdown_client(struct nfs_client *clp) | |
91 | { | |
92 | if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state)) | |
93 | nfs4_kill_renewd(clp); | |
94 | nfs4_shutdown_session(clp); | |
95 | nfs4_destroy_callback(clp); | |
96 | if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state)) | |
97 | nfs_idmap_delete(clp); | |
98 | ||
99 | rpc_destroy_wait_queue(&clp->cl_rpcwaitq); | |
100 | kfree(clp->cl_serverowner); | |
101 | kfree(clp->cl_serverscope); | |
102 | kfree(clp->cl_implid); | |
103 | } | |
104 | ||
105 | void nfs4_free_client(struct nfs_client *clp) | |
106 | { | |
107 | nfs4_shutdown_client(clp); | |
108 | nfs_free_client(clp); | |
109 | } | |
110 | ||
428360d7 BS |
111 | /* |
112 | * Initialize the NFS4 callback service | |
113 | */ | |
114 | static int nfs4_init_callback(struct nfs_client *clp) | |
115 | { | |
116 | int error; | |
117 | ||
118 | if (clp->rpc_ops->version == 4) { | |
119 | struct rpc_xprt *xprt; | |
120 | ||
121 | xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt); | |
122 | ||
123 | if (nfs4_has_session(clp)) { | |
124 | error = xprt_setup_backchannel(xprt, | |
125 | NFS41_BC_MIN_CALLBACKS); | |
126 | if (error < 0) | |
127 | return error; | |
128 | } | |
129 | ||
130 | error = nfs_callback_up(clp->cl_mvops->minor_version, xprt); | |
131 | if (error < 0) { | |
132 | dprintk("%s: failed to start callback. Error = %d\n", | |
133 | __func__, error); | |
134 | return error; | |
135 | } | |
136 | __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state); | |
137 | } | |
138 | return 0; | |
139 | } | |
140 | ||
141 | /* | |
142 | * Initialize the minor version specific parts of an NFS4 client record | |
143 | */ | |
144 | static int nfs4_init_client_minor_version(struct nfs_client *clp) | |
145 | { | |
146 | #if defined(CONFIG_NFS_V4_1) | |
147 | if (clp->cl_mvops->minor_version) { | |
148 | struct nfs4_session *session = NULL; | |
149 | /* | |
150 | * Create the session and mark it expired. | |
151 | * When a SEQUENCE operation encounters the expired session | |
152 | * it will do session recovery to initialize it. | |
153 | */ | |
154 | session = nfs4_alloc_session(clp); | |
155 | if (!session) | |
156 | return -ENOMEM; | |
157 | ||
158 | clp->cl_session = session; | |
159 | /* | |
160 | * The create session reply races with the server back | |
161 | * channel probe. Mark the client NFS_CS_SESSION_INITING | |
162 | * so that the client back channel can find the | |
163 | * nfs_client struct | |
164 | */ | |
165 | nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING); | |
166 | } | |
167 | #endif /* CONFIG_NFS_V4_1 */ | |
168 | ||
169 | return nfs4_init_callback(clp); | |
170 | } | |
171 | ||
172 | /** | |
173 | * nfs4_init_client - Initialise an NFS4 client record | |
174 | * | |
175 | * @clp: nfs_client to initialise | |
176 | * @timeparms: timeout parameters for underlying RPC transport | |
177 | * @ip_addr: callback IP address in presentation format | |
178 | * @authflavor: authentication flavor for underlying RPC transport | |
179 | * | |
180 | * Returns pointer to an NFS client, or an ERR_PTR value. | |
181 | */ | |
182 | struct nfs_client *nfs4_init_client(struct nfs_client *clp, | |
183 | const struct rpc_timeout *timeparms, | |
184 | const char *ip_addr, | |
185 | rpc_authflavor_t authflavour) | |
186 | { | |
187 | char buf[INET6_ADDRSTRLEN + 1]; | |
05f4c350 | 188 | struct nfs_client *old; |
428360d7 BS |
189 | int error; |
190 | ||
191 | if (clp->cl_cons_state == NFS_CS_READY) { | |
192 | /* the client is initialised already */ | |
193 | dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp); | |
194 | return clp; | |
195 | } | |
196 | ||
197 | /* Check NFS protocol revision and initialize RPC op vector */ | |
198 | clp->rpc_ops = &nfs_v4_clientops; | |
199 | ||
200 | __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags); | |
201 | error = nfs_create_rpc_client(clp, timeparms, authflavour); | |
202 | if (error < 0) | |
203 | goto error; | |
204 | ||
205 | /* If no clientaddr= option was specified, find a usable cb address */ | |
206 | if (ip_addr == NULL) { | |
207 | struct sockaddr_storage cb_addr; | |
208 | struct sockaddr *sap = (struct sockaddr *)&cb_addr; | |
209 | ||
210 | error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr)); | |
211 | if (error < 0) | |
212 | goto error; | |
213 | error = rpc_ntop(sap, buf, sizeof(buf)); | |
214 | if (error < 0) | |
215 | goto error; | |
216 | ip_addr = (const char *)buf; | |
217 | } | |
218 | strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr)); | |
219 | ||
220 | error = nfs_idmap_new(clp); | |
221 | if (error < 0) { | |
222 | dprintk("%s: failed to create idmapper. Error = %d\n", | |
223 | __func__, error); | |
224 | goto error; | |
225 | } | |
226 | __set_bit(NFS_CS_IDMAP, &clp->cl_res_state); | |
227 | ||
228 | error = nfs4_init_client_minor_version(clp); | |
229 | if (error < 0) | |
230 | goto error; | |
231 | ||
232 | if (!nfs4_has_session(clp)) | |
233 | nfs_mark_client_ready(clp, NFS_CS_READY); | |
05f4c350 CL |
234 | |
235 | error = nfs4_discover_server_trunking(clp, &old); | |
236 | if (error < 0) | |
237 | goto error; | |
238 | if (clp != old) { | |
239 | clp->cl_preserve_clid = true; | |
240 | nfs_put_client(clp); | |
241 | clp = old; | |
242 | atomic_inc(&clp->cl_count); | |
243 | } | |
244 | ||
428360d7 BS |
245 | return clp; |
246 | ||
247 | error: | |
248 | nfs_mark_client_ready(clp, error); | |
249 | nfs_put_client(clp); | |
250 | dprintk("<-- nfs4_init_client() = xerror %d\n", error); | |
251 | return ERR_PTR(error); | |
252 | } | |
fcf10398 | 253 | |
05f4c350 CL |
254 | /* |
255 | * Returns true if the client IDs match | |
256 | */ | |
257 | static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b) | |
258 | { | |
259 | if (a->cl_clientid != b->cl_clientid) { | |
260 | dprintk("NFS: --> %s client ID %llx does not match %llx\n", | |
261 | __func__, a->cl_clientid, b->cl_clientid); | |
262 | return false; | |
263 | } | |
264 | dprintk("NFS: --> %s client ID %llx matches %llx\n", | |
265 | __func__, a->cl_clientid, b->cl_clientid); | |
266 | return true; | |
267 | } | |
268 | ||
269 | /* | |
270 | * SETCLIENTID just did a callback update with the callback ident in | |
271 | * "drop," but server trunking discovery claims "drop" and "keep" are | |
272 | * actually the same server. Swap the callback IDs so that "keep" | |
273 | * will continue to use the callback ident the server now knows about, | |
274 | * and so that "keep"'s original callback ident is destroyed when | |
275 | * "drop" is freed. | |
276 | */ | |
277 | static void nfs4_swap_callback_idents(struct nfs_client *keep, | |
278 | struct nfs_client *drop) | |
279 | { | |
280 | struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id); | |
281 | unsigned int save = keep->cl_cb_ident; | |
282 | ||
283 | if (keep->cl_cb_ident == drop->cl_cb_ident) | |
284 | return; | |
285 | ||
286 | dprintk("%s: keeping callback ident %u and dropping ident %u\n", | |
287 | __func__, keep->cl_cb_ident, drop->cl_cb_ident); | |
288 | ||
289 | spin_lock(&nn->nfs_client_lock); | |
290 | ||
291 | idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident); | |
292 | keep->cl_cb_ident = drop->cl_cb_ident; | |
293 | ||
294 | idr_replace(&nn->cb_ident_idr, drop, save); | |
295 | drop->cl_cb_ident = save; | |
296 | ||
297 | spin_unlock(&nn->nfs_client_lock); | |
298 | } | |
299 | ||
300 | /** | |
301 | * nfs40_walk_client_list - Find server that recognizes a client ID | |
302 | * | |
303 | * @new: nfs_client with client ID to test | |
304 | * @result: OUT: found nfs_client, or new | |
305 | * @cred: credential to use for trunking test | |
306 | * | |
307 | * Returns zero, a negative errno, or a negative NFS4ERR status. | |
308 | * If zero is returned, an nfs_client pointer is planted in "result." | |
309 | * | |
310 | * NB: nfs40_walk_client_list() relies on the new nfs_client being | |
311 | * the last nfs_client on the list. | |
312 | */ | |
313 | int nfs40_walk_client_list(struct nfs_client *new, | |
314 | struct nfs_client **result, | |
315 | struct rpc_cred *cred) | |
316 | { | |
317 | struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id); | |
318 | struct nfs_client *pos, *n, *prev = NULL; | |
319 | struct nfs4_setclientid_res clid = { | |
320 | .clientid = new->cl_clientid, | |
321 | .confirm = new->cl_confirm, | |
322 | }; | |
323 | int status; | |
324 | ||
325 | spin_lock(&nn->nfs_client_lock); | |
326 | list_for_each_entry_safe(pos, n, &nn->nfs_client_list, cl_share_link) { | |
327 | /* If "pos" isn't marked ready, we can't trust the | |
328 | * remaining fields in "pos" */ | |
329 | if (pos->cl_cons_state < NFS_CS_READY) | |
330 | continue; | |
331 | ||
332 | if (pos->rpc_ops != new->rpc_ops) | |
333 | continue; | |
334 | ||
335 | if (pos->cl_proto != new->cl_proto) | |
336 | continue; | |
337 | ||
338 | if (pos->cl_minorversion != new->cl_minorversion) | |
339 | continue; | |
340 | ||
341 | if (pos->cl_clientid != new->cl_clientid) | |
342 | continue; | |
343 | ||
344 | atomic_inc(&pos->cl_count); | |
345 | spin_unlock(&nn->nfs_client_lock); | |
346 | ||
347 | if (prev) | |
348 | nfs_put_client(prev); | |
349 | ||
350 | status = nfs4_proc_setclientid_confirm(pos, &clid, cred); | |
351 | if (status == 0) { | |
352 | nfs4_swap_callback_idents(pos, new); | |
353 | ||
354 | nfs_put_client(pos); | |
355 | *result = pos; | |
356 | dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n", | |
357 | __func__, pos, atomic_read(&pos->cl_count)); | |
358 | return 0; | |
359 | } | |
360 | if (status != -NFS4ERR_STALE_CLIENTID) { | |
361 | nfs_put_client(pos); | |
362 | dprintk("NFS: <-- %s status = %d, no result\n", | |
363 | __func__, status); | |
364 | return status; | |
365 | } | |
366 | ||
367 | spin_lock(&nn->nfs_client_lock); | |
368 | prev = pos; | |
369 | } | |
370 | ||
371 | /* | |
372 | * No matching nfs_client found. This should be impossible, | |
373 | * because the new nfs_client has already been added to | |
374 | * nfs_client_list by nfs_get_client(). | |
375 | * | |
376 | * Don't BUG(), since the caller is holding a mutex. | |
377 | */ | |
378 | if (prev) | |
379 | nfs_put_client(prev); | |
380 | spin_unlock(&nn->nfs_client_lock); | |
381 | pr_err("NFS: %s Error: no matching nfs_client found\n", __func__); | |
382 | return -NFS4ERR_STALE_CLIENTID; | |
383 | } | |
384 | ||
385 | #ifdef CONFIG_NFS_V4_1 | |
386 | /* | |
387 | * Returns true if the server owners match | |
388 | */ | |
389 | static bool | |
390 | nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b) | |
391 | { | |
392 | struct nfs41_server_owner *o1 = a->cl_serverowner; | |
393 | struct nfs41_server_owner *o2 = b->cl_serverowner; | |
394 | ||
395 | if (o1->minor_id != o2->minor_id) { | |
396 | dprintk("NFS: --> %s server owner minor IDs do not match\n", | |
397 | __func__); | |
398 | return false; | |
399 | } | |
400 | ||
401 | if (o1->major_id_sz != o2->major_id_sz) | |
402 | goto out_major_mismatch; | |
403 | if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0) | |
404 | goto out_major_mismatch; | |
405 | ||
406 | dprintk("NFS: --> %s server owners match\n", __func__); | |
407 | return true; | |
408 | ||
409 | out_major_mismatch: | |
410 | dprintk("NFS: --> %s server owner major IDs do not match\n", | |
411 | __func__); | |
412 | return false; | |
413 | } | |
414 | ||
415 | /** | |
416 | * nfs41_walk_client_list - Find nfs_client that matches a client/server owner | |
417 | * | |
418 | * @new: nfs_client with client ID to test | |
419 | * @result: OUT: found nfs_client, or new | |
420 | * @cred: credential to use for trunking test | |
421 | * | |
422 | * Returns zero, a negative errno, or a negative NFS4ERR status. | |
423 | * If zero is returned, an nfs_client pointer is planted in "result." | |
424 | * | |
425 | * NB: nfs41_walk_client_list() relies on the new nfs_client being | |
426 | * the last nfs_client on the list. | |
427 | */ | |
428 | int nfs41_walk_client_list(struct nfs_client *new, | |
429 | struct nfs_client **result, | |
430 | struct rpc_cred *cred) | |
431 | { | |
432 | struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id); | |
433 | struct nfs_client *pos, *n, *prev = NULL; | |
434 | int error; | |
435 | ||
436 | spin_lock(&nn->nfs_client_lock); | |
437 | list_for_each_entry_safe(pos, n, &nn->nfs_client_list, cl_share_link) { | |
438 | /* If "pos" isn't marked ready, we can't trust the | |
439 | * remaining fields in "pos", especially the client | |
440 | * ID and serverowner fields. Wait for CREATE_SESSION | |
441 | * to finish. */ | |
442 | if (pos->cl_cons_state < NFS_CS_READY) { | |
443 | atomic_inc(&pos->cl_count); | |
444 | spin_unlock(&nn->nfs_client_lock); | |
445 | ||
446 | if (prev) | |
447 | nfs_put_client(prev); | |
448 | prev = pos; | |
449 | ||
450 | error = nfs_wait_client_init_complete(pos); | |
451 | if (error < 0) { | |
452 | nfs_put_client(pos); | |
453 | continue; | |
454 | } | |
455 | ||
456 | spin_lock(&nn->nfs_client_lock); | |
457 | } | |
458 | ||
459 | if (pos->rpc_ops != new->rpc_ops) | |
460 | continue; | |
461 | ||
462 | if (pos->cl_proto != new->cl_proto) | |
463 | continue; | |
464 | ||
465 | if (pos->cl_minorversion != new->cl_minorversion) | |
466 | continue; | |
467 | ||
468 | if (!nfs4_match_clientids(pos, new)) | |
469 | continue; | |
470 | ||
471 | if (!nfs4_match_serverowners(pos, new)) | |
472 | continue; | |
473 | ||
474 | spin_unlock(&nn->nfs_client_lock); | |
475 | dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n", | |
476 | __func__, pos, atomic_read(&pos->cl_count)); | |
477 | ||
478 | *result = pos; | |
479 | return 0; | |
480 | } | |
481 | ||
482 | /* | |
483 | * No matching nfs_client found. This should be impossible, | |
484 | * because the new nfs_client has already been added to | |
485 | * nfs_client_list by nfs_get_client(). | |
486 | * | |
487 | * Don't BUG(), since the caller is holding a mutex. | |
488 | */ | |
489 | spin_unlock(&nn->nfs_client_lock); | |
490 | pr_err("NFS: %s Error: no matching nfs_client found\n", __func__); | |
491 | return -NFS4ERR_STALE_CLIENTID; | |
492 | } | |
493 | #endif /* CONFIG_NFS_V4_1 */ | |
494 | ||
fcf10398 BS |
495 | static void nfs4_destroy_server(struct nfs_server *server) |
496 | { | |
497 | nfs_server_return_all_delegations(server); | |
498 | unset_pnfs_layoutdriver(server); | |
499 | nfs4_purge_state_owners(server); | |
500 | } | |
501 | ||
502 | /* | |
503 | * NFSv4.0 callback thread helper | |
504 | * | |
505 | * Find a client by callback identifier | |
506 | */ | |
507 | struct nfs_client * | |
508 | nfs4_find_client_ident(struct net *net, int cb_ident) | |
509 | { | |
510 | struct nfs_client *clp; | |
511 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
512 | ||
513 | spin_lock(&nn->nfs_client_lock); | |
514 | clp = idr_find(&nn->cb_ident_idr, cb_ident); | |
515 | if (clp) | |
516 | atomic_inc(&clp->cl_count); | |
517 | spin_unlock(&nn->nfs_client_lock); | |
518 | return clp; | |
519 | } | |
520 | ||
521 | #if defined(CONFIG_NFS_V4_1) | |
522 | /* Common match routine for v4.0 and v4.1 callback services */ | |
523 | static bool nfs4_cb_match_client(const struct sockaddr *addr, | |
524 | struct nfs_client *clp, u32 minorversion) | |
525 | { | |
526 | struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; | |
527 | ||
528 | /* Don't match clients that failed to initialise */ | |
529 | if (!(clp->cl_cons_state == NFS_CS_READY || | |
530 | clp->cl_cons_state == NFS_CS_SESSION_INITING)) | |
531 | return false; | |
532 | ||
533 | smp_rmb(); | |
534 | ||
535 | /* Match the version and minorversion */ | |
536 | if (clp->rpc_ops->version != 4 || | |
537 | clp->cl_minorversion != minorversion) | |
538 | return false; | |
539 | ||
540 | /* Match only the IP address, not the port number */ | |
541 | if (!nfs_sockaddr_match_ipaddr(addr, clap)) | |
542 | return false; | |
543 | ||
544 | return true; | |
545 | } | |
546 | ||
547 | /* | |
548 | * NFSv4.1 callback thread helper | |
549 | * For CB_COMPOUND calls, find a client by IP address, protocol version, | |
550 | * minorversion, and sessionID | |
551 | * | |
552 | * Returns NULL if no such client | |
553 | */ | |
554 | struct nfs_client * | |
555 | nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr, | |
556 | struct nfs4_sessionid *sid) | |
557 | { | |
558 | struct nfs_client *clp; | |
559 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
560 | ||
561 | spin_lock(&nn->nfs_client_lock); | |
562 | list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { | |
563 | if (nfs4_cb_match_client(addr, clp, 1) == false) | |
564 | continue; | |
565 | ||
566 | if (!nfs4_has_session(clp)) | |
567 | continue; | |
568 | ||
569 | /* Match sessionid*/ | |
570 | if (memcmp(clp->cl_session->sess_id.data, | |
571 | sid->data, NFS4_MAX_SESSIONID_LEN) != 0) | |
572 | continue; | |
573 | ||
574 | atomic_inc(&clp->cl_count); | |
575 | spin_unlock(&nn->nfs_client_lock); | |
576 | return clp; | |
577 | } | |
578 | spin_unlock(&nn->nfs_client_lock); | |
579 | return NULL; | |
580 | } | |
581 | ||
582 | #else /* CONFIG_NFS_V4_1 */ | |
583 | ||
584 | struct nfs_client * | |
585 | nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr, | |
586 | struct nfs4_sessionid *sid) | |
587 | { | |
588 | return NULL; | |
589 | } | |
590 | #endif /* CONFIG_NFS_V4_1 */ | |
591 | ||
592 | /* | |
593 | * Set up an NFS4 client | |
594 | */ | |
595 | static int nfs4_set_client(struct nfs_server *server, | |
596 | const char *hostname, | |
597 | const struct sockaddr *addr, | |
598 | const size_t addrlen, | |
599 | const char *ip_addr, | |
600 | rpc_authflavor_t authflavour, | |
601 | int proto, const struct rpc_timeout *timeparms, | |
602 | u32 minorversion, struct net *net) | |
603 | { | |
604 | struct nfs_client_initdata cl_init = { | |
605 | .hostname = hostname, | |
606 | .addr = addr, | |
607 | .addrlen = addrlen, | |
ab7017a3 | 608 | .nfs_mod = &nfs_v4, |
fcf10398 BS |
609 | .proto = proto, |
610 | .minorversion = minorversion, | |
611 | .net = net, | |
612 | }; | |
613 | struct nfs_client *clp; | |
614 | int error; | |
615 | ||
616 | dprintk("--> nfs4_set_client()\n"); | |
617 | ||
618 | if (server->flags & NFS_MOUNT_NORESVPORT) | |
619 | set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags); | |
620 | ||
621 | /* Allocate or find a client reference we can use */ | |
622 | clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour); | |
623 | if (IS_ERR(clp)) { | |
624 | error = PTR_ERR(clp); | |
625 | goto error; | |
626 | } | |
627 | ||
628 | /* | |
629 | * Query for the lease time on clientid setup or renewal | |
630 | * | |
631 | * Note that this will be set on nfs_clients that were created | |
632 | * only for the DS role and did not set this bit, but now will | |
633 | * serve a dual role. | |
634 | */ | |
635 | set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state); | |
636 | ||
637 | server->nfs_client = clp; | |
638 | dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp); | |
639 | return 0; | |
640 | error: | |
641 | dprintk("<-- nfs4_set_client() = xerror %d\n", error); | |
642 | return error; | |
643 | } | |
644 | ||
645 | /* | |
646 | * Set up a pNFS Data Server client. | |
647 | * | |
648 | * Return any existing nfs_client that matches server address,port,version | |
649 | * and minorversion. | |
650 | * | |
651 | * For a new nfs_client, use a soft mount (default), a low retrans and a | |
652 | * low timeout interval so that if a connection is lost, we retry through | |
653 | * the MDS. | |
654 | */ | |
655 | struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp, | |
656 | const struct sockaddr *ds_addr, int ds_addrlen, | |
657 | int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans) | |
658 | { | |
659 | struct nfs_client_initdata cl_init = { | |
660 | .addr = ds_addr, | |
661 | .addrlen = ds_addrlen, | |
ab7017a3 | 662 | .nfs_mod = &nfs_v4, |
fcf10398 BS |
663 | .proto = ds_proto, |
664 | .minorversion = mds_clp->cl_minorversion, | |
665 | .net = mds_clp->cl_net, | |
666 | }; | |
667 | struct rpc_timeout ds_timeout; | |
668 | struct nfs_client *clp; | |
669 | ||
670 | /* | |
671 | * Set an authflavor equual to the MDS value. Use the MDS nfs_client | |
672 | * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS | |
673 | * (section 13.1 RFC 5661). | |
674 | */ | |
675 | nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans); | |
676 | clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr, | |
677 | mds_clp->cl_rpcclient->cl_auth->au_flavor); | |
678 | ||
679 | dprintk("<-- %s %p\n", __func__, clp); | |
680 | return clp; | |
681 | } | |
682 | EXPORT_SYMBOL_GPL(nfs4_set_ds_client); | |
683 | ||
684 | /* | |
685 | * Session has been established, and the client marked ready. | |
686 | * Set the mount rsize and wsize with negotiated fore channel | |
687 | * attributes which will be bound checked in nfs_server_set_fsinfo. | |
688 | */ | |
689 | static void nfs4_session_set_rwsize(struct nfs_server *server) | |
690 | { | |
691 | #ifdef CONFIG_NFS_V4_1 | |
692 | struct nfs4_session *sess; | |
693 | u32 server_resp_sz; | |
694 | u32 server_rqst_sz; | |
695 | ||
696 | if (!nfs4_has_session(server->nfs_client)) | |
697 | return; | |
698 | sess = server->nfs_client->cl_session; | |
699 | server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead; | |
700 | server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead; | |
701 | ||
702 | if (server->rsize > server_resp_sz) | |
703 | server->rsize = server_resp_sz; | |
704 | if (server->wsize > server_rqst_sz) | |
705 | server->wsize = server_rqst_sz; | |
706 | #endif /* CONFIG_NFS_V4_1 */ | |
707 | } | |
708 | ||
709 | static int nfs4_server_common_setup(struct nfs_server *server, | |
710 | struct nfs_fh *mntfh) | |
711 | { | |
712 | struct nfs_fattr *fattr; | |
713 | int error; | |
714 | ||
715 | BUG_ON(!server->nfs_client); | |
716 | BUG_ON(!server->nfs_client->rpc_ops); | |
717 | BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); | |
718 | ||
719 | /* data servers support only a subset of NFSv4.1 */ | |
720 | if (is_ds_only_client(server->nfs_client)) | |
721 | return -EPROTONOSUPPORT; | |
722 | ||
723 | fattr = nfs_alloc_fattr(); | |
724 | if (fattr == NULL) | |
725 | return -ENOMEM; | |
726 | ||
727 | /* We must ensure the session is initialised first */ | |
728 | error = nfs4_init_session(server); | |
729 | if (error < 0) | |
730 | goto out; | |
731 | ||
732 | /* Probe the root fh to retrieve its FSID and filehandle */ | |
733 | error = nfs4_get_rootfh(server, mntfh); | |
734 | if (error < 0) | |
735 | goto out; | |
736 | ||
737 | dprintk("Server FSID: %llx:%llx\n", | |
738 | (unsigned long long) server->fsid.major, | |
739 | (unsigned long long) server->fsid.minor); | |
740 | dprintk("Mount FH: %d\n", mntfh->size); | |
741 | ||
742 | nfs4_session_set_rwsize(server); | |
743 | ||
744 | error = nfs_probe_fsinfo(server, mntfh, fattr); | |
745 | if (error < 0) | |
746 | goto out; | |
747 | ||
748 | if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) | |
749 | server->namelen = NFS4_MAXNAMLEN; | |
750 | ||
751 | nfs_server_insert_lists(server); | |
752 | server->mount_time = jiffies; | |
753 | server->destroy = nfs4_destroy_server; | |
754 | out: | |
755 | nfs_free_fattr(fattr); | |
756 | return error; | |
757 | } | |
758 | ||
759 | /* | |
760 | * Create a version 4 volume record | |
761 | */ | |
762 | static int nfs4_init_server(struct nfs_server *server, | |
763 | const struct nfs_parsed_mount_data *data) | |
764 | { | |
765 | struct rpc_timeout timeparms; | |
766 | int error; | |
767 | ||
768 | dprintk("--> nfs4_init_server()\n"); | |
769 | ||
770 | nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, | |
771 | data->timeo, data->retrans); | |
772 | ||
773 | /* Initialise the client representation from the mount data */ | |
774 | server->flags = data->flags; | |
775 | server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK; | |
776 | if (!(data->flags & NFS_MOUNT_NORDIRPLUS)) | |
777 | server->caps |= NFS_CAP_READDIRPLUS; | |
778 | server->options = data->options; | |
779 | ||
780 | /* Get a client record */ | |
781 | error = nfs4_set_client(server, | |
782 | data->nfs_server.hostname, | |
783 | (const struct sockaddr *)&data->nfs_server.address, | |
784 | data->nfs_server.addrlen, | |
785 | data->client_address, | |
786 | data->auth_flavors[0], | |
787 | data->nfs_server.protocol, | |
788 | &timeparms, | |
789 | data->minorversion, | |
790 | data->net); | |
791 | if (error < 0) | |
792 | goto error; | |
793 | ||
794 | /* | |
795 | * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower | |
796 | * authentication. | |
797 | */ | |
798 | if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX) | |
799 | server->caps |= NFS_CAP_UIDGID_NOMAP; | |
800 | ||
801 | if (data->rsize) | |
802 | server->rsize = nfs_block_size(data->rsize, NULL); | |
803 | if (data->wsize) | |
804 | server->wsize = nfs_block_size(data->wsize, NULL); | |
805 | ||
806 | server->acregmin = data->acregmin * HZ; | |
807 | server->acregmax = data->acregmax * HZ; | |
808 | server->acdirmin = data->acdirmin * HZ; | |
809 | server->acdirmax = data->acdirmax * HZ; | |
810 | ||
811 | server->port = data->nfs_server.port; | |
812 | ||
813 | error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]); | |
814 | ||
815 | error: | |
816 | /* Done */ | |
817 | dprintk("<-- nfs4_init_server() = %d\n", error); | |
818 | return error; | |
819 | } | |
820 | ||
821 | /* | |
822 | * Create a version 4 volume record | |
823 | * - keyed on server and FSID | |
824 | */ | |
1179acc6 BS |
825 | /*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data, |
826 | struct nfs_fh *mntfh)*/ | |
827 | struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info, | |
828 | struct nfs_subversion *nfs_mod) | |
fcf10398 BS |
829 | { |
830 | struct nfs_server *server; | |
831 | int error; | |
832 | ||
833 | dprintk("--> nfs4_create_server()\n"); | |
834 | ||
835 | server = nfs_alloc_server(); | |
836 | if (!server) | |
837 | return ERR_PTR(-ENOMEM); | |
838 | ||
839 | /* set up the general RPC client */ | |
1179acc6 | 840 | error = nfs4_init_server(server, mount_info->parsed); |
fcf10398 BS |
841 | if (error < 0) |
842 | goto error; | |
843 | ||
1179acc6 | 844 | error = nfs4_server_common_setup(server, mount_info->mntfh); |
fcf10398 BS |
845 | if (error < 0) |
846 | goto error; | |
847 | ||
848 | dprintk("<-- nfs4_create_server() = %p\n", server); | |
849 | return server; | |
850 | ||
851 | error: | |
852 | nfs_free_server(server); | |
853 | dprintk("<-- nfs4_create_server() = error %d\n", error); | |
854 | return ERR_PTR(error); | |
855 | } | |
856 | ||
857 | /* | |
858 | * Create an NFS4 referral server record | |
859 | */ | |
860 | struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data, | |
861 | struct nfs_fh *mntfh) | |
862 | { | |
863 | struct nfs_client *parent_client; | |
864 | struct nfs_server *server, *parent_server; | |
865 | int error; | |
866 | ||
867 | dprintk("--> nfs4_create_referral_server()\n"); | |
868 | ||
869 | server = nfs_alloc_server(); | |
870 | if (!server) | |
871 | return ERR_PTR(-ENOMEM); | |
872 | ||
873 | parent_server = NFS_SB(data->sb); | |
874 | parent_client = parent_server->nfs_client; | |
875 | ||
876 | /* Initialise the client representation from the parent server */ | |
877 | nfs_server_copy_userdata(server, parent_server); | |
878 | server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR; | |
879 | ||
880 | /* Get a client representation. | |
881 | * Note: NFSv4 always uses TCP, */ | |
882 | error = nfs4_set_client(server, data->hostname, | |
883 | data->addr, | |
884 | data->addrlen, | |
885 | parent_client->cl_ipaddr, | |
886 | data->authflavor, | |
887 | rpc_protocol(parent_server->client), | |
888 | parent_server->client->cl_timeout, | |
889 | parent_client->cl_mvops->minor_version, | |
890 | parent_client->cl_net); | |
891 | if (error < 0) | |
892 | goto error; | |
893 | ||
894 | error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor); | |
895 | if (error < 0) | |
896 | goto error; | |
897 | ||
898 | error = nfs4_server_common_setup(server, mntfh); | |
899 | if (error < 0) | |
900 | goto error; | |
901 | ||
902 | dprintk("<-- nfs_create_referral_server() = %p\n", server); | |
903 | return server; | |
904 | ||
905 | error: | |
906 | nfs_free_server(server); | |
907 | dprintk("<-- nfs4_create_referral_server() = error %d\n", error); | |
908 | return ERR_PTR(error); | |
909 | } |