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