quorum.c 10 KB

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