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> | |
6aa23d76 | 13 | #include <linux/sunrpc/rpc_pipe_fs.h> |
428360d7 BS |
14 | #include "internal.h" |
15 | #include "callback.h" | |
fcf10398 | 16 | #include "delegation.h" |
73e39aaa | 17 | #include "nfs4session.h" |
fcf10398 BS |
18 | #include "pnfs.h" |
19 | #include "netns.h" | |
428360d7 BS |
20 | |
21 | #define NFSDBG_FACILITY NFSDBG_CLIENT | |
22 | ||
ec409897 BS |
23 | /* |
24 | * Get a unique NFSv4.0 callback identifier which will be used | |
25 | * by the V4.0 callback service to lookup the nfs_client struct | |
26 | */ | |
27 | static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion) | |
28 | { | |
29 | int ret = 0; | |
30 | struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); | |
31 | ||
32 | if (clp->rpc_ops->version != 4 || minorversion != 0) | |
33 | return ret; | |
d6870312 | 34 | idr_preload(GFP_KERNEL); |
ec409897 | 35 | spin_lock(&nn->nfs_client_lock); |
d6870312 TH |
36 | ret = idr_alloc(&nn->cb_ident_idr, clp, 0, 0, GFP_NOWAIT); |
37 | if (ret >= 0) | |
38 | clp->cl_cb_ident = ret; | |
ec409897 | 39 | spin_unlock(&nn->nfs_client_lock); |
d6870312 TH |
40 | idr_preload_end(); |
41 | return ret < 0 ? ret : 0; | |
ec409897 BS |
42 | } |
43 | ||
44 | #ifdef CONFIG_NFS_V4_1 | |
0e20162e AA |
45 | /** |
46 | * Per auth flavor data server rpc clients | |
47 | */ | |
48 | struct nfs4_ds_server { | |
49 | struct list_head list; /* ds_clp->cl_ds_clients */ | |
50 | struct rpc_clnt *rpc_clnt; | |
51 | }; | |
52 | ||
53 | /** | |
54 | * Common lookup case for DS I/O | |
55 | */ | |
56 | static struct nfs4_ds_server * | |
57 | nfs4_find_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor) | |
58 | { | |
59 | struct nfs4_ds_server *dss; | |
60 | ||
61 | rcu_read_lock(); | |
62 | list_for_each_entry_rcu(dss, &ds_clp->cl_ds_clients, list) { | |
63 | if (dss->rpc_clnt->cl_auth->au_flavor != flavor) | |
64 | continue; | |
65 | goto out; | |
66 | } | |
67 | dss = NULL; | |
68 | out: | |
69 | rcu_read_unlock(); | |
70 | return dss; | |
71 | } | |
72 | ||
73 | static struct nfs4_ds_server * | |
74 | nfs4_add_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor, | |
75 | struct nfs4_ds_server *new) | |
76 | { | |
77 | struct nfs4_ds_server *dss; | |
78 | ||
79 | spin_lock(&ds_clp->cl_lock); | |
80 | list_for_each_entry(dss, &ds_clp->cl_ds_clients, list) { | |
81 | if (dss->rpc_clnt->cl_auth->au_flavor != flavor) | |
82 | continue; | |
83 | goto out; | |
84 | } | |
85 | if (new) | |
86 | list_add_rcu(&new->list, &ds_clp->cl_ds_clients); | |
87 | dss = new; | |
88 | out: | |
89 | spin_unlock(&ds_clp->cl_lock); /* need some lock to protect list */ | |
90 | return dss; | |
91 | } | |
92 | ||
93 | static struct nfs4_ds_server * | |
94 | nfs4_alloc_ds_server(struct nfs_client *ds_clp, rpc_authflavor_t flavor) | |
95 | { | |
96 | struct nfs4_ds_server *dss; | |
97 | ||
98 | dss = kmalloc(sizeof(*dss), GFP_NOFS); | |
99 | if (dss == NULL) | |
100 | return ERR_PTR(-ENOMEM); | |
101 | ||
102 | dss->rpc_clnt = rpc_clone_client_set_auth(ds_clp->cl_rpcclient, flavor); | |
103 | if (IS_ERR(dss->rpc_clnt)) { | |
104 | int err = PTR_ERR(dss->rpc_clnt); | |
105 | kfree (dss); | |
106 | return ERR_PTR(err); | |
107 | } | |
108 | INIT_LIST_HEAD(&dss->list); | |
109 | ||
110 | return dss; | |
111 | } | |
112 | ||
113 | static void | |
114 | nfs4_free_ds_server(struct nfs4_ds_server *dss) | |
115 | { | |
116 | rpc_release_client(dss->rpc_clnt); | |
117 | kfree(dss); | |
118 | } | |
119 | ||
120 | /** | |
121 | * Find or create a DS rpc client with th MDS server rpc client auth flavor | |
122 | * in the nfs_client cl_ds_clients list. | |
123 | */ | |
124 | struct rpc_clnt * | |
125 | nfs4_find_or_create_ds_client(struct nfs_client *ds_clp, struct inode *inode) | |
126 | { | |
127 | struct nfs4_ds_server *dss, *new; | |
128 | rpc_authflavor_t flavor = NFS_SERVER(inode)->client->cl_auth->au_flavor; | |
129 | ||
130 | dss = nfs4_find_ds_client(ds_clp, flavor); | |
131 | if (dss != NULL) | |
132 | goto out; | |
133 | new = nfs4_alloc_ds_server(ds_clp, flavor); | |
134 | if (IS_ERR(new)) | |
135 | return ERR_CAST(new); | |
136 | dss = nfs4_add_ds_client(ds_clp, flavor, new); | |
137 | if (dss != new) | |
138 | nfs4_free_ds_server(new); | |
139 | out: | |
140 | return dss->rpc_clnt; | |
141 | } | |
142 | EXPORT_SYMBOL_GPL(nfs4_find_or_create_ds_client); | |
143 | ||
144 | static void | |
145 | nfs4_shutdown_ds_clients(struct nfs_client *clp) | |
146 | { | |
147 | struct nfs4_ds_server *dss; | |
148 | LIST_HEAD(shutdown_list); | |
149 | ||
150 | while (!list_empty(&clp->cl_ds_clients)) { | |
151 | dss = list_entry(clp->cl_ds_clients.next, | |
152 | struct nfs4_ds_server, list); | |
153 | list_del(&dss->list); | |
154 | rpc_shutdown_client(dss->rpc_clnt); | |
155 | kfree (dss); | |
156 | } | |
157 | } | |
158 | ||
abf79bb3 | 159 | void nfs41_shutdown_client(struct nfs_client *clp) |
ec409897 BS |
160 | { |
161 | if (nfs4_has_session(clp)) { | |
0e20162e | 162 | nfs4_shutdown_ds_clients(clp); |
ec409897 BS |
163 | nfs4_destroy_session(clp->cl_session); |
164 | nfs4_destroy_clientid(clp); | |
165 | } | |
166 | ||
167 | } | |
abf79bb3 CL |
168 | #endif /* CONFIG_NFS_V4_1 */ |
169 | ||
170 | void nfs40_shutdown_client(struct nfs_client *clp) | |
ec409897 | 171 | { |
abf79bb3 | 172 | if (clp->cl_slot_tbl) { |
20b9a902 | 173 | nfs4_shutdown_slot_table(clp->cl_slot_tbl); |
abf79bb3 CL |
174 | kfree(clp->cl_slot_tbl); |
175 | } | |
ec409897 | 176 | } |
ec409897 BS |
177 | |
178 | struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init) | |
179 | { | |
180 | int err; | |
181 | struct nfs_client *clp = nfs_alloc_client(cl_init); | |
182 | if (IS_ERR(clp)) | |
183 | return clp; | |
184 | ||
185 | err = nfs_get_cb_ident_idr(clp, cl_init->minorversion); | |
186 | if (err) | |
187 | goto error; | |
188 | ||
42c2c424 SD |
189 | if (cl_init->minorversion > NFS4_MAX_MINOR_VERSION) { |
190 | err = -EINVAL; | |
191 | goto error; | |
192 | } | |
193 | ||
ec409897 BS |
194 | spin_lock_init(&clp->cl_lock); |
195 | INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state); | |
0e20162e | 196 | INIT_LIST_HEAD(&clp->cl_ds_clients); |
ec409897 BS |
197 | rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client"); |
198 | clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED; | |
199 | clp->cl_minorversion = cl_init->minorversion; | |
200 | clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion]; | |
c9fdeb28 | 201 | clp->cl_mig_gen = 1; |
ec409897 BS |
202 | return clp; |
203 | ||
204 | error: | |
7653f6ff | 205 | nfs_free_client(clp); |
ec409897 BS |
206 | return ERR_PTR(err); |
207 | } | |
208 | ||
209 | /* | |
210 | * Destroy the NFS4 callback service | |
211 | */ | |
212 | static void nfs4_destroy_callback(struct nfs_client *clp) | |
213 | { | |
214 | if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state)) | |
c946556b | 215 | nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net); |
ec409897 BS |
216 | } |
217 | ||
218 | static void nfs4_shutdown_client(struct nfs_client *clp) | |
219 | { | |
220 | if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state)) | |
221 | nfs4_kill_renewd(clp); | |
abf79bb3 | 222 | clp->cl_mvops->shutdown_client(clp); |
ec409897 BS |
223 | nfs4_destroy_callback(clp); |
224 | if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state)) | |
225 | nfs_idmap_delete(clp); | |
226 | ||
227 | rpc_destroy_wait_queue(&clp->cl_rpcwaitq); | |
228 | kfree(clp->cl_serverowner); | |
229 | kfree(clp->cl_serverscope); | |
230 | kfree(clp->cl_implid); | |
231 | } | |
232 | ||
233 | void nfs4_free_client(struct nfs_client *clp) | |
234 | { | |
235 | nfs4_shutdown_client(clp); | |
236 | nfs_free_client(clp); | |
237 | } | |
238 | ||
428360d7 BS |
239 | /* |
240 | * Initialize the NFS4 callback service | |
241 | */ | |
242 | static int nfs4_init_callback(struct nfs_client *clp) | |
243 | { | |
c2ef47b7 | 244 | struct rpc_xprt *xprt; |
428360d7 BS |
245 | int error; |
246 | ||
c2ef47b7 | 247 | xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt); |
428360d7 | 248 | |
c2ef47b7 CL |
249 | if (nfs4_has_session(clp)) { |
250 | error = xprt_setup_backchannel(xprt, NFS41_BC_MIN_CALLBACKS); | |
251 | if (error < 0) | |
428360d7 | 252 | return error; |
428360d7 | 253 | } |
c2ef47b7 CL |
254 | |
255 | error = nfs_callback_up(clp->cl_mvops->minor_version, xprt); | |
256 | if (error < 0) { | |
257 | dprintk("%s: failed to start callback. Error = %d\n", | |
258 | __func__, error); | |
259 | return error; | |
260 | } | |
261 | __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state); | |
262 | ||
428360d7 BS |
263 | return 0; |
264 | } | |
265 | ||
abf79bb3 CL |
266 | /** |
267 | * nfs40_init_client - nfs_client initialization tasks for NFSv4.0 | |
268 | * @clp - nfs_client to initialize | |
269 | * | |
270 | * Returns zero on success, or a negative errno if some error occurred. | |
271 | */ | |
272 | int nfs40_init_client(struct nfs_client *clp) | |
273 | { | |
274 | struct nfs4_slot_table *tbl; | |
275 | int ret; | |
276 | ||
277 | tbl = kzalloc(sizeof(*tbl), GFP_NOFS); | |
278 | if (tbl == NULL) | |
279 | return -ENOMEM; | |
280 | ||
281 | ret = nfs4_setup_slot_table(tbl, NFS4_MAX_SLOT_TABLE, | |
282 | "NFSv4.0 transport Slot table"); | |
283 | if (ret) { | |
284 | kfree(tbl); | |
285 | return ret; | |
286 | } | |
287 | ||
288 | clp->cl_slot_tbl = tbl; | |
289 | return 0; | |
290 | } | |
291 | ||
292 | #if defined(CONFIG_NFS_V4_1) | |
293 | ||
294 | /** | |
295 | * nfs41_init_client - nfs_client initialization tasks for NFSv4.1+ | |
296 | * @clp - nfs_client to initialize | |
297 | * | |
298 | * Returns zero on success, or a negative errno if some error occurred. | |
299 | */ | |
300 | int nfs41_init_client(struct nfs_client *clp) | |
301 | { | |
302 | struct nfs4_session *session = NULL; | |
303 | ||
304 | /* | |
305 | * Create the session and mark it expired. | |
306 | * When a SEQUENCE operation encounters the expired session | |
307 | * it will do session recovery to initialize it. | |
308 | */ | |
309 | session = nfs4_alloc_session(clp); | |
310 | if (!session) | |
311 | return -ENOMEM; | |
312 | ||
313 | clp->cl_session = session; | |
314 | ||
315 | /* | |
316 | * The create session reply races with the server back | |
317 | * channel probe. Mark the client NFS_CS_SESSION_INITING | |
318 | * so that the client back channel can find the | |
319 | * nfs_client struct | |
320 | */ | |
321 | nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING); | |
322 | return 0; | |
323 | } | |
324 | ||
325 | #endif /* CONFIG_NFS_V4_1 */ | |
326 | ||
428360d7 BS |
327 | /* |
328 | * Initialize the minor version specific parts of an NFS4 client record | |
329 | */ | |
330 | static int nfs4_init_client_minor_version(struct nfs_client *clp) | |
331 | { | |
abf79bb3 | 332 | int ret; |
428360d7 | 333 | |
abf79bb3 CL |
334 | ret = clp->cl_mvops->init_client(clp); |
335 | if (ret) | |
336 | return ret; | |
428360d7 BS |
337 | return nfs4_init_callback(clp); |
338 | } | |
339 | ||
340 | /** | |
341 | * nfs4_init_client - Initialise an NFS4 client record | |
342 | * | |
343 | * @clp: nfs_client to initialise | |
344 | * @timeparms: timeout parameters for underlying RPC transport | |
345 | * @ip_addr: callback IP address in presentation format | |
346 | * @authflavor: authentication flavor for underlying RPC transport | |
347 | * | |
348 | * Returns pointer to an NFS client, or an ERR_PTR value. | |
349 | */ | |
350 | struct nfs_client *nfs4_init_client(struct nfs_client *clp, | |
351 | const struct rpc_timeout *timeparms, | |
f8407299 | 352 | const char *ip_addr) |
428360d7 BS |
353 | { |
354 | char buf[INET6_ADDRSTRLEN + 1]; | |
05f4c350 | 355 | struct nfs_client *old; |
428360d7 BS |
356 | int error; |
357 | ||
358 | if (clp->cl_cons_state == NFS_CS_READY) { | |
359 | /* the client is initialised already */ | |
360 | dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp); | |
361 | return clp; | |
362 | } | |
363 | ||
364 | /* Check NFS protocol revision and initialize RPC op vector */ | |
365 | clp->rpc_ops = &nfs_v4_clientops; | |
366 | ||
98f98cf5 TM |
367 | if (clp->cl_minorversion != 0) |
368 | __set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags); | |
428360d7 | 369 | __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags); |
99875249 | 370 | __set_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags); |
6aa23d76 | 371 | |
0ea9de0e | 372 | error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_GSS_KRB5I); |
23631227 | 373 | if (error == -EINVAL) |
83c168bf | 374 | error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX); |
428360d7 BS |
375 | if (error < 0) |
376 | goto error; | |
377 | ||
378 | /* If no clientaddr= option was specified, find a usable cb address */ | |
379 | if (ip_addr == NULL) { | |
380 | struct sockaddr_storage cb_addr; | |
381 | struct sockaddr *sap = (struct sockaddr *)&cb_addr; | |
382 | ||
383 | error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr)); | |
384 | if (error < 0) | |
385 | goto error; | |
386 | error = rpc_ntop(sap, buf, sizeof(buf)); | |
387 | if (error < 0) | |
388 | goto error; | |
389 | ip_addr = (const char *)buf; | |
390 | } | |
391 | strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr)); | |
392 | ||
393 | error = nfs_idmap_new(clp); | |
394 | if (error < 0) { | |
395 | dprintk("%s: failed to create idmapper. Error = %d\n", | |
396 | __func__, error); | |
397 | goto error; | |
398 | } | |
399 | __set_bit(NFS_CS_IDMAP, &clp->cl_res_state); | |
400 | ||
401 | error = nfs4_init_client_minor_version(clp); | |
402 | if (error < 0) | |
403 | goto error; | |
404 | ||
405 | if (!nfs4_has_session(clp)) | |
406 | nfs_mark_client_ready(clp, NFS_CS_READY); | |
05f4c350 CL |
407 | |
408 | error = nfs4_discover_server_trunking(clp, &old); | |
409 | if (error < 0) | |
410 | goto error; | |
05f4c350 | 411 | |
abad2fa5 WAA |
412 | if (clp != old) |
413 | clp->cl_preserve_clid = true; | |
414 | nfs_put_client(clp); | |
415 | return old; | |
428360d7 BS |
416 | |
417 | error: | |
418 | nfs_mark_client_ready(clp, error); | |
419 | nfs_put_client(clp); | |
420 | dprintk("<-- nfs4_init_client() = xerror %d\n", error); | |
421 | return ERR_PTR(error); | |
422 | } | |
fcf10398 | 423 | |
05f4c350 CL |
424 | /* |
425 | * SETCLIENTID just did a callback update with the callback ident in | |
426 | * "drop," but server trunking discovery claims "drop" and "keep" are | |
427 | * actually the same server. Swap the callback IDs so that "keep" | |
428 | * will continue to use the callback ident the server now knows about, | |
429 | * and so that "keep"'s original callback ident is destroyed when | |
430 | * "drop" is freed. | |
431 | */ | |
432 | static void nfs4_swap_callback_idents(struct nfs_client *keep, | |
433 | struct nfs_client *drop) | |
434 | { | |
435 | struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id); | |
436 | unsigned int save = keep->cl_cb_ident; | |
437 | ||
438 | if (keep->cl_cb_ident == drop->cl_cb_ident) | |
439 | return; | |
440 | ||
441 | dprintk("%s: keeping callback ident %u and dropping ident %u\n", | |
442 | __func__, keep->cl_cb_ident, drop->cl_cb_ident); | |
443 | ||
444 | spin_lock(&nn->nfs_client_lock); | |
445 | ||
446 | idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident); | |
447 | keep->cl_cb_ident = drop->cl_cb_ident; | |
448 | ||
449 | idr_replace(&nn->cb_ident_idr, drop, save); | |
450 | drop->cl_cb_ident = save; | |
451 | ||
452 | spin_unlock(&nn->nfs_client_lock); | |
453 | } | |
454 | ||
455 | /** | |
456 | * nfs40_walk_client_list - Find server that recognizes a client ID | |
457 | * | |
458 | * @new: nfs_client with client ID to test | |
459 | * @result: OUT: found nfs_client, or new | |
460 | * @cred: credential to use for trunking test | |
461 | * | |
462 | * Returns zero, a negative errno, or a negative NFS4ERR status. | |
463 | * If zero is returned, an nfs_client pointer is planted in "result." | |
464 | * | |
465 | * NB: nfs40_walk_client_list() relies on the new nfs_client being | |
466 | * the last nfs_client on the list. | |
467 | */ | |
468 | int nfs40_walk_client_list(struct nfs_client *new, | |
469 | struct nfs_client **result, | |
470 | struct rpc_cred *cred) | |
471 | { | |
472 | struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id); | |
7b1f1fd1 | 473 | struct nfs_client *pos, *prev = NULL; |
05f4c350 CL |
474 | struct nfs4_setclientid_res clid = { |
475 | .clientid = new->cl_clientid, | |
476 | .confirm = new->cl_confirm, | |
477 | }; | |
4ae19c2d | 478 | int status = -NFS4ERR_STALE_CLIENTID; |
05f4c350 CL |
479 | |
480 | spin_lock(&nn->nfs_client_lock); | |
7b1f1fd1 | 481 | list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) { |
080af20c SD |
482 | |
483 | if (pos->rpc_ops != new->rpc_ops) | |
484 | continue; | |
485 | ||
486 | if (pos->cl_proto != new->cl_proto) | |
487 | continue; | |
488 | ||
489 | if (pos->cl_minorversion != new->cl_minorversion) | |
490 | continue; | |
491 | ||
05f4c350 CL |
492 | /* If "pos" isn't marked ready, we can't trust the |
493 | * remaining fields in "pos" */ | |
7b1f1fd1 TM |
494 | if (pos->cl_cons_state > NFS_CS_READY) { |
495 | atomic_inc(&pos->cl_count); | |
496 | spin_unlock(&nn->nfs_client_lock); | |
497 | ||
fe0bf118 | 498 | nfs_put_client(prev); |
7b1f1fd1 TM |
499 | prev = pos; |
500 | ||
501 | status = nfs_wait_client_init_complete(pos); | |
7b1f1fd1 | 502 | if (status < 0) |
64590daa TM |
503 | goto out; |
504 | status = -NFS4ERR_STALE_CLIENTID; | |
505 | spin_lock(&nn->nfs_client_lock); | |
7b1f1fd1 TM |
506 | } |
507 | if (pos->cl_cons_state != NFS_CS_READY) | |
05f4c350 CL |
508 | continue; |
509 | ||
05f4c350 CL |
510 | if (pos->cl_clientid != new->cl_clientid) |
511 | continue; | |
512 | ||
513 | atomic_inc(&pos->cl_count); | |
514 | spin_unlock(&nn->nfs_client_lock); | |
515 | ||
fe0bf118 | 516 | nfs_put_client(prev); |
4ae19c2d | 517 | prev = pos; |
05f4c350 CL |
518 | |
519 | status = nfs4_proc_setclientid_confirm(pos, &clid, cred); | |
4ae19c2d TM |
520 | switch (status) { |
521 | case -NFS4ERR_STALE_CLIENTID: | |
522 | break; | |
523 | case 0: | |
05f4c350 CL |
524 | nfs4_swap_callback_idents(pos, new); |
525 | ||
4ae19c2d | 526 | prev = NULL; |
05f4c350 CL |
527 | *result = pos; |
528 | dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n", | |
529 | __func__, pos, atomic_read(&pos->cl_count)); | |
f9b7ebdf TM |
530 | goto out; |
531 | case -ERESTARTSYS: | |
532 | case -ETIMEDOUT: | |
533 | /* The callback path may have been inadvertently | |
534 | * changed. Schedule recovery! | |
535 | */ | |
536 | nfs4_schedule_path_down_recovery(pos); | |
4ae19c2d TM |
537 | default: |
538 | goto out; | |
05f4c350 CL |
539 | } |
540 | ||
541 | spin_lock(&nn->nfs_client_lock); | |
05f4c350 | 542 | } |
4ae19c2d | 543 | spin_unlock(&nn->nfs_client_lock); |
05f4c350 | 544 | |
202c312d | 545 | /* No match found. The server lost our clientid */ |
4ae19c2d | 546 | out: |
fe0bf118 | 547 | nfs_put_client(prev); |
4ae19c2d TM |
548 | dprintk("NFS: <-- %s status = %d\n", __func__, status); |
549 | return status; | |
05f4c350 CL |
550 | } |
551 | ||
552 | #ifdef CONFIG_NFS_V4_1 | |
f9d640f3 TM |
553 | /* |
554 | * Returns true if the client IDs match | |
555 | */ | |
556 | static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b) | |
557 | { | |
558 | if (a->cl_clientid != b->cl_clientid) { | |
559 | dprintk("NFS: --> %s client ID %llx does not match %llx\n", | |
560 | __func__, a->cl_clientid, b->cl_clientid); | |
561 | return false; | |
562 | } | |
563 | dprintk("NFS: --> %s client ID %llx matches %llx\n", | |
564 | __func__, a->cl_clientid, b->cl_clientid); | |
565 | return true; | |
566 | } | |
567 | ||
05f4c350 CL |
568 | /* |
569 | * Returns true if the server owners match | |
570 | */ | |
571 | static bool | |
572 | nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b) | |
573 | { | |
574 | struct nfs41_server_owner *o1 = a->cl_serverowner; | |
575 | struct nfs41_server_owner *o2 = b->cl_serverowner; | |
576 | ||
577 | if (o1->minor_id != o2->minor_id) { | |
578 | dprintk("NFS: --> %s server owner minor IDs do not match\n", | |
579 | __func__); | |
580 | return false; | |
581 | } | |
582 | ||
583 | if (o1->major_id_sz != o2->major_id_sz) | |
584 | goto out_major_mismatch; | |
585 | if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0) | |
586 | goto out_major_mismatch; | |
587 | ||
588 | dprintk("NFS: --> %s server owners match\n", __func__); | |
589 | return true; | |
590 | ||
591 | out_major_mismatch: | |
592 | dprintk("NFS: --> %s server owner major IDs do not match\n", | |
593 | __func__); | |
594 | return false; | |
595 | } | |
596 | ||
597 | /** | |
598 | * nfs41_walk_client_list - Find nfs_client that matches a client/server owner | |
599 | * | |
600 | * @new: nfs_client with client ID to test | |
601 | * @result: OUT: found nfs_client, or new | |
602 | * @cred: credential to use for trunking test | |
603 | * | |
604 | * Returns zero, a negative errno, or a negative NFS4ERR status. | |
605 | * If zero is returned, an nfs_client pointer is planted in "result." | |
606 | * | |
607 | * NB: nfs41_walk_client_list() relies on the new nfs_client being | |
608 | * the last nfs_client on the list. | |
609 | */ | |
610 | int nfs41_walk_client_list(struct nfs_client *new, | |
611 | struct nfs_client **result, | |
612 | struct rpc_cred *cred) | |
613 | { | |
614 | struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id); | |
7b1f1fd1 | 615 | struct nfs_client *pos, *prev = NULL; |
202c312d | 616 | int status = -NFS4ERR_STALE_CLIENTID; |
05f4c350 CL |
617 | |
618 | spin_lock(&nn->nfs_client_lock); | |
7b1f1fd1 | 619 | list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) { |
080af20c SD |
620 | |
621 | if (pos->rpc_ops != new->rpc_ops) | |
622 | continue; | |
623 | ||
624 | if (pos->cl_proto != new->cl_proto) | |
625 | continue; | |
626 | ||
627 | if (pos->cl_minorversion != new->cl_minorversion) | |
628 | continue; | |
629 | ||
05f4c350 CL |
630 | /* If "pos" isn't marked ready, we can't trust the |
631 | * remaining fields in "pos", especially the client | |
632 | * ID and serverowner fields. Wait for CREATE_SESSION | |
633 | * to finish. */ | |
7b1f1fd1 | 634 | if (pos->cl_cons_state > NFS_CS_READY) { |
05f4c350 CL |
635 | atomic_inc(&pos->cl_count); |
636 | spin_unlock(&nn->nfs_client_lock); | |
637 | ||
fe0bf118 | 638 | nfs_put_client(prev); |
05f4c350 CL |
639 | prev = pos; |
640 | ||
202c312d | 641 | status = nfs_wait_client_init_complete(pos); |
7b1f1fd1 TM |
642 | if (status == 0) { |
643 | nfs4_schedule_lease_recovery(pos); | |
644 | status = nfs4_wait_clnt_recover(pos); | |
05f4c350 | 645 | } |
05f4c350 | 646 | spin_lock(&nn->nfs_client_lock); |
65436ec0 | 647 | if (status < 0) |
64590daa TM |
648 | break; |
649 | status = -NFS4ERR_STALE_CLIENTID; | |
05f4c350 | 650 | } |
7b1f1fd1 TM |
651 | if (pos->cl_cons_state != NFS_CS_READY) |
652 | continue; | |
05f4c350 | 653 | |
05f4c350 CL |
654 | if (!nfs4_match_clientids(pos, new)) |
655 | continue; | |
656 | ||
657 | if (!nfs4_match_serverowners(pos, new)) | |
658 | continue; | |
659 | ||
4ae19c2d | 660 | atomic_inc(&pos->cl_count); |
7b1f1fd1 | 661 | *result = pos; |
eb04e0ac | 662 | status = 0; |
05f4c350 CL |
663 | dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n", |
664 | __func__, pos, atomic_read(&pos->cl_count)); | |
7b1f1fd1 | 665 | break; |
05f4c350 CL |
666 | } |
667 | ||
202c312d | 668 | /* No matching nfs_client found. */ |
05f4c350 | 669 | spin_unlock(&nn->nfs_client_lock); |
202c312d | 670 | dprintk("NFS: <-- %s status = %d\n", __func__, status); |
fe0bf118 | 671 | nfs_put_client(prev); |
202c312d | 672 | return status; |
05f4c350 CL |
673 | } |
674 | #endif /* CONFIG_NFS_V4_1 */ | |
675 | ||
fcf10398 BS |
676 | static void nfs4_destroy_server(struct nfs_server *server) |
677 | { | |
678 | nfs_server_return_all_delegations(server); | |
679 | unset_pnfs_layoutdriver(server); | |
680 | nfs4_purge_state_owners(server); | |
681 | } | |
682 | ||
683 | /* | |
684 | * NFSv4.0 callback thread helper | |
685 | * | |
686 | * Find a client by callback identifier | |
687 | */ | |
688 | struct nfs_client * | |
689 | nfs4_find_client_ident(struct net *net, int cb_ident) | |
690 | { | |
691 | struct nfs_client *clp; | |
692 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
693 | ||
694 | spin_lock(&nn->nfs_client_lock); | |
695 | clp = idr_find(&nn->cb_ident_idr, cb_ident); | |
696 | if (clp) | |
697 | atomic_inc(&clp->cl_count); | |
698 | spin_unlock(&nn->nfs_client_lock); | |
699 | return clp; | |
700 | } | |
701 | ||
702 | #if defined(CONFIG_NFS_V4_1) | |
703 | /* Common match routine for v4.0 and v4.1 callback services */ | |
704 | static bool nfs4_cb_match_client(const struct sockaddr *addr, | |
705 | struct nfs_client *clp, u32 minorversion) | |
706 | { | |
707 | struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; | |
708 | ||
709 | /* Don't match clients that failed to initialise */ | |
710 | if (!(clp->cl_cons_state == NFS_CS_READY || | |
711 | clp->cl_cons_state == NFS_CS_SESSION_INITING)) | |
712 | return false; | |
713 | ||
714 | smp_rmb(); | |
715 | ||
716 | /* Match the version and minorversion */ | |
717 | if (clp->rpc_ops->version != 4 || | |
718 | clp->cl_minorversion != minorversion) | |
719 | return false; | |
720 | ||
721 | /* Match only the IP address, not the port number */ | |
722 | if (!nfs_sockaddr_match_ipaddr(addr, clap)) | |
723 | return false; | |
724 | ||
725 | return true; | |
726 | } | |
727 | ||
728 | /* | |
729 | * NFSv4.1 callback thread helper | |
730 | * For CB_COMPOUND calls, find a client by IP address, protocol version, | |
731 | * minorversion, and sessionID | |
732 | * | |
733 | * Returns NULL if no such client | |
734 | */ | |
735 | struct nfs_client * | |
736 | nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr, | |
459de2ed | 737 | struct nfs4_sessionid *sid, u32 minorversion) |
fcf10398 BS |
738 | { |
739 | struct nfs_client *clp; | |
740 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
741 | ||
742 | spin_lock(&nn->nfs_client_lock); | |
743 | list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { | |
459de2ed | 744 | if (nfs4_cb_match_client(addr, clp, minorversion) == false) |
fcf10398 BS |
745 | continue; |
746 | ||
747 | if (!nfs4_has_session(clp)) | |
748 | continue; | |
749 | ||
750 | /* Match sessionid*/ | |
751 | if (memcmp(clp->cl_session->sess_id.data, | |
752 | sid->data, NFS4_MAX_SESSIONID_LEN) != 0) | |
753 | continue; | |
754 | ||
755 | atomic_inc(&clp->cl_count); | |
756 | spin_unlock(&nn->nfs_client_lock); | |
757 | return clp; | |
758 | } | |
759 | spin_unlock(&nn->nfs_client_lock); | |
760 | return NULL; | |
761 | } | |
762 | ||
763 | #else /* CONFIG_NFS_V4_1 */ | |
764 | ||
765 | struct nfs_client * | |
766 | nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr, | |
459de2ed | 767 | struct nfs4_sessionid *sid, u32 minorversion) |
fcf10398 BS |
768 | { |
769 | return NULL; | |
770 | } | |
771 | #endif /* CONFIG_NFS_V4_1 */ | |
772 | ||
773 | /* | |
774 | * Set up an NFS4 client | |
775 | */ | |
776 | static int nfs4_set_client(struct nfs_server *server, | |
777 | const char *hostname, | |
778 | const struct sockaddr *addr, | |
779 | const size_t addrlen, | |
780 | const char *ip_addr, | |
781 | rpc_authflavor_t authflavour, | |
782 | int proto, const struct rpc_timeout *timeparms, | |
783 | u32 minorversion, struct net *net) | |
784 | { | |
785 | struct nfs_client_initdata cl_init = { | |
786 | .hostname = hostname, | |
787 | .addr = addr, | |
788 | .addrlen = addrlen, | |
ab7017a3 | 789 | .nfs_mod = &nfs_v4, |
fcf10398 BS |
790 | .proto = proto, |
791 | .minorversion = minorversion, | |
792 | .net = net, | |
793 | }; | |
794 | struct nfs_client *clp; | |
795 | int error; | |
796 | ||
797 | dprintk("--> nfs4_set_client()\n"); | |
798 | ||
799 | if (server->flags & NFS_MOUNT_NORESVPORT) | |
800 | set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags); | |
f112bb48 CL |
801 | if (server->options & NFS_OPTION_MIGRATION) |
802 | set_bit(NFS_CS_MIGRATION, &cl_init.init_flags); | |
fcf10398 BS |
803 | |
804 | /* Allocate or find a client reference we can use */ | |
805 | clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour); | |
806 | if (IS_ERR(clp)) { | |
807 | error = PTR_ERR(clp); | |
808 | goto error; | |
809 | } | |
810 | ||
811 | /* | |
812 | * Query for the lease time on clientid setup or renewal | |
813 | * | |
814 | * Note that this will be set on nfs_clients that were created | |
815 | * only for the DS role and did not set this bit, but now will | |
816 | * serve a dual role. | |
817 | */ | |
818 | set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state); | |
819 | ||
820 | server->nfs_client = clp; | |
821 | dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp); | |
822 | return 0; | |
823 | error: | |
824 | dprintk("<-- nfs4_set_client() = xerror %d\n", error); | |
825 | return error; | |
826 | } | |
827 | ||
828 | /* | |
829 | * Set up a pNFS Data Server client. | |
830 | * | |
831 | * Return any existing nfs_client that matches server address,port,version | |
832 | * and minorversion. | |
833 | * | |
834 | * For a new nfs_client, use a soft mount (default), a low retrans and a | |
835 | * low timeout interval so that if a connection is lost, we retry through | |
836 | * the MDS. | |
837 | */ | |
838 | struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp, | |
839 | const struct sockaddr *ds_addr, int ds_addrlen, | |
840 | int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans) | |
841 | { | |
842 | struct nfs_client_initdata cl_init = { | |
843 | .addr = ds_addr, | |
844 | .addrlen = ds_addrlen, | |
ab7017a3 | 845 | .nfs_mod = &nfs_v4, |
fcf10398 BS |
846 | .proto = ds_proto, |
847 | .minorversion = mds_clp->cl_minorversion, | |
848 | .net = mds_clp->cl_net, | |
849 | }; | |
850 | struct rpc_timeout ds_timeout; | |
851 | struct nfs_client *clp; | |
a363e32e PT |
852 | char buf[INET6_ADDRSTRLEN + 1]; |
853 | ||
854 | if (rpc_ntop(ds_addr, buf, sizeof(buf)) <= 0) | |
855 | return ERR_PTR(-EINVAL); | |
856 | cl_init.hostname = buf; | |
fcf10398 BS |
857 | |
858 | /* | |
859 | * Set an authflavor equual to the MDS value. Use the MDS nfs_client | |
860 | * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS | |
861 | * (section 13.1 RFC 5661). | |
862 | */ | |
863 | nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans); | |
864 | clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr, | |
865 | mds_clp->cl_rpcclient->cl_auth->au_flavor); | |
866 | ||
867 | dprintk("<-- %s %p\n", __func__, clp); | |
868 | return clp; | |
869 | } | |
870 | EXPORT_SYMBOL_GPL(nfs4_set_ds_client); | |
871 | ||
872 | /* | |
873 | * Session has been established, and the client marked ready. | |
874 | * Set the mount rsize and wsize with negotiated fore channel | |
875 | * attributes which will be bound checked in nfs_server_set_fsinfo. | |
876 | */ | |
877 | static void nfs4_session_set_rwsize(struct nfs_server *server) | |
878 | { | |
879 | #ifdef CONFIG_NFS_V4_1 | |
880 | struct nfs4_session *sess; | |
881 | u32 server_resp_sz; | |
882 | u32 server_rqst_sz; | |
883 | ||
884 | if (!nfs4_has_session(server->nfs_client)) | |
885 | return; | |
886 | sess = server->nfs_client->cl_session; | |
887 | server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead; | |
888 | server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead; | |
889 | ||
890 | if (server->rsize > server_resp_sz) | |
891 | server->rsize = server_resp_sz; | |
892 | if (server->wsize > server_rqst_sz) | |
893 | server->wsize = server_rqst_sz; | |
894 | #endif /* CONFIG_NFS_V4_1 */ | |
895 | } | |
896 | ||
897 | static int nfs4_server_common_setup(struct nfs_server *server, | |
5e6b1990 | 898 | struct nfs_fh *mntfh, bool auth_probe) |
fcf10398 BS |
899 | { |
900 | struct nfs_fattr *fattr; | |
901 | int error; | |
902 | ||
fcf10398 BS |
903 | /* data servers support only a subset of NFSv4.1 */ |
904 | if (is_ds_only_client(server->nfs_client)) | |
905 | return -EPROTONOSUPPORT; | |
906 | ||
907 | fattr = nfs_alloc_fattr(); | |
908 | if (fattr == NULL) | |
909 | return -ENOMEM; | |
910 | ||
911 | /* We must ensure the session is initialised first */ | |
18aad3d5 | 912 | error = nfs4_init_session(server->nfs_client); |
fcf10398 BS |
913 | if (error < 0) |
914 | goto out; | |
915 | ||
39c6daae TM |
916 | /* Set the basic capabilities */ |
917 | server->caps |= server->nfs_client->cl_mvops->init_caps; | |
918 | if (server->flags & NFS_MOUNT_NORDIRPLUS) | |
919 | server->caps &= ~NFS_CAP_READDIRPLUS; | |
920 | /* | |
921 | * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower | |
922 | * authentication. | |
923 | */ | |
924 | if (nfs4_disable_idmapping && | |
925 | server->client->cl_auth->au_flavor == RPC_AUTH_UNIX) | |
926 | server->caps |= NFS_CAP_UIDGID_NOMAP; | |
927 | ||
928 | ||
fcf10398 | 929 | /* Probe the root fh to retrieve its FSID and filehandle */ |
5e6b1990 | 930 | error = nfs4_get_rootfh(server, mntfh, auth_probe); |
fcf10398 BS |
931 | if (error < 0) |
932 | goto out; | |
933 | ||
934 | dprintk("Server FSID: %llx:%llx\n", | |
935 | (unsigned long long) server->fsid.major, | |
936 | (unsigned long long) server->fsid.minor); | |
9e6ee76d | 937 | nfs_display_fhandle(mntfh, "Pseudo-fs root FH"); |
fcf10398 BS |
938 | |
939 | nfs4_session_set_rwsize(server); | |
940 | ||
941 | error = nfs_probe_fsinfo(server, mntfh, fattr); | |
942 | if (error < 0) | |
943 | goto out; | |
944 | ||
945 | if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) | |
946 | server->namelen = NFS4_MAXNAMLEN; | |
947 | ||
948 | nfs_server_insert_lists(server); | |
949 | server->mount_time = jiffies; | |
950 | server->destroy = nfs4_destroy_server; | |
951 | out: | |
952 | nfs_free_fattr(fattr); | |
953 | return error; | |
954 | } | |
955 | ||
956 | /* | |
957 | * Create a version 4 volume record | |
958 | */ | |
959 | static int nfs4_init_server(struct nfs_server *server, | |
a3f73c27 | 960 | struct nfs_parsed_mount_data *data) |
fcf10398 BS |
961 | { |
962 | struct rpc_timeout timeparms; | |
963 | int error; | |
964 | ||
965 | dprintk("--> nfs4_init_server()\n"); | |
966 | ||
967 | nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, | |
968 | data->timeo, data->retrans); | |
969 | ||
970 | /* Initialise the client representation from the mount data */ | |
971 | server->flags = data->flags; | |
fcf10398 | 972 | server->options = data->options; |
0f5f49b8 | 973 | server->auth_info = data->auth_info; |
fcf10398 | 974 | |
4d4b69dd WAA |
975 | /* Use the first specified auth flavor. If this flavor isn't |
976 | * allowed by the server, use the SECINFO path to try the | |
977 | * other specified flavors */ | |
a3f73c27 WAA |
978 | if (data->auth_info.flavor_len >= 1) |
979 | data->selected_flavor = data->auth_info.flavors[0]; | |
980 | else | |
981 | data->selected_flavor = RPC_AUTH_UNIX; | |
5e6b1990 | 982 | |
fcf10398 BS |
983 | /* Get a client record */ |
984 | error = nfs4_set_client(server, | |
985 | data->nfs_server.hostname, | |
986 | (const struct sockaddr *)&data->nfs_server.address, | |
987 | data->nfs_server.addrlen, | |
988 | data->client_address, | |
a3f73c27 | 989 | data->selected_flavor, |
fcf10398 BS |
990 | data->nfs_server.protocol, |
991 | &timeparms, | |
992 | data->minorversion, | |
993 | data->net); | |
994 | if (error < 0) | |
995 | goto error; | |
996 | ||
fcf10398 BS |
997 | if (data->rsize) |
998 | server->rsize = nfs_block_size(data->rsize, NULL); | |
999 | if (data->wsize) | |
1000 | server->wsize = nfs_block_size(data->wsize, NULL); | |
1001 | ||
1002 | server->acregmin = data->acregmin * HZ; | |
1003 | server->acregmax = data->acregmax * HZ; | |
1004 | server->acdirmin = data->acdirmin * HZ; | |
1005 | server->acdirmax = data->acdirmax * HZ; | |
1006 | ||
1007 | server->port = data->nfs_server.port; | |
1008 | ||
a3f73c27 WAA |
1009 | error = nfs_init_server_rpcclient(server, &timeparms, |
1010 | data->selected_flavor); | |
fcf10398 BS |
1011 | |
1012 | error: | |
1013 | /* Done */ | |
1014 | dprintk("<-- nfs4_init_server() = %d\n", error); | |
1015 | return error; | |
1016 | } | |
1017 | ||
1018 | /* | |
1019 | * Create a version 4 volume record | |
1020 | * - keyed on server and FSID | |
1021 | */ | |
1179acc6 BS |
1022 | /*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data, |
1023 | struct nfs_fh *mntfh)*/ | |
1024 | struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info, | |
1025 | struct nfs_subversion *nfs_mod) | |
fcf10398 BS |
1026 | { |
1027 | struct nfs_server *server; | |
5e6b1990 | 1028 | bool auth_probe; |
fcf10398 BS |
1029 | int error; |
1030 | ||
1031 | dprintk("--> nfs4_create_server()\n"); | |
1032 | ||
1033 | server = nfs_alloc_server(); | |
1034 | if (!server) | |
1035 | return ERR_PTR(-ENOMEM); | |
1036 | ||
a3f73c27 | 1037 | auth_probe = mount_info->parsed->auth_info.flavor_len < 1; |
5e6b1990 | 1038 | |
fcf10398 | 1039 | /* set up the general RPC client */ |
1179acc6 | 1040 | error = nfs4_init_server(server, mount_info->parsed); |
fcf10398 BS |
1041 | if (error < 0) |
1042 | goto error; | |
1043 | ||
5e6b1990 | 1044 | error = nfs4_server_common_setup(server, mount_info->mntfh, auth_probe); |
fcf10398 BS |
1045 | if (error < 0) |
1046 | goto error; | |
1047 | ||
1048 | dprintk("<-- nfs4_create_server() = %p\n", server); | |
1049 | return server; | |
1050 | ||
1051 | error: | |
1052 | nfs_free_server(server); | |
1053 | dprintk("<-- nfs4_create_server() = error %d\n", error); | |
1054 | return ERR_PTR(error); | |
1055 | } | |
1056 | ||
1057 | /* | |
1058 | * Create an NFS4 referral server record | |
1059 | */ | |
1060 | struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data, | |
1061 | struct nfs_fh *mntfh) | |
1062 | { | |
1063 | struct nfs_client *parent_client; | |
1064 | struct nfs_server *server, *parent_server; | |
5837f6df | 1065 | bool auth_probe; |
fcf10398 BS |
1066 | int error; |
1067 | ||
1068 | dprintk("--> nfs4_create_referral_server()\n"); | |
1069 | ||
1070 | server = nfs_alloc_server(); | |
1071 | if (!server) | |
1072 | return ERR_PTR(-ENOMEM); | |
1073 | ||
1074 | parent_server = NFS_SB(data->sb); | |
1075 | parent_client = parent_server->nfs_client; | |
1076 | ||
1077 | /* Initialise the client representation from the parent server */ | |
1078 | nfs_server_copy_userdata(server, parent_server); | |
fcf10398 BS |
1079 | |
1080 | /* Get a client representation. | |
1081 | * Note: NFSv4 always uses TCP, */ | |
1082 | error = nfs4_set_client(server, data->hostname, | |
1083 | data->addr, | |
1084 | data->addrlen, | |
1085 | parent_client->cl_ipaddr, | |
1086 | data->authflavor, | |
1087 | rpc_protocol(parent_server->client), | |
1088 | parent_server->client->cl_timeout, | |
1089 | parent_client->cl_mvops->minor_version, | |
1090 | parent_client->cl_net); | |
1091 | if (error < 0) | |
1092 | goto error; | |
1093 | ||
1094 | error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor); | |
1095 | if (error < 0) | |
1096 | goto error; | |
1097 | ||
5837f6df WAA |
1098 | auth_probe = parent_server->auth_info.flavor_len < 1; |
1099 | ||
1100 | error = nfs4_server_common_setup(server, mntfh, auth_probe); | |
fcf10398 BS |
1101 | if (error < 0) |
1102 | goto error; | |
1103 | ||
1104 | dprintk("<-- nfs_create_referral_server() = %p\n", server); | |
1105 | return server; | |
1106 | ||
1107 | error: | |
1108 | nfs_free_server(server); | |
1109 | dprintk("<-- nfs4_create_referral_server() = error %d\n", error); | |
1110 | return ERR_PTR(error); | |
1111 | } | |
32e62b7c CL |
1112 | |
1113 | /* | |
1114 | * Grab the destination's particulars, including lease expiry time. | |
1115 | * | |
1116 | * Returns zero if probe succeeded and retrieved FSID matches the FSID | |
1117 | * we have cached. | |
1118 | */ | |
1119 | static int nfs_probe_destination(struct nfs_server *server) | |
1120 | { | |
1121 | struct inode *inode = server->super->s_root->d_inode; | |
1122 | struct nfs_fattr *fattr; | |
1123 | int error; | |
1124 | ||
1125 | fattr = nfs_alloc_fattr(); | |
1126 | if (fattr == NULL) | |
1127 | return -ENOMEM; | |
1128 | ||
1129 | /* Sanity: the probe won't work if the destination server | |
1130 | * does not recognize the migrated FH. */ | |
1131 | error = nfs_probe_fsinfo(server, NFS_FH(inode), fattr); | |
1132 | ||
1133 | nfs_free_fattr(fattr); | |
1134 | return error; | |
1135 | } | |
1136 | ||
1137 | /** | |
1138 | * nfs4_update_server - Move an nfs_server to a different nfs_client | |
1139 | * | |
1140 | * @server: represents FSID to be moved | |
1141 | * @hostname: new end-point's hostname | |
1142 | * @sap: new end-point's socket address | |
1143 | * @salen: size of "sap" | |
292f503c | 1144 | * @net: net namespace |
32e62b7c CL |
1145 | * |
1146 | * The nfs_server must be quiescent before this function is invoked. | |
1147 | * Either its session is drained (NFSv4.1+), or its transport is | |
1148 | * plugged and drained (NFSv4.0). | |
1149 | * | |
1150 | * Returns zero on success, or a negative errno value. | |
1151 | */ | |
1152 | int nfs4_update_server(struct nfs_server *server, const char *hostname, | |
292f503c | 1153 | struct sockaddr *sap, size_t salen, struct net *net) |
32e62b7c CL |
1154 | { |
1155 | struct nfs_client *clp = server->nfs_client; | |
1156 | struct rpc_clnt *clnt = server->client; | |
1157 | struct xprt_create xargs = { | |
1158 | .ident = clp->cl_proto, | |
292f503c | 1159 | .net = net, |
32e62b7c CL |
1160 | .dstaddr = sap, |
1161 | .addrlen = salen, | |
1162 | .servername = hostname, | |
1163 | }; | |
1164 | char buf[INET6_ADDRSTRLEN + 1]; | |
1165 | struct sockaddr_storage address; | |
1166 | struct sockaddr *localaddr = (struct sockaddr *)&address; | |
1167 | int error; | |
1168 | ||
1169 | dprintk("--> %s: move FSID %llx:%llx to \"%s\")\n", __func__, | |
1170 | (unsigned long long)server->fsid.major, | |
1171 | (unsigned long long)server->fsid.minor, | |
1172 | hostname); | |
1173 | ||
1174 | error = rpc_switch_client_transport(clnt, &xargs, clnt->cl_timeout); | |
1175 | if (error != 0) { | |
1176 | dprintk("<-- %s(): rpc_switch_client_transport returned %d\n", | |
1177 | __func__, error); | |
1178 | goto out; | |
1179 | } | |
1180 | ||
1181 | error = rpc_localaddr(clnt, localaddr, sizeof(address)); | |
1182 | if (error != 0) { | |
1183 | dprintk("<-- %s(): rpc_localaddr returned %d\n", | |
1184 | __func__, error); | |
1185 | goto out; | |
1186 | } | |
1187 | ||
1188 | error = -EAFNOSUPPORT; | |
1189 | if (rpc_ntop(localaddr, buf, sizeof(buf)) == 0) { | |
1190 | dprintk("<-- %s(): rpc_ntop returned %d\n", | |
1191 | __func__, error); | |
1192 | goto out; | |
1193 | } | |
1194 | ||
1195 | nfs_server_remove_lists(server); | |
1196 | error = nfs4_set_client(server, hostname, sap, salen, buf, | |
1197 | clp->cl_rpcclient->cl_auth->au_flavor, | |
1198 | clp->cl_proto, clnt->cl_timeout, | |
292f503c | 1199 | clp->cl_minorversion, net); |
32e62b7c CL |
1200 | nfs_put_client(clp); |
1201 | if (error != 0) { | |
1202 | nfs_server_insert_lists(server); | |
1203 | dprintk("<-- %s(): nfs4_set_client returned %d\n", | |
1204 | __func__, error); | |
1205 | goto out; | |
1206 | } | |
1207 | ||
1208 | if (server->nfs_client->cl_hostname == NULL) | |
1209 | server->nfs_client->cl_hostname = kstrdup(hostname, GFP_KERNEL); | |
1210 | nfs_server_insert_lists(server); | |
1211 | ||
1212 | error = nfs_probe_destination(server); | |
1213 | if (error < 0) | |
1214 | goto out; | |
1215 | ||
1216 | dprintk("<-- %s() succeeded\n", __func__); | |
1217 | ||
1218 | out: | |
1219 | return error; | |
1220 | } |