]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - fs/afs/rotate.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Handle fileserver selection and rotation.
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
11 #include <linux/sched.h>
12 #include <linux/delay.h>
13 #include <linux/sched/signal.h>
18 * Begin an operation on the fileserver.
20 * Fileserver operations are serialised on the server by vnode, so we serialise
21 * them here also using the io_lock.
23 bool afs_begin_vnode_operation(struct afs_fs_cursor
*fc
, struct afs_vnode
*vnode
,
24 struct key
*key
, bool intr
)
26 memset(fc
, 0, sizeof(*fc
));
29 fc
->ac
.error
= SHRT_MAX
;
30 fc
->error
= -EDESTADDRREQ
;
33 fc
->flags
|= AFS_FS_CURSOR_INTR
;
34 if (mutex_lock_interruptible(&vnode
->io_lock
) < 0) {
36 fc
->flags
|= AFS_FS_CURSOR_STOP
;
40 mutex_lock(&vnode
->io_lock
);
43 if (vnode
->lock_state
!= AFS_VNODE_LOCK_NONE
)
44 fc
->flags
|= AFS_FS_CURSOR_CUR_ONLY
;
49 * Begin iteration through a server list, starting with the vnode's last used
50 * server if possible, or the last recorded good server if not.
52 static bool afs_start_fs_iteration(struct afs_fs_cursor
*fc
,
53 struct afs_vnode
*vnode
)
55 struct afs_cb_interest
*cbi
;
58 read_lock(&vnode
->volume
->servers_lock
);
59 fc
->server_list
= afs_get_serverlist(vnode
->volume
->servers
);
60 read_unlock(&vnode
->volume
->servers_lock
);
62 fc
->untried
= (1UL << fc
->server_list
->nr_servers
) - 1;
63 fc
->index
= READ_ONCE(fc
->server_list
->preferred
);
65 cbi
= rcu_dereference_protected(vnode
->cb_interest
,
66 lockdep_is_held(&vnode
->io_lock
));
68 /* See if the vnode's preferred record is still available */
69 for (i
= 0; i
< fc
->server_list
->nr_servers
; i
++) {
70 if (fc
->server_list
->servers
[i
].cb_interest
== cbi
) {
76 /* If we have a lock outstanding on a server that's no longer
77 * serving this vnode, then we can't switch to another server
78 * and have to return an error.
80 if (fc
->flags
& AFS_FS_CURSOR_CUR_ONLY
) {
85 /* Note that the callback promise is effectively broken */
86 write_seqlock(&vnode
->cb_lock
);
87 ASSERTCMP(cbi
, ==, rcu_access_pointer(vnode
->cb_interest
));
88 rcu_assign_pointer(vnode
->cb_interest
, NULL
);
89 if (test_and_clear_bit(AFS_VNODE_CB_PROMISED
, &vnode
->flags
))
91 write_sequnlock(&vnode
->cb_lock
);
93 afs_put_cb_interest(afs_v2net(vnode
), cbi
);
102 * Post volume busy note.
104 static void afs_busy(struct afs_volume
*volume
, u32 abort_code
)
108 switch (abort_code
) {
109 case VOFFLINE
: m
= "offline"; break;
110 case VRESTARTING
: m
= "restarting"; break;
111 case VSALVAGING
: m
= "being salvaged"; break;
112 default: m
= "busy"; break;
115 pr_notice("kAFS: Volume %llu '%s' is %s\n", volume
->vid
, volume
->name
, m
);
119 * Sleep and retry the operation to the same fileserver.
121 static bool afs_sleep_and_retry(struct afs_fs_cursor
*fc
)
123 if (fc
->flags
& AFS_FS_CURSOR_INTR
) {
124 msleep_interruptible(1000);
125 if (signal_pending(current
)) {
126 fc
->error
= -ERESTARTSYS
;
137 * Select the fileserver to use. May be called multiple times to rotate
138 * through the fileservers.
140 bool afs_select_fileserver(struct afs_fs_cursor
*fc
)
142 struct afs_addr_list
*alist
;
143 struct afs_server
*server
;
144 struct afs_vnode
*vnode
= fc
->vnode
;
147 int error
= fc
->ac
.error
, i
;
149 _enter("%lx[%d],%lx[%d],%d,%d",
150 fc
->untried
, fc
->index
,
151 fc
->ac
.tried
, fc
->ac
.index
,
152 error
, fc
->ac
.abort_code
);
154 if (fc
->flags
& AFS_FS_CURSOR_STOP
) {
155 _leave(" = f [stopped]");
161 /* Evaluate the result of the previous operation, if there was one. */
168 /* Success or local failure. Stop. */
170 fc
->flags
|= AFS_FS_CURSOR_STOP
;
171 _leave(" = f [okay/local %d]", error
);
175 /* The far side rejected the operation on some grounds. This
176 * might involve the server being busy or the volume having been moved.
178 switch (fc
->ac
.abort_code
) {
180 /* This fileserver doesn't know about the volume.
181 * - May indicate that the VL is wrong - retry once and compare
183 * - May indicate that the fileserver couldn't attach to the vol.
185 if (fc
->flags
& AFS_FS_CURSOR_VNOVOL
) {
186 fc
->error
= -EREMOTEIO
;
190 write_lock(&vnode
->volume
->servers_lock
);
191 fc
->server_list
->vnovol_mask
|= 1 << fc
->index
;
192 write_unlock(&vnode
->volume
->servers_lock
);
194 set_bit(AFS_VOLUME_NEEDS_UPDATE
, &vnode
->volume
->flags
);
195 error
= afs_check_volume_status(vnode
->volume
, fc
);
197 goto failed_set_error
;
199 if (test_bit(AFS_VOLUME_DELETED
, &vnode
->volume
->flags
)) {
200 fc
->error
= -ENOMEDIUM
;
204 /* If the server list didn't change, then assume that
205 * it's the fileserver having trouble.
207 if (vnode
->volume
->servers
== fc
->server_list
) {
208 fc
->error
= -EREMOTEIO
;
213 fc
->flags
|= AFS_FS_CURSOR_VNOVOL
;
214 _leave(" = t [vnovol]");
217 case VSALVAGE
: /* TODO: Should this return an error or iterate? */
223 fc
->error
= afs_abort_to_error(fc
->ac
.abort_code
);
227 if (!test_and_set_bit(AFS_VOLUME_OFFLINE
, &vnode
->volume
->flags
)) {
228 afs_busy(vnode
->volume
, fc
->ac
.abort_code
);
229 clear_bit(AFS_VOLUME_BUSY
, &vnode
->volume
->flags
);
231 if (fc
->flags
& AFS_FS_CURSOR_NO_VSLEEP
) {
235 if (fc
->flags
& AFS_FS_CURSOR_CUR_ONLY
) {
244 /* Retry after going round all the servers unless we
245 * have a file lock we need to maintain.
247 if (fc
->flags
& AFS_FS_CURSOR_NO_VSLEEP
) {
251 if (!test_and_set_bit(AFS_VOLUME_BUSY
, &vnode
->volume
->flags
)) {
252 afs_busy(vnode
->volume
, fc
->ac
.abort_code
);
253 clear_bit(AFS_VOLUME_OFFLINE
, &vnode
->volume
->flags
);
256 if (fc
->flags
& AFS_FS_CURSOR_CUR_ONLY
) {
257 if (!afs_sleep_and_retry(fc
))
260 /* Retry with same server & address */
261 _leave(" = t [vbusy]");
265 fc
->flags
|= AFS_FS_CURSOR_VBUSY
;
269 /* The volume migrated to another server. We consider
270 * consider all locks and callbacks broken and request
271 * an update from the VLDB.
273 * We also limit the number of VMOVED hops we will
274 * honour, just in case someone sets up a loop.
276 if (fc
->flags
& AFS_FS_CURSOR_VMOVED
) {
277 fc
->error
= -EREMOTEIO
;
280 fc
->flags
|= AFS_FS_CURSOR_VMOVED
;
282 set_bit(AFS_VOLUME_WAIT
, &vnode
->volume
->flags
);
283 set_bit(AFS_VOLUME_NEEDS_UPDATE
, &vnode
->volume
->flags
);
284 error
= afs_check_volume_status(vnode
->volume
, fc
);
286 goto failed_set_error
;
288 /* If the server list didn't change, then the VLDB is
289 * out of sync with the fileservers. This is hopefully
290 * a temporary condition, however, so we don't want to
291 * permanently block access to the file.
293 * TODO: Try other fileservers if we can.
295 * TODO: Retry a few times with sleeps.
297 if (vnode
->volume
->servers
== fc
->server_list
) {
298 fc
->error
= -ENOMEDIUM
;
302 goto restart_from_beginning
;
305 clear_bit(AFS_VOLUME_OFFLINE
, &vnode
->volume
->flags
);
306 clear_bit(AFS_VOLUME_BUSY
, &vnode
->volume
->flags
);
307 fc
->error
= afs_abort_to_error(fc
->ac
.abort_code
);
313 if (fc
->error
!= -EDESTADDRREQ
)
314 goto iterate_address
;
324 goto iterate_address
;
327 _debug("call reset");
332 restart_from_beginning
:
334 afs_end_cursor(&fc
->ac
);
335 afs_put_cb_interest(afs_v2net(vnode
), fc
->cbi
);
337 afs_put_serverlist(afs_v2net(vnode
), fc
->server_list
);
338 fc
->server_list
= NULL
;
341 /* See if we need to do an update of the volume record. Note that the
342 * volume may have moved or even have been deleted.
344 error
= afs_check_volume_status(vnode
->volume
, fc
);
346 goto failed_set_error
;
348 if (!afs_start_fs_iteration(fc
, vnode
))
351 _debug("__ VOL %llx __", vnode
->volume
->vid
);
352 error
= afs_probe_fileservers(afs_v2net(vnode
), fc
->key
, fc
->server_list
);
354 goto failed_set_error
;
357 _debug("pick [%lx]", fc
->untried
);
359 error
= afs_wait_for_fs_probes(fc
->server_list
, fc
->untried
);
361 goto failed_set_error
;
363 /* Pick the untried server with the lowest RTT. If we have outstanding
364 * callbacks, we stick with the server we're already using if we can.
367 _debug("cbi %u", fc
->index
);
368 if (test_bit(fc
->index
, &fc
->untried
))
369 goto selected_server
;
370 afs_put_cb_interest(afs_v2net(vnode
), fc
->cbi
);
377 for (i
= 0; i
< fc
->server_list
->nr_servers
; i
++) {
378 struct afs_server
*s
= fc
->server_list
->servers
[i
].server
;
380 if (!test_bit(i
, &fc
->untried
) || !s
->probe
.responded
)
382 if (s
->probe
.rtt
< rtt
) {
389 goto no_more_servers
;
392 _debug("use %d", fc
->index
);
393 __clear_bit(fc
->index
, &fc
->untried
);
395 /* We're starting on a different fileserver from the list. We need to
396 * check it, create a callback intercept, find its address list and
397 * probe its capabilities before we use it.
399 ASSERTCMP(fc
->ac
.alist
, ==, NULL
);
400 server
= fc
->server_list
->servers
[fc
->index
].server
;
402 if (!afs_check_server_record(fc
, server
))
405 _debug("USING SERVER: %pU", &server
->uuid
);
407 /* Make sure we've got a callback interest record for this server. We
408 * have to link it in before we send the request as we can be sent a
409 * break request before we've finished decoding the reply and
410 * installing the vnode.
412 error
= afs_register_server_cb_interest(vnode
, fc
->server_list
,
415 goto failed_set_error
;
417 fc
->cbi
= afs_get_cb_interest(
418 rcu_dereference_protected(vnode
->cb_interest
,
419 lockdep_is_held(&vnode
->io_lock
)));
421 read_lock(&server
->fs_lock
);
422 alist
= rcu_dereference_protected(server
->addresses
,
423 lockdep_is_held(&server
->fs_lock
));
424 afs_get_addrlist(alist
);
425 read_unlock(&server
->fs_lock
);
427 memset(&fc
->ac
, 0, sizeof(fc
->ac
));
430 fc
->ac
.alist
= alist
;
432 afs_put_addrlist(alist
);
437 ASSERT(fc
->ac
.alist
);
438 /* Iterate over the current server's address list to try and find an
439 * address on which it will respond to us.
441 if (!afs_iterate_addresses(&fc
->ac
))
444 _debug("address [%u] %u/%u", fc
->index
, fc
->ac
.index
, fc
->ac
.alist
->nr_addrs
);
451 afs_end_cursor(&fc
->ac
);
455 /* That's all the servers poked to no good effect. Try again if some
458 if (fc
->flags
& AFS_FS_CURSOR_VBUSY
)
459 goto restart_from_beginning
;
461 e
.error
= -EDESTADDRREQ
;
463 for (i
= 0; i
< fc
->server_list
->nr_servers
; i
++) {
464 struct afs_server
*s
= fc
->server_list
->servers
[i
].server
;
466 afs_prioritise_error(&e
, READ_ONCE(s
->probe
.error
),
467 s
->probe
.abort_code
);
475 fc
->flags
|= AFS_FS_CURSOR_STOP
;
476 afs_end_cursor(&fc
->ac
);
477 _leave(" = f [failed %d]", fc
->error
);
482 * Select the same fileserver we used for a vnode before and only that
483 * fileserver. We use this when we have a lock on that file, which is backed
484 * only by the fileserver we obtained it from.
486 bool afs_select_current_fileserver(struct afs_fs_cursor
*fc
)
488 struct afs_vnode
*vnode
= fc
->vnode
;
489 struct afs_cb_interest
*cbi
;
490 struct afs_addr_list
*alist
;
491 int error
= fc
->ac
.error
;
495 cbi
= rcu_dereference_protected(vnode
->cb_interest
,
496 lockdep_is_held(&vnode
->io_lock
));
502 fc
->flags
|= AFS_FS_CURSOR_STOP
;
506 fc
->cbi
= afs_get_cb_interest(cbi
);
508 read_lock(&cbi
->server
->fs_lock
);
509 alist
= rcu_dereference_protected(cbi
->server
->addresses
,
510 lockdep_is_held(&cbi
->server
->fs_lock
));
511 afs_get_addrlist(alist
);
512 read_unlock(&cbi
->server
->fs_lock
);
515 fc
->flags
|= AFS_FS_CURSOR_STOP
;
519 memset(&fc
->ac
, 0, sizeof(fc
->ac
));
520 fc
->ac
.alist
= alist
;
522 goto iterate_address
;
526 /* Success or local failure. Stop. */
528 fc
->flags
|= AFS_FS_CURSOR_STOP
;
529 _leave(" = f [okay/local %d]", error
);
533 fc
->error
= afs_abort_to_error(fc
->ac
.abort_code
);
534 fc
->flags
|= AFS_FS_CURSOR_STOP
;
535 _leave(" = f [abort]");
548 goto iterate_address
;
552 /* Iterate over the current server's address list to try and find an
553 * address on which it will respond to us.
555 if (afs_iterate_addresses(&fc
->ac
)) {
560 afs_end_cursor(&fc
->ac
);
565 * Dump cursor state in the case of the error being EDESTADDRREQ.
567 static void afs_dump_edestaddrreq(const struct afs_fs_cursor
*fc
)
572 if (!IS_ENABLED(CONFIG_AFS_DEBUG_CURSOR
) || count
> 3)
578 pr_notice("EDESTADDR occurred\n");
579 pr_notice("FC: cbb=%x cbb2=%x fl=%hx err=%hd\n",
580 fc
->cb_break
, fc
->cb_break_2
, fc
->flags
, fc
->error
);
581 pr_notice("FC: ut=%lx ix=%d ni=%u\n",
582 fc
->untried
, fc
->index
, fc
->nr_iterations
);
584 if (fc
->server_list
) {
585 const struct afs_server_list
*sl
= fc
->server_list
;
586 pr_notice("FC: SL nr=%u pr=%u vnov=%hx\n",
587 sl
->nr_servers
, sl
->preferred
, sl
->vnovol_mask
);
588 for (i
= 0; i
< sl
->nr_servers
; i
++) {
589 const struct afs_server
*s
= sl
->servers
[i
].server
;
590 pr_notice("FC: server fl=%lx av=%u %pU\n",
591 s
->flags
, s
->addr_version
, &s
->uuid
);
593 const struct afs_addr_list
*a
=
594 rcu_dereference(s
->addresses
);
595 pr_notice("FC: - av=%u nr=%u/%u/%u pr=%u\n",
597 a
->nr_ipv4
, a
->nr_addrs
, a
->max_addrs
,
599 pr_notice("FC: - pr=%lx R=%lx F=%lx\n",
600 a
->probed
, a
->responded
, a
->failed
);
601 if (a
== fc
->ac
.alist
)
602 pr_notice("FC: - current\n");
607 pr_notice("AC: t=%lx ax=%u ac=%d er=%d r=%u ni=%u\n",
608 fc
->ac
.tried
, fc
->ac
.index
, fc
->ac
.abort_code
, fc
->ac
.error
,
609 fc
->ac
.responded
, fc
->ac
.nr_iterations
);
614 * Tidy up a filesystem cursor and unlock the vnode.
616 int afs_end_vnode_operation(struct afs_fs_cursor
*fc
)
618 struct afs_net
*net
= afs_v2net(fc
->vnode
);
620 if (fc
->error
== -EDESTADDRREQ
||
621 fc
->error
== -EADDRNOTAVAIL
||
622 fc
->error
== -ENETUNREACH
||
623 fc
->error
== -EHOSTUNREACH
)
624 afs_dump_edestaddrreq(fc
);
626 mutex_unlock(&fc
->vnode
->io_lock
);
628 afs_end_cursor(&fc
->ac
);
629 afs_put_cb_interest(net
, fc
->cbi
);
630 afs_put_serverlist(net
, fc
->server_list
);
632 if (fc
->error
== -ECONNABORTED
)
633 fc
->error
= afs_abort_to_error(fc
->ac
.abort_code
);