1 /* Handle fileserver selection and rotation.
3 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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;
141 int error = fc->ac.error, i;
143 _enter("%lx[%d],%lx[%d],%d,%d",
144 fc->untried, fc->index,
145 fc->ac.tried, fc->ac.index,
146 error, fc->ac.abort_code);
148 if (fc->flags & AFS_FS_CURSOR_STOP) {
149 _leave(" = f [stopped]");
155 /* Evaluate the result of the previous operation, if there was one. */
162 /* Success or local failure. Stop. */
164 fc->flags |= AFS_FS_CURSOR_STOP;
165 _leave(" = f [okay/local %d]", error);
169 /* The far side rejected the operation on some grounds. This
170 * might involve the server being busy or the volume having been moved.
172 switch (fc->ac.abort_code) {
174 /* This fileserver doesn't know about the volume.
175 * - May indicate that the VL is wrong - retry once and compare
177 * - May indicate that the fileserver couldn't attach to the vol.
179 if (fc->flags & AFS_FS_CURSOR_VNOVOL) {
180 fc->error = -EREMOTEIO;
184 write_lock(&vnode->volume->servers_lock);
185 fc->server_list->vnovol_mask |= 1 << fc->index;
186 write_unlock(&vnode->volume->servers_lock);
188 set_bit(AFS_VOLUME_NEEDS_UPDATE, &vnode->volume->flags);
189 error = afs_check_volume_status(vnode->volume, fc->key);
191 goto failed_set_error;
193 if (test_bit(AFS_VOLUME_DELETED, &vnode->volume->flags)) {
194 fc->error = -ENOMEDIUM;
198 /* If the server list didn't change, then assume that
199 * it's the fileserver having trouble.
201 if (vnode->volume->servers == fc->server_list) {
202 fc->error = -EREMOTEIO;
207 fc->flags |= AFS_FS_CURSOR_VNOVOL;
208 _leave(" = t [vnovol]");
211 case VSALVAGE: /* TODO: Should this return an error or iterate? */
217 fc->error = afs_abort_to_error(fc->ac.abort_code);
221 if (!test_and_set_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags)) {
222 afs_busy(vnode->volume, fc->ac.abort_code);
223 clear_bit(AFS_VOLUME_BUSY, &vnode->volume->flags);
225 if (fc->flags & AFS_FS_CURSOR_NO_VSLEEP) {
229 if (fc->flags & AFS_FS_CURSOR_CUR_ONLY) {
238 /* Retry after going round all the servers unless we
239 * have a file lock we need to maintain.
241 if (fc->flags & AFS_FS_CURSOR_NO_VSLEEP) {
245 if (!test_and_set_bit(AFS_VOLUME_BUSY, &vnode->volume->flags)) {
246 afs_busy(vnode->volume, fc->ac.abort_code);
247 clear_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags);
250 if (fc->flags & AFS_FS_CURSOR_CUR_ONLY) {
251 if (!afs_sleep_and_retry(fc))
254 /* Retry with same server & address */
255 _leave(" = t [vbusy]");
259 fc->flags |= AFS_FS_CURSOR_VBUSY;
263 /* The volume migrated to another server. We consider
264 * consider all locks and callbacks broken and request
265 * an update from the VLDB.
267 * We also limit the number of VMOVED hops we will
268 * honour, just in case someone sets up a loop.
270 if (fc->flags & AFS_FS_CURSOR_VMOVED) {
271 fc->error = -EREMOTEIO;
274 fc->flags |= AFS_FS_CURSOR_VMOVED;
276 set_bit(AFS_VOLUME_WAIT, &vnode->volume->flags);
277 set_bit(AFS_VOLUME_NEEDS_UPDATE, &vnode->volume->flags);
278 error = afs_check_volume_status(vnode->volume, fc->key);
280 goto failed_set_error;
282 /* If the server list didn't change, then the VLDB is
283 * out of sync with the fileservers. This is hopefully
284 * a temporary condition, however, so we don't want to
285 * permanently block access to the file.
287 * TODO: Try other fileservers if we can.
289 * TODO: Retry a few times with sleeps.
291 if (vnode->volume->servers == fc->server_list) {
292 fc->error = -ENOMEDIUM;
296 goto restart_from_beginning;
299 clear_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags);
300 clear_bit(AFS_VOLUME_BUSY, &vnode->volume->flags);
301 fc->error = afs_abort_to_error(fc->ac.abort_code);
307 if (fc->error != -EDESTADDRREQ)
308 goto iterate_address;
318 goto iterate_address;
321 _debug("call reset");
326 restart_from_beginning:
328 afs_end_cursor(&fc->ac);
329 afs_put_cb_interest(afs_v2net(vnode), fc->cbi);
331 afs_put_serverlist(afs_v2net(vnode), fc->server_list);
332 fc->server_list = NULL;
335 /* See if we need to do an update of the volume record. Note that the
336 * volume may have moved or even have been deleted.
338 error = afs_check_volume_status(vnode->volume, fc->key);
340 goto failed_set_error;
342 if (!afs_start_fs_iteration(fc, vnode))
345 _debug("__ VOL %llx __", vnode->volume->vid);
346 error = afs_probe_fileservers(afs_v2net(vnode), fc->key, fc->server_list);
348 goto failed_set_error;
351 _debug("pick [%lx]", fc->untried);
353 error = afs_wait_for_fs_probes(fc->server_list, fc->untried);
355 goto failed_set_error;
357 /* Pick the untried server with the lowest RTT. If we have outstanding
358 * callbacks, we stick with the server we're already using if we can.
361 _debug("cbi %u", fc->index);
362 if (test_bit(fc->index, &fc->untried))
363 goto selected_server;
364 afs_put_cb_interest(afs_v2net(vnode), fc->cbi);
371 for (i = 0; i < fc->server_list->nr_servers; i++) {
372 struct afs_server *s = fc->server_list->servers[i].server;
374 if (!test_bit(i, &fc->untried) || !s->probe.responded)
376 if (s->probe.rtt < rtt) {
383 goto no_more_servers;
386 _debug("use %d", fc->index);
387 __clear_bit(fc->index, &fc->untried);
389 /* We're starting on a different fileserver from the list. We need to
390 * check it, create a callback intercept, find its address list and
391 * probe its capabilities before we use it.
393 ASSERTCMP(fc->ac.alist, ==, NULL);
394 server = fc->server_list->servers[fc->index].server;
396 if (!afs_check_server_record(fc, server))
399 _debug("USING SERVER: %pU", &server->uuid);
401 /* Make sure we've got a callback interest record for this server. We
402 * have to link it in before we send the request as we can be sent a
403 * break request before we've finished decoding the reply and
404 * installing the vnode.
406 error = afs_register_server_cb_interest(vnode, fc->server_list,
409 goto failed_set_error;
411 fc->cbi = afs_get_cb_interest(vnode->cb_interest);
413 read_lock(&server->fs_lock);
414 alist = rcu_dereference_protected(server->addresses,
415 lockdep_is_held(&server->fs_lock));
416 afs_get_addrlist(alist);
417 read_unlock(&server->fs_lock);
419 memset(&fc->ac, 0, sizeof(fc->ac));
422 fc->ac.alist = alist;
424 afs_put_addrlist(alist);
429 ASSERT(fc->ac.alist);
430 /* Iterate over the current server's address list to try and find an
431 * address on which it will respond to us.
433 if (!afs_iterate_addresses(&fc->ac))
436 _debug("address [%u] %u/%u", fc->index, fc->ac.index, fc->ac.alist->nr_addrs);
443 afs_end_cursor(&fc->ac);
447 /* That's all the servers poked to no good effect. Try again if some
450 if (fc->flags & AFS_FS_CURSOR_VBUSY)
451 goto restart_from_beginning;
453 e.error = -EDESTADDRREQ;
455 for (i = 0; i < fc->server_list->nr_servers; i++) {
456 struct afs_server *s = fc->server_list->servers[i].server;
458 afs_prioritise_error(&e, READ_ONCE(s->probe.error),
459 s->probe.abort_code);
465 fc->flags |= AFS_FS_CURSOR_STOP;
466 afs_end_cursor(&fc->ac);
467 _leave(" = f [failed %d]", fc->error);
472 * Select the same fileserver we used for a vnode before and only that
473 * fileserver. We use this when we have a lock on that file, which is backed
474 * only by the fileserver we obtained it from.
476 bool afs_select_current_fileserver(struct afs_fs_cursor *fc)
478 struct afs_vnode *vnode = fc->vnode;
479 struct afs_cb_interest *cbi = vnode->cb_interest;
480 struct afs_addr_list *alist;
481 int error = fc->ac.error;
489 fc->flags |= AFS_FS_CURSOR_STOP;
493 fc->cbi = afs_get_cb_interest(vnode->cb_interest);
495 read_lock(&cbi->server->fs_lock);
496 alist = rcu_dereference_protected(cbi->server->addresses,
497 lockdep_is_held(&cbi->server->fs_lock));
498 afs_get_addrlist(alist);
499 read_unlock(&cbi->server->fs_lock);
502 fc->flags |= AFS_FS_CURSOR_STOP;
506 memset(&fc->ac, 0, sizeof(fc->ac));
507 fc->ac.alist = alist;
509 goto iterate_address;
513 /* Success or local failure. Stop. */
515 fc->flags |= AFS_FS_CURSOR_STOP;
516 _leave(" = f [okay/local %d]", error);
520 fc->error = afs_abort_to_error(fc->ac.abort_code);
521 fc->flags |= AFS_FS_CURSOR_STOP;
522 _leave(" = f [abort]");
535 goto iterate_address;
539 /* Iterate over the current server's address list to try and find an
540 * address on which it will respond to us.
542 if (afs_iterate_addresses(&fc->ac)) {
547 afs_end_cursor(&fc->ac);
552 * Dump cursor state in the case of the error being EDESTADDRREQ.
554 static void afs_dump_edestaddrreq(const struct afs_fs_cursor *fc)
559 if (!IS_ENABLED(CONFIG_AFS_DEBUG_CURSOR) || count > 3)
565 pr_notice("EDESTADDR occurred\n");
566 pr_notice("FC: cbb=%x cbb2=%x fl=%hx err=%hd\n",
567 fc->cb_break, fc->cb_break_2, fc->flags, fc->error);
568 pr_notice("FC: ut=%lx ix=%d ni=%u\n",
569 fc->untried, fc->index, fc->nr_iterations);
571 if (fc->server_list) {
572 const struct afs_server_list *sl = fc->server_list;
573 pr_notice("FC: SL nr=%u pr=%u vnov=%hx\n",
574 sl->nr_servers, sl->preferred, sl->vnovol_mask);
575 for (i = 0; i < sl->nr_servers; i++) {
576 const struct afs_server *s = sl->servers[i].server;
577 pr_notice("FC: server fl=%lx av=%u %pU\n",
578 s->flags, s->addr_version, &s->uuid);
580 const struct afs_addr_list *a =
581 rcu_dereference(s->addresses);
582 pr_notice("FC: - av=%u nr=%u/%u/%u pr=%u\n",
584 a->nr_ipv4, a->nr_addrs, a->max_addrs,
586 pr_notice("FC: - pr=%lx R=%lx F=%lx\n",
587 a->probed, a->responded, a->failed);
588 if (a == fc->ac.alist)
589 pr_notice("FC: - current\n");
594 pr_notice("AC: t=%lx ax=%u ac=%d er=%d r=%u ni=%u\n",
595 fc->ac.tried, fc->ac.index, fc->ac.abort_code, fc->ac.error,
596 fc->ac.responded, fc->ac.nr_iterations);
601 * Tidy up a filesystem cursor and unlock the vnode.
603 int afs_end_vnode_operation(struct afs_fs_cursor *fc)
605 struct afs_net *net = afs_v2net(fc->vnode);
607 if (fc->error == -EDESTADDRREQ ||
608 fc->error == -EADDRNOTAVAIL ||
609 fc->error == -ENETUNREACH ||
610 fc->error == -EHOSTUNREACH)
611 afs_dump_edestaddrreq(fc);
613 mutex_unlock(&fc->vnode->io_lock);
615 afs_end_cursor(&fc->ac);
616 afs_put_cb_interest(net, fc->cbi);
617 afs_put_serverlist(net, fc->server_list);
619 if (fc->error == -ECONNABORTED)
620 fc->error = afs_abort_to_error(fc->ac.abort_code);