quorum.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. *
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * Copyright (C) 2005 Oracle. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with this program; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 021110-1307, USA.
  21. */
  22. /* This quorum hack is only here until we transition to some more rational
  23. * approach that is driven from userspace. Honest. No foolin'.
  24. *
  25. * Imagine two nodes lose network connectivity to each other but they're still
  26. * up and operating in every other way. Presumably a network timeout indicates
  27. * that a node is broken and should be recovered. They can't both recover each
  28. * other and both carry on without serialising their access to the file system.
  29. * They need to decide who is authoritative. Now extend that problem to
  30. * arbitrary groups of nodes losing connectivity between each other.
  31. *
  32. * So we declare that a node which has given up on connecting to a majority
  33. * of nodes who are still heartbeating will fence itself.
  34. *
  35. * There are huge opportunities for races here. After we give up on a node's
  36. * connection we need to wait long enough to give heartbeat an opportunity
  37. * to declare the node as truly dead. We also need to be careful with the
  38. * race between when we see a node start heartbeating and when we connect
  39. * to it.
  40. *
  41. * So nodes that are in this transtion put a hold on the quorum decision
  42. * with a counter. As they fall out of this transition they drop the count
  43. * and if they're the last, they fire off the decision.
  44. */
  45. #include <linux/kernel.h>
  46. #include <linux/slab.h>
  47. #include <linux/workqueue.h>
  48. #include "heartbeat.h"
  49. #include "nodemanager.h"
  50. #define MLOG_MASK_PREFIX ML_QUORUM
  51. #include "masklog.h"
  52. #include "quorum.h"
  53. static struct o2quo_state {
  54. spinlock_t qs_lock;
  55. struct work_struct qs_work;
  56. int qs_pending;
  57. int qs_heartbeating;
  58. unsigned long qs_hb_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
  59. int qs_connected;
  60. unsigned long qs_conn_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
  61. int qs_holds;
  62. unsigned long qs_hold_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
  63. } o2quo_state;
  64. /* this is horribly heavy-handed. It should instead flip the file
  65. * system RO and call some userspace script. */
  66. static void o2quo_fence_self(void)
  67. {
  68. /* panic spins with interrupts enabled. with preempt
  69. * threads can still schedule, etc, etc */
  70. o2hb_stop_all_regions();
  71. panic("ocfs2 is very sorry to be fencing this system by panicing\n");
  72. }
  73. /* Indicate that a timeout occured on a hearbeat region write. The
  74. * other nodes in the cluster may consider us dead at that time so we
  75. * want to "fence" ourselves so that we don't scribble on the disk
  76. * after they think they've recovered us. This can't solve all
  77. * problems related to writeout after recovery but this hack can at
  78. * least close some of those gaps. When we have real fencing, this can
  79. * go away as our node would be fenced externally before other nodes
  80. * begin recovery. */
  81. void o2quo_disk_timeout(void)
  82. {
  83. o2quo_fence_self();
  84. }
  85. static void o2quo_make_decision(void *arg)
  86. {
  87. int quorum;
  88. int lowest_hb, lowest_reachable = 0, fence = 0;
  89. struct o2quo_state *qs = &o2quo_state;
  90. spin_lock(&qs->qs_lock);
  91. lowest_hb = find_first_bit(qs->qs_hb_bm, O2NM_MAX_NODES);
  92. if (lowest_hb != O2NM_MAX_NODES)
  93. lowest_reachable = test_bit(lowest_hb, qs->qs_conn_bm);
  94. mlog(0, "heartbeating: %d, connected: %d, "
  95. "lowest: %d (%sreachable)\n", qs->qs_heartbeating,
  96. qs->qs_connected, lowest_hb, lowest_reachable ? "" : "un");
  97. if (!test_bit(o2nm_this_node(), qs->qs_hb_bm) ||
  98. qs->qs_heartbeating == 1)
  99. goto out;
  100. if (qs->qs_heartbeating & 1) {
  101. /* the odd numbered cluster case is straight forward --
  102. * if we can't talk to the majority we're hosed */
  103. quorum = (qs->qs_heartbeating + 1)/2;
  104. if (qs->qs_connected < quorum) {
  105. mlog(ML_ERROR, "fencing this node because it is "
  106. "only connected to %u nodes and %u is needed "
  107. "to make a quorum out of %u heartbeating nodes\n",
  108. qs->qs_connected, quorum,
  109. qs->qs_heartbeating);
  110. fence = 1;
  111. }
  112. } else {
  113. /* the even numbered cluster adds the possibility of each half
  114. * of the cluster being able to talk amongst themselves.. in
  115. * that case we're hosed if we can't talk to the group that has
  116. * the lowest numbered node */
  117. quorum = qs->qs_heartbeating / 2;
  118. if (qs->qs_connected < quorum) {
  119. mlog(ML_ERROR, "fencing this node because it is "
  120. "only connected to %u nodes and %u is needed "
  121. "to make a quorum out of %u heartbeating nodes\n",
  122. qs->qs_connected, quorum,
  123. qs->qs_heartbeating);
  124. fence = 1;
  125. }
  126. else if ((qs->qs_connected == quorum) &&
  127. !lowest_reachable) {
  128. mlog(ML_ERROR, "fencing this node because it is "
  129. "connected to a half-quorum of %u out of %u "
  130. "nodes which doesn't include the lowest active "
  131. "node %u\n", quorum, qs->qs_heartbeating,
  132. lowest_hb);
  133. fence = 1;
  134. }
  135. }
  136. out:
  137. spin_unlock(&qs->qs_lock);
  138. if (fence)
  139. o2quo_fence_self();
  140. }
  141. static void o2quo_set_hold(struct o2quo_state *qs, u8 node)
  142. {
  143. assert_spin_locked(&qs->qs_lock);
  144. if (!test_and_set_bit(node, qs->qs_hold_bm)) {
  145. qs->qs_holds++;
  146. mlog_bug_on_msg(qs->qs_holds == O2NM_MAX_NODES,
  147. "node %u\n", node);
  148. mlog(0, "node %u, %d total\n", node, qs->qs_holds);
  149. }
  150. }
  151. static void o2quo_clear_hold(struct o2quo_state *qs, u8 node)
  152. {
  153. assert_spin_locked(&qs->qs_lock);
  154. if (test_and_clear_bit(node, qs->qs_hold_bm)) {
  155. mlog(0, "node %u, %d total\n", node, qs->qs_holds - 1);
  156. if (--qs->qs_holds == 0) {
  157. if (qs->qs_pending) {
  158. qs->qs_pending = 0;
  159. schedule_work(&qs->qs_work);
  160. }
  161. }
  162. mlog_bug_on_msg(qs->qs_holds < 0, "node %u, holds %d\n",
  163. node, qs->qs_holds);
  164. }
  165. }
  166. /* as a node comes up we delay the quorum decision until we know the fate of
  167. * the connection. the hold will be droped in conn_up or hb_down. it might be
  168. * perpetuated by con_err until hb_down. if we already have a conn, we might
  169. * be dropping a hold that conn_up got. */
  170. void o2quo_hb_up(u8 node)
  171. {
  172. struct o2quo_state *qs = &o2quo_state;
  173. spin_lock(&qs->qs_lock);
  174. qs->qs_heartbeating++;
  175. mlog_bug_on_msg(qs->qs_heartbeating == O2NM_MAX_NODES,
  176. "node %u\n", node);
  177. mlog_bug_on_msg(test_bit(node, qs->qs_hb_bm), "node %u\n", node);
  178. set_bit(node, qs->qs_hb_bm);
  179. mlog(0, "node %u, %d total\n", node, qs->qs_heartbeating);
  180. if (!test_bit(node, qs->qs_conn_bm))
  181. o2quo_set_hold(qs, node);
  182. else
  183. o2quo_clear_hold(qs, node);
  184. spin_unlock(&qs->qs_lock);
  185. }
  186. /* hb going down releases any holds we might have had due to this node from
  187. * conn_up, conn_err, or hb_up */
  188. void o2quo_hb_down(u8 node)
  189. {
  190. struct o2quo_state *qs = &o2quo_state;
  191. spin_lock(&qs->qs_lock);
  192. qs->qs_heartbeating--;
  193. mlog_bug_on_msg(qs->qs_heartbeating < 0,
  194. "node %u, %d heartbeating\n",
  195. node, qs->qs_heartbeating);
  196. mlog_bug_on_msg(!test_bit(node, qs->qs_hb_bm), "node %u\n", node);
  197. clear_bit(node, qs->qs_hb_bm);
  198. mlog(0, "node %u, %d total\n", node, qs->qs_heartbeating);
  199. o2quo_clear_hold(qs, node);
  200. spin_unlock(&qs->qs_lock);
  201. }
  202. /* this tells us that we've decided that the node is still heartbeating
  203. * even though we've lost it's conn. it must only be called after conn_err
  204. * and indicates that we must now make a quorum decision in the future,
  205. * though we might be doing so after waiting for holds to drain. Here
  206. * we'll be dropping the hold from conn_err. */
  207. void o2quo_hb_still_up(u8 node)
  208. {
  209. struct o2quo_state *qs = &o2quo_state;
  210. spin_lock(&qs->qs_lock);
  211. mlog(0, "node %u\n", node);
  212. qs->qs_pending = 1;
  213. o2quo_clear_hold(qs, node);
  214. spin_unlock(&qs->qs_lock);
  215. }
  216. /* This is analagous to hb_up. as a node's connection comes up we delay the
  217. * quorum decision until we see it heartbeating. the hold will be droped in
  218. * hb_up or hb_down. it might be perpetuated by con_err until hb_down. if
  219. * it's already heartbeating we we might be dropping a hold that conn_up got.
  220. * */
  221. void o2quo_conn_up(u8 node)
  222. {
  223. struct o2quo_state *qs = &o2quo_state;
  224. spin_lock(&qs->qs_lock);
  225. qs->qs_connected++;
  226. mlog_bug_on_msg(qs->qs_connected == O2NM_MAX_NODES,
  227. "node %u\n", node);
  228. mlog_bug_on_msg(test_bit(node, qs->qs_conn_bm), "node %u\n", node);
  229. set_bit(node, qs->qs_conn_bm);
  230. mlog(0, "node %u, %d total\n", node, qs->qs_connected);
  231. if (!test_bit(node, qs->qs_hb_bm))
  232. o2quo_set_hold(qs, node);
  233. else
  234. o2quo_clear_hold(qs, node);
  235. spin_unlock(&qs->qs_lock);
  236. }
  237. /* we've decided that we won't ever be connecting to the node again. if it's
  238. * still heartbeating we grab a hold that will delay decisions until either the
  239. * node stops heartbeating from hb_down or the caller decides that the node is
  240. * still up and calls still_up */
  241. void o2quo_conn_err(u8 node)
  242. {
  243. struct o2quo_state *qs = &o2quo_state;
  244. spin_lock(&qs->qs_lock);
  245. if (test_bit(node, qs->qs_conn_bm)) {
  246. qs->qs_connected--;
  247. mlog_bug_on_msg(qs->qs_connected < 0,
  248. "node %u, connected %d\n",
  249. node, qs->qs_connected);
  250. clear_bit(node, qs->qs_conn_bm);
  251. }
  252. mlog(0, "node %u, %d total\n", node, qs->qs_connected);
  253. if (test_bit(node, qs->qs_hb_bm))
  254. o2quo_set_hold(qs, node);
  255. spin_unlock(&qs->qs_lock);
  256. }
  257. void o2quo_init(void)
  258. {
  259. struct o2quo_state *qs = &o2quo_state;
  260. spin_lock_init(&qs->qs_lock);
  261. INIT_WORK(&qs->qs_work, o2quo_make_decision, NULL);
  262. }
  263. void o2quo_exit(void)
  264. {
  265. flush_scheduled_work();
  266. }