quorum.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 <linux/reboot.h>
  49. #include "heartbeat.h"
  50. #include "nodemanager.h"
  51. #define MLOG_MASK_PREFIX ML_QUORUM
  52. #include "masklog.h"
  53. #include "quorum.h"
  54. static struct o2quo_state {
  55. spinlock_t qs_lock;
  56. struct work_struct qs_work;
  57. int qs_pending;
  58. int qs_heartbeating;
  59. unsigned long qs_hb_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
  60. int qs_connected;
  61. unsigned long qs_conn_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
  62. int qs_holds;
  63. unsigned long qs_hold_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
  64. } o2quo_state;
  65. /* this is horribly heavy-handed. It should instead flip the file
  66. * system RO and call some userspace script. */
  67. static void o2quo_fence_self(void)
  68. {
  69. /* panic spins with interrupts enabled. with preempt
  70. * threads can still schedule, etc, etc */
  71. o2hb_stop_all_regions();
  72. printk("ocfs2 is very sorry to be fencing this system by restarting\n");
  73. emergency_restart();
  74. }
  75. /* Indicate that a timeout occured on a hearbeat region write. The
  76. * other nodes in the cluster may consider us dead at that time so we
  77. * want to "fence" ourselves so that we don't scribble on the disk
  78. * after they think they've recovered us. This can't solve all
  79. * problems related to writeout after recovery but this hack can at
  80. * least close some of those gaps. When we have real fencing, this can
  81. * go away as our node would be fenced externally before other nodes
  82. * begin recovery. */
  83. void o2quo_disk_timeout(void)
  84. {
  85. o2quo_fence_self();
  86. }
  87. static void o2quo_make_decision(struct work_struct *work)
  88. {
  89. int quorum;
  90. int lowest_hb, lowest_reachable = 0, fence = 0;
  91. struct o2quo_state *qs = &o2quo_state;
  92. spin_lock(&qs->qs_lock);
  93. lowest_hb = find_first_bit(qs->qs_hb_bm, O2NM_MAX_NODES);
  94. if (lowest_hb != O2NM_MAX_NODES)
  95. lowest_reachable = test_bit(lowest_hb, qs->qs_conn_bm);
  96. mlog(0, "heartbeating: %d, connected: %d, "
  97. "lowest: %d (%sreachable)\n", qs->qs_heartbeating,
  98. qs->qs_connected, lowest_hb, lowest_reachable ? "" : "un");
  99. if (!test_bit(o2nm_this_node(), qs->qs_hb_bm) ||
  100. qs->qs_heartbeating == 1)
  101. goto out;
  102. if (qs->qs_heartbeating & 1) {
  103. /* the odd numbered cluster case is straight forward --
  104. * if we can't talk to the majority we're hosed */
  105. quorum = (qs->qs_heartbeating + 1)/2;
  106. if (qs->qs_connected < quorum) {
  107. mlog(ML_ERROR, "fencing this node because it is "
  108. "only connected to %u nodes and %u is needed "
  109. "to make a quorum out of %u heartbeating nodes\n",
  110. qs->qs_connected, quorum,
  111. qs->qs_heartbeating);
  112. fence = 1;
  113. }
  114. } else {
  115. /* the even numbered cluster adds the possibility of each half
  116. * of the cluster being able to talk amongst themselves.. in
  117. * that case we're hosed if we can't talk to the group that has
  118. * the lowest numbered node */
  119. quorum = qs->qs_heartbeating / 2;
  120. if (qs->qs_connected < quorum) {
  121. mlog(ML_ERROR, "fencing this node because it is "
  122. "only connected to %u nodes and %u is needed "
  123. "to make a quorum out of %u heartbeating nodes\n",
  124. qs->qs_connected, quorum,
  125. qs->qs_heartbeating);
  126. fence = 1;
  127. }
  128. else if ((qs->qs_connected == quorum) &&
  129. !lowest_reachable) {
  130. mlog(ML_ERROR, "fencing this node because it is "
  131. "connected to a half-quorum of %u out of %u "
  132. "nodes which doesn't include the lowest active "
  133. "node %u\n", quorum, qs->qs_heartbeating,
  134. lowest_hb);
  135. fence = 1;
  136. }
  137. }
  138. out:
  139. spin_unlock(&qs->qs_lock);
  140. if (fence)
  141. o2quo_fence_self();
  142. }
  143. static void o2quo_set_hold(struct o2quo_state *qs, u8 node)
  144. {
  145. assert_spin_locked(&qs->qs_lock);
  146. if (!test_and_set_bit(node, qs->qs_hold_bm)) {
  147. qs->qs_holds++;
  148. mlog_bug_on_msg(qs->qs_holds == O2NM_MAX_NODES,
  149. "node %u\n", node);
  150. mlog(0, "node %u, %d total\n", node, qs->qs_holds);
  151. }
  152. }
  153. static void o2quo_clear_hold(struct o2quo_state *qs, u8 node)
  154. {
  155. assert_spin_locked(&qs->qs_lock);
  156. if (test_and_clear_bit(node, qs->qs_hold_bm)) {
  157. mlog(0, "node %u, %d total\n", node, qs->qs_holds - 1);
  158. if (--qs->qs_holds == 0) {
  159. if (qs->qs_pending) {
  160. qs->qs_pending = 0;
  161. schedule_work(&qs->qs_work);
  162. }
  163. }
  164. mlog_bug_on_msg(qs->qs_holds < 0, "node %u, holds %d\n",
  165. node, qs->qs_holds);
  166. }
  167. }
  168. /* as a node comes up we delay the quorum decision until we know the fate of
  169. * the connection. the hold will be droped in conn_up or hb_down. it might be
  170. * perpetuated by con_err until hb_down. if we already have a conn, we might
  171. * be dropping a hold that conn_up got. */
  172. void o2quo_hb_up(u8 node)
  173. {
  174. struct o2quo_state *qs = &o2quo_state;
  175. spin_lock(&qs->qs_lock);
  176. qs->qs_heartbeating++;
  177. mlog_bug_on_msg(qs->qs_heartbeating == O2NM_MAX_NODES,
  178. "node %u\n", node);
  179. mlog_bug_on_msg(test_bit(node, qs->qs_hb_bm), "node %u\n", node);
  180. set_bit(node, qs->qs_hb_bm);
  181. mlog(0, "node %u, %d total\n", node, qs->qs_heartbeating);
  182. if (!test_bit(node, qs->qs_conn_bm))
  183. o2quo_set_hold(qs, node);
  184. else
  185. o2quo_clear_hold(qs, node);
  186. spin_unlock(&qs->qs_lock);
  187. }
  188. /* hb going down releases any holds we might have had due to this node from
  189. * conn_up, conn_err, or hb_up */
  190. void o2quo_hb_down(u8 node)
  191. {
  192. struct o2quo_state *qs = &o2quo_state;
  193. spin_lock(&qs->qs_lock);
  194. qs->qs_heartbeating--;
  195. mlog_bug_on_msg(qs->qs_heartbeating < 0,
  196. "node %u, %d heartbeating\n",
  197. node, qs->qs_heartbeating);
  198. mlog_bug_on_msg(!test_bit(node, qs->qs_hb_bm), "node %u\n", node);
  199. clear_bit(node, qs->qs_hb_bm);
  200. mlog(0, "node %u, %d total\n", node, qs->qs_heartbeating);
  201. o2quo_clear_hold(qs, node);
  202. spin_unlock(&qs->qs_lock);
  203. }
  204. /* this tells us that we've decided that the node is still heartbeating
  205. * even though we've lost it's conn. it must only be called after conn_err
  206. * and indicates that we must now make a quorum decision in the future,
  207. * though we might be doing so after waiting for holds to drain. Here
  208. * we'll be dropping the hold from conn_err. */
  209. void o2quo_hb_still_up(u8 node)
  210. {
  211. struct o2quo_state *qs = &o2quo_state;
  212. spin_lock(&qs->qs_lock);
  213. mlog(0, "node %u\n", node);
  214. qs->qs_pending = 1;
  215. o2quo_clear_hold(qs, node);
  216. spin_unlock(&qs->qs_lock);
  217. }
  218. /* This is analagous to hb_up. as a node's connection comes up we delay the
  219. * quorum decision until we see it heartbeating. the hold will be droped in
  220. * hb_up or hb_down. it might be perpetuated by con_err until hb_down. if
  221. * it's already heartbeating we we might be dropping a hold that conn_up got.
  222. * */
  223. void o2quo_conn_up(u8 node)
  224. {
  225. struct o2quo_state *qs = &o2quo_state;
  226. spin_lock(&qs->qs_lock);
  227. qs->qs_connected++;
  228. mlog_bug_on_msg(qs->qs_connected == O2NM_MAX_NODES,
  229. "node %u\n", node);
  230. mlog_bug_on_msg(test_bit(node, qs->qs_conn_bm), "node %u\n", node);
  231. set_bit(node, qs->qs_conn_bm);
  232. mlog(0, "node %u, %d total\n", node, qs->qs_connected);
  233. if (!test_bit(node, qs->qs_hb_bm))
  234. o2quo_set_hold(qs, node);
  235. else
  236. o2quo_clear_hold(qs, node);
  237. spin_unlock(&qs->qs_lock);
  238. }
  239. /* we've decided that we won't ever be connecting to the node again. if it's
  240. * still heartbeating we grab a hold that will delay decisions until either the
  241. * node stops heartbeating from hb_down or the caller decides that the node is
  242. * still up and calls still_up */
  243. void o2quo_conn_err(u8 node)
  244. {
  245. struct o2quo_state *qs = &o2quo_state;
  246. spin_lock(&qs->qs_lock);
  247. if (test_bit(node, qs->qs_conn_bm)) {
  248. qs->qs_connected--;
  249. mlog_bug_on_msg(qs->qs_connected < 0,
  250. "node %u, connected %d\n",
  251. node, qs->qs_connected);
  252. clear_bit(node, qs->qs_conn_bm);
  253. }
  254. mlog(0, "node %u, %d total\n", node, qs->qs_connected);
  255. if (test_bit(node, qs->qs_hb_bm))
  256. o2quo_set_hold(qs, node);
  257. spin_unlock(&qs->qs_lock);
  258. }
  259. void o2quo_init(void)
  260. {
  261. struct o2quo_state *qs = &o2quo_state;
  262. spin_lock_init(&qs->qs_lock);
  263. INIT_WORK(&qs->qs_work, o2quo_make_decision);
  264. }
  265. void o2quo_exit(void)
  266. {
  267. flush_scheduled_work();
  268. }