1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2005 Oracle. All rights reserved.
7 /* This quorum hack is only here until we transition to some more rational
8 * approach that is driven from userspace. Honest. No foolin'.
10 * Imagine two nodes lose network connectivity to each other but they're still
11 * up and operating in every other way. Presumably a network timeout indicates
12 * that a node is broken and should be recovered. They can't both recover each
13 * other and both carry on without serialising their access to the file system.
14 * They need to decide who is authoritative. Now extend that problem to
15 * arbitrary groups of nodes losing connectivity between each other.
17 * So we declare that a node which has given up on connecting to a majority
18 * of nodes who are still heartbeating will fence itself.
20 * There are huge opportunities for races here. After we give up on a node's
21 * connection we need to wait long enough to give heartbeat an opportunity
22 * to declare the node as truly dead. We also need to be careful with the
23 * race between when we see a node start heartbeating and when we connect
26 * So nodes that are in this transtion put a hold on the quorum decision
27 * with a counter. As they fall out of this transition they drop the count
28 * and if they're the last, they fire off the decision.
30 #include <linux/kernel.h>
31 #include <linux/workqueue.h>
32 #include <linux/reboot.h>
34 #include "heartbeat.h"
35 #include "nodemanager.h"
36 #define MLOG_MASK_PREFIX ML_QUORUM
40 static struct o2quo_state
{
42 struct work_struct qs_work
;
45 unsigned long qs_hb_bm
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
47 unsigned long qs_conn_bm
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
49 unsigned long qs_hold_bm
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
52 /* this is horribly heavy-handed. It should instead flip the file
53 * system RO and call some userspace script. */
54 static void o2quo_fence_self(void)
56 /* panic spins with interrupts enabled. with preempt
57 * threads can still schedule, etc, etc */
58 o2hb_stop_all_regions();
60 switch (o2nm_single_cluster
->cl_fence_method
) {
61 case O2NM_FENCE_PANIC
:
62 panic("*** ocfs2 is very sorry to be fencing this system by "
66 WARN_ON(o2nm_single_cluster
->cl_fence_method
>=
69 case O2NM_FENCE_RESET
:
70 printk(KERN_ERR
"*** ocfs2 is very sorry to be fencing this "
71 "system by restarting ***\n");
77 /* Indicate that a timeout occurred on a heartbeat region write. The
78 * other nodes in the cluster may consider us dead at that time so we
79 * want to "fence" ourselves so that we don't scribble on the disk
80 * after they think they've recovered us. This can't solve all
81 * problems related to writeout after recovery but this hack can at
82 * least close some of those gaps. When we have real fencing, this can
83 * go away as our node would be fenced externally before other nodes
85 void o2quo_disk_timeout(void)
90 static void o2quo_make_decision(struct work_struct
*work
)
93 int lowest_hb
, lowest_reachable
= 0, fence
= 0;
94 struct o2quo_state
*qs
= &o2quo_state
;
96 spin_lock_bh(&qs
->qs_lock
);
98 lowest_hb
= find_first_bit(qs
->qs_hb_bm
, O2NM_MAX_NODES
);
99 if (lowest_hb
!= O2NM_MAX_NODES
)
100 lowest_reachable
= test_bit(lowest_hb
, qs
->qs_conn_bm
);
102 mlog(0, "heartbeating: %d, connected: %d, "
103 "lowest: %d (%sreachable)\n", qs
->qs_heartbeating
,
104 qs
->qs_connected
, lowest_hb
, lowest_reachable
? "" : "un");
106 if (!test_bit(o2nm_this_node(), qs
->qs_hb_bm
) ||
107 qs
->qs_heartbeating
== 1)
110 if (qs
->qs_heartbeating
& 1) {
111 /* the odd numbered cluster case is straight forward --
112 * if we can't talk to the majority we're hosed */
113 quorum
= (qs
->qs_heartbeating
+ 1)/2;
114 if (qs
->qs_connected
< quorum
) {
115 mlog(ML_ERROR
, "fencing this node because it is "
116 "only connected to %u nodes and %u is needed "
117 "to make a quorum out of %u heartbeating nodes\n",
118 qs
->qs_connected
, quorum
,
119 qs
->qs_heartbeating
);
123 /* the even numbered cluster adds the possibility of each half
124 * of the cluster being able to talk amongst themselves.. in
125 * that case we're hosed if we can't talk to the group that has
126 * the lowest numbered node */
127 quorum
= qs
->qs_heartbeating
/ 2;
128 if (qs
->qs_connected
< quorum
) {
129 mlog(ML_ERROR
, "fencing this node because it is "
130 "only connected to %u nodes and %u is needed "
131 "to make a quorum out of %u heartbeating nodes\n",
132 qs
->qs_connected
, quorum
,
133 qs
->qs_heartbeating
);
136 else if ((qs
->qs_connected
== quorum
) &&
138 mlog(ML_ERROR
, "fencing this node because it is "
139 "connected to a half-quorum of %u out of %u "
140 "nodes which doesn't include the lowest active "
141 "node %u\n", quorum
, qs
->qs_heartbeating
,
149 spin_unlock_bh(&qs
->qs_lock
);
152 mlog(ML_NOTICE
, "not fencing this node, heartbeating: %d, "
153 "connected: %d, lowest: %d (%sreachable)\n",
154 qs
->qs_heartbeating
, qs
->qs_connected
, lowest_hb
,
155 lowest_reachable
? "" : "un");
156 spin_unlock_bh(&qs
->qs_lock
);
162 static void o2quo_set_hold(struct o2quo_state
*qs
, u8 node
)
164 assert_spin_locked(&qs
->qs_lock
);
166 if (!test_and_set_bit(node
, qs
->qs_hold_bm
)) {
168 mlog_bug_on_msg(qs
->qs_holds
== O2NM_MAX_NODES
,
170 mlog(0, "node %u, %d total\n", node
, qs
->qs_holds
);
174 static void o2quo_clear_hold(struct o2quo_state
*qs
, u8 node
)
176 assert_spin_locked(&qs
->qs_lock
);
178 if (test_and_clear_bit(node
, qs
->qs_hold_bm
)) {
179 mlog(0, "node %u, %d total\n", node
, qs
->qs_holds
- 1);
180 if (--qs
->qs_holds
== 0) {
181 if (qs
->qs_pending
) {
183 schedule_work(&qs
->qs_work
);
186 mlog_bug_on_msg(qs
->qs_holds
< 0, "node %u, holds %d\n",
191 /* as a node comes up we delay the quorum decision until we know the fate of
192 * the connection. the hold will be droped in conn_up or hb_down. it might be
193 * perpetuated by con_err until hb_down. if we already have a conn, we might
194 * be dropping a hold that conn_up got. */
195 void o2quo_hb_up(u8 node
)
197 struct o2quo_state
*qs
= &o2quo_state
;
199 spin_lock_bh(&qs
->qs_lock
);
201 qs
->qs_heartbeating
++;
202 mlog_bug_on_msg(qs
->qs_heartbeating
== O2NM_MAX_NODES
,
204 mlog_bug_on_msg(test_bit(node
, qs
->qs_hb_bm
), "node %u\n", node
);
205 set_bit(node
, qs
->qs_hb_bm
);
207 mlog(0, "node %u, %d total\n", node
, qs
->qs_heartbeating
);
209 if (!test_bit(node
, qs
->qs_conn_bm
))
210 o2quo_set_hold(qs
, node
);
212 o2quo_clear_hold(qs
, node
);
214 spin_unlock_bh(&qs
->qs_lock
);
217 /* hb going down releases any holds we might have had due to this node from
218 * conn_up, conn_err, or hb_up */
219 void o2quo_hb_down(u8 node
)
221 struct o2quo_state
*qs
= &o2quo_state
;
223 spin_lock_bh(&qs
->qs_lock
);
225 qs
->qs_heartbeating
--;
226 mlog_bug_on_msg(qs
->qs_heartbeating
< 0,
227 "node %u, %d heartbeating\n",
228 node
, qs
->qs_heartbeating
);
229 mlog_bug_on_msg(!test_bit(node
, qs
->qs_hb_bm
), "node %u\n", node
);
230 clear_bit(node
, qs
->qs_hb_bm
);
232 mlog(0, "node %u, %d total\n", node
, qs
->qs_heartbeating
);
234 o2quo_clear_hold(qs
, node
);
236 spin_unlock_bh(&qs
->qs_lock
);
239 /* this tells us that we've decided that the node is still heartbeating
240 * even though we've lost it's conn. it must only be called after conn_err
241 * and indicates that we must now make a quorum decision in the future,
242 * though we might be doing so after waiting for holds to drain. Here
243 * we'll be dropping the hold from conn_err. */
244 void o2quo_hb_still_up(u8 node
)
246 struct o2quo_state
*qs
= &o2quo_state
;
248 spin_lock_bh(&qs
->qs_lock
);
250 mlog(0, "node %u\n", node
);
253 o2quo_clear_hold(qs
, node
);
255 spin_unlock_bh(&qs
->qs_lock
);
258 /* This is analogous to hb_up. as a node's connection comes up we delay the
259 * quorum decision until we see it heartbeating. the hold will be droped in
260 * hb_up or hb_down. it might be perpetuated by con_err until hb_down. if
261 * it's already heartbeating we might be dropping a hold that conn_up got.
263 void o2quo_conn_up(u8 node
)
265 struct o2quo_state
*qs
= &o2quo_state
;
267 spin_lock_bh(&qs
->qs_lock
);
270 mlog_bug_on_msg(qs
->qs_connected
== O2NM_MAX_NODES
,
272 mlog_bug_on_msg(test_bit(node
, qs
->qs_conn_bm
), "node %u\n", node
);
273 set_bit(node
, qs
->qs_conn_bm
);
275 mlog(0, "node %u, %d total\n", node
, qs
->qs_connected
);
277 if (!test_bit(node
, qs
->qs_hb_bm
))
278 o2quo_set_hold(qs
, node
);
280 o2quo_clear_hold(qs
, node
);
282 spin_unlock_bh(&qs
->qs_lock
);
285 /* we've decided that we won't ever be connecting to the node again. if it's
286 * still heartbeating we grab a hold that will delay decisions until either the
287 * node stops heartbeating from hb_down or the caller decides that the node is
288 * still up and calls still_up */
289 void o2quo_conn_err(u8 node
)
291 struct o2quo_state
*qs
= &o2quo_state
;
293 spin_lock_bh(&qs
->qs_lock
);
295 if (test_bit(node
, qs
->qs_conn_bm
)) {
297 mlog_bug_on_msg(qs
->qs_connected
< 0,
298 "node %u, connected %d\n",
299 node
, qs
->qs_connected
);
301 clear_bit(node
, qs
->qs_conn_bm
);
303 if (test_bit(node
, qs
->qs_hb_bm
))
304 o2quo_set_hold(qs
, node
);
307 mlog(0, "node %u, %d total\n", node
, qs
->qs_connected
);
310 spin_unlock_bh(&qs
->qs_lock
);
313 void o2quo_init(void)
315 struct o2quo_state
*qs
= &o2quo_state
;
317 spin_lock_init(&qs
->qs_lock
);
318 INIT_WORK(&qs
->qs_work
, o2quo_make_decision
);
321 void o2quo_exit(void)
323 struct o2quo_state
*qs
= &o2quo_state
;
325 flush_work(&qs
->qs_work
);