1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46#include <linux/kernel.h>
47#include <linux/workqueue.h>
48#include <linux/reboot.h>
49
50#include "heartbeat.h"
51#include "nodemanager.h"
52#define MLOG_MASK_PREFIX ML_QUORUM
53#include "masklog.h"
54#include "quorum.h"
55
56static struct o2quo_state {
57 spinlock_t qs_lock;
58 struct work_struct qs_work;
59 int qs_pending;
60 int qs_heartbeating;
61 unsigned long qs_hb_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
62 int qs_connected;
63 unsigned long qs_conn_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
64 int qs_holds;
65 unsigned long qs_hold_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
66} o2quo_state;
67
68
69
70static void o2quo_fence_self(void)
71{
72
73
74 o2hb_stop_all_regions();
75
76 switch (o2nm_single_cluster->cl_fence_method) {
77 case O2NM_FENCE_PANIC:
78 panic("*** ocfs2 is very sorry to be fencing this system by "
79 "panicing ***\n");
80 break;
81 default:
82 WARN_ON(o2nm_single_cluster->cl_fence_method >=
83 O2NM_FENCE_METHODS);
84 case O2NM_FENCE_RESET:
85 printk(KERN_ERR "*** ocfs2 is very sorry to be fencing this "
86 "system by restarting ***\n");
87 emergency_restart();
88 break;
89 };
90}
91
92
93
94
95
96
97
98
99
100void o2quo_disk_timeout(void)
101{
102 o2quo_fence_self();
103}
104
105static void o2quo_make_decision(struct work_struct *work)
106{
107 int quorum;
108 int lowest_hb, lowest_reachable = 0, fence = 0;
109 struct o2quo_state *qs = &o2quo_state;
110
111 spin_lock(&qs->qs_lock);
112
113 lowest_hb = find_first_bit(qs->qs_hb_bm, O2NM_MAX_NODES);
114 if (lowest_hb != O2NM_MAX_NODES)
115 lowest_reachable = test_bit(lowest_hb, qs->qs_conn_bm);
116
117 mlog(0, "heartbeating: %d, connected: %d, "
118 "lowest: %d (%sreachable)\n", qs->qs_heartbeating,
119 qs->qs_connected, lowest_hb, lowest_reachable ? "" : "un");
120
121 if (!test_bit(o2nm_this_node(), qs->qs_hb_bm) ||
122 qs->qs_heartbeating == 1)
123 goto out;
124
125 if (qs->qs_heartbeating & 1) {
126
127
128 quorum = (qs->qs_heartbeating + 1)/2;
129 if (qs->qs_connected < quorum) {
130 mlog(ML_ERROR, "fencing this node because it is "
131 "only connected to %u nodes and %u is needed "
132 "to make a quorum out of %u heartbeating nodes\n",
133 qs->qs_connected, quorum,
134 qs->qs_heartbeating);
135 fence = 1;
136 }
137 } else {
138
139
140
141
142 quorum = qs->qs_heartbeating / 2;
143 if (qs->qs_connected < quorum) {
144 mlog(ML_ERROR, "fencing this node because it is "
145 "only connected to %u nodes and %u is needed "
146 "to make a quorum out of %u heartbeating nodes\n",
147 qs->qs_connected, quorum,
148 qs->qs_heartbeating);
149 fence = 1;
150 }
151 else if ((qs->qs_connected == quorum) &&
152 !lowest_reachable) {
153 mlog(ML_ERROR, "fencing this node because it is "
154 "connected to a half-quorum of %u out of %u "
155 "nodes which doesn't include the lowest active "
156 "node %u\n", quorum, qs->qs_heartbeating,
157 lowest_hb);
158 fence = 1;
159 }
160 }
161
162out:
163 if (fence) {
164 spin_unlock(&qs->qs_lock);
165 o2quo_fence_self();
166 } else {
167 mlog(ML_NOTICE, "not fencing this node, heartbeating: %d, "
168 "connected: %d, lowest: %d (%sreachable)\n",
169 qs->qs_heartbeating, qs->qs_connected, lowest_hb,
170 lowest_reachable ? "" : "un");
171 spin_unlock(&qs->qs_lock);
172
173 }
174
175}
176
177static void o2quo_set_hold(struct o2quo_state *qs, u8 node)
178{
179 assert_spin_locked(&qs->qs_lock);
180
181 if (!test_and_set_bit(node, qs->qs_hold_bm)) {
182 qs->qs_holds++;
183 mlog_bug_on_msg(qs->qs_holds == O2NM_MAX_NODES,
184 "node %u\n", node);
185 mlog(0, "node %u, %d total\n", node, qs->qs_holds);
186 }
187}
188
189static void o2quo_clear_hold(struct o2quo_state *qs, u8 node)
190{
191 assert_spin_locked(&qs->qs_lock);
192
193 if (test_and_clear_bit(node, qs->qs_hold_bm)) {
194 mlog(0, "node %u, %d total\n", node, qs->qs_holds - 1);
195 if (--qs->qs_holds == 0) {
196 if (qs->qs_pending) {
197 qs->qs_pending = 0;
198 schedule_work(&qs->qs_work);
199 }
200 }
201 mlog_bug_on_msg(qs->qs_holds < 0, "node %u, holds %d\n",
202 node, qs->qs_holds);
203 }
204}
205
206
207
208
209
210void o2quo_hb_up(u8 node)
211{
212 struct o2quo_state *qs = &o2quo_state;
213
214 spin_lock(&qs->qs_lock);
215
216 qs->qs_heartbeating++;
217 mlog_bug_on_msg(qs->qs_heartbeating == O2NM_MAX_NODES,
218 "node %u\n", node);
219 mlog_bug_on_msg(test_bit(node, qs->qs_hb_bm), "node %u\n", node);
220 set_bit(node, qs->qs_hb_bm);
221
222 mlog(0, "node %u, %d total\n", node, qs->qs_heartbeating);
223
224 if (!test_bit(node, qs->qs_conn_bm))
225 o2quo_set_hold(qs, node);
226 else
227 o2quo_clear_hold(qs, node);
228
229 spin_unlock(&qs->qs_lock);
230}
231
232
233
234void o2quo_hb_down(u8 node)
235{
236 struct o2quo_state *qs = &o2quo_state;
237
238 spin_lock(&qs->qs_lock);
239
240 qs->qs_heartbeating--;
241 mlog_bug_on_msg(qs->qs_heartbeating < 0,
242 "node %u, %d heartbeating\n",
243 node, qs->qs_heartbeating);
244 mlog_bug_on_msg(!test_bit(node, qs->qs_hb_bm), "node %u\n", node);
245 clear_bit(node, qs->qs_hb_bm);
246
247 mlog(0, "node %u, %d total\n", node, qs->qs_heartbeating);
248
249 o2quo_clear_hold(qs, node);
250
251 spin_unlock(&qs->qs_lock);
252}
253
254
255
256
257
258
259void o2quo_hb_still_up(u8 node)
260{
261 struct o2quo_state *qs = &o2quo_state;
262
263 spin_lock(&qs->qs_lock);
264
265 mlog(0, "node %u\n", node);
266
267 qs->qs_pending = 1;
268 o2quo_clear_hold(qs, node);
269
270 spin_unlock(&qs->qs_lock);
271}
272
273
274
275
276
277
278void o2quo_conn_up(u8 node)
279{
280 struct o2quo_state *qs = &o2quo_state;
281
282 spin_lock(&qs->qs_lock);
283
284 qs->qs_connected++;
285 mlog_bug_on_msg(qs->qs_connected == O2NM_MAX_NODES,
286 "node %u\n", node);
287 mlog_bug_on_msg(test_bit(node, qs->qs_conn_bm), "node %u\n", node);
288 set_bit(node, qs->qs_conn_bm);
289
290 mlog(0, "node %u, %d total\n", node, qs->qs_connected);
291
292 if (!test_bit(node, qs->qs_hb_bm))
293 o2quo_set_hold(qs, node);
294 else
295 o2quo_clear_hold(qs, node);
296
297 spin_unlock(&qs->qs_lock);
298}
299
300
301
302
303
304void o2quo_conn_err(u8 node)
305{
306 struct o2quo_state *qs = &o2quo_state;
307
308 spin_lock(&qs->qs_lock);
309
310 if (test_bit(node, qs->qs_conn_bm)) {
311 qs->qs_connected--;
312 mlog_bug_on_msg(qs->qs_connected < 0,
313 "node %u, connected %d\n",
314 node, qs->qs_connected);
315
316 clear_bit(node, qs->qs_conn_bm);
317 }
318
319 mlog(0, "node %u, %d total\n", node, qs->qs_connected);
320
321 if (test_bit(node, qs->qs_hb_bm))
322 o2quo_set_hold(qs, node);
323
324 spin_unlock(&qs->qs_lock);
325}
326
327void o2quo_init(void)
328{
329 struct o2quo_state *qs = &o2quo_state;
330
331 spin_lock_init(&qs->qs_lock);
332 INIT_WORK(&qs->qs_work, o2quo_make_decision);
333}
334
335void o2quo_exit(void)
336{
337 struct o2quo_state *qs = &o2quo_state;
338
339 flush_work(&qs->qs_work);
340}
341