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->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 queue_work(afs_wq, &cell->manager);
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_source == DNS_RECORD_UNAVAILABLE) {
62 vc->error = -EDESTADDRREQ;
67 read_lock(&cell->vl_servers_lock);
68 vc->server_list = afs_get_vlserverlist(
69 rcu_dereference_protected(cell->vl_servers,
70 lockdep_is_held(&cell->vl_servers_lock)));
71 read_unlock(&cell->vl_servers_lock);
72 if (!vc->server_list->nr_servers)
75 vc->untried = (1UL << vc->server_list->nr_servers) - 1;
81 * Select the vlserver to use. May be called multiple times to rotate
82 * through the vlservers.
84 bool afs_select_vlserver(struct afs_vl_cursor *vc)
86 struct afs_addr_list *alist;
87 struct afs_vlserver *vlserver;
90 int error = vc->ac.error, i;
92 _enter("%lx[%d],%lx[%d],%d,%d",
93 vc->untried, vc->index,
94 vc->ac.tried, vc->ac.index,
95 error, vc->ac.abort_code);
97 if (vc->flags & AFS_VL_CURSOR_STOP) {
98 _leave(" = f [stopped]");
104 /* Evaluate the result of the previous operation, if there was one. */
111 /* Success or local failure. Stop. */
113 vc->flags |= AFS_VL_CURSOR_STOP;
114 _leave(" = f [okay/local %d]", vc->ac.error);
118 /* The far side rejected the operation on some grounds. This
119 * might involve the server being busy or the volume having been moved.
121 switch (vc->ac.abort_code) {
123 case AFSVL_BADVOLOPER:
125 /* The server went weird. */
126 vc->error = -EREMOTEIO;
127 //write_lock(&vc->cell->vl_servers_lock);
128 //vc->server_list->weird_mask |= 1 << vc->index;
129 //write_unlock(&vc->cell->vl_servers_lock);
133 vc->error = afs_abort_to_error(vc->ac.abort_code);
145 _debug("no conn %d", error);
147 goto iterate_address;
150 _debug("call reset");
152 vc->flags |= AFS_VL_CURSOR_RETRY;
156 restart_from_beginning:
158 afs_end_cursor(&vc->ac);
159 afs_put_vlserverlist(vc->cell->net, vc->server_list);
160 vc->server_list = NULL;
161 if (vc->flags & AFS_VL_CURSOR_RETRIED)
163 vc->flags |= AFS_VL_CURSOR_RETRIED;
167 if (!afs_start_vl_iteration(vc))
170 error = afs_send_vl_probes(vc->cell->net, vc->key, vc->server_list);
172 goto failed_set_error;
175 _debug("pick [%lx]", vc->untried);
177 error = afs_wait_for_vl_probes(vc->server_list, vc->untried);
179 goto failed_set_error;
181 /* Pick the untried server with the lowest RTT. */
182 vc->index = vc->server_list->preferred;
183 if (test_bit(vc->index, &vc->untried))
184 goto selected_server;
188 for (i = 0; i < vc->server_list->nr_servers; i++) {
189 struct afs_vlserver *s = vc->server_list->servers[i].server;
191 if (!test_bit(i, &vc->untried) || !s->probe.responded)
193 if (s->probe.rtt < rtt) {
200 goto no_more_servers;
203 _debug("use %d", vc->index);
204 __clear_bit(vc->index, &vc->untried);
206 /* We're starting on a different vlserver from the list. We need to
207 * check it, find its address list and probe its capabilities before we
210 ASSERTCMP(vc->ac.alist, ==, NULL);
211 vlserver = vc->server_list->servers[vc->index].server;
212 vc->server = vlserver;
214 _debug("USING VLSERVER: %s", vlserver->name);
216 read_lock(&vlserver->lock);
217 alist = rcu_dereference_protected(vlserver->addresses,
218 lockdep_is_held(&vlserver->lock));
219 afs_get_addrlist(alist);
220 read_unlock(&vlserver->lock);
222 memset(&vc->ac, 0, sizeof(vc->ac));
225 vc->ac.alist = alist;
227 afs_put_addrlist(alist);
232 ASSERT(vc->ac.alist);
233 /* Iterate over the current server's address list to try and find an
234 * address on which it will respond to us.
236 if (!afs_iterate_addresses(&vc->ac))
239 _debug("VL address %d/%d", vc->ac.index, vc->ac.alist->nr_addrs);
241 _leave(" = t %pISpc", &vc->ac.alist->addrs[vc->ac.index].transport);
246 afs_end_cursor(&vc->ac);
250 /* That's all the servers poked to no good effect. Try again if some
253 if (vc->flags & AFS_VL_CURSOR_RETRY)
254 goto restart_from_beginning;
256 e.error = -EDESTADDRREQ;
258 for (i = 0; i < vc->server_list->nr_servers; i++) {
259 struct afs_vlserver *s = vc->server_list->servers[i].server;
261 afs_prioritise_error(&e, READ_ONCE(s->probe.error),
262 s->probe.abort_code);
268 vc->flags |= AFS_VL_CURSOR_STOP;
269 afs_end_cursor(&vc->ac);
270 _leave(" = f [failed %d]", vc->error);
275 * Dump cursor state in the case of the error being EDESTADDRREQ.
277 static void afs_vl_dump_edestaddrreq(const struct afs_vl_cursor *vc)
282 if (!IS_ENABLED(CONFIG_AFS_DEBUG_CURSOR) || count > 3)
287 pr_notice("EDESTADDR occurred\n");
288 pr_notice("VC: ut=%lx ix=%u ni=%hu fl=%hx err=%hd\n",
289 vc->untried, vc->index, vc->nr_iterations, vc->flags, vc->error);
291 if (vc->server_list) {
292 const struct afs_vlserver_list *sl = vc->server_list;
293 pr_notice("VC: SL nr=%u ix=%u\n",
294 sl->nr_servers, sl->index);
295 for (i = 0; i < sl->nr_servers; i++) {
296 const struct afs_vlserver *s = sl->servers[i].server;
297 pr_notice("VC: server %s+%hu fl=%lx E=%hd\n",
298 s->name, s->port, s->flags, s->probe.error);
300 const struct afs_addr_list *a =
301 rcu_dereference(s->addresses);
302 pr_notice("VC: - nr=%u/%u/%u pf=%u\n",
303 a->nr_ipv4, a->nr_addrs, a->max_addrs,
305 pr_notice("VC: - pr=%lx R=%lx F=%lx\n",
306 a->probed, a->responded, a->failed);
307 if (a == vc->ac.alist)
308 pr_notice("VC: - current\n");
313 pr_notice("AC: t=%lx ax=%u ac=%d er=%d r=%u ni=%u\n",
314 vc->ac.tried, vc->ac.index, vc->ac.abort_code, vc->ac.error,
315 vc->ac.responded, vc->ac.nr_iterations);
320 * Tidy up a volume location server cursor and unlock the vnode.
322 int afs_end_vlserver_operation(struct afs_vl_cursor *vc)
324 struct afs_net *net = vc->cell->net;
326 if (vc->error == -EDESTADDRREQ ||
327 vc->error == -EADDRNOTAVAIL ||
328 vc->error == -ENETUNREACH ||
329 vc->error == -EHOSTUNREACH)
330 afs_vl_dump_edestaddrreq(vc);
332 afs_end_cursor(&vc->ac);
333 afs_put_vlserverlist(net, vc->server_list);
335 if (vc->error == -ECONNABORTED)
336 vc->error = afs_abort_to_error(vc->ac.abort_code);