vl_rotate.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Handle vlserver selection and rotation.
  3. *
  4. * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/signal.h>
  10. #include "internal.h"
  11. #include "afs_vl.h"
  12. /*
  13. * Begin an operation on a volume location server.
  14. */
  15. bool afs_begin_vlserver_operation(struct afs_vl_cursor *vc, struct afs_cell *cell,
  16. struct key *key)
  17. {
  18. memset(vc, 0, sizeof(*vc));
  19. vc->cell = cell;
  20. vc->key = key;
  21. vc->error = -EDESTADDRREQ;
  22. vc->ac.error = SHRT_MAX;
  23. if (signal_pending(current)) {
  24. vc->error = -EINTR;
  25. vc->flags |= AFS_VL_CURSOR_STOP;
  26. return false;
  27. }
  28. return true;
  29. }
  30. /*
  31. * Begin iteration through a server list, starting with the last used server if
  32. * possible, or the last recorded good server if not.
  33. */
  34. static bool afs_start_vl_iteration(struct afs_vl_cursor *vc)
  35. {
  36. struct afs_cell *cell = vc->cell;
  37. unsigned int dns_lookup_count;
  38. if (cell->dns_source == DNS_RECORD_UNAVAILABLE ||
  39. cell->dns_expiry <= ktime_get_real_seconds()) {
  40. dns_lookup_count = smp_load_acquire(&cell->dns_lookup_count);
  41. set_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags);
  42. afs_queue_cell(cell, afs_cell_trace_get_queue_dns);
  43. if (cell->dns_source == DNS_RECORD_UNAVAILABLE) {
  44. if (wait_var_event_interruptible(
  45. &cell->dns_lookup_count,
  46. smp_load_acquire(&cell->dns_lookup_count)
  47. != dns_lookup_count) < 0) {
  48. vc->error = -ERESTARTSYS;
  49. return false;
  50. }
  51. }
  52. /* Status load is ordered after lookup counter load */
  53. if (cell->dns_source == DNS_RECORD_UNAVAILABLE) {
  54. vc->error = -EDESTADDRREQ;
  55. return false;
  56. }
  57. }
  58. read_lock(&cell->vl_servers_lock);
  59. vc->server_list = afs_get_vlserverlist(
  60. rcu_dereference_protected(cell->vl_servers,
  61. lockdep_is_held(&cell->vl_servers_lock)));
  62. read_unlock(&cell->vl_servers_lock);
  63. if (!vc->server_list->nr_servers)
  64. return false;
  65. vc->untried = (1UL << vc->server_list->nr_servers) - 1;
  66. vc->index = -1;
  67. return true;
  68. }
  69. /*
  70. * Select the vlserver to use. May be called multiple times to rotate
  71. * through the vlservers.
  72. */
  73. bool afs_select_vlserver(struct afs_vl_cursor *vc)
  74. {
  75. struct afs_addr_list *alist;
  76. struct afs_vlserver *vlserver;
  77. struct afs_error e;
  78. u32 rtt;
  79. int error = vc->ac.error, i;
  80. _enter("%lx[%d],%lx[%d],%d,%d",
  81. vc->untried, vc->index,
  82. vc->ac.tried, vc->ac.index,
  83. error, vc->ac.abort_code);
  84. if (vc->flags & AFS_VL_CURSOR_STOP) {
  85. _leave(" = f [stopped]");
  86. return false;
  87. }
  88. vc->nr_iterations++;
  89. /* Evaluate the result of the previous operation, if there was one. */
  90. switch (error) {
  91. case SHRT_MAX:
  92. goto start;
  93. default:
  94. case 0:
  95. /* Success or local failure. Stop. */
  96. vc->error = error;
  97. vc->flags |= AFS_VL_CURSOR_STOP;
  98. _leave(" = f [okay/local %d]", vc->ac.error);
  99. return false;
  100. case -ECONNABORTED:
  101. /* The far side rejected the operation on some grounds. This
  102. * might involve the server being busy or the volume having been moved.
  103. */
  104. switch (vc->ac.abort_code) {
  105. case AFSVL_IO:
  106. case AFSVL_BADVOLOPER:
  107. case AFSVL_NOMEM:
  108. /* The server went weird. */
  109. vc->error = -EREMOTEIO;
  110. //write_lock(&vc->cell->vl_servers_lock);
  111. //vc->server_list->weird_mask |= 1 << vc->index;
  112. //write_unlock(&vc->cell->vl_servers_lock);
  113. goto next_server;
  114. default:
  115. vc->error = afs_abort_to_error(vc->ac.abort_code);
  116. goto failed;
  117. }
  118. case -ERFKILL:
  119. case -EADDRNOTAVAIL:
  120. case -ENETUNREACH:
  121. case -EHOSTUNREACH:
  122. case -EHOSTDOWN:
  123. case -ECONNREFUSED:
  124. case -ETIMEDOUT:
  125. case -ETIME:
  126. _debug("no conn %d", error);
  127. vc->error = error;
  128. goto iterate_address;
  129. case -ECONNRESET:
  130. _debug("call reset");
  131. vc->error = error;
  132. vc->flags |= AFS_VL_CURSOR_RETRY;
  133. goto next_server;
  134. case -EOPNOTSUPP:
  135. _debug("notsupp");
  136. goto next_server;
  137. }
  138. restart_from_beginning:
  139. _debug("restart");
  140. afs_end_cursor(&vc->ac);
  141. afs_put_vlserverlist(vc->cell->net, vc->server_list);
  142. vc->server_list = NULL;
  143. if (vc->flags & AFS_VL_CURSOR_RETRIED)
  144. goto failed;
  145. vc->flags |= AFS_VL_CURSOR_RETRIED;
  146. start:
  147. _debug("start");
  148. if (!afs_start_vl_iteration(vc))
  149. goto failed;
  150. error = afs_send_vl_probes(vc->cell->net, vc->key, vc->server_list);
  151. if (error < 0)
  152. goto failed_set_error;
  153. pick_server:
  154. _debug("pick [%lx]", vc->untried);
  155. error = afs_wait_for_vl_probes(vc->server_list, vc->untried);
  156. if (error < 0)
  157. goto failed_set_error;
  158. /* Pick the untried server with the lowest RTT. */
  159. vc->index = vc->server_list->preferred;
  160. if (test_bit(vc->index, &vc->untried))
  161. goto selected_server;
  162. vc->index = -1;
  163. rtt = U32_MAX;
  164. for (i = 0; i < vc->server_list->nr_servers; i++) {
  165. struct afs_vlserver *s = vc->server_list->servers[i].server;
  166. if (!test_bit(i, &vc->untried) ||
  167. !test_bit(AFS_VLSERVER_FL_RESPONDING, &s->flags))
  168. continue;
  169. if (s->probe.rtt < rtt) {
  170. vc->index = i;
  171. rtt = s->probe.rtt;
  172. }
  173. }
  174. if (vc->index == -1)
  175. goto no_more_servers;
  176. selected_server:
  177. _debug("use %d", vc->index);
  178. __clear_bit(vc->index, &vc->untried);
  179. /* We're starting on a different vlserver from the list. We need to
  180. * check it, find its address list and probe its capabilities before we
  181. * use it.
  182. */
  183. ASSERTCMP(vc->ac.alist, ==, NULL);
  184. vlserver = vc->server_list->servers[vc->index].server;
  185. vc->server = vlserver;
  186. _debug("USING VLSERVER: %s", vlserver->name);
  187. read_lock(&vlserver->lock);
  188. alist = rcu_dereference_protected(vlserver->addresses,
  189. lockdep_is_held(&vlserver->lock));
  190. afs_get_addrlist(alist);
  191. read_unlock(&vlserver->lock);
  192. memset(&vc->ac, 0, sizeof(vc->ac));
  193. if (!vc->ac.alist)
  194. vc->ac.alist = alist;
  195. else
  196. afs_put_addrlist(alist);
  197. vc->ac.index = -1;
  198. iterate_address:
  199. ASSERT(vc->ac.alist);
  200. /* Iterate over the current server's address list to try and find an
  201. * address on which it will respond to us.
  202. */
  203. if (!afs_iterate_addresses(&vc->ac))
  204. goto next_server;
  205. _debug("VL address %d/%d", vc->ac.index, vc->ac.alist->nr_addrs);
  206. _leave(" = t %pISpc", &vc->ac.alist->addrs[vc->ac.index].transport);
  207. return true;
  208. next_server:
  209. _debug("next");
  210. afs_end_cursor(&vc->ac);
  211. goto pick_server;
  212. no_more_servers:
  213. /* That's all the servers poked to no good effect. Try again if some
  214. * of them were busy.
  215. */
  216. if (vc->flags & AFS_VL_CURSOR_RETRY)
  217. goto restart_from_beginning;
  218. e.error = -EDESTADDRREQ;
  219. e.responded = false;
  220. for (i = 0; i < vc->server_list->nr_servers; i++) {
  221. struct afs_vlserver *s = vc->server_list->servers[i].server;
  222. if (test_bit(AFS_VLSERVER_FL_RESPONDING, &s->flags))
  223. e.responded = true;
  224. afs_prioritise_error(&e, READ_ONCE(s->probe.error),
  225. s->probe.abort_code);
  226. }
  227. error = e.error;
  228. failed_set_error:
  229. vc->error = error;
  230. failed:
  231. vc->flags |= AFS_VL_CURSOR_STOP;
  232. afs_end_cursor(&vc->ac);
  233. _leave(" = f [failed %d]", vc->error);
  234. return false;
  235. }
  236. /*
  237. * Dump cursor state in the case of the error being EDESTADDRREQ.
  238. */
  239. static void afs_vl_dump_edestaddrreq(const struct afs_vl_cursor *vc)
  240. {
  241. static int count;
  242. int i;
  243. if (!IS_ENABLED(CONFIG_AFS_DEBUG_CURSOR) || count > 3)
  244. return;
  245. count++;
  246. rcu_read_lock();
  247. pr_notice("EDESTADDR occurred\n");
  248. pr_notice("VC: ut=%lx ix=%u ni=%hu fl=%hx err=%hd\n",
  249. vc->untried, vc->index, vc->nr_iterations, vc->flags, vc->error);
  250. if (vc->server_list) {
  251. const struct afs_vlserver_list *sl = vc->server_list;
  252. pr_notice("VC: SL nr=%u ix=%u\n",
  253. sl->nr_servers, sl->index);
  254. for (i = 0; i < sl->nr_servers; i++) {
  255. const struct afs_vlserver *s = sl->servers[i].server;
  256. pr_notice("VC: server %s+%hu fl=%lx E=%hd\n",
  257. s->name, s->port, s->flags, s->probe.error);
  258. if (s->addresses) {
  259. const struct afs_addr_list *a =
  260. rcu_dereference(s->addresses);
  261. pr_notice("VC: - nr=%u/%u/%u pf=%u\n",
  262. a->nr_ipv4, a->nr_addrs, a->max_addrs,
  263. a->preferred);
  264. pr_notice("VC: - R=%lx F=%lx\n",
  265. a->responded, a->failed);
  266. if (a == vc->ac.alist)
  267. pr_notice("VC: - current\n");
  268. }
  269. }
  270. }
  271. pr_notice("AC: t=%lx ax=%u ac=%d er=%d r=%u ni=%u\n",
  272. vc->ac.tried, vc->ac.index, vc->ac.abort_code, vc->ac.error,
  273. vc->ac.responded, vc->ac.nr_iterations);
  274. rcu_read_unlock();
  275. }
  276. /*
  277. * Tidy up a volume location server cursor and unlock the vnode.
  278. */
  279. int afs_end_vlserver_operation(struct afs_vl_cursor *vc)
  280. {
  281. struct afs_net *net = vc->cell->net;
  282. if (vc->error == -EDESTADDRREQ ||
  283. vc->error == -EADDRNOTAVAIL ||
  284. vc->error == -ENETUNREACH ||
  285. vc->error == -EHOSTUNREACH)
  286. afs_vl_dump_edestaddrreq(vc);
  287. afs_end_cursor(&vc->ac);
  288. afs_put_vlserverlist(net, vc->server_list);
  289. if (vc->error == -ECONNABORTED)
  290. vc->error = afs_abort_to_error(vc->ac.abort_code);
  291. return vc->error;
  292. }