vl_rotate.c 8.0 KB

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