1 /* Handle fileserver selection and rotation.
3 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
15 #include <linux/sched.h>
16 #include <linux/delay.h>
17 #include <linux/sched/signal.h>
22 * Begin an operation on the fileserver.
24 * Fileserver operations are serialised on the server by vnode, so we serialise
25 * them here also using the io_lock.
27 bool afs_begin_vnode_operation(struct afs_fs_cursor *fc, struct afs_vnode *vnode,
30 memset(fc, 0, sizeof(*fc));
33 fc->ac.error = SHRT_MAX;
34 fc->error = -EDESTADDRREQ;
36 if (mutex_lock_interruptible(&vnode->io_lock) < 0) {
38 fc->flags |= AFS_FS_CURSOR_STOP;
42 if (vnode->lock_state != AFS_VNODE_LOCK_NONE)
43 fc->flags |= AFS_FS_CURSOR_CUR_ONLY;
48 * Begin iteration through a server list, starting with the vnode's last used
49 * server if possible, or the last recorded good server if not.
51 static bool afs_start_fs_iteration(struct afs_fs_cursor *fc,
52 struct afs_vnode *vnode)
54 struct afs_cb_interest *cbi;
57 read_lock(&vnode->volume->servers_lock);
58 fc->server_list = afs_get_serverlist(vnode->volume->servers);
59 read_unlock(&vnode->volume->servers_lock);
61 fc->untried = (1UL << fc->server_list->nr_servers) - 1;
62 fc->index = READ_ONCE(fc->server_list->preferred);
64 cbi = vnode->cb_interest;
66 /* See if the vnode's preferred record is still available */
67 for (i = 0; i < fc->server_list->nr_servers; i++) {
68 if (fc->server_list->servers[i].cb_interest == cbi) {
74 /* If we have a lock outstanding on a server that's no longer
75 * serving this vnode, then we can't switch to another server
76 * and have to return an error.
78 if (fc->flags & AFS_FS_CURSOR_CUR_ONLY) {
83 /* Note that the callback promise is effectively broken */
84 write_seqlock(&vnode->cb_lock);
85 ASSERTCMP(cbi, ==, vnode->cb_interest);
86 vnode->cb_interest = NULL;
87 if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags))
89 write_sequnlock(&vnode->cb_lock);
91 afs_put_cb_interest(afs_v2net(vnode), cbi);
100 * Post volume busy note.
102 static void afs_busy(struct afs_volume *volume, u32 abort_code)
106 switch (abort_code) {
107 case VOFFLINE: m = "offline"; break;
108 case VRESTARTING: m = "restarting"; break;
109 case VSALVAGING: m = "being salvaged"; break;
110 default: m = "busy"; break;
113 pr_notice("kAFS: Volume %llu '%s' is %s\n", volume->vid, volume->name, m);
117 * Sleep and retry the operation to the same fileserver.
119 static bool afs_sleep_and_retry(struct afs_fs_cursor *fc)
121 msleep_interruptible(1000);
122 if (signal_pending(current)) {
123 fc->error = -ERESTARTSYS;
131 * Select the fileserver to use. May be called multiple times to rotate
132 * through the fileservers.
134 bool afs_select_fileserver(struct afs_fs_cursor *fc)
136 struct afs_addr_list *alist;
137 struct afs_server *server;
138 struct afs_vnode *vnode = fc->vnode;
140 int error = fc->ac.error, i;
142 _enter("%lx[%d],%lx[%d],%d,%d",
143 fc->untried, fc->index,
144 fc->ac.tried, fc->ac.index,
145 error, fc->ac.abort_code);
147 if (fc->flags & AFS_FS_CURSOR_STOP) {
148 _leave(" = f [stopped]");
154 /* Evaluate the result of the previous operation, if there was one. */
161 /* Success or local failure. Stop. */
163 fc->flags |= AFS_FS_CURSOR_STOP;
164 _leave(" = f [okay/local %d]", error);
168 /* The far side rejected the operation on some grounds. This
169 * might involve the server being busy or the volume having been moved.
171 switch (fc->ac.abort_code) {
173 /* This fileserver doesn't know about the volume.
174 * - May indicate that the VL is wrong - retry once and compare
176 * - May indicate that the fileserver couldn't attach to the vol.
178 if (fc->flags & AFS_FS_CURSOR_VNOVOL) {
179 fc->error = -EREMOTEIO;
183 write_lock(&vnode->volume->servers_lock);
184 fc->server_list->vnovol_mask |= 1 << fc->index;
185 write_unlock(&vnode->volume->servers_lock);
187 set_bit(AFS_VOLUME_NEEDS_UPDATE, &vnode->volume->flags);
188 error = afs_check_volume_status(vnode->volume, fc->key);
190 goto failed_set_error;
192 if (test_bit(AFS_VOLUME_DELETED, &vnode->volume->flags)) {
193 fc->error = -ENOMEDIUM;
197 /* If the server list didn't change, then assume that
198 * it's the fileserver having trouble.
200 if (vnode->volume->servers == fc->server_list) {
201 fc->error = -EREMOTEIO;
206 fc->flags |= AFS_FS_CURSOR_VNOVOL;
207 _leave(" = t [vnovol]");
210 case VSALVAGE: /* TODO: Should this return an error or iterate? */
216 fc->error = afs_abort_to_error(fc->ac.abort_code);
220 if (!test_and_set_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags)) {
221 afs_busy(vnode->volume, fc->ac.abort_code);
222 clear_bit(AFS_VOLUME_BUSY, &vnode->volume->flags);
224 if (fc->flags & AFS_FS_CURSOR_NO_VSLEEP) {
228 if (fc->flags & AFS_FS_CURSOR_CUR_ONLY) {
237 /* Retry after going round all the servers unless we
238 * have a file lock we need to maintain.
240 if (fc->flags & AFS_FS_CURSOR_NO_VSLEEP) {
244 if (!test_and_set_bit(AFS_VOLUME_BUSY, &vnode->volume->flags)) {
245 afs_busy(vnode->volume, fc->ac.abort_code);
246 clear_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags);
249 if (fc->flags & AFS_FS_CURSOR_CUR_ONLY) {
250 if (!afs_sleep_and_retry(fc))
253 /* Retry with same server & address */
254 _leave(" = t [vbusy]");
258 fc->flags |= AFS_FS_CURSOR_VBUSY;
262 /* The volume migrated to another server. We consider
263 * consider all locks and callbacks broken and request
264 * an update from the VLDB.
266 * We also limit the number of VMOVED hops we will
267 * honour, just in case someone sets up a loop.
269 if (fc->flags & AFS_FS_CURSOR_VMOVED) {
270 fc->error = -EREMOTEIO;
273 fc->flags |= AFS_FS_CURSOR_VMOVED;
275 set_bit(AFS_VOLUME_WAIT, &vnode->volume->flags);
276 set_bit(AFS_VOLUME_NEEDS_UPDATE, &vnode->volume->flags);
277 error = afs_check_volume_status(vnode->volume, fc->key);
279 goto failed_set_error;
281 /* If the server list didn't change, then the VLDB is
282 * out of sync with the fileservers. This is hopefully
283 * a temporary condition, however, so we don't want to
284 * permanently block access to the file.
286 * TODO: Try other fileservers if we can.
288 * TODO: Retry a few times with sleeps.
290 if (vnode->volume->servers == fc->server_list) {
291 fc->error = -ENOMEDIUM;
295 goto restart_from_beginning;
298 clear_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags);
299 clear_bit(AFS_VOLUME_BUSY, &vnode->volume->flags);
300 fc->error = afs_abort_to_error(fc->ac.abort_code);
306 if (fc->error != -EDESTADDRREQ)
307 goto iterate_address;
314 goto iterate_address;
317 _debug("call reset");
322 restart_from_beginning:
324 afs_end_cursor(&fc->ac);
325 afs_put_cb_interest(afs_v2net(vnode), fc->cbi);
327 afs_put_serverlist(afs_v2net(vnode), fc->server_list);
328 fc->server_list = NULL;
331 /* See if we need to do an update of the volume record. Note that the
332 * volume may have moved or even have been deleted.
334 error = afs_check_volume_status(vnode->volume, fc->key);
336 goto failed_set_error;
338 if (!afs_start_fs_iteration(fc, vnode))
341 _debug("__ VOL %llx __", vnode->volume->vid);
342 error = afs_probe_fileservers(afs_v2net(vnode), fc->key, fc->server_list);
344 goto failed_set_error;
347 _debug("pick [%lx]", fc->untried);
349 error = afs_wait_for_fs_probes(fc->server_list, fc->untried);
351 goto failed_set_error;
353 /* Pick the untried server with the lowest RTT. If we have outstanding
354 * callbacks, we stick with the server we're already using if we can.
357 _debug("cbi %u", fc->index);
358 if (test_bit(fc->index, &fc->untried))
359 goto selected_server;
360 afs_put_cb_interest(afs_v2net(vnode), fc->cbi);
367 for (i = 0; i < fc->server_list->nr_servers; i++) {
368 struct afs_server *s = fc->server_list->servers[i].server;
370 if (!test_bit(i, &fc->untried) || !s->probe.responded)
372 if (s->probe.rtt < rtt) {
379 goto no_more_servers;
382 _debug("use %d", fc->index);
383 __clear_bit(fc->index, &fc->untried);
385 /* We're starting on a different fileserver from the list. We need to
386 * check it, create a callback intercept, find its address list and
387 * probe its capabilities before we use it.
389 ASSERTCMP(fc->ac.alist, ==, NULL);
390 server = fc->server_list->servers[fc->index].server;
392 if (!afs_check_server_record(fc, server))
395 _debug("USING SERVER: %pU", &server->uuid);
397 /* Make sure we've got a callback interest record for this server. We
398 * have to link it in before we send the request as we can be sent a
399 * break request before we've finished decoding the reply and
400 * installing the vnode.
402 error = afs_register_server_cb_interest(vnode, fc->server_list,
405 goto failed_set_error;
407 fc->cbi = afs_get_cb_interest(vnode->cb_interest);
409 read_lock(&server->fs_lock);
410 alist = rcu_dereference_protected(server->addresses,
411 lockdep_is_held(&server->fs_lock));
412 afs_get_addrlist(alist);
413 read_unlock(&server->fs_lock);
415 memset(&fc->ac, 0, sizeof(fc->ac));
418 fc->ac.alist = alist;
420 afs_put_addrlist(alist);
425 ASSERT(fc->ac.alist);
426 /* Iterate over the current server's address list to try and find an
427 * address on which it will respond to us.
429 if (!afs_iterate_addresses(&fc->ac))
432 _debug("address [%u] %u/%u", fc->index, fc->ac.index, fc->ac.alist->nr_addrs);
439 afs_end_cursor(&fc->ac);
443 /* That's all the servers poked to no good effect. Try again if some
446 if (fc->flags & AFS_FS_CURSOR_VBUSY)
447 goto restart_from_beginning;
450 error = -EDESTADDRREQ;
451 for (i = 0; i < fc->server_list->nr_servers; i++) {
452 struct afs_server *s = fc->server_list->servers[i].server;
453 int probe_error = READ_ONCE(s->probe.error);
455 switch (probe_error) {
459 if (error == -ETIMEDOUT ||
464 if (error == -ENOMEM ||
469 if (error == -ENETUNREACH)
472 if (error == -EHOSTUNREACH)
475 if (error == -ECONNREFUSED)
478 if (error == -ECONNRESET)
480 case -ECONNRESET: /* Responded, but call expired. */
481 if (error == -ECONNABORTED)
484 abort_code = s->probe.abort_code;
490 if (error == -ECONNABORTED)
491 error = afs_abort_to_error(abort_code);
496 fc->flags |= AFS_FS_CURSOR_STOP;
497 afs_end_cursor(&fc->ac);
498 _leave(" = f [failed %d]", fc->error);
503 * Select the same fileserver we used for a vnode before and only that
504 * fileserver. We use this when we have a lock on that file, which is backed
505 * only by the fileserver we obtained it from.
507 bool afs_select_current_fileserver(struct afs_fs_cursor *fc)
509 struct afs_vnode *vnode = fc->vnode;
510 struct afs_cb_interest *cbi = vnode->cb_interest;
511 struct afs_addr_list *alist;
512 int error = fc->ac.error;
520 fc->flags |= AFS_FS_CURSOR_STOP;
524 fc->cbi = afs_get_cb_interest(vnode->cb_interest);
526 read_lock(&cbi->server->fs_lock);
527 alist = rcu_dereference_protected(cbi->server->addresses,
528 lockdep_is_held(&cbi->server->fs_lock));
529 afs_get_addrlist(alist);
530 read_unlock(&cbi->server->fs_lock);
533 fc->flags |= AFS_FS_CURSOR_STOP;
537 memset(&fc->ac, 0, sizeof(fc->ac));
538 fc->ac.alist = alist;
540 goto iterate_address;
544 /* Success or local failure. Stop. */
546 fc->flags |= AFS_FS_CURSOR_STOP;
547 _leave(" = f [okay/local %d]", error);
551 fc->error = afs_abort_to_error(fc->ac.abort_code);
552 fc->flags |= AFS_FS_CURSOR_STOP;
553 _leave(" = f [abort]");
563 goto iterate_address;
567 /* Iterate over the current server's address list to try and find an
568 * address on which it will respond to us.
570 if (afs_iterate_addresses(&fc->ac)) {
575 afs_end_cursor(&fc->ac);
580 * Dump cursor state in the case of the error being EDESTADDRREQ.
582 static void afs_dump_edestaddrreq(const struct afs_fs_cursor *fc)
587 if (!IS_ENABLED(CONFIG_AFS_DEBUG_CURSOR) || count > 3)
593 pr_notice("EDESTADDR occurred\n");
594 pr_notice("FC: cbb=%x cbb2=%x fl=%hx err=%hd\n",
595 fc->cb_break, fc->cb_break_2, fc->flags, fc->error);
596 pr_notice("FC: ut=%lx ix=%d ni=%u\n",
597 fc->untried, fc->index, fc->nr_iterations);
599 if (fc->server_list) {
600 const struct afs_server_list *sl = fc->server_list;
601 pr_notice("FC: SL nr=%u pr=%u vnov=%hx\n",
602 sl->nr_servers, sl->preferred, sl->vnovol_mask);
603 for (i = 0; i < sl->nr_servers; i++) {
604 const struct afs_server *s = sl->servers[i].server;
605 pr_notice("FC: server fl=%lx av=%u %pU\n",
606 s->flags, s->addr_version, &s->uuid);
608 const struct afs_addr_list *a =
609 rcu_dereference(s->addresses);
610 pr_notice("FC: - av=%u nr=%u/%u/%u pr=%u\n",
612 a->nr_ipv4, a->nr_addrs, a->max_addrs,
614 pr_notice("FC: - pr=%lx R=%lx F=%lx\n",
615 a->probed, a->responded, a->failed);
616 if (a == fc->ac.alist)
617 pr_notice("FC: - current\n");
622 pr_notice("AC: t=%lx ax=%u ac=%d er=%d r=%u ni=%u\n",
623 fc->ac.tried, fc->ac.index, fc->ac.abort_code, fc->ac.error,
624 fc->ac.responded, fc->ac.nr_iterations);
629 * Tidy up a filesystem cursor and unlock the vnode.
631 int afs_end_vnode_operation(struct afs_fs_cursor *fc)
633 struct afs_net *net = afs_v2net(fc->vnode);
635 if (fc->error == -EDESTADDRREQ ||
636 fc->error == -ENETUNREACH ||
637 fc->error == -EHOSTUNREACH)
638 afs_dump_edestaddrreq(fc);
640 mutex_unlock(&fc->vnode->io_lock);
642 afs_end_cursor(&fc->ac);
643 afs_put_cb_interest(net, fc->cbi);
644 afs_put_serverlist(net, fc->server_list);
646 if (fc->error == -ECONNABORTED)
647 fc->error = afs_abort_to_error(fc->ac.abort_code);