quorum.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. if (fence) {
  151. spin_unlock(&qs->qs_lock);
  152. o2quo_fence_self();
  153. } else {
  154. mlog(ML_NOTICE, "not fencing this node, heartbeating: %d, "
  155. "connected: %d, lowest: %d (%sreachable)\n",
  156. qs->qs_heartbeating, qs->qs_connected, lowest_hb,
  157. lowest_reachable ? "" : "un");
  158. spin_unlock(&qs->qs_lock);
  159. }
  160. }
  161. static void o2quo_set_hold(struct o2quo_state *qs, u8 node)
  162. {
  163. assert_spin_locked(&qs->qs_lock);
  164. if (!test_and_set_bit(node, qs->qs_hold_bm)) {
  165. qs->qs_holds++;
  166. mlog_bug_on_msg(qs->qs_holds == O2NM_MAX_NODES,
  167. "node %u\n", node);
  168. mlog(0, "node %u, %d total\n", node, qs->qs_holds);
  169. }
  170. }
  171. static void o2quo_clear_hold(struct o2quo_state *qs, u8 node)
  172. {
  173. assert_spin_locked(&qs->qs_lock);
  174. if (test_and_clear_bit(node, qs->qs_hold_bm)) {
  175. mlog(0, "node %u, %d total\n", node, qs->qs_holds - 1);
  176. if (--qs->qs_holds == 0) {
  177. if (qs->qs_pending) {
  178. qs->qs_pending = 0;
  179. schedule_work(&qs->qs_work);
  180. }
  181. }
  182. mlog_bug_on_msg(qs->qs_holds < 0, "node %u, holds %d\n",
  183. node, qs->qs_holds);
  184. }
  185. }
  186. /* as a node comes up we delay the quorum decision until we know the fate of
  187. * the connection. the hold will be droped in conn_up or hb_down. it might be
  188. * perpetuated by con_err until hb_down. if we already have a conn, we might
  189. * be dropping a hold that conn_up got. */
  190. void o2quo_hb_up(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 == O2NM_MAX_NODES,
  196. "node %u\n", node);
  197. mlog_bug_on_msg(test_bit(node, qs->qs_hb_bm), "node %u\n", node);
  198. set_bit(node, qs->qs_hb_bm);
  199. mlog(0, "node %u, %d total\n", node, qs->qs_heartbeating);
  200. if (!test_bit(node, qs->qs_conn_bm))
  201. o2quo_set_hold(qs, node);
  202. else
  203. o2quo_clear_hold(qs, node);
  204. spin_unlock(&qs->qs_lock);
  205. }
  206. /* hb going down releases any holds we might have had due to this node from
  207. * conn_up, conn_err, or hb_up */
  208. void o2quo_hb_down(u8 node)
  209. {
  210. struct o2quo_state *qs = &o2quo_state;
  211. spin_lock(&qs->qs_lock);
  212. qs->qs_heartbeating--;
  213. mlog_bug_on_msg(qs->qs_heartbeating < 0,
  214. "node %u, %d heartbeating\n",
  215. node, qs->qs_heartbeating);
  216. mlog_bug_on_msg(!test_bit(node, qs->qs_hb_bm), "node %u\n", node);
  217. clear_bit(node, qs->qs_hb_bm);
  218. mlog(0, "node %u, %d total\n", node, qs->qs_heartbeating);
  219. o2quo_clear_hold(qs, node);
  220. spin_unlock(&qs->qs_lock);
  221. }
  222. /* this tells us that we've decided that the node is still heartbeating
  223. * even though we've lost it's conn. it must only be called after conn_err
  224. * and indicates that we must now make a quorum decision in the future,
  225. * though we might be doing so after waiting for holds to drain. Here
  226. * we'll be dropping the hold from conn_err. */
  227. void o2quo_hb_still_up(u8 node)
  228. {
  229. struct o2quo_state *qs = &o2quo_state;
  230. spin_lock(&qs->qs_lock);
  231. mlog(0, "node %u\n", node);
  232. qs->qs_pending = 1;
  233. o2quo_clear_hold(qs, node);
  234. spin_unlock(&qs->qs_lock);
  235. }
  236. /* This is analogous to hb_up. as a node's connection comes up we delay the
  237. * quorum decision until we see it heartbeating. the hold will be droped in
  238. * hb_up or hb_down. it might be perpetuated by con_err until hb_down. if
  239. * it's already heartbeating we might be dropping a hold that conn_up got.
  240. * */
  241. void o2quo_conn_up(u8 node)
  242. {
  243. struct o2quo_state *qs = &o2quo_state;
  244. spin_lock(&qs->qs_lock);
  245. qs->qs_connected++;
  246. mlog_bug_on_msg(qs->qs_connected == O2NM_MAX_NODES,
  247. "node %u\n", node);
  248. mlog_bug_on_msg(test_bit(node, qs->qs_conn_bm), "node %u\n", node);
  249. set_bit(node, qs->qs_conn_bm);
  250. mlog(0, "node %u, %d total\n", node, qs->qs_connected);
  251. if (!test_bit(node, qs->qs_hb_bm))
  252. o2quo_set_hold(qs, node);
  253. else
  254. o2quo_clear_hold(qs, node);
  255. spin_unlock(&qs->qs_lock);
  256. }
  257. /* we've decided that we won't ever be connecting to the node again. if it's
  258. * still heartbeating we grab a hold that will delay decisions until either the
  259. * node stops heartbeating from hb_down or the caller decides that the node is
  260. * still up and calls still_up */
  261. void o2quo_conn_err(u8 node)
  262. {
  263. struct o2quo_state *qs = &o2quo_state;
  264. spin_lock(&qs->qs_lock);
  265. if (test_bit(node, qs->qs_conn_bm)) {
  266. qs->qs_connected--;
  267. mlog_bug_on_msg(qs->qs_connected < 0,
  268. "node %u, connected %d\n",
  269. node, qs->qs_connected);
  270. clear_bit(node, qs->qs_conn_bm);
  271. }
  272. mlog(0, "node %u, %d total\n", node, qs->qs_connected);
  273. if (test_bit(node, qs->qs_hb_bm))
  274. o2quo_set_hold(qs, node);
  275. spin_unlock(&qs->qs_lock);
  276. }
  277. void o2quo_init(void)
  278. {
  279. struct o2quo_state *qs = &o2quo_state;
  280. spin_lock_init(&qs->qs_lock);
  281. INIT_WORK(&qs->qs_work, o2quo_make_decision);
  282. }
  283. void o2quo_exit(void)
  284. {
  285. struct o2quo_state *qs = &o2quo_state;
  286. flush_work(&qs->qs_work);
  287. }