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.
5 * Written by David Howells (dhowells@redhat.com)
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->cumul_error.error = -EDESTADDRREQ;
24 vc->nr_iterations = -1;
26 if (signal_pending(current)) {
27 vc->cumul_error.error = -EINTR;
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->cumul_error.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);
63 vc->cumul_error.error = -ENOENT;
67 if (cell->dns_source == DNS_RECORD_UNAVAILABLE) {
68 vc->cumul_error.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;
95 s32 abort_code = vc->call_abort_code;
96 int error = vc->call_error, i;
100 _enter("%lx[%d],%lx[%d],%d,%d",
101 vc->untried, vc->index,
102 vc->ac.tried, vc->ac.index,
105 if (vc->flags & AFS_VL_CURSOR_STOP) {
106 _leave(" = f [stopped]");
110 if (vc->nr_iterations == 0)
113 /* Evaluate the result of the previous operation, if there was one. */
117 /* Success or local failure. Stop. */
118 vc->cumul_error.error = error;
119 vc->flags |= AFS_VL_CURSOR_STOP;
120 _leave(" = f [okay/local %d]", vc->cumul_error.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 (abort_code) {
129 case AFSVL_BADVOLOPER:
131 /* The server went weird. */
132 afs_prioritise_error(&vc->cumul_error, -EREMOTEIO, abort_code);
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 afs_prioritise_error(&vc->cumul_error, error, abort_code);
151 _debug("no conn %d", error);
152 afs_prioritise_error(&vc->cumul_error, error, 0);
153 goto iterate_address;
156 _debug("call reset");
157 afs_prioritise_error(&vc->cumul_error, error, 0);
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 afs_prioritise_error(&vc->cumul_error, error, 0);
187 _debug("pick [%lx]", vc->untried);
189 error = afs_wait_for_vl_probes(vc->server_list, vc->untried);
191 afs_prioritise_error(&vc->cumul_error, error, 0);
195 /* Pick the untried server with the lowest RTT. */
196 vc->index = vc->server_list->preferred;
197 if (test_bit(vc->index, &vc->untried))
198 goto selected_server;
202 for (i = 0; i < vc->server_list->nr_servers; i++) {
203 struct afs_vlserver *s = vc->server_list->servers[i].server;
205 if (!test_bit(i, &vc->untried) ||
206 !test_bit(AFS_VLSERVER_FL_RESPONDING, &s->flags))
208 if (s->probe.rtt < rtt) {
215 goto no_more_servers;
218 _debug("use %d", vc->index);
219 __clear_bit(vc->index, &vc->untried);
221 /* We're starting on a different vlserver from the list. We need to
222 * check it, find its address list and probe its capabilities before we
225 ASSERTCMP(vc->ac.alist, ==, NULL);
226 vlserver = vc->server_list->servers[vc->index].server;
227 vc->server = vlserver;
229 _debug("USING VLSERVER: %s", vlserver->name);
231 read_lock(&vlserver->lock);
232 alist = rcu_dereference_protected(vlserver->addresses,
233 lockdep_is_held(&vlserver->lock));
234 afs_get_addrlist(alist);
235 read_unlock(&vlserver->lock);
237 memset(&vc->ac, 0, sizeof(vc->ac));
240 vc->ac.alist = alist;
242 afs_put_addrlist(alist);
247 ASSERT(vc->ac.alist);
248 /* Iterate over the current server's address list to try and find an
249 * address on which it will respond to us.
251 if (!afs_iterate_addresses(&vc->ac))
254 _debug("VL address %d/%d", vc->ac.index, vc->ac.alist->nr_addrs);
256 vc->call_responded = false;
257 _leave(" = t %pISpc", rxrpc_kernel_remote_addr(vc->ac.alist->addrs[vc->ac.index].peer));
262 afs_end_cursor(&vc->ac);
266 /* That's all the servers poked to no good effect. Try again if some
269 if (vc->flags & AFS_VL_CURSOR_RETRY)
270 goto restart_from_beginning;
272 for (i = 0; i < vc->server_list->nr_servers; i++) {
273 struct afs_vlserver *s = vc->server_list->servers[i].server;
275 if (test_bit(AFS_VLSERVER_FL_RESPONDING, &s->flags))
276 vc->cumul_error.responded = true;
277 afs_prioritise_error(&vc->cumul_error, READ_ONCE(s->probe.error),
278 s->probe.abort_code);
282 vc->flags |= AFS_VL_CURSOR_STOP;
283 afs_end_cursor(&vc->ac);
284 _leave(" = f [failed %d]", vc->cumul_error.error);
289 * Dump cursor state in the case of the error being EDESTADDRREQ.
291 static void afs_vl_dump_edestaddrreq(const struct afs_vl_cursor *vc)
293 struct afs_cell *cell = vc->cell;
297 if (!IS_ENABLED(CONFIG_AFS_DEBUG_CURSOR) || count > 3)
302 pr_notice("EDESTADDR occurred\n");
303 pr_notice("CELL: %s err=%d\n", cell->name, cell->error);
304 pr_notice("DNS: src=%u st=%u lc=%x\n",
305 cell->dns_source, cell->dns_status, cell->dns_lookup_count);
306 pr_notice("VC: ut=%lx ix=%u ni=%hu fl=%hx err=%hd\n",
307 vc->untried, vc->index, vc->nr_iterations, vc->flags,
308 vc->cumul_error.error);
309 pr_notice("VC: call er=%d ac=%d r=%u\n",
310 vc->call_error, vc->call_abort_code, vc->call_responded);
312 if (vc->server_list) {
313 const struct afs_vlserver_list *sl = vc->server_list;
314 pr_notice("VC: SL nr=%u ix=%u\n",
315 sl->nr_servers, sl->index);
316 for (i = 0; i < sl->nr_servers; i++) {
317 const struct afs_vlserver *s = sl->servers[i].server;
318 pr_notice("VC: server %s+%hu fl=%lx E=%hd\n",
319 s->name, s->port, s->flags, s->probe.error);
321 const struct afs_addr_list *a =
322 rcu_dereference(s->addresses);
323 pr_notice("VC: - nr=%u/%u/%u pf=%u\n",
324 a->nr_ipv4, a->nr_addrs, a->max_addrs,
326 pr_notice("VC: - R=%lx F=%lx\n",
327 a->responded, a->failed);
328 if (a == vc->ac.alist)
329 pr_notice("VC: - current\n");
334 pr_notice("AC: t=%lx ax=%u ni=%u\n",
335 vc->ac.tried, vc->ac.index, vc->ac.nr_iterations);
340 * Tidy up a volume location server cursor and unlock the vnode.
342 int afs_end_vlserver_operation(struct afs_vl_cursor *vc)
344 struct afs_net *net = vc->cell->net;
346 switch (vc->cumul_error.error) {
351 afs_vl_dump_edestaddrreq(vc);
355 afs_end_cursor(&vc->ac);
356 afs_put_vlserverlist(net, vc->server_list);
357 return vc->cumul_error.error;