Merge tag 'block-5.9-2020-08-14' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / fs / afs / vl_probe.c
blobe3aa013c21779241e399014b504a3978e5fe7c35
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS vlserver probing
4 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
8 #include <linux/sched.h>
9 #include <linux/slab.h>
10 #include "afs_fs.h"
11 #include "internal.h"
12 #include "protocol_yfs.h"
14 static bool afs_vl_probe_done(struct afs_vlserver *server)
16 if (!atomic_dec_and_test(&server->probe_outstanding))
17 return false;
19 wake_up_var(&server->probe_outstanding);
20 clear_bit_unlock(AFS_VLSERVER_FL_PROBING, &server->flags);
21 wake_up_bit(&server->flags, AFS_VLSERVER_FL_PROBING);
22 return true;
26 * Process the result of probing a vlserver. This is called after successful
27 * or failed delivery of an VL.GetCapabilities operation.
29 void afs_vlserver_probe_result(struct afs_call *call)
31 struct afs_addr_list *alist = call->alist;
32 struct afs_vlserver *server = call->vlserver;
33 unsigned int server_index = call->server_index;
34 unsigned int rtt_us = 0;
35 unsigned int index = call->addr_ix;
36 bool have_result = false;
37 int ret = call->error;
39 _enter("%s,%u,%u,%d,%d", server->name, server_index, index, ret, call->abort_code);
41 spin_lock(&server->probe_lock);
43 switch (ret) {
44 case 0:
45 server->probe.error = 0;
46 goto responded;
47 case -ECONNABORTED:
48 if (!server->probe.responded) {
49 server->probe.abort_code = call->abort_code;
50 server->probe.error = ret;
52 goto responded;
53 case -ENOMEM:
54 case -ENONET:
55 server->probe.local_failure = true;
56 afs_io_error(call, afs_io_error_vl_probe_fail);
57 goto out;
58 case -ECONNRESET: /* Responded, but call expired. */
59 case -ERFKILL:
60 case -EADDRNOTAVAIL:
61 case -ENETUNREACH:
62 case -EHOSTUNREACH:
63 case -EHOSTDOWN:
64 case -ECONNREFUSED:
65 case -ETIMEDOUT:
66 case -ETIME:
67 default:
68 clear_bit(index, &alist->responded);
69 set_bit(index, &alist->failed);
70 if (!server->probe.responded &&
71 (server->probe.error == 0 ||
72 server->probe.error == -ETIMEDOUT ||
73 server->probe.error == -ETIME))
74 server->probe.error = ret;
75 afs_io_error(call, afs_io_error_vl_probe_fail);
76 goto out;
79 responded:
80 set_bit(index, &alist->responded);
81 clear_bit(index, &alist->failed);
83 if (call->service_id == YFS_VL_SERVICE) {
84 server->probe.is_yfs = true;
85 set_bit(AFS_VLSERVER_FL_IS_YFS, &server->flags);
86 alist->addrs[index].srx_service = call->service_id;
87 } else {
88 server->probe.not_yfs = true;
89 if (!server->probe.is_yfs) {
90 clear_bit(AFS_VLSERVER_FL_IS_YFS, &server->flags);
91 alist->addrs[index].srx_service = call->service_id;
95 rtt_us = rxrpc_kernel_get_srtt(call->net->socket, call->rxcall);
96 if (rtt_us < server->probe.rtt) {
97 server->probe.rtt = rtt_us;
98 alist->preferred = index;
99 have_result = true;
102 smp_wmb(); /* Set rtt before responded. */
103 server->probe.responded = true;
104 set_bit(AFS_VLSERVER_FL_PROBED, &server->flags);
105 out:
106 spin_unlock(&server->probe_lock);
108 _debug("probe [%u][%u] %pISpc rtt=%u ret=%d",
109 server_index, index, &alist->addrs[index].transport, rtt_us, ret);
111 have_result |= afs_vl_probe_done(server);
112 if (have_result) {
113 server->probe.have_result = true;
114 wake_up_var(&server->probe.have_result);
115 wake_up_all(&server->probe_wq);
120 * Probe all of a vlserver's addresses to find out the best route and to
121 * query its capabilities.
123 static bool afs_do_probe_vlserver(struct afs_net *net,
124 struct afs_vlserver *server,
125 struct key *key,
126 unsigned int server_index,
127 struct afs_error *_e)
129 struct afs_addr_cursor ac = {
130 .index = 0,
132 struct afs_call *call;
133 bool in_progress = false;
135 _enter("%s", server->name);
137 read_lock(&server->lock);
138 ac.alist = rcu_dereference_protected(server->addresses,
139 lockdep_is_held(&server->lock));
140 read_unlock(&server->lock);
142 atomic_set(&server->probe_outstanding, ac.alist->nr_addrs);
143 memset(&server->probe, 0, sizeof(server->probe));
144 server->probe.rtt = UINT_MAX;
146 for (ac.index = 0; ac.index < ac.alist->nr_addrs; ac.index++) {
147 call = afs_vl_get_capabilities(net, &ac, key, server,
148 server_index);
149 if (!IS_ERR(call)) {
150 afs_put_call(call);
151 in_progress = true;
152 } else {
153 afs_prioritise_error(_e, PTR_ERR(call), ac.abort_code);
157 if (!in_progress)
158 afs_vl_probe_done(server);
159 return in_progress;
163 * Send off probes to all unprobed servers.
165 int afs_send_vl_probes(struct afs_net *net, struct key *key,
166 struct afs_vlserver_list *vllist)
168 struct afs_vlserver *server;
169 struct afs_error e;
170 bool in_progress = false;
171 int i;
173 e.error = 0;
174 e.responded = false;
175 for (i = 0; i < vllist->nr_servers; i++) {
176 server = vllist->servers[i].server;
177 if (test_bit(AFS_VLSERVER_FL_PROBED, &server->flags))
178 continue;
180 if (!test_and_set_bit_lock(AFS_VLSERVER_FL_PROBING, &server->flags) &&
181 afs_do_probe_vlserver(net, server, key, i, &e))
182 in_progress = true;
185 return in_progress ? 0 : e.error;
189 * Wait for the first as-yet untried server to respond.
191 int afs_wait_for_vl_probes(struct afs_vlserver_list *vllist,
192 unsigned long untried)
194 struct wait_queue_entry *waits;
195 struct afs_vlserver *server;
196 unsigned int rtt = UINT_MAX;
197 bool have_responders = false;
198 int pref = -1, i;
200 _enter("%u,%lx", vllist->nr_servers, untried);
202 /* Only wait for servers that have a probe outstanding. */
203 for (i = 0; i < vllist->nr_servers; i++) {
204 if (test_bit(i, &untried)) {
205 server = vllist->servers[i].server;
206 if (!test_bit(AFS_VLSERVER_FL_PROBING, &server->flags))
207 __clear_bit(i, &untried);
208 if (server->probe.responded)
209 have_responders = true;
212 if (have_responders || !untried)
213 return 0;
215 waits = kmalloc(array_size(vllist->nr_servers, sizeof(*waits)), GFP_KERNEL);
216 if (!waits)
217 return -ENOMEM;
219 for (i = 0; i < vllist->nr_servers; i++) {
220 if (test_bit(i, &untried)) {
221 server = vllist->servers[i].server;
222 init_waitqueue_entry(&waits[i], current);
223 add_wait_queue(&server->probe_wq, &waits[i]);
227 for (;;) {
228 bool still_probing = false;
230 set_current_state(TASK_INTERRUPTIBLE);
231 for (i = 0; i < vllist->nr_servers; i++) {
232 if (test_bit(i, &untried)) {
233 server = vllist->servers[i].server;
234 if (server->probe.responded)
235 goto stop;
236 if (test_bit(AFS_VLSERVER_FL_PROBING, &server->flags))
237 still_probing = true;
241 if (!still_probing || signal_pending(current))
242 goto stop;
243 schedule();
246 stop:
247 set_current_state(TASK_RUNNING);
249 for (i = 0; i < vllist->nr_servers; i++) {
250 if (test_bit(i, &untried)) {
251 server = vllist->servers[i].server;
252 if (server->probe.responded &&
253 server->probe.rtt < rtt) {
254 pref = i;
255 rtt = server->probe.rtt;
258 remove_wait_queue(&server->probe_wq, &waits[i]);
262 kfree(waits);
264 if (pref == -1 && signal_pending(current))
265 return -ERESTARTSYS;
267 if (pref >= 0)
268 vllist->preferred = pref;
270 _leave(" = 0 [%u]", pref);
271 return 0;