Thomas Gleixner | b4d0d23 | 2019-05-20 19:08:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 2 | /* AFS fileserver probing |
| 3 | * |
| 4 | * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include "afs_fs.h" |
| 11 | #include "internal.h" |
| 12 | #include "protocol_yfs.h" |
| 13 | |
| 14 | static bool afs_fs_probe_done(struct afs_server *server) |
| 15 | { |
| 16 | if (!atomic_dec_and_test(&server->probe_outstanding)) |
| 17 | return false; |
| 18 | |
| 19 | wake_up_var(&server->probe_outstanding); |
| 20 | clear_bit_unlock(AFS_SERVER_FL_PROBING, &server->flags); |
| 21 | wake_up_bit(&server->flags, AFS_SERVER_FL_PROBING); |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | /* |
| 26 | * Process the result of probing a fileserver. This is called after successful |
| 27 | * or failed delivery of an FS.GetCapabilities operation. |
| 28 | */ |
| 29 | void afs_fileserver_probe_result(struct afs_call *call) |
| 30 | { |
| 31 | struct afs_addr_list *alist = call->alist; |
David Howells | ffba718 | 2019-05-09 22:22:50 +0100 | [diff] [blame] | 32 | struct afs_server *server = call->server; |
| 33 | unsigned int server_index = call->server_index; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 34 | unsigned int index = call->addr_ix; |
| 35 | unsigned int rtt = UINT_MAX; |
| 36 | bool have_result = false; |
| 37 | u64 _rtt; |
| 38 | int ret = call->error; |
| 39 | |
| 40 | _enter("%pU,%u", &server->uuid, index); |
| 41 | |
| 42 | spin_lock(&server->probe_lock); |
| 43 | |
| 44 | switch (ret) { |
| 45 | case 0: |
| 46 | server->probe.error = 0; |
| 47 | goto responded; |
| 48 | case -ECONNABORTED: |
| 49 | if (!server->probe.responded) { |
| 50 | server->probe.abort_code = call->abort_code; |
| 51 | server->probe.error = ret; |
| 52 | } |
| 53 | goto responded; |
| 54 | case -ENOMEM: |
| 55 | case -ENONET: |
| 56 | server->probe.local_failure = true; |
| 57 | afs_io_error(call, afs_io_error_fs_probe_fail); |
| 58 | goto out; |
| 59 | case -ECONNRESET: /* Responded, but call expired. */ |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 60 | case -ERFKILL: |
| 61 | case -EADDRNOTAVAIL: |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 62 | case -ENETUNREACH: |
| 63 | case -EHOSTUNREACH: |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 64 | case -EHOSTDOWN: |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 65 | case -ECONNREFUSED: |
| 66 | case -ETIMEDOUT: |
| 67 | case -ETIME: |
| 68 | default: |
| 69 | clear_bit(index, &alist->responded); |
| 70 | set_bit(index, &alist->failed); |
| 71 | if (!server->probe.responded && |
| 72 | (server->probe.error == 0 || |
| 73 | server->probe.error == -ETIMEDOUT || |
| 74 | server->probe.error == -ETIME)) |
| 75 | server->probe.error = ret; |
| 76 | afs_io_error(call, afs_io_error_fs_probe_fail); |
| 77 | goto out; |
| 78 | } |
| 79 | |
| 80 | responded: |
| 81 | set_bit(index, &alist->responded); |
| 82 | clear_bit(index, &alist->failed); |
| 83 | |
| 84 | if (call->service_id == YFS_FS_SERVICE) { |
| 85 | server->probe.is_yfs = true; |
| 86 | set_bit(AFS_SERVER_FL_IS_YFS, &server->flags); |
| 87 | alist->addrs[index].srx_service = call->service_id; |
| 88 | } else { |
| 89 | server->probe.not_yfs = true; |
| 90 | if (!server->probe.is_yfs) { |
| 91 | clear_bit(AFS_SERVER_FL_IS_YFS, &server->flags); |
| 92 | alist->addrs[index].srx_service = call->service_id; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* Get the RTT and scale it to fit into a 32-bit value that represents |
| 97 | * over a minute of time so that we can access it with one instruction |
| 98 | * on a 32-bit system. |
| 99 | */ |
| 100 | _rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall); |
| 101 | _rtt /= 64; |
| 102 | rtt = (_rtt > UINT_MAX) ? UINT_MAX : _rtt; |
| 103 | if (rtt < server->probe.rtt) { |
| 104 | server->probe.rtt = rtt; |
| 105 | alist->preferred = index; |
| 106 | have_result = true; |
| 107 | } |
| 108 | |
| 109 | smp_wmb(); /* Set rtt before responded. */ |
| 110 | server->probe.responded = true; |
| 111 | set_bit(AFS_SERVER_FL_PROBED, &server->flags); |
| 112 | out: |
| 113 | spin_unlock(&server->probe_lock); |
| 114 | |
| 115 | _debug("probe [%u][%u] %pISpc rtt=%u ret=%d", |
| 116 | server_index, index, &alist->addrs[index].transport, |
| 117 | (unsigned int)rtt, ret); |
| 118 | |
| 119 | have_result |= afs_fs_probe_done(server); |
| 120 | if (have_result) { |
| 121 | server->probe.have_result = true; |
| 122 | wake_up_var(&server->probe.have_result); |
| 123 | wake_up_all(&server->probe_wq); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Probe all of a fileserver's addresses to find out the best route and to |
| 129 | * query its capabilities. |
| 130 | */ |
| 131 | static int afs_do_probe_fileserver(struct afs_net *net, |
| 132 | struct afs_server *server, |
| 133 | struct key *key, |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 134 | unsigned int server_index, |
| 135 | struct afs_error *_e) |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 136 | { |
| 137 | struct afs_addr_cursor ac = { |
| 138 | .index = 0, |
| 139 | }; |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 140 | struct afs_call *call; |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 141 | bool in_progress = false; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 142 | |
| 143 | _enter("%pU", &server->uuid); |
| 144 | |
| 145 | read_lock(&server->fs_lock); |
| 146 | ac.alist = rcu_dereference_protected(server->addresses, |
| 147 | lockdep_is_held(&server->fs_lock)); |
David Howells | 9efcc4a1 | 2020-03-26 15:24:07 +0000 | [diff] [blame] | 148 | afs_get_addrlist(ac.alist); |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 149 | read_unlock(&server->fs_lock); |
| 150 | |
| 151 | atomic_set(&server->probe_outstanding, ac.alist->nr_addrs); |
| 152 | memset(&server->probe, 0, sizeof(server->probe)); |
| 153 | server->probe.rtt = UINT_MAX; |
| 154 | |
| 155 | for (ac.index = 0; ac.index < ac.alist->nr_addrs; ac.index++) { |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 156 | call = afs_fs_get_capabilities(net, server, &ac, key, server_index); |
| 157 | if (!IS_ERR(call)) { |
| 158 | afs_put_call(call); |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 159 | in_progress = true; |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 160 | } else { |
| 161 | afs_prioritise_error(_e, PTR_ERR(call), ac.abort_code); |
| 162 | } |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 163 | } |
| 164 | |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 165 | if (!in_progress) |
| 166 | afs_fs_probe_done(server); |
David Howells | 9efcc4a1 | 2020-03-26 15:24:07 +0000 | [diff] [blame] | 167 | afs_put_addrlist(ac.alist); |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 168 | return in_progress; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Send off probes to all unprobed servers. |
| 173 | */ |
| 174 | int afs_probe_fileservers(struct afs_net *net, struct key *key, |
| 175 | struct afs_server_list *list) |
| 176 | { |
| 177 | struct afs_server *server; |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 178 | struct afs_error e; |
| 179 | bool in_progress = false; |
| 180 | int i; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 181 | |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 182 | e.error = 0; |
| 183 | e.responded = false; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 184 | for (i = 0; i < list->nr_servers; i++) { |
| 185 | server = list->servers[i].server; |
| 186 | if (test_bit(AFS_SERVER_FL_PROBED, &server->flags)) |
| 187 | continue; |
| 188 | |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 189 | if (!test_and_set_bit_lock(AFS_SERVER_FL_PROBING, &server->flags) && |
| 190 | afs_do_probe_fileserver(net, server, key, i, &e)) |
| 191 | in_progress = true; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 192 | } |
| 193 | |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 194 | return in_progress ? 0 : e.error; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Wait for the first as-yet untried fileserver to respond. |
| 199 | */ |
| 200 | int afs_wait_for_fs_probes(struct afs_server_list *slist, unsigned long untried) |
| 201 | { |
| 202 | struct wait_queue_entry *waits; |
| 203 | struct afs_server *server; |
| 204 | unsigned int rtt = UINT_MAX; |
| 205 | bool have_responders = false; |
| 206 | int pref = -1, i; |
| 207 | |
| 208 | _enter("%u,%lx", slist->nr_servers, untried); |
| 209 | |
| 210 | /* Only wait for servers that have a probe outstanding. */ |
| 211 | for (i = 0; i < slist->nr_servers; i++) { |
| 212 | if (test_bit(i, &untried)) { |
| 213 | server = slist->servers[i].server; |
| 214 | if (!test_bit(AFS_SERVER_FL_PROBING, &server->flags)) |
| 215 | __clear_bit(i, &untried); |
| 216 | if (server->probe.responded) |
| 217 | have_responders = true; |
| 218 | } |
| 219 | } |
| 220 | if (have_responders || !untried) |
| 221 | return 0; |
| 222 | |
| 223 | waits = kmalloc(array_size(slist->nr_servers, sizeof(*waits)), GFP_KERNEL); |
| 224 | if (!waits) |
| 225 | return -ENOMEM; |
| 226 | |
| 227 | for (i = 0; i < slist->nr_servers; i++) { |
| 228 | if (test_bit(i, &untried)) { |
| 229 | server = slist->servers[i].server; |
| 230 | init_waitqueue_entry(&waits[i], current); |
| 231 | add_wait_queue(&server->probe_wq, &waits[i]); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | for (;;) { |
| 236 | bool still_probing = false; |
| 237 | |
| 238 | set_current_state(TASK_INTERRUPTIBLE); |
| 239 | for (i = 0; i < slist->nr_servers; i++) { |
| 240 | if (test_bit(i, &untried)) { |
| 241 | server = slist->servers[i].server; |
| 242 | if (server->probe.responded) |
| 243 | goto stop; |
| 244 | if (test_bit(AFS_SERVER_FL_PROBING, &server->flags)) |
| 245 | still_probing = true; |
| 246 | } |
| 247 | } |
| 248 | |
Davidlohr Bueso | 08d405c | 2019-01-03 15:28:58 -0800 | [diff] [blame] | 249 | if (!still_probing || signal_pending(current)) |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 250 | goto stop; |
| 251 | schedule(); |
| 252 | } |
| 253 | |
| 254 | stop: |
| 255 | set_current_state(TASK_RUNNING); |
| 256 | |
| 257 | for (i = 0; i < slist->nr_servers; i++) { |
| 258 | if (test_bit(i, &untried)) { |
| 259 | server = slist->servers[i].server; |
| 260 | if (server->probe.responded && |
| 261 | server->probe.rtt < rtt) { |
| 262 | pref = i; |
| 263 | rtt = server->probe.rtt; |
| 264 | } |
| 265 | |
| 266 | remove_wait_queue(&server->probe_wq, &waits[i]); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | kfree(waits); |
| 271 | |
| 272 | if (pref == -1 && signal_pending(current)) |
| 273 | return -ERESTARTSYS; |
| 274 | |
| 275 | if (pref >= 0) |
| 276 | slist->preferred = pref; |
| 277 | return 0; |
| 278 | } |