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> |
5976687a | 9 | #include <linux/sunrpc/addr.h> |
428360d7 BS |
10 | #include <linux/sunrpc/auth.h> |
11 | #include <linux/sunrpc/xprt.h> | |
12 | #include <linux/sunrpc/bc_xprt.h> | |
13 | #include "internal.h" | |
14 | #include "callback.h" | |
fcf10398 | 15 | #include "delegation.h" |
73e39aaa | 16 | #include "nfs4session.h" |
fcf10398 BS |
17 | #include "pnfs.h" |
18 | #include "netns.h" | |
428360d7 BS |
19 | |
20 | #define NFSDBG_FACILITY NFSDBG_CLIENT | |
21 | ||
ec409897 BS |
22 | /* |
23 | * Get a unique NFSv4.0 callback identifier which will be used | |
24 | * by the V4.0 callback service to lookup the nfs_client struct | |
25 | */ | |
26 | static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion) | |
27 | { | |
28 | int ret = 0; | |
29 | struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); | |
30 | ||
31 | if (clp->rpc_ops->version != 4 || minorversion != 0) | |
32 | return ret; | |
d6870312 | 33 | idr_preload(GFP_KERNEL); |
ec409897 | 34 | spin_lock(&nn->nfs_client_lock); |
308b77ea | 35 | ret = idr_alloc(&nn->cb_ident_idr, clp, 1, 0, GFP_NOWAIT); |
d6870312 TH |
36 | if (ret >= 0) |
37 | clp->cl_cb_ident = ret; | |
ec409897 | 38 | spin_unlock(&nn->nfs_client_lock); |
d6870312 TH |
39 | idr_preload_end(); |
40 | return ret < 0 ? ret : 0; | |
ec409897 BS |
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 | ||
98f98cf5 TM |
201 | if (clp->cl_minorversion != 0) |
202 | __set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags); | |
428360d7 | 203 | __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags); |
4edaa308 | 204 | error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_GSS_KRB5I); |
23631227 | 205 | if (error == -EINVAL) |
83c168bf | 206 | error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX); |
428360d7 BS |
207 | if (error < 0) |
208 | goto error; | |
209 | ||
210 | /* If no clientaddr= option was specified, find a usable cb address */ | |
211 | if (ip_addr == NULL) { | |
212 | struct sockaddr_storage cb_addr; | |
213 | struct sockaddr *sap = (struct sockaddr *)&cb_addr; | |
214 | ||
215 | error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr)); | |
216 | if (error < 0) | |
217 | goto error; | |
218 | error = rpc_ntop(sap, buf, sizeof(buf)); | |
219 | if (error < 0) | |
220 | goto error; | |
221 | ip_addr = (const char *)buf; | |
222 | } | |
223 | strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr)); | |
224 | ||
225 | error = nfs_idmap_new(clp); | |
226 | if (error < 0) { | |
227 | dprintk("%s: failed to create idmapper. Error = %d\n", | |
228 | __func__, error); | |
229 | goto error; | |
230 | } | |
231 | __set_bit(NFS_CS_IDMAP, &clp->cl_res_state); | |
232 | ||
233 | error = nfs4_init_client_minor_version(clp); | |
234 | if (error < 0) | |
235 | goto error; | |
236 | ||
237 | if (!nfs4_has_session(clp)) | |
238 | nfs_mark_client_ready(clp, NFS_CS_READY); | |
05f4c350 CL |
239 | |
240 | error = nfs4_discover_server_trunking(clp, &old); | |
241 | if (error < 0) | |
242 | goto error; | |
05f4c350 | 243 | |
daab6e7d WAA |
244 | if (clp != old) |
245 | clp->cl_preserve_clid = true; | |
246 | nfs_put_client(clp); | |
247 | return old; | |
428360d7 BS |
248 | |
249 | error: | |
250 | nfs_mark_client_ready(clp, error); | |
251 | nfs_put_client(clp); | |
252 | dprintk("<-- nfs4_init_client() = xerror %d\n", error); | |
253 | return ERR_PTR(error); | |
254 | } | |
fcf10398 | 255 | |
05f4c350 CL |
256 | /* |
257 | * SETCLIENTID just did a callback update with the callback ident in | |
258 | * "drop," but server trunking discovery claims "drop" and "keep" are | |
259 | * actually the same server. Swap the callback IDs so that "keep" | |
260 | * will continue to use the callback ident the server now knows about, | |
261 | * and so that "keep"'s original callback ident is destroyed when | |
262 | * "drop" is freed. | |
263 | */ | |
264 | static void nfs4_swap_callback_idents(struct nfs_client *keep, | |
265 | struct nfs_client *drop) | |
266 | { | |
267 | struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id); | |
268 | unsigned int save = keep->cl_cb_ident; | |
269 | ||
270 | if (keep->cl_cb_ident == drop->cl_cb_ident) | |
271 | return; | |
272 | ||
273 | dprintk("%s: keeping callback ident %u and dropping ident %u\n", | |
274 | __func__, keep->cl_cb_ident, drop->cl_cb_ident); | |
275 | ||
276 | spin_lock(&nn->nfs_client_lock); | |
277 | ||
278 | idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident); | |
279 | keep->cl_cb_ident = drop->cl_cb_ident; | |
280 | ||
281 | idr_replace(&nn->cb_ident_idr, drop, save); | |
282 | drop->cl_cb_ident = save; | |
283 | ||
284 | spin_unlock(&nn->nfs_client_lock); | |
285 | } | |
286 | ||
287 | /** | |
288 | * nfs40_walk_client_list - Find server that recognizes a client ID | |
289 | * | |
290 | * @new: nfs_client with client ID to test | |
291 | * @result: OUT: found nfs_client, or new | |
292 | * @cred: credential to use for trunking test | |
293 | * | |
294 | * Returns zero, a negative errno, or a negative NFS4ERR status. | |
295 | * If zero is returned, an nfs_client pointer is planted in "result." | |
296 | * | |
297 | * NB: nfs40_walk_client_list() relies on the new nfs_client being | |
298 | * the last nfs_client on the list. | |
299 | */ | |
300 | int nfs40_walk_client_list(struct nfs_client *new, | |
301 | struct nfs_client **result, | |
302 | struct rpc_cred *cred) | |
303 | { | |
304 | struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id); | |
7b1f1fd1 | 305 | struct nfs_client *pos, *prev = NULL; |
05f4c350 CL |
306 | struct nfs4_setclientid_res clid = { |
307 | .clientid = new->cl_clientid, | |
308 | .confirm = new->cl_confirm, | |
309 | }; | |
4ae19c2d | 310 | int status = -NFS4ERR_STALE_CLIENTID; |
05f4c350 CL |
311 | |
312 | spin_lock(&nn->nfs_client_lock); | |
7b1f1fd1 | 313 | list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) { |
648d333d SD |
314 | |
315 | if (pos->rpc_ops != new->rpc_ops) | |
316 | continue; | |
317 | ||
318 | if (pos->cl_proto != new->cl_proto) | |
319 | continue; | |
320 | ||
321 | if (pos->cl_minorversion != new->cl_minorversion) | |
322 | continue; | |
323 | ||
05f4c350 CL |
324 | /* If "pos" isn't marked ready, we can't trust the |
325 | * remaining fields in "pos" */ | |
7b1f1fd1 TM |
326 | if (pos->cl_cons_state > NFS_CS_READY) { |
327 | atomic_inc(&pos->cl_count); | |
328 | spin_unlock(&nn->nfs_client_lock); | |
329 | ||
330 | if (prev) | |
331 | nfs_put_client(prev); | |
332 | prev = pos; | |
333 | ||
334 | status = nfs_wait_client_init_complete(pos); | |
7b1f1fd1 | 335 | if (status < 0) |
14ff66ce TM |
336 | goto out; |
337 | status = -NFS4ERR_STALE_CLIENTID; | |
338 | spin_lock(&nn->nfs_client_lock); | |
7b1f1fd1 TM |
339 | } |
340 | if (pos->cl_cons_state != NFS_CS_READY) | |
05f4c350 CL |
341 | continue; |
342 | ||
05f4c350 CL |
343 | if (pos->cl_clientid != new->cl_clientid) |
344 | continue; | |
345 | ||
346 | atomic_inc(&pos->cl_count); | |
347 | spin_unlock(&nn->nfs_client_lock); | |
348 | ||
349 | if (prev) | |
350 | nfs_put_client(prev); | |
4ae19c2d | 351 | prev = pos; |
05f4c350 CL |
352 | |
353 | status = nfs4_proc_setclientid_confirm(pos, &clid, cred); | |
4ae19c2d TM |
354 | switch (status) { |
355 | case -NFS4ERR_STALE_CLIENTID: | |
356 | break; | |
357 | case 0: | |
05f4c350 CL |
358 | nfs4_swap_callback_idents(pos, new); |
359 | ||
4ae19c2d | 360 | prev = NULL; |
05f4c350 CL |
361 | *result = pos; |
362 | dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n", | |
363 | __func__, pos, atomic_read(&pos->cl_count)); | |
4ae19c2d TM |
364 | default: |
365 | goto out; | |
05f4c350 CL |
366 | } |
367 | ||
368 | spin_lock(&nn->nfs_client_lock); | |
05f4c350 | 369 | } |
4ae19c2d | 370 | spin_unlock(&nn->nfs_client_lock); |
05f4c350 | 371 | |
202c312d | 372 | /* No match found. The server lost our clientid */ |
4ae19c2d | 373 | out: |
05f4c350 CL |
374 | if (prev) |
375 | nfs_put_client(prev); | |
4ae19c2d TM |
376 | dprintk("NFS: <-- %s status = %d\n", __func__, status); |
377 | return status; | |
05f4c350 CL |
378 | } |
379 | ||
380 | #ifdef CONFIG_NFS_V4_1 | |
f9d640f3 TM |
381 | /* |
382 | * Returns true if the client IDs match | |
383 | */ | |
384 | static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b) | |
385 | { | |
386 | if (a->cl_clientid != b->cl_clientid) { | |
387 | dprintk("NFS: --> %s client ID %llx does not match %llx\n", | |
388 | __func__, a->cl_clientid, b->cl_clientid); | |
389 | return false; | |
390 | } | |
391 | dprintk("NFS: --> %s client ID %llx matches %llx\n", | |
392 | __func__, a->cl_clientid, b->cl_clientid); | |
393 | return true; | |
394 | } | |
395 | ||
05f4c350 | 396 | /* |
a8c44ea0 | 397 | * Returns true if the server major ids match |
05f4c350 CL |
398 | */ |
399 | static bool | |
a8c44ea0 | 400 | nfs4_check_clientid_trunking(struct nfs_client *a, struct nfs_client *b) |
05f4c350 CL |
401 | { |
402 | struct nfs41_server_owner *o1 = a->cl_serverowner; | |
403 | struct nfs41_server_owner *o2 = b->cl_serverowner; | |
404 | ||
05f4c350 CL |
405 | if (o1->major_id_sz != o2->major_id_sz) |
406 | goto out_major_mismatch; | |
407 | if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0) | |
408 | goto out_major_mismatch; | |
409 | ||
410 | dprintk("NFS: --> %s server owners match\n", __func__); | |
411 | return true; | |
412 | ||
413 | out_major_mismatch: | |
414 | dprintk("NFS: --> %s server owner major IDs do not match\n", | |
415 | __func__); | |
416 | return false; | |
417 | } | |
418 | ||
419 | /** | |
420 | * nfs41_walk_client_list - Find nfs_client that matches a client/server owner | |
421 | * | |
422 | * @new: nfs_client with client ID to test | |
423 | * @result: OUT: found nfs_client, or new | |
424 | * @cred: credential to use for trunking test | |
425 | * | |
426 | * Returns zero, a negative errno, or a negative NFS4ERR status. | |
427 | * If zero is returned, an nfs_client pointer is planted in "result." | |
428 | * | |
429 | * NB: nfs41_walk_client_list() relies on the new nfs_client being | |
430 | * the last nfs_client on the list. | |
431 | */ | |
432 | int nfs41_walk_client_list(struct nfs_client *new, | |
433 | struct nfs_client **result, | |
434 | struct rpc_cred *cred) | |
435 | { | |
436 | struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id); | |
7b1f1fd1 | 437 | struct nfs_client *pos, *prev = NULL; |
202c312d | 438 | int status = -NFS4ERR_STALE_CLIENTID; |
05f4c350 CL |
439 | |
440 | spin_lock(&nn->nfs_client_lock); | |
7b1f1fd1 | 441 | list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) { |
648d333d SD |
442 | |
443 | if (pos->rpc_ops != new->rpc_ops) | |
444 | continue; | |
445 | ||
446 | if (pos->cl_proto != new->cl_proto) | |
447 | continue; | |
448 | ||
449 | if (pos->cl_minorversion != new->cl_minorversion) | |
450 | continue; | |
451 | ||
05f4c350 CL |
452 | /* If "pos" isn't marked ready, we can't trust the |
453 | * remaining fields in "pos", especially the client | |
454 | * ID and serverowner fields. Wait for CREATE_SESSION | |
455 | * to finish. */ | |
7b1f1fd1 | 456 | if (pos->cl_cons_state > NFS_CS_READY) { |
05f4c350 CL |
457 | atomic_inc(&pos->cl_count); |
458 | spin_unlock(&nn->nfs_client_lock); | |
459 | ||
460 | if (prev) | |
461 | nfs_put_client(prev); | |
462 | prev = pos; | |
463 | ||
202c312d | 464 | status = nfs_wait_client_init_complete(pos); |
535516f2 | 465 | if (pos->cl_cons_state == NFS_CS_SESSION_INITING) { |
7b1f1fd1 TM |
466 | nfs4_schedule_lease_recovery(pos); |
467 | status = nfs4_wait_clnt_recover(pos); | |
05f4c350 | 468 | } |
05f4c350 | 469 | spin_lock(&nn->nfs_client_lock); |
65436ec0 | 470 | if (status < 0) |
14ff66ce TM |
471 | break; |
472 | status = -NFS4ERR_STALE_CLIENTID; | |
05f4c350 | 473 | } |
7b1f1fd1 TM |
474 | if (pos->cl_cons_state != NFS_CS_READY) |
475 | continue; | |
05f4c350 | 476 | |
05f4c350 CL |
477 | if (!nfs4_match_clientids(pos, new)) |
478 | continue; | |
479 | ||
a8c44ea0 TM |
480 | /* |
481 | * Note that session trunking is just a special subcase of | |
482 | * client id trunking. In either case, we want to fall back | |
483 | * to using the existing nfs_client. | |
484 | */ | |
485 | if (!nfs4_check_clientid_trunking(pos, new)) | |
05f4c350 CL |
486 | continue; |
487 | ||
4ae19c2d | 488 | atomic_inc(&pos->cl_count); |
7b1f1fd1 | 489 | *result = pos; |
eb04e0ac | 490 | status = 0; |
05f4c350 CL |
491 | dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n", |
492 | __func__, pos, atomic_read(&pos->cl_count)); | |
7b1f1fd1 | 493 | break; |
05f4c350 CL |
494 | } |
495 | ||
202c312d | 496 | /* No matching nfs_client found. */ |
05f4c350 | 497 | spin_unlock(&nn->nfs_client_lock); |
202c312d | 498 | dprintk("NFS: <-- %s status = %d\n", __func__, status); |
7b1f1fd1 TM |
499 | if (prev) |
500 | nfs_put_client(prev); | |
202c312d | 501 | return status; |
05f4c350 CL |
502 | } |
503 | #endif /* CONFIG_NFS_V4_1 */ | |
504 | ||
fcf10398 BS |
505 | static void nfs4_destroy_server(struct nfs_server *server) |
506 | { | |
507 | nfs_server_return_all_delegations(server); | |
508 | unset_pnfs_layoutdriver(server); | |
509 | nfs4_purge_state_owners(server); | |
510 | } | |
511 | ||
512 | /* | |
513 | * NFSv4.0 callback thread helper | |
514 | * | |
515 | * Find a client by callback identifier | |
516 | */ | |
517 | struct nfs_client * | |
518 | nfs4_find_client_ident(struct net *net, int cb_ident) | |
519 | { | |
520 | struct nfs_client *clp; | |
521 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
522 | ||
523 | spin_lock(&nn->nfs_client_lock); | |
524 | clp = idr_find(&nn->cb_ident_idr, cb_ident); | |
525 | if (clp) | |
526 | atomic_inc(&clp->cl_count); | |
527 | spin_unlock(&nn->nfs_client_lock); | |
528 | return clp; | |
529 | } | |
530 | ||
531 | #if defined(CONFIG_NFS_V4_1) | |
532 | /* Common match routine for v4.0 and v4.1 callback services */ | |
533 | static bool nfs4_cb_match_client(const struct sockaddr *addr, | |
534 | struct nfs_client *clp, u32 minorversion) | |
535 | { | |
536 | struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; | |
537 | ||
538 | /* Don't match clients that failed to initialise */ | |
539 | if (!(clp->cl_cons_state == NFS_CS_READY || | |
540 | clp->cl_cons_state == NFS_CS_SESSION_INITING)) | |
541 | return false; | |
542 | ||
543 | smp_rmb(); | |
544 | ||
545 | /* Match the version and minorversion */ | |
546 | if (clp->rpc_ops->version != 4 || | |
547 | clp->cl_minorversion != minorversion) | |
548 | return false; | |
549 | ||
550 | /* Match only the IP address, not the port number */ | |
551 | if (!nfs_sockaddr_match_ipaddr(addr, clap)) | |
552 | return false; | |
553 | ||
554 | return true; | |
555 | } | |
556 | ||
557 | /* | |
558 | * NFSv4.1 callback thread helper | |
559 | * For CB_COMPOUND calls, find a client by IP address, protocol version, | |
560 | * minorversion, and sessionID | |
561 | * | |
562 | * Returns NULL if no such client | |
563 | */ | |
564 | struct nfs_client * | |
565 | nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr, | |
566 | struct nfs4_sessionid *sid) | |
567 | { | |
568 | struct nfs_client *clp; | |
569 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
570 | ||
571 | spin_lock(&nn->nfs_client_lock); | |
572 | list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { | |
573 | if (nfs4_cb_match_client(addr, clp, 1) == false) | |
574 | continue; | |
575 | ||
576 | if (!nfs4_has_session(clp)) | |
577 | continue; | |
578 | ||
579 | /* Match sessionid*/ | |
580 | if (memcmp(clp->cl_session->sess_id.data, | |
581 | sid->data, NFS4_MAX_SESSIONID_LEN) != 0) | |
582 | continue; | |
583 | ||
584 | atomic_inc(&clp->cl_count); | |
585 | spin_unlock(&nn->nfs_client_lock); | |
586 | return clp; | |
587 | } | |
588 | spin_unlock(&nn->nfs_client_lock); | |
589 | return NULL; | |
590 | } | |
591 | ||
592 | #else /* CONFIG_NFS_V4_1 */ | |
593 | ||
594 | struct nfs_client * | |
595 | nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr, | |
596 | struct nfs4_sessionid *sid) | |
597 | { | |
598 | return NULL; | |
599 | } | |
600 | #endif /* CONFIG_NFS_V4_1 */ | |
601 | ||
602 | /* | |
603 | * Set up an NFS4 client | |
604 | */ | |
605 | static int nfs4_set_client(struct nfs_server *server, | |
606 | const char *hostname, | |
607 | const struct sockaddr *addr, | |
608 | const size_t addrlen, | |
609 | const char *ip_addr, | |
610 | rpc_authflavor_t authflavour, | |
611 | int proto, const struct rpc_timeout *timeparms, | |
612 | u32 minorversion, struct net *net) | |
613 | { | |
614 | struct nfs_client_initdata cl_init = { | |
615 | .hostname = hostname, | |
616 | .addr = addr, | |
617 | .addrlen = addrlen, | |
ab7017a3 | 618 | .nfs_mod = &nfs_v4, |
fcf10398 BS |
619 | .proto = proto, |
620 | .minorversion = minorversion, | |
621 | .net = net, | |
622 | }; | |
623 | struct nfs_client *clp; | |
624 | int error; | |
625 | ||
626 | dprintk("--> nfs4_set_client()\n"); | |
627 | ||
628 | if (server->flags & NFS_MOUNT_NORESVPORT) | |
629 | set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags); | |
630 | ||
631 | /* Allocate or find a client reference we can use */ | |
632 | clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour); | |
633 | if (IS_ERR(clp)) { | |
634 | error = PTR_ERR(clp); | |
635 | goto error; | |
636 | } | |
637 | ||
638 | /* | |
639 | * Query for the lease time on clientid setup or renewal | |
640 | * | |
641 | * Note that this will be set on nfs_clients that were created | |
642 | * only for the DS role and did not set this bit, but now will | |
643 | * serve a dual role. | |
644 | */ | |
645 | set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state); | |
646 | ||
647 | server->nfs_client = clp; | |
648 | dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp); | |
649 | return 0; | |
650 | error: | |
651 | dprintk("<-- nfs4_set_client() = xerror %d\n", error); | |
652 | return error; | |
653 | } | |
654 | ||
655 | /* | |
656 | * Set up a pNFS Data Server client. | |
657 | * | |
658 | * Return any existing nfs_client that matches server address,port,version | |
659 | * and minorversion. | |
660 | * | |
661 | * For a new nfs_client, use a soft mount (default), a low retrans and a | |
662 | * low timeout interval so that if a connection is lost, we retry through | |
663 | * the MDS. | |
664 | */ | |
665 | struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp, | |
666 | const struct sockaddr *ds_addr, int ds_addrlen, | |
667 | int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans) | |
668 | { | |
669 | struct nfs_client_initdata cl_init = { | |
670 | .addr = ds_addr, | |
671 | .addrlen = ds_addrlen, | |
ab7017a3 | 672 | .nfs_mod = &nfs_v4, |
fcf10398 BS |
673 | .proto = ds_proto, |
674 | .minorversion = mds_clp->cl_minorversion, | |
675 | .net = mds_clp->cl_net, | |
676 | }; | |
677 | struct rpc_timeout ds_timeout; | |
678 | struct nfs_client *clp; | |
679 | ||
680 | /* | |
681 | * Set an authflavor equual to the MDS value. Use the MDS nfs_client | |
682 | * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS | |
683 | * (section 13.1 RFC 5661). | |
684 | */ | |
685 | nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans); | |
686 | clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr, | |
687 | mds_clp->cl_rpcclient->cl_auth->au_flavor); | |
688 | ||
689 | dprintk("<-- %s %p\n", __func__, clp); | |
690 | return clp; | |
691 | } | |
692 | EXPORT_SYMBOL_GPL(nfs4_set_ds_client); | |
693 | ||
694 | /* | |
695 | * Session has been established, and the client marked ready. | |
696 | * Set the mount rsize and wsize with negotiated fore channel | |
697 | * attributes which will be bound checked in nfs_server_set_fsinfo. | |
698 | */ | |
699 | static void nfs4_session_set_rwsize(struct nfs_server *server) | |
700 | { | |
701 | #ifdef CONFIG_NFS_V4_1 | |
702 | struct nfs4_session *sess; | |
703 | u32 server_resp_sz; | |
704 | u32 server_rqst_sz; | |
705 | ||
706 | if (!nfs4_has_session(server->nfs_client)) | |
707 | return; | |
708 | sess = server->nfs_client->cl_session; | |
709 | server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead; | |
710 | server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead; | |
711 | ||
712 | if (server->rsize > server_resp_sz) | |
713 | server->rsize = server_resp_sz; | |
714 | if (server->wsize > server_rqst_sz) | |
715 | server->wsize = server_rqst_sz; | |
716 | #endif /* CONFIG_NFS_V4_1 */ | |
717 | } | |
718 | ||
719 | static int nfs4_server_common_setup(struct nfs_server *server, | |
720 | struct nfs_fh *mntfh) | |
721 | { | |
722 | struct nfs_fattr *fattr; | |
723 | int error; | |
724 | ||
fcf10398 BS |
725 | /* data servers support only a subset of NFSv4.1 */ |
726 | if (is_ds_only_client(server->nfs_client)) | |
727 | return -EPROTONOSUPPORT; | |
728 | ||
729 | fattr = nfs_alloc_fattr(); | |
730 | if (fattr == NULL) | |
731 | return -ENOMEM; | |
732 | ||
733 | /* We must ensure the session is initialised first */ | |
734 | error = nfs4_init_session(server); | |
735 | if (error < 0) | |
736 | goto out; | |
737 | ||
39c6daae TM |
738 | /* Set the basic capabilities */ |
739 | server->caps |= server->nfs_client->cl_mvops->init_caps; | |
740 | if (server->flags & NFS_MOUNT_NORDIRPLUS) | |
741 | server->caps &= ~NFS_CAP_READDIRPLUS; | |
742 | /* | |
743 | * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower | |
744 | * authentication. | |
745 | */ | |
746 | if (nfs4_disable_idmapping && | |
747 | server->client->cl_auth->au_flavor == RPC_AUTH_UNIX) | |
748 | server->caps |= NFS_CAP_UIDGID_NOMAP; | |
749 | ||
750 | ||
fcf10398 BS |
751 | /* Probe the root fh to retrieve its FSID and filehandle */ |
752 | error = nfs4_get_rootfh(server, mntfh); | |
753 | if (error < 0) | |
754 | goto out; | |
755 | ||
756 | dprintk("Server FSID: %llx:%llx\n", | |
757 | (unsigned long long) server->fsid.major, | |
758 | (unsigned long long) server->fsid.minor); | |
759 | dprintk("Mount FH: %d\n", mntfh->size); | |
760 | ||
761 | nfs4_session_set_rwsize(server); | |
762 | ||
763 | error = nfs_probe_fsinfo(server, mntfh, fattr); | |
764 | if (error < 0) | |
765 | goto out; | |
766 | ||
767 | if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) | |
768 | server->namelen = NFS4_MAXNAMLEN; | |
769 | ||
770 | nfs_server_insert_lists(server); | |
771 | server->mount_time = jiffies; | |
772 | server->destroy = nfs4_destroy_server; | |
773 | out: | |
774 | nfs_free_fattr(fattr); | |
775 | return error; | |
776 | } | |
777 | ||
778 | /* | |
779 | * Create a version 4 volume record | |
780 | */ | |
781 | static int nfs4_init_server(struct nfs_server *server, | |
782 | const struct nfs_parsed_mount_data *data) | |
783 | { | |
784 | struct rpc_timeout timeparms; | |
785 | int error; | |
786 | ||
787 | dprintk("--> nfs4_init_server()\n"); | |
788 | ||
789 | nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, | |
790 | data->timeo, data->retrans); | |
791 | ||
792 | /* Initialise the client representation from the mount data */ | |
793 | server->flags = data->flags; | |
fcf10398 BS |
794 | server->options = data->options; |
795 | ||
796 | /* Get a client record */ | |
797 | error = nfs4_set_client(server, | |
798 | data->nfs_server.hostname, | |
799 | (const struct sockaddr *)&data->nfs_server.address, | |
800 | data->nfs_server.addrlen, | |
801 | data->client_address, | |
802 | data->auth_flavors[0], | |
803 | data->nfs_server.protocol, | |
804 | &timeparms, | |
805 | data->minorversion, | |
806 | data->net); | |
807 | if (error < 0) | |
808 | goto error; | |
809 | ||
fcf10398 BS |
810 | if (data->rsize) |
811 | server->rsize = nfs_block_size(data->rsize, NULL); | |
812 | if (data->wsize) | |
813 | server->wsize = nfs_block_size(data->wsize, NULL); | |
814 | ||
815 | server->acregmin = data->acregmin * HZ; | |
816 | server->acregmax = data->acregmax * HZ; | |
817 | server->acdirmin = data->acdirmin * HZ; | |
818 | server->acdirmax = data->acdirmax * HZ; | |
819 | ||
820 | server->port = data->nfs_server.port; | |
821 | ||
822 | error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]); | |
823 | ||
824 | error: | |
825 | /* Done */ | |
826 | dprintk("<-- nfs4_init_server() = %d\n", error); | |
827 | return error; | |
828 | } | |
829 | ||
830 | /* | |
831 | * Create a version 4 volume record | |
832 | * - keyed on server and FSID | |
833 | */ | |
1179acc6 BS |
834 | /*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data, |
835 | struct nfs_fh *mntfh)*/ | |
836 | struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info, | |
837 | struct nfs_subversion *nfs_mod) | |
fcf10398 BS |
838 | { |
839 | struct nfs_server *server; | |
840 | int error; | |
841 | ||
842 | dprintk("--> nfs4_create_server()\n"); | |
843 | ||
844 | server = nfs_alloc_server(); | |
845 | if (!server) | |
846 | return ERR_PTR(-ENOMEM); | |
847 | ||
848 | /* set up the general RPC client */ | |
1179acc6 | 849 | error = nfs4_init_server(server, mount_info->parsed); |
fcf10398 BS |
850 | if (error < 0) |
851 | goto error; | |
852 | ||
1179acc6 | 853 | error = nfs4_server_common_setup(server, mount_info->mntfh); |
fcf10398 BS |
854 | if (error < 0) |
855 | goto error; | |
856 | ||
857 | dprintk("<-- nfs4_create_server() = %p\n", server); | |
858 | return server; | |
859 | ||
860 | error: | |
861 | nfs_free_server(server); | |
862 | dprintk("<-- nfs4_create_server() = error %d\n", error); | |
863 | return ERR_PTR(error); | |
864 | } | |
865 | ||
866 | /* | |
867 | * Create an NFS4 referral server record | |
868 | */ | |
869 | struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data, | |
870 | struct nfs_fh *mntfh) | |
871 | { | |
872 | struct nfs_client *parent_client; | |
873 | struct nfs_server *server, *parent_server; | |
874 | int error; | |
875 | ||
876 | dprintk("--> nfs4_create_referral_server()\n"); | |
877 | ||
878 | server = nfs_alloc_server(); | |
879 | if (!server) | |
880 | return ERR_PTR(-ENOMEM); | |
881 | ||
882 | parent_server = NFS_SB(data->sb); | |
883 | parent_client = parent_server->nfs_client; | |
884 | ||
885 | /* Initialise the client representation from the parent server */ | |
886 | nfs_server_copy_userdata(server, parent_server); | |
fcf10398 BS |
887 | |
888 | /* Get a client representation. | |
889 | * Note: NFSv4 always uses TCP, */ | |
890 | error = nfs4_set_client(server, data->hostname, | |
891 | data->addr, | |
892 | data->addrlen, | |
893 | parent_client->cl_ipaddr, | |
894 | data->authflavor, | |
895 | rpc_protocol(parent_server->client), | |
896 | parent_server->client->cl_timeout, | |
897 | parent_client->cl_mvops->minor_version, | |
898 | parent_client->cl_net); | |
899 | if (error < 0) | |
900 | goto error; | |
901 | ||
902 | error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor); | |
903 | if (error < 0) | |
904 | goto error; | |
905 | ||
906 | error = nfs4_server_common_setup(server, mntfh); | |
907 | if (error < 0) | |
908 | goto error; | |
909 | ||
910 | dprintk("<-- nfs_create_referral_server() = %p\n", server); | |
911 | return server; | |
912 | ||
913 | error: | |
914 | nfs_free_server(server); | |
915 | dprintk("<-- nfs4_create_referral_server() = error %d\n", error); | |
916 | return ERR_PTR(error); | |
917 | } |