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;
76 int error = vc->ac.error, i;
78 _enter("%lx[%d],%lx[%d],%d,%d",
79 vc->untried, vc->index,
80 vc->ac.tried, vc->ac.index,
81 error, vc->ac.abort_code);
83 if (vc->flags & AFS_VL_CURSOR_STOP) {
84 _leave(" = f [stopped]");
90 /* Evaluate the result of the previous operation, if there was one. */
97 /* Success or local failure. Stop. */
99 vc->flags |= AFS_VL_CURSOR_STOP;
100 _leave(" = f [okay/local %d]", vc->ac.error);
104 /* The far side rejected the operation on some grounds. This
105 * might involve the server being busy or the volume having been moved.
107 switch (vc->ac.abort_code) {
109 case AFSVL_BADVOLOPER:
111 /* The server went weird. */
112 vc->error = -EREMOTEIO;
113 //write_lock(&vc->cell->vl_servers_lock);
114 //vc->server_list->weird_mask |= 1 << vc->index;
115 //write_unlock(&vc->cell->vl_servers_lock);
119 vc->error = afs_abort_to_error(vc->ac.abort_code);
131 _debug("no conn %d", error);
133 goto iterate_address;
136 _debug("call reset");
138 vc->flags |= AFS_VL_CURSOR_RETRY;
142 restart_from_beginning:
144 afs_end_cursor(&vc->ac);
145 afs_put_vlserverlist(vc->cell->net, vc->server_list);
146 vc->server_list = NULL;
147 if (vc->flags & AFS_VL_CURSOR_RETRIED)
149 vc->flags |= AFS_VL_CURSOR_RETRIED;
153 if (!afs_start_vl_iteration(vc))
156 error = afs_send_vl_probes(vc->cell->net, vc->key, vc->server_list);
158 goto failed_set_error;
161 _debug("pick [%lx]", vc->untried);
163 error = afs_wait_for_vl_probes(vc->server_list, vc->untried);
165 goto failed_set_error;
167 /* Pick the untried server with the lowest RTT. */
168 vc->index = vc->server_list->preferred;
169 if (test_bit(vc->index, &vc->untried))
170 goto selected_server;
174 for (i = 0; i < vc->server_list->nr_servers; i++) {
175 struct afs_vlserver *s = vc->server_list->servers[i].server;
177 if (!test_bit(i, &vc->untried) || !s->probe.responded)
179 if (s->probe.rtt < rtt) {
186 goto no_more_servers;
189 _debug("use %d", vc->index);
190 __clear_bit(vc->index, &vc->untried);
192 /* We're starting on a different vlserver from the list. We need to
193 * check it, find its address list and probe its capabilities before we
196 ASSERTCMP(vc->ac.alist, ==, NULL);
197 vlserver = vc->server_list->servers[vc->index].server;
198 vc->server = vlserver;
200 _debug("USING VLSERVER: %s", vlserver->name);
202 read_lock(&vlserver->lock);
203 alist = rcu_dereference_protected(vlserver->addresses,
204 lockdep_is_held(&vlserver->lock));
205 afs_get_addrlist(alist);
206 read_unlock(&vlserver->lock);
208 memset(&vc->ac, 0, sizeof(vc->ac));
211 vc->ac.alist = alist;
213 afs_put_addrlist(alist);
218 ASSERT(vc->ac.alist);
219 /* Iterate over the current server's address list to try and find an
220 * address on which it will respond to us.
222 if (!afs_iterate_addresses(&vc->ac))
225 _debug("VL address %d/%d", vc->ac.index, vc->ac.alist->nr_addrs);
227 _leave(" = t %pISpc", &vc->ac.alist->addrs[vc->ac.index].transport);
232 afs_end_cursor(&vc->ac);
236 /* That's all the servers poked to no good effect. Try again if some
239 if (vc->flags & AFS_VL_CURSOR_RETRY)
240 goto restart_from_beginning;
242 e.error = -EDESTADDRREQ;
244 for (i = 0; i < vc->server_list->nr_servers; i++) {
245 struct afs_vlserver *s = vc->server_list->servers[i].server;
247 afs_prioritise_error(&e, READ_ONCE(s->probe.error),
248 s->probe.abort_code);
254 vc->flags |= AFS_VL_CURSOR_STOP;
255 afs_end_cursor(&vc->ac);
256 _leave(" = f [failed %d]", vc->error);
261 * Dump cursor state in the case of the error being EDESTADDRREQ.
263 static void afs_vl_dump_edestaddrreq(const struct afs_vl_cursor *vc)
268 if (!IS_ENABLED(CONFIG_AFS_DEBUG_CURSOR) || count > 3)
273 pr_notice("EDESTADDR occurred\n");
274 pr_notice("VC: ut=%lx ix=%u ni=%hu fl=%hx err=%hd\n",
275 vc->untried, vc->index, vc->nr_iterations, vc->flags, vc->error);
277 if (vc->server_list) {
278 const struct afs_vlserver_list *sl = vc->server_list;
279 pr_notice("VC: SL nr=%u ix=%u\n",
280 sl->nr_servers, sl->index);
281 for (i = 0; i < sl->nr_servers; i++) {
282 const struct afs_vlserver *s = sl->servers[i].server;
283 pr_notice("VC: server %s+%hu fl=%lx E=%hd\n",
284 s->name, s->port, s->flags, s->probe.error);
286 const struct afs_addr_list *a =
287 rcu_dereference(s->addresses);
288 pr_notice("VC: - nr=%u/%u/%u pf=%u\n",
289 a->nr_ipv4, a->nr_addrs, a->max_addrs,
291 pr_notice("VC: - pr=%lx R=%lx F=%lx\n",
292 a->probed, a->responded, a->failed);
293 if (a == vc->ac.alist)
294 pr_notice("VC: - current\n");
299 pr_notice("AC: t=%lx ax=%u ac=%d er=%d r=%u ni=%u\n",
300 vc->ac.tried, vc->ac.index, vc->ac.abort_code, vc->ac.error,
301 vc->ac.responded, vc->ac.nr_iterations);
306 * Tidy up a volume location server cursor and unlock the vnode.
308 int afs_end_vlserver_operation(struct afs_vl_cursor *vc)
310 struct afs_net *net = vc->cell->net;
312 if (vc->error == -EDESTADDRREQ ||
313 vc->error == -EADDRNOTAVAIL ||
314 vc->error == -ENETUNREACH ||
315 vc->error == -EHOSTUNREACH)
316 afs_vl_dump_edestaddrreq(vc);
318 afs_end_cursor(&vc->ac);
319 afs_put_vlserverlist(net, vc->server_list);
321 if (vc->error == -ECONNABORTED)
322 vc->error = afs_abort_to_error(vc->ac.abort_code);