1 /* Handle vlserver selection and rotation.
3 * Copyright (C) 2018 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/sched.h>
14 #include <linux/sched/signal.h>
19 * Begin an operation on a volume location server.
21 bool afs_begin_vlserver_operation(struct afs_vl_cursor *vc, struct afs_cell *cell,
24 memset(vc, 0, sizeof(*vc));
27 vc->error = -EDESTADDRREQ;
28 vc->ac.error = SHRT_MAX;
30 if (signal_pending(current)) {
32 vc->flags |= AFS_VL_CURSOR_STOP;
40 * Begin iteration through a server list, starting with the last used server if
41 * possible, or the last recorded good server if not.
43 static bool afs_start_vl_iteration(struct afs_vl_cursor *vc)
45 struct afs_cell *cell = vc->cell;
47 if (wait_on_bit(&cell->flags, AFS_CELL_FL_NO_LOOKUP_YET,
48 TASK_INTERRUPTIBLE)) {
49 vc->error = -ERESTARTSYS;
53 read_lock(&cell->vl_servers_lock);
54 vc->server_list = afs_get_vlserverlist(
55 rcu_dereference_protected(cell->vl_servers,
56 lockdep_is_held(&cell->vl_servers_lock)));
57 read_unlock(&cell->vl_servers_lock);
58 if (!vc->server_list || !vc->server_list->nr_servers)
61 vc->untried = (1UL << vc->server_list->nr_servers) - 1;
67 * Select the vlserver to use. May be called multiple times to rotate
68 * through the vlservers.
70 bool afs_select_vlserver(struct afs_vl_cursor *vc)
72 struct afs_addr_list *alist;
73 struct afs_vlserver *vlserver;
75 int error = vc->ac.error, abort_code, i;
77 _enter("%lx[%d],%lx[%d],%d,%d",
78 vc->untried, vc->index,
79 vc->ac.tried, vc->ac.index,
80 error, vc->ac.abort_code);
82 if (vc->flags & AFS_VL_CURSOR_STOP) {
83 _leave(" = f [stopped]");
89 /* Evaluate the result of the previous operation, if there was one. */
96 /* Success or local failure. Stop. */
98 vc->flags |= AFS_VL_CURSOR_STOP;
99 _leave(" = f [okay/local %d]", vc->ac.error);
103 /* The far side rejected the operation on some grounds. This
104 * might involve the server being busy or the volume having been moved.
106 switch (vc->ac.abort_code) {
108 case AFSVL_BADVOLOPER:
110 /* The server went weird. */
111 vc->error = -EREMOTEIO;
112 //write_lock(&vc->cell->vl_servers_lock);
113 //vc->server_list->weird_mask |= 1 << vc->index;
114 //write_unlock(&vc->cell->vl_servers_lock);
118 vc->error = afs_abort_to_error(vc->ac.abort_code);
127 _debug("no conn %d", error);
129 goto iterate_address;
132 _debug("call reset");
134 vc->flags |= AFS_VL_CURSOR_RETRY;
138 restart_from_beginning:
140 afs_end_cursor(&vc->ac);
141 afs_put_vlserverlist(vc->cell->net, vc->server_list);
142 vc->server_list = NULL;
143 if (vc->flags & AFS_VL_CURSOR_RETRIED)
145 vc->flags |= AFS_VL_CURSOR_RETRIED;
149 if (!afs_start_vl_iteration(vc))
152 error = afs_send_vl_probes(vc->cell->net, vc->key, vc->server_list);
154 goto failed_set_error;
157 _debug("pick [%lx]", vc->untried);
159 error = afs_wait_for_vl_probes(vc->server_list, vc->untried);
161 goto failed_set_error;
163 /* Pick the untried server with the lowest RTT. */
164 vc->index = vc->server_list->preferred;
165 if (test_bit(vc->index, &vc->untried))
166 goto selected_server;
170 for (i = 0; i < vc->server_list->nr_servers; i++) {
171 struct afs_vlserver *s = vc->server_list->servers[i].server;
173 if (!test_bit(i, &vc->untried) || !s->probe.responded)
175 if (s->probe.rtt < rtt) {
182 goto no_more_servers;
185 _debug("use %d", vc->index);
186 __clear_bit(vc->index, &vc->untried);
188 /* We're starting on a different vlserver from the list. We need to
189 * check it, find its address list and probe its capabilities before we
192 ASSERTCMP(vc->ac.alist, ==, NULL);
193 vlserver = vc->server_list->servers[vc->index].server;
194 vc->server = vlserver;
196 _debug("USING VLSERVER: %s", vlserver->name);
198 read_lock(&vlserver->lock);
199 alist = rcu_dereference_protected(vlserver->addresses,
200 lockdep_is_held(&vlserver->lock));
201 afs_get_addrlist(alist);
202 read_unlock(&vlserver->lock);
204 memset(&vc->ac, 0, sizeof(vc->ac));
207 vc->ac.alist = alist;
209 afs_put_addrlist(alist);
214 ASSERT(vc->ac.alist);
215 /* Iterate over the current server's address list to try and find an
216 * address on which it will respond to us.
218 if (!afs_iterate_addresses(&vc->ac))
221 _debug("VL address %d/%d", vc->ac.index, vc->ac.alist->nr_addrs);
223 _leave(" = t %pISpc", &vc->ac.alist->addrs[vc->ac.index].transport);
228 afs_end_cursor(&vc->ac);
232 /* That's all the servers poked to no good effect. Try again if some
235 if (vc->flags & AFS_VL_CURSOR_RETRY)
236 goto restart_from_beginning;
239 error = -EDESTADDRREQ;
240 for (i = 0; i < vc->server_list->nr_servers; i++) {
241 struct afs_vlserver *s = vc->server_list->servers[i].server;
242 int probe_error = READ_ONCE(s->probe.error);
244 switch (probe_error) {
248 if (error == -ETIMEDOUT ||
253 if (error == -ENOMEM ||
258 if (error == -ENETUNREACH)
261 if (error == -EHOSTUNREACH)
264 if (error == -ECONNREFUSED)
267 if (error == -ECONNRESET)
269 case -ECONNRESET: /* Responded, but call expired. */
270 if (error == -ECONNABORTED)
273 abort_code = s->probe.abort_code;
279 if (error == -ECONNABORTED)
280 error = afs_abort_to_error(abort_code);
285 vc->flags |= AFS_VL_CURSOR_STOP;
286 afs_end_cursor(&vc->ac);
287 _leave(" = f [failed %d]", vc->error);
292 * Dump cursor state in the case of the error being EDESTADDRREQ.
294 static void afs_vl_dump_edestaddrreq(const struct afs_vl_cursor *vc)
299 if (!IS_ENABLED(CONFIG_AFS_DEBUG_CURSOR) || count > 3)
304 pr_notice("EDESTADDR occurred\n");
305 pr_notice("VC: ut=%lx ix=%u ni=%hu fl=%hx err=%hd\n",
306 vc->untried, vc->index, vc->nr_iterations, vc->flags, vc->error);
308 if (vc->server_list) {
309 const struct afs_vlserver_list *sl = vc->server_list;
310 pr_notice("VC: SL nr=%u ix=%u\n",
311 sl->nr_servers, sl->index);
312 for (i = 0; i < sl->nr_servers; i++) {
313 const struct afs_vlserver *s = sl->servers[i].server;
314 pr_notice("VC: server %s+%hu fl=%lx E=%hd\n",
315 s->name, s->port, s->flags, s->probe.error);
317 const struct afs_addr_list *a =
318 rcu_dereference(s->addresses);
319 pr_notice("VC: - nr=%u/%u/%u pf=%u\n",
320 a->nr_ipv4, a->nr_addrs, a->max_addrs,
322 pr_notice("VC: - pr=%lx R=%lx F=%lx\n",
323 a->probed, a->responded, a->failed);
324 if (a == vc->ac.alist)
325 pr_notice("VC: - current\n");
330 pr_notice("AC: t=%lx ax=%u ac=%d er=%d r=%u ni=%u\n",
331 vc->ac.tried, vc->ac.index, vc->ac.abort_code, vc->ac.error,
332 vc->ac.responded, vc->ac.nr_iterations);
337 * Tidy up a volume location server cursor and unlock the vnode.
339 int afs_end_vlserver_operation(struct afs_vl_cursor *vc)
341 struct afs_net *net = vc->cell->net;
343 if (vc->error == -EDESTADDRREQ ||
344 vc->error == -ENETUNREACH ||
345 vc->error == -EHOSTUNREACH)
346 afs_vl_dump_edestaddrreq(vc);
348 afs_end_cursor(&vc->ac);
349 afs_put_vlserverlist(net, vc->server_list);
351 if (vc->error == -ECONNABORTED)
352 vc->error = afs_abort_to_error(vc->ac.abort_code);