1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Handle vlserver selection and rotation.
4 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/sched/signal.h>
15 * Begin an operation on a volume location server.
17 bool afs_begin_vlserver_operation(struct afs_vl_cursor *vc, struct afs_cell *cell,
20 memset(vc, 0, sizeof(*vc));
23 vc->error = -EDESTADDRREQ;
24 vc->ac.error = SHRT_MAX;
26 if (signal_pending(current)) {
28 vc->flags |= AFS_VL_CURSOR_STOP;
36 * Begin iteration through a server list, starting with the last used server if
37 * possible, or the last recorded good server if not.
39 static bool afs_start_vl_iteration(struct afs_vl_cursor *vc)
41 struct afs_cell *cell = vc->cell;
42 unsigned int dns_lookup_count;
44 if (cell->dns_source == DNS_RECORD_UNAVAILABLE ||
45 cell->dns_expiry <= ktime_get_real_seconds()) {
46 dns_lookup_count = smp_load_acquire(&cell->dns_lookup_count);
47 set_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags);
48 afs_queue_cell(cell, afs_cell_trace_get_queue_dns);
50 if (cell->dns_source == DNS_RECORD_UNAVAILABLE) {
51 if (wait_var_event_interruptible(
52 &cell->dns_lookup_count,
53 smp_load_acquire(&cell->dns_lookup_count)
54 != dns_lookup_count) < 0) {
55 vc->error = -ERESTARTSYS;
60 /* Status load is ordered after lookup counter load */
61 if (cell->dns_status == DNS_LOOKUP_GOT_NOT_FOUND) {
62 pr_warn("No record of cell %s\n", cell->name);
67 if (cell->dns_source == DNS_RECORD_UNAVAILABLE) {
68 vc->error = -EDESTADDRREQ;
73 read_lock(&cell->vl_servers_lock);
74 vc->server_list = afs_get_vlserverlist(
75 rcu_dereference_protected(cell->vl_servers,
76 lockdep_is_held(&cell->vl_servers_lock)));
77 read_unlock(&cell->vl_servers_lock);
78 if (!vc->server_list->nr_servers)
81 vc->untried = (1UL << vc->server_list->nr_servers) - 1;
87 * Select the vlserver to use. May be called multiple times to rotate
88 * through the vlservers.
90 bool afs_select_vlserver(struct afs_vl_cursor *vc)
92 struct afs_addr_list *alist;
93 struct afs_vlserver *vlserver;
96 int error = vc->ac.error, i;
98 _enter("%lx[%d],%lx[%d],%d,%d",
99 vc->untried, vc->index,
100 vc->ac.tried, vc->ac.index,
101 error, vc->ac.abort_code);
103 if (vc->flags & AFS_VL_CURSOR_STOP) {
104 _leave(" = f [stopped]");
110 /* Evaluate the result of the previous operation, if there was one. */
117 /* Success or local failure. Stop. */
119 vc->flags |= AFS_VL_CURSOR_STOP;
120 _leave(" = f [okay/local %d]", vc->ac.error);
124 /* The far side rejected the operation on some grounds. This
125 * might involve the server being busy or the volume having been moved.
127 switch (vc->ac.abort_code) {
129 case AFSVL_BADVOLOPER:
131 /* The server went weird. */
132 vc->error = -EREMOTEIO;
133 //write_lock(&vc->cell->vl_servers_lock);
134 //vc->server_list->weird_mask |= 1 << vc->index;
135 //write_unlock(&vc->cell->vl_servers_lock);
139 vc->error = afs_abort_to_error(vc->ac.abort_code);
151 _debug("no conn %d", error);
153 goto iterate_address;
156 _debug("call reset");
158 vc->flags |= AFS_VL_CURSOR_RETRY;
166 restart_from_beginning:
168 afs_end_cursor(&vc->ac);
169 afs_put_vlserverlist(vc->cell->net, vc->server_list);
170 vc->server_list = NULL;
171 if (vc->flags & AFS_VL_CURSOR_RETRIED)
173 vc->flags |= AFS_VL_CURSOR_RETRIED;
177 if (!afs_start_vl_iteration(vc))
180 error = afs_send_vl_probes(vc->cell->net, vc->key, vc->server_list);
182 goto failed_set_error;
185 _debug("pick [%lx]", vc->untried);
187 error = afs_wait_for_vl_probes(vc->server_list, vc->untried);
189 goto failed_set_error;
191 /* Pick the untried server with the lowest RTT. */
192 vc->index = vc->server_list->preferred;
193 if (test_bit(vc->index, &vc->untried))
194 goto selected_server;
198 for (i = 0; i < vc->server_list->nr_servers; i++) {
199 struct afs_vlserver *s = vc->server_list->servers[i].server;
201 if (!test_bit(i, &vc->untried) ||
202 !test_bit(AFS_VLSERVER_FL_RESPONDING, &s->flags))
204 if (s->probe.rtt < rtt) {
211 goto no_more_servers;
214 _debug("use %d", vc->index);
215 __clear_bit(vc->index, &vc->untried);
217 /* We're starting on a different vlserver from the list. We need to
218 * check it, find its address list and probe its capabilities before we
221 ASSERTCMP(vc->ac.alist, ==, NULL);
222 vlserver = vc->server_list->servers[vc->index].server;
223 vc->server = vlserver;
225 _debug("USING VLSERVER: %s", vlserver->name);
227 read_lock(&vlserver->lock);
228 alist = rcu_dereference_protected(vlserver->addresses,
229 lockdep_is_held(&vlserver->lock));
230 afs_get_addrlist(alist);
231 read_unlock(&vlserver->lock);
233 memset(&vc->ac, 0, sizeof(vc->ac));
236 vc->ac.alist = alist;
238 afs_put_addrlist(alist);
243 ASSERT(vc->ac.alist);
244 /* Iterate over the current server's address list to try and find an
245 * address on which it will respond to us.
247 if (!afs_iterate_addresses(&vc->ac))
250 _debug("VL address %d/%d", vc->ac.index, vc->ac.alist->nr_addrs);
252 _leave(" = t %pISpc", &vc->ac.alist->addrs[vc->ac.index].transport);
257 afs_end_cursor(&vc->ac);
261 /* That's all the servers poked to no good effect. Try again if some
264 if (vc->flags & AFS_VL_CURSOR_RETRY)
265 goto restart_from_beginning;
267 e.error = -EDESTADDRREQ;
269 for (i = 0; i < vc->server_list->nr_servers; i++) {
270 struct afs_vlserver *s = vc->server_list->servers[i].server;
272 if (test_bit(AFS_VLSERVER_FL_RESPONDING, &s->flags))
274 afs_prioritise_error(&e, READ_ONCE(s->probe.error),
275 s->probe.abort_code);
283 vc->flags |= AFS_VL_CURSOR_STOP;
284 afs_end_cursor(&vc->ac);
285 _leave(" = f [failed %d]", vc->error);
290 * Dump cursor state in the case of the error being EDESTADDRREQ.
292 static void afs_vl_dump_edestaddrreq(const struct afs_vl_cursor *vc)
294 struct afs_cell *cell = vc->cell;
298 if (!IS_ENABLED(CONFIG_AFS_DEBUG_CURSOR) || count > 3)
303 pr_notice("EDESTADDR occurred\n");
304 pr_notice("CELL: %s err=%d\n", cell->name, cell->error);
305 pr_notice("DNS: src=%u st=%u lc=%x\n",
306 cell->dns_source, cell->dns_status, cell->dns_lookup_count);
307 pr_notice("VC: ut=%lx ix=%u ni=%hu fl=%hx err=%hd\n",
308 vc->untried, vc->index, vc->nr_iterations, vc->flags, vc->error);
310 if (vc->server_list) {
311 const struct afs_vlserver_list *sl = vc->server_list;
312 pr_notice("VC: SL nr=%u ix=%u\n",
313 sl->nr_servers, sl->index);
314 for (i = 0; i < sl->nr_servers; i++) {
315 const struct afs_vlserver *s = sl->servers[i].server;
316 pr_notice("VC: server %s+%hu fl=%lx E=%hd\n",
317 s->name, s->port, s->flags, s->probe.error);
319 const struct afs_addr_list *a =
320 rcu_dereference(s->addresses);
321 pr_notice("VC: - nr=%u/%u/%u pf=%u\n",
322 a->nr_ipv4, a->nr_addrs, a->max_addrs,
324 pr_notice("VC: - R=%lx F=%lx\n",
325 a->responded, a->failed);
326 if (a == vc->ac.alist)
327 pr_notice("VC: - current\n");
332 pr_notice("AC: t=%lx ax=%u ac=%d er=%d r=%u ni=%u\n",
333 vc->ac.tried, vc->ac.index, vc->ac.abort_code, vc->ac.error,
334 vc->ac.responded, vc->ac.nr_iterations);
339 * Tidy up a volume location server cursor and unlock the vnode.
341 int afs_end_vlserver_operation(struct afs_vl_cursor *vc)
343 struct afs_net *net = vc->cell->net;
345 if (vc->error == -EDESTADDRREQ ||
346 vc->error == -EADDRNOTAVAIL ||
347 vc->error == -ENETUNREACH ||
348 vc->error == -EHOSTUNREACH)
349 afs_vl_dump_edestaddrreq(vc);
351 afs_end_cursor(&vc->ac);
352 afs_put_vlserverlist(net, vc->server_list);
354 if (vc->error == -ECONNABORTED)
355 vc->error = afs_abort_to_error(vc->ac.abort_code);