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