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 iteration through a server list, starting with the vnode's last used
19 * server if possible, or the last recorded good server if not.
21 static bool afs_start_fs_iteration(struct afs_operation *op,
22 struct afs_vnode *vnode)
24 struct afs_cb_interest *cbi;
27 read_lock(&op->volume->servers_lock);
28 op->server_list = afs_get_serverlist(
29 rcu_dereference_protected(op->volume->servers,
30 lockdep_is_held(&op->volume->servers_lock)));
31 read_unlock(&op->volume->servers_lock);
33 op->untried = (1UL << op->server_list->nr_servers) - 1;
34 op->index = READ_ONCE(op->server_list->preferred);
36 cbi = rcu_dereference_protected(vnode->cb_interest,
37 lockdep_is_held(&vnode->io_lock));
39 /* See if the vnode's preferred record is still available */
40 for (i = 0; i < op->server_list->nr_servers; i++) {
41 if (op->server_list->servers[i].cb_interest == cbi) {
47 /* If we have a lock outstanding on a server that's no longer
48 * serving this vnode, then we can't switch to another server
49 * and have to return an error.
51 if (op->flags & AFS_OPERATION_CUR_ONLY) {
56 /* Note that the callback promise is effectively broken */
57 write_seqlock(&vnode->cb_lock);
58 ASSERTCMP(cbi, ==, rcu_access_pointer(vnode->cb_interest));
59 rcu_assign_pointer(vnode->cb_interest, NULL);
60 if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags))
62 write_sequnlock(&vnode->cb_lock);
64 afs_put_cb_interest(op->net, cbi);
73 * Post volume busy note.
75 static void afs_busy(struct afs_volume *volume, u32 abort_code)
80 case VOFFLINE: m = "offline"; break;
81 case VRESTARTING: m = "restarting"; break;
82 case VSALVAGING: m = "being salvaged"; break;
83 default: m = "busy"; break;
86 pr_notice("kAFS: Volume %llu '%s' is %s\n", volume->vid, volume->name, m);
90 * Sleep and retry the operation to the same fileserver.
92 static bool afs_sleep_and_retry(struct afs_operation *op)
94 if (!(op->flags & AFS_OPERATION_UNINTR)) {
95 msleep_interruptible(1000);
96 if (signal_pending(current)) {
97 op->error = -ERESTARTSYS;
108 * Select the fileserver to use. May be called multiple times to rotate
109 * through the fileservers.
111 bool afs_select_fileserver(struct afs_operation *op)
113 struct afs_addr_list *alist;
114 struct afs_server *server;
115 struct afs_vnode *vnode = op->file[0].vnode;
118 int error = op->ac.error, i;
120 _enter("%lx[%d],%lx[%d],%d,%d",
121 op->untried, op->index,
122 op->ac.tried, op->ac.index,
123 error, op->ac.abort_code);
125 if (op->flags & AFS_OPERATION_STOP) {
126 _leave(" = f [stopped]");
132 /* Evaluate the result of the previous operation, if there was one. */
139 /* Success or local failure. Stop. */
141 op->flags |= AFS_OPERATION_STOP;
142 _leave(" = f [okay/local %d]", error);
146 /* The far side rejected the operation on some grounds. This
147 * might involve the server being busy or the volume having been moved.
149 switch (op->ac.abort_code) {
151 /* This fileserver doesn't know about the volume.
152 * - May indicate that the VL is wrong - retry once and compare
154 * - May indicate that the fileserver couldn't attach to the vol.
156 if (op->flags & AFS_OPERATION_VNOVOL) {
157 op->error = -EREMOTEIO;
161 write_lock(&op->volume->servers_lock);
162 op->server_list->vnovol_mask |= 1 << op->index;
163 write_unlock(&op->volume->servers_lock);
165 set_bit(AFS_VOLUME_NEEDS_UPDATE, &op->volume->flags);
166 error = afs_check_volume_status(op->volume, op);
168 goto failed_set_error;
170 if (test_bit(AFS_VOLUME_DELETED, &op->volume->flags)) {
171 op->error = -ENOMEDIUM;
175 /* If the server list didn't change, then assume that
176 * it's the fileserver having trouble.
178 if (rcu_access_pointer(op->volume->servers) == op->server_list) {
179 op->error = -EREMOTEIO;
184 op->flags |= AFS_OPERATION_VNOVOL;
185 _leave(" = t [vnovol]");
188 case VSALVAGE: /* TODO: Should this return an error or iterate? */
194 op->error = afs_abort_to_error(op->ac.abort_code);
198 if (!test_and_set_bit(AFS_VOLUME_OFFLINE, &op->volume->flags)) {
199 afs_busy(op->volume, op->ac.abort_code);
200 clear_bit(AFS_VOLUME_BUSY, &op->volume->flags);
202 if (op->flags & AFS_OPERATION_NO_VSLEEP) {
206 if (op->flags & AFS_OPERATION_CUR_ONLY) {
215 /* Retry after going round all the servers unless we
216 * have a file lock we need to maintain.
218 if (op->flags & AFS_OPERATION_NO_VSLEEP) {
222 if (!test_and_set_bit(AFS_VOLUME_BUSY, &op->volume->flags)) {
223 afs_busy(op->volume, op->ac.abort_code);
224 clear_bit(AFS_VOLUME_OFFLINE, &op->volume->flags);
227 if (op->flags & AFS_OPERATION_CUR_ONLY) {
228 if (!afs_sleep_and_retry(op))
231 /* Retry with same server & address */
232 _leave(" = t [vbusy]");
236 op->flags |= AFS_OPERATION_VBUSY;
240 /* The volume migrated to another server. We consider
241 * consider all locks and callbacks broken and request
242 * an update from the VLDB.
244 * We also limit the number of VMOVED hops we will
245 * honour, just in case someone sets up a loop.
247 if (op->flags & AFS_OPERATION_VMOVED) {
248 op->error = -EREMOTEIO;
251 op->flags |= AFS_OPERATION_VMOVED;
253 set_bit(AFS_VOLUME_WAIT, &op->volume->flags);
254 set_bit(AFS_VOLUME_NEEDS_UPDATE, &op->volume->flags);
255 error = afs_check_volume_status(op->volume, op);
257 goto failed_set_error;
259 /* If the server list didn't change, then the VLDB is
260 * out of sync with the fileservers. This is hopefully
261 * a temporary condition, however, so we don't want to
262 * permanently block access to the file.
264 * TODO: Try other fileservers if we can.
266 * TODO: Retry a few times with sleeps.
268 if (rcu_access_pointer(op->volume->servers) == op->server_list) {
269 op->error = -ENOMEDIUM;
273 goto restart_from_beginning;
276 clear_bit(AFS_VOLUME_OFFLINE, &op->volume->flags);
277 clear_bit(AFS_VOLUME_BUSY, &op->volume->flags);
278 op->error = afs_abort_to_error(op->ac.abort_code);
284 if (op->error != -EDESTADDRREQ)
285 goto iterate_address;
295 goto iterate_address;
298 _debug("call reset");
303 restart_from_beginning:
305 afs_end_cursor(&op->ac);
306 afs_put_cb_interest(op->net, op->cbi);
308 afs_put_serverlist(op->net, op->server_list);
309 op->server_list = NULL;
312 /* See if we need to do an update of the volume record. Note that the
313 * volume may have moved or even have been deleted.
315 error = afs_check_volume_status(op->volume, op);
317 goto failed_set_error;
319 if (!afs_start_fs_iteration(op, vnode))
322 _debug("__ VOL %llx __", op->volume->vid);
325 _debug("pick [%lx]", op->untried);
327 error = afs_wait_for_fs_probes(op->server_list, op->untried);
329 goto failed_set_error;
331 /* Pick the untried server with the lowest RTT. If we have outstanding
332 * callbacks, we stick with the server we're already using if we can.
335 _debug("cbi %u", op->index);
336 if (test_bit(op->index, &op->untried))
337 goto selected_server;
338 afs_put_cb_interest(op->net, op->cbi);
345 for (i = 0; i < op->server_list->nr_servers; i++) {
346 struct afs_server *s = op->server_list->servers[i].server;
348 if (!test_bit(i, &op->untried) || !s->probe.responded)
350 if (s->probe.rtt < rtt) {
357 goto no_more_servers;
360 _debug("use %d", op->index);
361 __clear_bit(op->index, &op->untried);
363 /* We're starting on a different fileserver from the list. We need to
364 * check it, create a callback intercept, find its address list and
365 * probe its capabilities before we use it.
367 ASSERTCMP(op->ac.alist, ==, NULL);
368 server = op->server_list->servers[op->index].server;
370 if (!afs_check_server_record(op, server))
373 _debug("USING SERVER: %pU", &server->uuid);
375 /* Make sure we've got a callback interest record for this server. We
376 * have to link it in before we send the request as we can be sent a
377 * break request before we've finished decoding the reply and
378 * installing the vnode.
380 error = afs_register_server_cb_interest(vnode, op->server_list,
383 goto failed_set_error;
385 op->cbi = afs_get_cb_interest(
386 rcu_dereference_protected(vnode->cb_interest,
387 lockdep_is_held(&vnode->io_lock)));
389 read_lock(&server->fs_lock);
390 alist = rcu_dereference_protected(server->addresses,
391 lockdep_is_held(&server->fs_lock));
392 afs_get_addrlist(alist);
393 read_unlock(&server->fs_lock);
395 memset(&op->ac, 0, sizeof(op->ac));
398 op->ac.alist = alist;
400 afs_put_addrlist(alist);
405 ASSERT(op->ac.alist);
406 /* Iterate over the current server's address list to try and find an
407 * address on which it will respond to us.
409 if (!afs_iterate_addresses(&op->ac))
412 _debug("address [%u] %u/%u", op->index, op->ac.index, op->ac.alist->nr_addrs);
419 afs_end_cursor(&op->ac);
423 /* That's all the servers poked to no good effect. Try again if some
426 if (op->flags & AFS_OPERATION_VBUSY)
427 goto restart_from_beginning;
429 e.error = -EDESTADDRREQ;
431 for (i = 0; i < op->server_list->nr_servers; i++) {
432 struct afs_server *s = op->server_list->servers[i].server;
434 afs_prioritise_error(&e, READ_ONCE(s->probe.error),
435 s->probe.abort_code);
443 op->flags |= AFS_OPERATION_STOP;
444 afs_end_cursor(&op->ac);
445 _leave(" = f [failed %d]", op->error);
450 * Select the same fileserver we used for a vnode before and only that
451 * fileserver. We use this when we have a lock on that file, which is backed
452 * only by the fileserver we obtained it from.
454 bool afs_select_current_fileserver(struct afs_operation *op)
456 struct afs_cb_interest *cbi;
457 struct afs_addr_list *alist;
458 int error = op->ac.error;
467 op->flags |= AFS_OPERATION_STOP;
471 read_lock(&cbi->server->fs_lock);
472 alist = rcu_dereference_protected(cbi->server->addresses,
473 lockdep_is_held(&cbi->server->fs_lock));
474 afs_get_addrlist(alist);
475 read_unlock(&cbi->server->fs_lock);
478 op->flags |= AFS_OPERATION_STOP;
482 memset(&op->ac, 0, sizeof(op->ac));
483 op->ac.alist = alist;
485 goto iterate_address;
489 /* Success or local failure. Stop. */
491 op->flags |= AFS_OPERATION_STOP;
492 _leave(" = f [okay/local %d]", error);
496 op->error = afs_abort_to_error(op->ac.abort_code);
497 op->flags |= AFS_OPERATION_STOP;
498 _leave(" = f [abort]");
511 goto iterate_address;
515 /* Iterate over the current server's address list to try and find an
516 * address on which it will respond to us.
518 if (afs_iterate_addresses(&op->ac)) {
523 afs_end_cursor(&op->ac);
528 * Dump cursor state in the case of the error being EDESTADDRREQ.
530 void afs_dump_edestaddrreq(const struct afs_operation *op)
535 if (!IS_ENABLED(CONFIG_AFS_DEBUG_CURSOR) || count > 3)
541 pr_notice("EDESTADDR occurred\n");
542 pr_notice("FC: cbb=%x cbb2=%x fl=%x err=%hd\n",
543 op->file[0].cb_break_before,
544 op->file[1].cb_break_before, op->flags, op->error);
545 pr_notice("FC: ut=%lx ix=%d ni=%u\n",
546 op->untried, op->index, op->nr_iterations);
548 if (op->server_list) {
549 const struct afs_server_list *sl = op->server_list;
550 pr_notice("FC: SL nr=%u pr=%u vnov=%hx\n",
551 sl->nr_servers, sl->preferred, sl->vnovol_mask);
552 for (i = 0; i < sl->nr_servers; i++) {
553 const struct afs_server *s = sl->servers[i].server;
554 pr_notice("FC: server fl=%lx av=%u %pU\n",
555 s->flags, s->addr_version, &s->uuid);
557 const struct afs_addr_list *a =
558 rcu_dereference(s->addresses);
559 pr_notice("FC: - av=%u nr=%u/%u/%u pr=%u\n",
561 a->nr_ipv4, a->nr_addrs, a->max_addrs,
563 pr_notice("FC: - R=%lx F=%lx\n",
564 a->responded, a->failed);
565 if (a == op->ac.alist)
566 pr_notice("FC: - current\n");
571 pr_notice("AC: t=%lx ax=%u ac=%d er=%d r=%u ni=%u\n",
572 op->ac.tried, op->ac.index, op->ac.abort_code, op->ac.error,
573 op->ac.responded, op->ac.nr_iterations);