1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2004, 2005 Oracle. All rights reserved.
4 */
5
6 #include "linux/kstrtox.h"
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
9 #include <linux/jiffies.h>
10 #include <linux/module.h>
11 #include <linux/fs.h>
12 #include <linux/bio.h>
13 #include <linux/blkdev.h>
14 #include <linux/delay.h>
15 #include <linux/file.h>
16 #include <linux/kthread.h>
17 #include <linux/configfs.h>
18 #include <linux/random.h>
19 #include <linux/crc32.h>
20 #include <linux/time.h>
21 #include <linux/debugfs.h>
22 #include <linux/slab.h>
23 #include <linux/bitmap.h>
24 #include <linux/ktime.h>
25 #include "heartbeat.h"
26 #include "tcp.h"
27 #include "nodemanager.h"
28 #include "quorum.h"
29
30 #include "masklog.h"
31
32
33 /*
34 * The first heartbeat pass had one global thread that would serialize all hb
35 * callback calls. This global serializing sem should only be removed once
36 * we've made sure that all callees can deal with being called concurrently
37 * from multiple hb region threads.
38 */
39 static DECLARE_RWSEM(o2hb_callback_sem);
40
41 /*
42 * multiple hb threads are watching multiple regions. A node is live
43 * whenever any of the threads sees activity from the node in its region.
44 */
45 static DEFINE_SPINLOCK(o2hb_live_lock);
46 static struct list_head o2hb_live_slots[O2NM_MAX_NODES];
47 static unsigned long o2hb_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
48 static LIST_HEAD(o2hb_node_events);
49 static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue);
50
51 /*
52 * In global heartbeat, we maintain a series of region bitmaps.
53 * - o2hb_region_bitmap allows us to limit the region number to max region.
54 * - o2hb_live_region_bitmap tracks live regions (seen steady iterations).
55 * - o2hb_quorum_region_bitmap tracks live regions that have seen all nodes
56 * heartbeat on it.
57 * - o2hb_failed_region_bitmap tracks the regions that have seen io timeouts.
58 */
59 static unsigned long o2hb_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
60 static unsigned long o2hb_live_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
61 static unsigned long o2hb_quorum_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
62 static unsigned long o2hb_failed_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
63
64 #define O2HB_DB_TYPE_LIVENODES 0
65 #define O2HB_DB_TYPE_LIVEREGIONS 1
66 #define O2HB_DB_TYPE_QUORUMREGIONS 2
67 #define O2HB_DB_TYPE_FAILEDREGIONS 3
68 #define O2HB_DB_TYPE_REGION_LIVENODES 4
69 #define O2HB_DB_TYPE_REGION_NUMBER 5
70 #define O2HB_DB_TYPE_REGION_ELAPSED_TIME 6
71 #define O2HB_DB_TYPE_REGION_PINNED 7
72 struct o2hb_debug_buf {
73 int db_type;
74 int db_size;
75 int db_len;
76 void *db_data;
77 };
78
79 static struct o2hb_debug_buf *o2hb_db_livenodes;
80 static struct o2hb_debug_buf *o2hb_db_liveregions;
81 static struct o2hb_debug_buf *o2hb_db_quorumregions;
82 static struct o2hb_debug_buf *o2hb_db_failedregions;
83
84 #define O2HB_DEBUG_DIR "o2hb"
85 #define O2HB_DEBUG_LIVENODES "livenodes"
86 #define O2HB_DEBUG_LIVEREGIONS "live_regions"
87 #define O2HB_DEBUG_QUORUMREGIONS "quorum_regions"
88 #define O2HB_DEBUG_FAILEDREGIONS "failed_regions"
89 #define O2HB_DEBUG_REGION_NUMBER "num"
90 #define O2HB_DEBUG_REGION_ELAPSED_TIME "elapsed_time_in_ms"
91 #define O2HB_DEBUG_REGION_PINNED "pinned"
92
93 static struct dentry *o2hb_debug_dir;
94
95 static LIST_HEAD(o2hb_all_regions);
96
97 static struct o2hb_callback {
98 struct list_head list;
99 } o2hb_callbacks[O2HB_NUM_CB];
100
101 static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type);
102
103 enum o2hb_heartbeat_modes {
104 O2HB_HEARTBEAT_LOCAL = 0,
105 O2HB_HEARTBEAT_GLOBAL,
106 O2HB_HEARTBEAT_NUM_MODES,
107 };
108
109 static const char *o2hb_heartbeat_mode_desc[O2HB_HEARTBEAT_NUM_MODES] = {
110 "local", /* O2HB_HEARTBEAT_LOCAL */
111 "global", /* O2HB_HEARTBEAT_GLOBAL */
112 };
113
114 unsigned int o2hb_dead_threshold = O2HB_DEFAULT_DEAD_THRESHOLD;
115 static unsigned int o2hb_heartbeat_mode = O2HB_HEARTBEAT_LOCAL;
116
117 /*
118 * o2hb_dependent_users tracks the number of registered callbacks that depend
119 * on heartbeat. o2net and o2dlm are two entities that register this callback.
120 * However only o2dlm depends on the heartbeat. It does not want the heartbeat
121 * to stop while a dlm domain is still active.
122 */
123 static unsigned int o2hb_dependent_users;
124
125 /*
126 * In global heartbeat mode, all regions are pinned if there are one or more
127 * dependent users and the quorum region count is <= O2HB_PIN_CUT_OFF. All
128 * regions are unpinned if the region count exceeds the cut off or the number
129 * of dependent users falls to zero.
130 */
131 #define O2HB_PIN_CUT_OFF 3
132
133 /*
134 * In local heartbeat mode, we assume the dlm domain name to be the same as
135 * region uuid. This is true for domains created for the file system but not
136 * necessarily true for userdlm domains. This is a known limitation.
137 *
138 * In global heartbeat mode, we pin/unpin all o2hb regions. This solution
139 * works for both file system and userdlm domains.
140 */
141 static int o2hb_region_pin(const char *region_uuid);
142 static void o2hb_region_unpin(const char *region_uuid);
143
144 /* Only sets a new threshold if there are no active regions.
145 *
146 * No locking or otherwise interesting code is required for reading
147 * o2hb_dead_threshold as it can't change once regions are active and
148 * it's not interesting to anyone until then anyway. */
o2hb_dead_threshold_set(unsigned int threshold)149 static void o2hb_dead_threshold_set(unsigned int threshold)
150 {
151 if (threshold > O2HB_MIN_DEAD_THRESHOLD) {
152 spin_lock(&o2hb_live_lock);
153 if (list_empty(&o2hb_all_regions))
154 o2hb_dead_threshold = threshold;
155 spin_unlock(&o2hb_live_lock);
156 }
157 }
158
o2hb_global_heartbeat_mode_set(unsigned int hb_mode)159 static int o2hb_global_heartbeat_mode_set(unsigned int hb_mode)
160 {
161 int ret = -1;
162
163 if (hb_mode < O2HB_HEARTBEAT_NUM_MODES) {
164 spin_lock(&o2hb_live_lock);
165 if (list_empty(&o2hb_all_regions)) {
166 o2hb_heartbeat_mode = hb_mode;
167 ret = 0;
168 }
169 spin_unlock(&o2hb_live_lock);
170 }
171
172 return ret;
173 }
174
175 struct o2hb_node_event {
176 struct list_head hn_item;
177 enum o2hb_callback_type hn_event_type;
178 struct o2nm_node *hn_node;
179 int hn_node_num;
180 };
181
182 struct o2hb_disk_slot {
183 struct o2hb_disk_heartbeat_block *ds_raw_block;
184 u8 ds_node_num;
185 u64 ds_last_time;
186 u64 ds_last_generation;
187 u16 ds_equal_samples;
188 u16 ds_changed_samples;
189 struct list_head ds_live_item;
190 };
191
192 /* each thread owns a region.. when we're asked to tear down the region
193 * we ask the thread to stop, who cleans up the region */
194 struct o2hb_region {
195 struct config_item hr_item;
196
197 struct list_head hr_all_item;
198 unsigned hr_unclean_stop:1,
199 hr_aborted_start:1,
200 hr_item_pinned:1,
201 hr_item_dropped:1,
202 hr_node_deleted:1;
203
204 /* protected by the hr_callback_sem */
205 struct task_struct *hr_task;
206
207 unsigned int hr_blocks;
208 unsigned long long hr_start_block;
209
210 unsigned int hr_block_bits;
211 unsigned int hr_block_bytes;
212
213 unsigned int hr_slots_per_page;
214 unsigned int hr_num_pages;
215
216 struct page **hr_slot_data;
217 struct file *hr_bdev_file;
218 struct o2hb_disk_slot *hr_slots;
219
220 /* live node map of this region */
221 unsigned long hr_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
222 unsigned int hr_region_num;
223
224 struct dentry *hr_debug_dir;
225 struct o2hb_debug_buf *hr_db_livenodes;
226 struct o2hb_debug_buf *hr_db_regnum;
227 struct o2hb_debug_buf *hr_db_elapsed_time;
228 struct o2hb_debug_buf *hr_db_pinned;
229
230 /* let the person setting up hb wait for it to return until it
231 * has reached a 'steady' state. This will be fixed when we have
232 * a more complete api that doesn't lead to this sort of fragility. */
233 atomic_t hr_steady_iterations;
234
235 /* terminate o2hb thread if it does not reach steady state
236 * (hr_steady_iterations == 0) within hr_unsteady_iterations */
237 atomic_t hr_unsteady_iterations;
238
239 unsigned int hr_timeout_ms;
240
241 /* randomized as the region goes up and down so that a node
242 * recognizes a node going up and down in one iteration */
243 u64 hr_generation;
244
245 struct delayed_work hr_write_timeout_work;
246 unsigned long hr_last_timeout_start;
247
248 /* negotiate timer, used to negotiate extending hb timeout. */
249 struct delayed_work hr_nego_timeout_work;
250 unsigned long hr_nego_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
251
252 /* Used during o2hb_check_slot to hold a copy of the block
253 * being checked because we temporarily have to zero out the
254 * crc field. */
255 struct o2hb_disk_heartbeat_block *hr_tmp_block;
256
257 /* Message key for negotiate timeout message. */
258 unsigned int hr_key;
259 struct list_head hr_handler_list;
260
261 /* last hb status, 0 for success, other value for error. */
262 int hr_last_hb_status;
263 };
264
reg_bdev(struct o2hb_region * reg)265 static inline struct block_device *reg_bdev(struct o2hb_region *reg)
266 {
267 return reg->hr_bdev_file ? file_bdev(reg->hr_bdev_file) : NULL;
268 }
269
270 struct o2hb_bio_wait_ctxt {
271 atomic_t wc_num_reqs;
272 struct completion wc_io_complete;
273 int wc_error;
274 };
275
276 #define O2HB_NEGO_TIMEOUT_MS (O2HB_MAX_WRITE_TIMEOUT_MS/2)
277
278 enum {
279 O2HB_NEGO_TIMEOUT_MSG = 1,
280 O2HB_NEGO_APPROVE_MSG = 2,
281 };
282
283 struct o2hb_nego_msg {
284 u8 node_num;
285 };
286
o2hb_write_timeout(struct work_struct * work)287 static void o2hb_write_timeout(struct work_struct *work)
288 {
289 int failed, quorum;
290 struct o2hb_region *reg =
291 container_of(work, struct o2hb_region,
292 hr_write_timeout_work.work);
293
294 mlog(ML_ERROR, "Heartbeat write timeout to device %pg after %u "
295 "milliseconds\n", reg_bdev(reg),
296 jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
297
298 if (o2hb_global_heartbeat_active()) {
299 spin_lock(&o2hb_live_lock);
300 if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
301 set_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
302 failed = bitmap_weight(o2hb_failed_region_bitmap,
303 O2NM_MAX_REGIONS);
304 quorum = bitmap_weight(o2hb_quorum_region_bitmap,
305 O2NM_MAX_REGIONS);
306 spin_unlock(&o2hb_live_lock);
307
308 mlog(ML_HEARTBEAT, "Number of regions %d, failed regions %d\n",
309 quorum, failed);
310
311 /*
312 * Fence if the number of failed regions >= half the number
313 * of quorum regions
314 */
315 if ((failed << 1) < quorum)
316 return;
317 }
318
319 o2quo_disk_timeout();
320 }
321
o2hb_arm_timeout(struct o2hb_region * reg)322 static void o2hb_arm_timeout(struct o2hb_region *reg)
323 {
324 /* Arm writeout only after thread reaches steady state */
325 if (atomic_read(®->hr_steady_iterations) != 0)
326 return;
327
328 mlog(ML_HEARTBEAT, "Queue write timeout for %u ms\n",
329 O2HB_MAX_WRITE_TIMEOUT_MS);
330
331 if (o2hb_global_heartbeat_active()) {
332 spin_lock(&o2hb_live_lock);
333 clear_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
334 spin_unlock(&o2hb_live_lock);
335 }
336 cancel_delayed_work(®->hr_write_timeout_work);
337 schedule_delayed_work(®->hr_write_timeout_work,
338 msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS));
339
340 cancel_delayed_work(®->hr_nego_timeout_work);
341 /* negotiate timeout must be less than write timeout. */
342 schedule_delayed_work(®->hr_nego_timeout_work,
343 msecs_to_jiffies(O2HB_NEGO_TIMEOUT_MS));
344 bitmap_zero(reg->hr_nego_node_bitmap, O2NM_MAX_NODES);
345 }
346
o2hb_disarm_timeout(struct o2hb_region * reg)347 static void o2hb_disarm_timeout(struct o2hb_region *reg)
348 {
349 cancel_delayed_work_sync(®->hr_write_timeout_work);
350 cancel_delayed_work_sync(®->hr_nego_timeout_work);
351 }
352
o2hb_send_nego_msg(int key,int type,u8 target)353 static int o2hb_send_nego_msg(int key, int type, u8 target)
354 {
355 struct o2hb_nego_msg msg;
356 int status, ret;
357
358 msg.node_num = o2nm_this_node();
359 again:
360 ret = o2net_send_message(type, key, &msg, sizeof(msg),
361 target, &status);
362
363 if (ret == -EAGAIN || ret == -ENOMEM) {
364 msleep(100);
365 goto again;
366 }
367
368 return ret;
369 }
370
o2hb_nego_timeout(struct work_struct * work)371 static void o2hb_nego_timeout(struct work_struct *work)
372 {
373 unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
374 int master_node, i, ret;
375 struct o2hb_region *reg;
376
377 reg = container_of(work, struct o2hb_region, hr_nego_timeout_work.work);
378 /* don't negotiate timeout if last hb failed since it is very
379 * possible io failed. Should let write timeout fence self.
380 */
381 if (reg->hr_last_hb_status)
382 return;
383
384 o2hb_fill_node_map(live_node_bitmap, O2NM_MAX_NODES);
385 /* lowest node as master node to make negotiate decision. */
386 master_node = find_first_bit(live_node_bitmap, O2NM_MAX_NODES);
387
388 if (master_node == o2nm_this_node()) {
389 if (!test_bit(master_node, reg->hr_nego_node_bitmap)) {
390 printk(KERN_NOTICE "o2hb: node %d hb write hung for %ds on region %s (%pg).\n",
391 o2nm_this_node(), O2HB_NEGO_TIMEOUT_MS/1000,
392 config_item_name(®->hr_item), reg_bdev(reg));
393 set_bit(master_node, reg->hr_nego_node_bitmap);
394 }
395 if (!bitmap_equal(reg->hr_nego_node_bitmap, live_node_bitmap,
396 O2NM_MAX_NODES)) {
397 /* check negotiate bitmap every second to do timeout
398 * approve decision.
399 */
400 schedule_delayed_work(®->hr_nego_timeout_work,
401 msecs_to_jiffies(1000));
402
403 return;
404 }
405
406 printk(KERN_NOTICE "o2hb: all nodes hb write hung, maybe region %s (%pg) is down.\n",
407 config_item_name(®->hr_item),
408 reg_bdev(reg));
409 /* approve negotiate timeout request. */
410 o2hb_arm_timeout(reg);
411
412 i = -1;
413 while ((i = find_next_bit(live_node_bitmap,
414 O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
415 if (i == master_node)
416 continue;
417
418 mlog(ML_HEARTBEAT, "send NEGO_APPROVE msg to node %d\n", i);
419 ret = o2hb_send_nego_msg(reg->hr_key,
420 O2HB_NEGO_APPROVE_MSG, i);
421 if (ret)
422 mlog(ML_ERROR, "send NEGO_APPROVE msg to node %d fail %d\n",
423 i, ret);
424 }
425 } else {
426 /* negotiate timeout with master node. */
427 printk(KERN_NOTICE "o2hb: node %d hb write hung for %ds on region %s (%pg), negotiate timeout with node %d.\n",
428 o2nm_this_node(), O2HB_NEGO_TIMEOUT_MS/1000, config_item_name(®->hr_item),
429 reg_bdev(reg), master_node);
430 ret = o2hb_send_nego_msg(reg->hr_key, O2HB_NEGO_TIMEOUT_MSG,
431 master_node);
432 if (ret)
433 mlog(ML_ERROR, "send NEGO_TIMEOUT msg to node %d fail %d\n",
434 master_node, ret);
435 }
436 }
437
o2hb_nego_timeout_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)438 static int o2hb_nego_timeout_handler(struct o2net_msg *msg, u32 len, void *data,
439 void **ret_data)
440 {
441 struct o2hb_region *reg = data;
442 struct o2hb_nego_msg *nego_msg;
443
444 nego_msg = (struct o2hb_nego_msg *)msg->buf;
445 printk(KERN_NOTICE "o2hb: receive negotiate timeout message from node %d on region %s (%pg).\n",
446 nego_msg->node_num, config_item_name(®->hr_item),
447 reg_bdev(reg));
448 if (nego_msg->node_num < O2NM_MAX_NODES)
449 set_bit(nego_msg->node_num, reg->hr_nego_node_bitmap);
450 else
451 mlog(ML_ERROR, "got nego timeout message from bad node.\n");
452
453 return 0;
454 }
455
o2hb_nego_approve_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)456 static int o2hb_nego_approve_handler(struct o2net_msg *msg, u32 len, void *data,
457 void **ret_data)
458 {
459 struct o2hb_region *reg = data;
460
461 printk(KERN_NOTICE "o2hb: negotiate timeout approved by master node on region %s (%pg).\n",
462 config_item_name(®->hr_item), reg_bdev(reg));
463 o2hb_arm_timeout(reg);
464 return 0;
465 }
466
o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt * wc)467 static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt *wc)
468 {
469 atomic_set(&wc->wc_num_reqs, 1);
470 init_completion(&wc->wc_io_complete);
471 wc->wc_error = 0;
472 }
473
474 /* Used in error paths too */
o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt * wc,unsigned int num)475 static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt *wc,
476 unsigned int num)
477 {
478 /* sadly atomic_sub_and_test() isn't available on all platforms. The
479 * good news is that the fast path only completes one at a time */
480 while(num--) {
481 if (atomic_dec_and_test(&wc->wc_num_reqs)) {
482 BUG_ON(num > 0);
483 complete(&wc->wc_io_complete);
484 }
485 }
486 }
487
o2hb_wait_on_io(struct o2hb_bio_wait_ctxt * wc)488 static void o2hb_wait_on_io(struct o2hb_bio_wait_ctxt *wc)
489 {
490 o2hb_bio_wait_dec(wc, 1);
491 wait_for_completion(&wc->wc_io_complete);
492 }
493
o2hb_bio_end_io(struct bio * bio)494 static void o2hb_bio_end_io(struct bio *bio)
495 {
496 struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
497
498 if (bio->bi_status) {
499 mlog(ML_ERROR, "IO Error %d\n", bio->bi_status);
500 wc->wc_error = blk_status_to_errno(bio->bi_status);
501 }
502
503 o2hb_bio_wait_dec(wc, 1);
504 bio_put(bio);
505 }
506
507 /* Setup a Bio to cover I/O against num_slots slots starting at
508 * start_slot. */
o2hb_setup_one_bio(struct o2hb_region * reg,struct o2hb_bio_wait_ctxt * wc,unsigned int * current_slot,unsigned int max_slots,blk_opf_t opf)509 static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
510 struct o2hb_bio_wait_ctxt *wc,
511 unsigned int *current_slot,
512 unsigned int max_slots, blk_opf_t opf)
513 {
514 int len, current_page;
515 unsigned int vec_len, vec_start;
516 unsigned int bits = reg->hr_block_bits;
517 unsigned int spp = reg->hr_slots_per_page;
518 unsigned int cs = *current_slot;
519 struct bio *bio;
520 struct page *page;
521
522 /* Testing has shown this allocation to take long enough under
523 * GFP_KERNEL that the local node can get fenced. It would be
524 * nicest if we could pre-allocate these bios and avoid this
525 * all together. */
526 bio = bio_alloc(reg_bdev(reg), 16, opf, GFP_ATOMIC);
527 if (!bio) {
528 mlog(ML_ERROR, "Could not alloc slots BIO!\n");
529 bio = ERR_PTR(-ENOMEM);
530 goto bail;
531 }
532
533 /* Must put everything in 512 byte sectors for the bio... */
534 bio->bi_iter.bi_sector = (reg->hr_start_block + cs) << (bits - 9);
535 bio->bi_private = wc;
536 bio->bi_end_io = o2hb_bio_end_io;
537
538 vec_start = (cs << bits) % PAGE_SIZE;
539 while(cs < max_slots) {
540 current_page = cs / spp;
541 page = reg->hr_slot_data[current_page];
542
543 vec_len = min(PAGE_SIZE - vec_start,
544 (max_slots-cs) * (PAGE_SIZE/spp) );
545
546 mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
547 current_page, vec_len, vec_start);
548
549 len = bio_add_page(bio, page, vec_len, vec_start);
550 if (len != vec_len) break;
551
552 cs += vec_len / (PAGE_SIZE/spp);
553 vec_start = 0;
554 }
555
556 bail:
557 *current_slot = cs;
558 return bio;
559 }
560
o2hb_read_slots(struct o2hb_region * reg,unsigned int begin_slot,unsigned int max_slots)561 static int o2hb_read_slots(struct o2hb_region *reg,
562 unsigned int begin_slot,
563 unsigned int max_slots)
564 {
565 unsigned int current_slot = begin_slot;
566 int status;
567 struct o2hb_bio_wait_ctxt wc;
568 struct bio *bio;
569
570 o2hb_bio_wait_init(&wc);
571
572 while(current_slot < max_slots) {
573 bio = o2hb_setup_one_bio(reg, &wc, ¤t_slot, max_slots,
574 REQ_OP_READ);
575 if (IS_ERR(bio)) {
576 status = PTR_ERR(bio);
577 mlog_errno(status);
578 goto bail_and_wait;
579 }
580
581 atomic_inc(&wc.wc_num_reqs);
582 submit_bio(bio);
583 }
584
585 status = 0;
586
587 bail_and_wait:
588 o2hb_wait_on_io(&wc);
589 if (wc.wc_error && !status)
590 status = wc.wc_error;
591
592 return status;
593 }
594
o2hb_issue_node_write(struct o2hb_region * reg,struct o2hb_bio_wait_ctxt * write_wc)595 static int o2hb_issue_node_write(struct o2hb_region *reg,
596 struct o2hb_bio_wait_ctxt *write_wc)
597 {
598 int status;
599 unsigned int slot;
600 struct bio *bio;
601
602 o2hb_bio_wait_init(write_wc);
603
604 slot = o2nm_this_node();
605
606 bio = o2hb_setup_one_bio(reg, write_wc, &slot, slot+1,
607 REQ_OP_WRITE | REQ_SYNC);
608 if (IS_ERR(bio)) {
609 status = PTR_ERR(bio);
610 mlog_errno(status);
611 goto bail;
612 }
613
614 atomic_inc(&write_wc->wc_num_reqs);
615 submit_bio(bio);
616
617 status = 0;
618 bail:
619 return status;
620 }
621
o2hb_compute_block_crc_le(struct o2hb_region * reg,struct o2hb_disk_heartbeat_block * hb_block)622 static u32 o2hb_compute_block_crc_le(struct o2hb_region *reg,
623 struct o2hb_disk_heartbeat_block *hb_block)
624 {
625 __le32 old_cksum;
626 u32 ret;
627
628 /* We want to compute the block crc with a 0 value in the
629 * hb_cksum field. Save it off here and replace after the
630 * crc. */
631 old_cksum = hb_block->hb_cksum;
632 hb_block->hb_cksum = 0;
633
634 ret = crc32_le(0, (unsigned char *) hb_block, reg->hr_block_bytes);
635
636 hb_block->hb_cksum = old_cksum;
637
638 return ret;
639 }
640
o2hb_dump_slot(struct o2hb_disk_heartbeat_block * hb_block)641 static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
642 {
643 mlog(ML_ERROR, "Dump slot information: seq = 0x%llx, node = %u, "
644 "cksum = 0x%x, generation 0x%llx\n",
645 (long long)le64_to_cpu(hb_block->hb_seq),
646 hb_block->hb_node, le32_to_cpu(hb_block->hb_cksum),
647 (long long)le64_to_cpu(hb_block->hb_generation));
648 }
649
o2hb_verify_crc(struct o2hb_region * reg,struct o2hb_disk_heartbeat_block * hb_block)650 static int o2hb_verify_crc(struct o2hb_region *reg,
651 struct o2hb_disk_heartbeat_block *hb_block)
652 {
653 u32 read, computed;
654
655 read = le32_to_cpu(hb_block->hb_cksum);
656 computed = o2hb_compute_block_crc_le(reg, hb_block);
657
658 return read == computed;
659 }
660
661 /*
662 * Compare the slot data with what we wrote in the last iteration.
663 * If the match fails, print an appropriate error message. This is to
664 * detect errors like... another node hearting on the same slot,
665 * flaky device that is losing writes, etc.
666 * Returns 1 if check succeeds, 0 otherwise.
667 */
o2hb_check_own_slot(struct o2hb_region * reg)668 static int o2hb_check_own_slot(struct o2hb_region *reg)
669 {
670 struct o2hb_disk_slot *slot;
671 struct o2hb_disk_heartbeat_block *hb_block;
672 char *errstr;
673
674 slot = ®->hr_slots[o2nm_this_node()];
675 /* Don't check on our 1st timestamp */
676 if (!slot->ds_last_time)
677 return 0;
678
679 hb_block = slot->ds_raw_block;
680 if (le64_to_cpu(hb_block->hb_seq) == slot->ds_last_time &&
681 le64_to_cpu(hb_block->hb_generation) == slot->ds_last_generation &&
682 hb_block->hb_node == slot->ds_node_num)
683 return 1;
684
685 #define ERRSTR1 "Another node is heartbeating on device"
686 #define ERRSTR2 "Heartbeat generation mismatch on device"
687 #define ERRSTR3 "Heartbeat sequence mismatch on device"
688
689 if (hb_block->hb_node != slot->ds_node_num)
690 errstr = ERRSTR1;
691 else if (le64_to_cpu(hb_block->hb_generation) !=
692 slot->ds_last_generation)
693 errstr = ERRSTR2;
694 else
695 errstr = ERRSTR3;
696
697 mlog(ML_ERROR, "%s (%pg): expected(%u:0x%llx, 0x%llx), "
698 "ondisk(%u:0x%llx, 0x%llx)\n", errstr, reg_bdev(reg),
699 slot->ds_node_num, (unsigned long long)slot->ds_last_generation,
700 (unsigned long long)slot->ds_last_time, hb_block->hb_node,
701 (unsigned long long)le64_to_cpu(hb_block->hb_generation),
702 (unsigned long long)le64_to_cpu(hb_block->hb_seq));
703
704 return 0;
705 }
706
o2hb_prepare_block(struct o2hb_region * reg,u64 generation)707 static inline void o2hb_prepare_block(struct o2hb_region *reg,
708 u64 generation)
709 {
710 int node_num;
711 u64 cputime;
712 struct o2hb_disk_slot *slot;
713 struct o2hb_disk_heartbeat_block *hb_block;
714
715 node_num = o2nm_this_node();
716 slot = ®->hr_slots[node_num];
717
718 hb_block = (struct o2hb_disk_heartbeat_block *)slot->ds_raw_block;
719 memset(hb_block, 0, reg->hr_block_bytes);
720 /* TODO: time stuff */
721 cputime = ktime_get_real_seconds();
722 if (!cputime)
723 cputime = 1;
724
725 hb_block->hb_seq = cpu_to_le64(cputime);
726 hb_block->hb_node = node_num;
727 hb_block->hb_generation = cpu_to_le64(generation);
728 hb_block->hb_dead_ms = cpu_to_le32(o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS);
729
730 /* This step must always happen last! */
731 hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
732 hb_block));
733
734 mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x\n",
735 (long long)generation,
736 le32_to_cpu(hb_block->hb_cksum));
737 }
738
o2hb_fire_callbacks(struct o2hb_callback * hbcall,struct o2nm_node * node,int idx)739 static void o2hb_fire_callbacks(struct o2hb_callback *hbcall,
740 struct o2nm_node *node,
741 int idx)
742 {
743 struct o2hb_callback_func *f;
744
745 list_for_each_entry(f, &hbcall->list, hc_item) {
746 mlog(ML_HEARTBEAT, "calling funcs %p\n", f);
747 (f->hc_func)(node, idx, f->hc_data);
748 }
749 }
750
751 /* Will run the list in order until we process the passed event */
o2hb_run_event_list(struct o2hb_node_event * queued_event)752 static void o2hb_run_event_list(struct o2hb_node_event *queued_event)
753 {
754 struct o2hb_callback *hbcall;
755 struct o2hb_node_event *event;
756
757 /* Holding callback sem assures we don't alter the callback
758 * lists when doing this, and serializes ourselves with other
759 * processes wanting callbacks. */
760 down_write(&o2hb_callback_sem);
761
762 spin_lock(&o2hb_live_lock);
763 while (!list_empty(&o2hb_node_events)
764 && !list_empty(&queued_event->hn_item)) {
765 event = list_entry(o2hb_node_events.next,
766 struct o2hb_node_event,
767 hn_item);
768 list_del_init(&event->hn_item);
769 spin_unlock(&o2hb_live_lock);
770
771 mlog(ML_HEARTBEAT, "Node %s event for %d\n",
772 event->hn_event_type == O2HB_NODE_UP_CB ? "UP" : "DOWN",
773 event->hn_node_num);
774
775 hbcall = hbcall_from_type(event->hn_event_type);
776
777 /* We should *never* have gotten on to the list with a
778 * bad type... This isn't something that we should try
779 * to recover from. */
780 BUG_ON(IS_ERR(hbcall));
781
782 o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num);
783
784 spin_lock(&o2hb_live_lock);
785 }
786 spin_unlock(&o2hb_live_lock);
787
788 up_write(&o2hb_callback_sem);
789 }
790
o2hb_queue_node_event(struct o2hb_node_event * event,enum o2hb_callback_type type,struct o2nm_node * node,int node_num)791 static void o2hb_queue_node_event(struct o2hb_node_event *event,
792 enum o2hb_callback_type type,
793 struct o2nm_node *node,
794 int node_num)
795 {
796 assert_spin_locked(&o2hb_live_lock);
797
798 BUG_ON((!node) && (type != O2HB_NODE_DOWN_CB));
799
800 event->hn_event_type = type;
801 event->hn_node = node;
802 event->hn_node_num = node_num;
803
804 mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
805 type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
806
807 list_add_tail(&event->hn_item, &o2hb_node_events);
808 }
809
o2hb_shutdown_slot(struct o2hb_disk_slot * slot)810 static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
811 {
812 struct o2hb_node_event event =
813 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
814 struct o2nm_node *node;
815 int queued = 0;
816
817 node = o2nm_get_node_by_num(slot->ds_node_num);
818 if (!node)
819 return;
820
821 spin_lock(&o2hb_live_lock);
822 if (!list_empty(&slot->ds_live_item)) {
823 mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
824 slot->ds_node_num);
825
826 list_del_init(&slot->ds_live_item);
827
828 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
829 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
830
831 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
832 slot->ds_node_num);
833 queued = 1;
834 }
835 }
836 spin_unlock(&o2hb_live_lock);
837
838 if (queued)
839 o2hb_run_event_list(&event);
840
841 o2nm_node_put(node);
842 }
843
o2hb_set_quorum_device(struct o2hb_region * reg)844 static void o2hb_set_quorum_device(struct o2hb_region *reg)
845 {
846 if (!o2hb_global_heartbeat_active())
847 return;
848
849 /* Prevent race with o2hb_heartbeat_group_drop_item() */
850 if (kthread_should_stop())
851 return;
852
853 /* Tag region as quorum only after thread reaches steady state */
854 if (atomic_read(®->hr_steady_iterations) != 0)
855 return;
856
857 spin_lock(&o2hb_live_lock);
858
859 if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
860 goto unlock;
861
862 /*
863 * A region can be added to the quorum only when it sees all
864 * live nodes heartbeat on it. In other words, the region has been
865 * added to all nodes.
866 */
867 if (!bitmap_equal(reg->hr_live_node_bitmap, o2hb_live_node_bitmap,
868 O2NM_MAX_NODES))
869 goto unlock;
870
871 printk(KERN_NOTICE "o2hb: Region %s (%pg) is now a quorum device\n",
872 config_item_name(®->hr_item), reg_bdev(reg));
873
874 set_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
875
876 /*
877 * If global heartbeat active, unpin all regions if the
878 * region count > CUT_OFF
879 */
880 if (bitmap_weight(o2hb_quorum_region_bitmap,
881 O2NM_MAX_REGIONS) > O2HB_PIN_CUT_OFF)
882 o2hb_region_unpin(NULL);
883 unlock:
884 spin_unlock(&o2hb_live_lock);
885 }
886
o2hb_check_slot(struct o2hb_region * reg,struct o2hb_disk_slot * slot)887 static int o2hb_check_slot(struct o2hb_region *reg,
888 struct o2hb_disk_slot *slot)
889 {
890 int changed = 0, gen_changed = 0;
891 struct o2hb_node_event event =
892 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
893 struct o2nm_node *node;
894 struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
895 u64 cputime;
896 unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
897 unsigned int slot_dead_ms;
898 int tmp;
899 int queued = 0;
900
901 memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
902
903 /*
904 * If a node is no longer configured but is still in the livemap, we
905 * may need to clear that bit from the livemap.
906 */
907 node = o2nm_get_node_by_num(slot->ds_node_num);
908 if (!node) {
909 spin_lock(&o2hb_live_lock);
910 tmp = test_bit(slot->ds_node_num, o2hb_live_node_bitmap);
911 spin_unlock(&o2hb_live_lock);
912 if (!tmp)
913 return 0;
914 }
915
916 if (!o2hb_verify_crc(reg, hb_block)) {
917 /* all paths from here will drop o2hb_live_lock for
918 * us. */
919 spin_lock(&o2hb_live_lock);
920
921 /* Don't print an error on the console in this case -
922 * a freshly formatted heartbeat area will not have a
923 * crc set on it. */
924 if (list_empty(&slot->ds_live_item))
925 goto out;
926
927 /* The node is live but pushed out a bad crc. We
928 * consider it a transient miss but don't populate any
929 * other values as they may be junk. */
930 mlog(ML_ERROR, "Node %d has written a bad crc to %pg\n",
931 slot->ds_node_num, reg_bdev(reg));
932 o2hb_dump_slot(hb_block);
933
934 slot->ds_equal_samples++;
935 goto fire_callbacks;
936 }
937
938 /* we don't care if these wrap.. the state transitions below
939 * clear at the right places */
940 cputime = le64_to_cpu(hb_block->hb_seq);
941 if (slot->ds_last_time != cputime)
942 slot->ds_changed_samples++;
943 else
944 slot->ds_equal_samples++;
945 slot->ds_last_time = cputime;
946
947 /* The node changed heartbeat generations. We assume this to
948 * mean it dropped off but came back before we timed out. We
949 * want to consider it down for the time being but don't want
950 * to lose any changed_samples state we might build up to
951 * considering it live again. */
952 if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
953 gen_changed = 1;
954 slot->ds_equal_samples = 0;
955 mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
956 "to 0x%llx)\n", slot->ds_node_num,
957 (long long)slot->ds_last_generation,
958 (long long)le64_to_cpu(hb_block->hb_generation));
959 }
960
961 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
962
963 mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
964 "seq %llu last %llu changed %u equal %u\n",
965 slot->ds_node_num, (long long)slot->ds_last_generation,
966 le32_to_cpu(hb_block->hb_cksum),
967 (unsigned long long)le64_to_cpu(hb_block->hb_seq),
968 (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
969 slot->ds_equal_samples);
970
971 spin_lock(&o2hb_live_lock);
972
973 fire_callbacks:
974 /* dead nodes only come to life after some number of
975 * changes at any time during their dead time */
976 if (list_empty(&slot->ds_live_item) &&
977 slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
978 mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
979 slot->ds_node_num, (long long)slot->ds_last_generation);
980
981 set_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
982
983 /* first on the list generates a callback */
984 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
985 mlog(ML_HEARTBEAT, "o2hb: Add node %d to live nodes "
986 "bitmap\n", slot->ds_node_num);
987 set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
988
989 o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
990 slot->ds_node_num);
991
992 changed = 1;
993 queued = 1;
994 }
995
996 list_add_tail(&slot->ds_live_item,
997 &o2hb_live_slots[slot->ds_node_num]);
998
999 slot->ds_equal_samples = 0;
1000
1001 /* We want to be sure that all nodes agree on the
1002 * number of milliseconds before a node will be
1003 * considered dead. The self-fencing timeout is
1004 * computed from this value, and a discrepancy might
1005 * result in heartbeat calling a node dead when it
1006 * hasn't self-fenced yet. */
1007 slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
1008 if (slot_dead_ms && slot_dead_ms != dead_ms) {
1009 /* TODO: Perhaps we can fail the region here. */
1010 mlog(ML_ERROR, "Node %d on device %pg has a dead count "
1011 "of %u ms, but our count is %u ms.\n"
1012 "Please double check your configuration values "
1013 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
1014 slot->ds_node_num, reg_bdev(reg),
1015 slot_dead_ms, dead_ms);
1016 }
1017 goto out;
1018 }
1019
1020 /* if the list is dead, we're done.. */
1021 if (list_empty(&slot->ds_live_item))
1022 goto out;
1023
1024 /* live nodes only go dead after enough consecutive missed
1025 * samples.. reset the missed counter whenever we see
1026 * activity */
1027 if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
1028 mlog(ML_HEARTBEAT, "Node %d left my region\n",
1029 slot->ds_node_num);
1030
1031 clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
1032
1033 /* last off the live_slot generates a callback */
1034 list_del_init(&slot->ds_live_item);
1035 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
1036 mlog(ML_HEARTBEAT, "o2hb: Remove node %d from live "
1037 "nodes bitmap\n", slot->ds_node_num);
1038 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
1039
1040 /* node can be null */
1041 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
1042 node, slot->ds_node_num);
1043
1044 changed = 1;
1045 queued = 1;
1046 }
1047
1048 /* We don't clear this because the node is still
1049 * actually writing new blocks. */
1050 if (!gen_changed)
1051 slot->ds_changed_samples = 0;
1052 goto out;
1053 }
1054 if (slot->ds_changed_samples) {
1055 slot->ds_changed_samples = 0;
1056 slot->ds_equal_samples = 0;
1057 }
1058 out:
1059 spin_unlock(&o2hb_live_lock);
1060
1061 if (queued)
1062 o2hb_run_event_list(&event);
1063
1064 if (node)
1065 o2nm_node_put(node);
1066 return changed;
1067 }
1068
o2hb_highest_node(unsigned long * nodes,int numbits)1069 static int o2hb_highest_node(unsigned long *nodes, int numbits)
1070 {
1071 return find_last_bit(nodes, numbits);
1072 }
1073
o2hb_lowest_node(unsigned long * nodes,int numbits)1074 static int o2hb_lowest_node(unsigned long *nodes, int numbits)
1075 {
1076 return find_first_bit(nodes, numbits);
1077 }
1078
o2hb_do_disk_heartbeat(struct o2hb_region * reg)1079 static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
1080 {
1081 int i, ret, highest_node, lowest_node;
1082 int membership_change = 0, own_slot_ok = 0;
1083 unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
1084 unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
1085 struct o2hb_bio_wait_ctxt write_wc;
1086
1087 ret = o2nm_configured_node_map(configured_nodes,
1088 sizeof(configured_nodes));
1089 if (ret) {
1090 mlog_errno(ret);
1091 goto bail;
1092 }
1093
1094 /*
1095 * If a node is not configured but is in the livemap, we still need
1096 * to read the slot so as to be able to remove it from the livemap.
1097 */
1098 o2hb_fill_node_map(live_node_bitmap, O2NM_MAX_NODES);
1099 i = -1;
1100 while ((i = find_next_bit(live_node_bitmap,
1101 O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
1102 set_bit(i, configured_nodes);
1103 }
1104
1105 highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
1106 lowest_node = o2hb_lowest_node(configured_nodes, O2NM_MAX_NODES);
1107 if (highest_node >= O2NM_MAX_NODES || lowest_node >= O2NM_MAX_NODES) {
1108 mlog(ML_NOTICE, "o2hb: No configured nodes found!\n");
1109 ret = -EINVAL;
1110 goto bail;
1111 }
1112
1113 /* No sense in reading the slots of nodes that don't exist
1114 * yet. Of course, if the node definitions have holes in them
1115 * then we're reading an empty slot anyway... Consider this
1116 * best-effort. */
1117 ret = o2hb_read_slots(reg, lowest_node, highest_node + 1);
1118 if (ret < 0) {
1119 mlog_errno(ret);
1120 goto bail;
1121 }
1122
1123 /* With an up to date view of the slots, we can check that no
1124 * other node has been improperly configured to heartbeat in
1125 * our slot. */
1126 own_slot_ok = o2hb_check_own_slot(reg);
1127
1128 /* fill in the proper info for our next heartbeat */
1129 o2hb_prepare_block(reg, reg->hr_generation);
1130
1131 ret = o2hb_issue_node_write(reg, &write_wc);
1132 if (ret < 0) {
1133 mlog_errno(ret);
1134 goto bail;
1135 }
1136
1137 i = -1;
1138 while((i = find_next_bit(configured_nodes,
1139 O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
1140 membership_change |= o2hb_check_slot(reg, ®->hr_slots[i]);
1141 }
1142
1143 /*
1144 * We have to be sure we've advertised ourselves on disk
1145 * before we can go to steady state. This ensures that
1146 * people we find in our steady state have seen us.
1147 */
1148 o2hb_wait_on_io(&write_wc);
1149 if (write_wc.wc_error) {
1150 /* Do not re-arm the write timeout on I/O error - we
1151 * can't be sure that the new block ever made it to
1152 * disk */
1153 mlog(ML_ERROR, "Write error %d on device \"%pg\"\n",
1154 write_wc.wc_error, reg_bdev(reg));
1155 ret = write_wc.wc_error;
1156 goto bail;
1157 }
1158
1159 /* Skip disarming the timeout if own slot has stale/bad data */
1160 if (own_slot_ok) {
1161 o2hb_set_quorum_device(reg);
1162 o2hb_arm_timeout(reg);
1163 reg->hr_last_timeout_start = jiffies;
1164 }
1165
1166 bail:
1167 /* let the person who launched us know when things are steady */
1168 if (atomic_read(®->hr_steady_iterations) != 0) {
1169 if (!ret && own_slot_ok && !membership_change) {
1170 if (atomic_dec_and_test(®->hr_steady_iterations))
1171 wake_up(&o2hb_steady_queue);
1172 }
1173 }
1174
1175 if (atomic_read(®->hr_steady_iterations) != 0) {
1176 if (atomic_dec_and_test(®->hr_unsteady_iterations)) {
1177 printk(KERN_NOTICE "o2hb: Unable to stabilize "
1178 "heartbeat on region %s (%pg)\n",
1179 config_item_name(®->hr_item),
1180 reg_bdev(reg));
1181 atomic_set(®->hr_steady_iterations, 0);
1182 reg->hr_aborted_start = 1;
1183 wake_up(&o2hb_steady_queue);
1184 ret = -EIO;
1185 }
1186 }
1187
1188 return ret;
1189 }
1190
1191 /*
1192 * we ride the region ref that the region dir holds. before the region
1193 * dir is removed and drops it ref it will wait to tear down this
1194 * thread.
1195 */
o2hb_thread(void * data)1196 static int o2hb_thread(void *data)
1197 {
1198 int i, ret;
1199 struct o2hb_region *reg = data;
1200 struct o2hb_bio_wait_ctxt write_wc;
1201 ktime_t before_hb, after_hb;
1202 unsigned int elapsed_msec;
1203
1204 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
1205
1206 set_user_nice(current, MIN_NICE);
1207
1208 /* Pin node */
1209 ret = o2nm_depend_this_node();
1210 if (ret) {
1211 mlog(ML_ERROR, "Node has been deleted, ret = %d\n", ret);
1212 reg->hr_node_deleted = 1;
1213 wake_up(&o2hb_steady_queue);
1214 return 0;
1215 }
1216
1217 while (!kthread_should_stop() &&
1218 !reg->hr_unclean_stop && !reg->hr_aborted_start) {
1219 /* We track the time spent inside
1220 * o2hb_do_disk_heartbeat so that we avoid more than
1221 * hr_timeout_ms between disk writes. On busy systems
1222 * this should result in a heartbeat which is less
1223 * likely to time itself out. */
1224 before_hb = ktime_get_real();
1225
1226 ret = o2hb_do_disk_heartbeat(reg);
1227 reg->hr_last_hb_status = ret;
1228
1229 after_hb = ktime_get_real();
1230
1231 elapsed_msec = (unsigned int)
1232 ktime_ms_delta(after_hb, before_hb);
1233
1234 mlog(ML_HEARTBEAT,
1235 "start = %lld, end = %lld, msec = %u, ret = %d\n",
1236 before_hb, after_hb, elapsed_msec, ret);
1237
1238 if (!kthread_should_stop() &&
1239 elapsed_msec < reg->hr_timeout_ms) {
1240 /* the kthread api has blocked signals for us so no
1241 * need to record the return value. */
1242 msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
1243 }
1244 }
1245
1246 o2hb_disarm_timeout(reg);
1247
1248 /* unclean stop is only used in very bad situation */
1249 for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
1250 o2hb_shutdown_slot(®->hr_slots[i]);
1251
1252 /* Explicit down notification - avoid forcing the other nodes
1253 * to timeout on this region when we could just as easily
1254 * write a clear generation - thus indicating to them that
1255 * this node has left this region.
1256 */
1257 if (!reg->hr_unclean_stop && !reg->hr_aborted_start) {
1258 o2hb_prepare_block(reg, 0);
1259 ret = o2hb_issue_node_write(reg, &write_wc);
1260 if (ret == 0)
1261 o2hb_wait_on_io(&write_wc);
1262 else
1263 mlog_errno(ret);
1264 }
1265
1266 /* Unpin node */
1267 o2nm_undepend_this_node();
1268
1269 mlog(ML_HEARTBEAT|ML_KTHREAD, "o2hb thread exiting\n");
1270
1271 return 0;
1272 }
1273
1274 #ifdef CONFIG_DEBUG_FS
o2hb_debug_open(struct inode * inode,struct file * file)1275 static int o2hb_debug_open(struct inode *inode, struct file *file)
1276 {
1277 struct o2hb_debug_buf *db = inode->i_private;
1278 struct o2hb_region *reg;
1279 unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1280 unsigned long lts;
1281 char *buf = NULL;
1282 int i = -1;
1283 int out = 0;
1284
1285 /* max_nodes should be the largest bitmap we pass here */
1286 BUG_ON(sizeof(map) < db->db_size);
1287
1288 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1289 if (!buf)
1290 goto bail;
1291
1292 switch (db->db_type) {
1293 case O2HB_DB_TYPE_LIVENODES:
1294 case O2HB_DB_TYPE_LIVEREGIONS:
1295 case O2HB_DB_TYPE_QUORUMREGIONS:
1296 case O2HB_DB_TYPE_FAILEDREGIONS:
1297 spin_lock(&o2hb_live_lock);
1298 memcpy(map, db->db_data, db->db_size);
1299 spin_unlock(&o2hb_live_lock);
1300 break;
1301
1302 case O2HB_DB_TYPE_REGION_LIVENODES:
1303 spin_lock(&o2hb_live_lock);
1304 reg = (struct o2hb_region *)db->db_data;
1305 memcpy(map, reg->hr_live_node_bitmap, db->db_size);
1306 spin_unlock(&o2hb_live_lock);
1307 break;
1308
1309 case O2HB_DB_TYPE_REGION_NUMBER:
1310 reg = (struct o2hb_region *)db->db_data;
1311 out += scnprintf(buf + out, PAGE_SIZE - out, "%d\n",
1312 reg->hr_region_num);
1313 goto done;
1314
1315 case O2HB_DB_TYPE_REGION_ELAPSED_TIME:
1316 reg = (struct o2hb_region *)db->db_data;
1317 lts = reg->hr_last_timeout_start;
1318 /* If 0, it has never been set before */
1319 if (lts)
1320 lts = jiffies_to_msecs(jiffies - lts);
1321 out += scnprintf(buf + out, PAGE_SIZE - out, "%lu\n", lts);
1322 goto done;
1323
1324 case O2HB_DB_TYPE_REGION_PINNED:
1325 reg = (struct o2hb_region *)db->db_data;
1326 out += scnprintf(buf + out, PAGE_SIZE - out, "%u\n",
1327 !!reg->hr_item_pinned);
1328 goto done;
1329
1330 default:
1331 goto done;
1332 }
1333
1334 while ((i = find_next_bit(map, db->db_len, i + 1)) < db->db_len)
1335 out += scnprintf(buf + out, PAGE_SIZE - out, "%d ", i);
1336 out += scnprintf(buf + out, PAGE_SIZE - out, "\n");
1337
1338 done:
1339 i_size_write(inode, out);
1340
1341 file->private_data = buf;
1342
1343 return 0;
1344 bail:
1345 return -ENOMEM;
1346 }
1347
o2hb_debug_release(struct inode * inode,struct file * file)1348 static int o2hb_debug_release(struct inode *inode, struct file *file)
1349 {
1350 kfree(file->private_data);
1351 return 0;
1352 }
1353
o2hb_debug_read(struct file * file,char __user * buf,size_t nbytes,loff_t * ppos)1354 static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1355 size_t nbytes, loff_t *ppos)
1356 {
1357 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
1358 i_size_read(file->f_mapping->host));
1359 }
1360 #else
o2hb_debug_open(struct inode * inode,struct file * file)1361 static int o2hb_debug_open(struct inode *inode, struct file *file)
1362 {
1363 return 0;
1364 }
o2hb_debug_release(struct inode * inode,struct file * file)1365 static int o2hb_debug_release(struct inode *inode, struct file *file)
1366 {
1367 return 0;
1368 }
o2hb_debug_read(struct file * file,char __user * buf,size_t nbytes,loff_t * ppos)1369 static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1370 size_t nbytes, loff_t *ppos)
1371 {
1372 return 0;
1373 }
1374 #endif /* CONFIG_DEBUG_FS */
1375
1376 static const struct file_operations o2hb_debug_fops = {
1377 .open = o2hb_debug_open,
1378 .release = o2hb_debug_release,
1379 .read = o2hb_debug_read,
1380 .llseek = generic_file_llseek,
1381 };
1382
o2hb_exit(void)1383 void o2hb_exit(void)
1384 {
1385 debugfs_remove_recursive(o2hb_debug_dir);
1386 kfree(o2hb_db_livenodes);
1387 kfree(o2hb_db_liveregions);
1388 kfree(o2hb_db_quorumregions);
1389 kfree(o2hb_db_failedregions);
1390 }
1391
o2hb_debug_create(const char * name,struct dentry * dir,struct o2hb_debug_buf ** db,int db_len,int type,int size,int len,void * data)1392 static void o2hb_debug_create(const char *name, struct dentry *dir,
1393 struct o2hb_debug_buf **db, int db_len, int type,
1394 int size, int len, void *data)
1395 {
1396 *db = kmalloc(db_len, GFP_KERNEL);
1397 if (!*db)
1398 return;
1399
1400 (*db)->db_type = type;
1401 (*db)->db_size = size;
1402 (*db)->db_len = len;
1403 (*db)->db_data = data;
1404
1405 debugfs_create_file(name, S_IFREG|S_IRUSR, dir, *db, &o2hb_debug_fops);
1406 }
1407
o2hb_debug_init(void)1408 static void o2hb_debug_init(void)
1409 {
1410 o2hb_debug_dir = debugfs_create_dir(O2HB_DEBUG_DIR, NULL);
1411
1412 o2hb_debug_create(O2HB_DEBUG_LIVENODES, o2hb_debug_dir,
1413 &o2hb_db_livenodes, sizeof(*o2hb_db_livenodes),
1414 O2HB_DB_TYPE_LIVENODES, sizeof(o2hb_live_node_bitmap),
1415 O2NM_MAX_NODES, o2hb_live_node_bitmap);
1416
1417 o2hb_debug_create(O2HB_DEBUG_LIVEREGIONS, o2hb_debug_dir,
1418 &o2hb_db_liveregions, sizeof(*o2hb_db_liveregions),
1419 O2HB_DB_TYPE_LIVEREGIONS,
1420 sizeof(o2hb_live_region_bitmap), O2NM_MAX_REGIONS,
1421 o2hb_live_region_bitmap);
1422
1423 o2hb_debug_create(O2HB_DEBUG_QUORUMREGIONS, o2hb_debug_dir,
1424 &o2hb_db_quorumregions,
1425 sizeof(*o2hb_db_quorumregions),
1426 O2HB_DB_TYPE_QUORUMREGIONS,
1427 sizeof(o2hb_quorum_region_bitmap), O2NM_MAX_REGIONS,
1428 o2hb_quorum_region_bitmap);
1429
1430 o2hb_debug_create(O2HB_DEBUG_FAILEDREGIONS, o2hb_debug_dir,
1431 &o2hb_db_failedregions,
1432 sizeof(*o2hb_db_failedregions),
1433 O2HB_DB_TYPE_FAILEDREGIONS,
1434 sizeof(o2hb_failed_region_bitmap), O2NM_MAX_REGIONS,
1435 o2hb_failed_region_bitmap);
1436 }
1437
o2hb_init(void)1438 void o2hb_init(void)
1439 {
1440 int i;
1441
1442 for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
1443 INIT_LIST_HEAD(&o2hb_callbacks[i].list);
1444
1445 for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
1446 INIT_LIST_HEAD(&o2hb_live_slots[i]);
1447
1448 bitmap_zero(o2hb_live_node_bitmap, O2NM_MAX_NODES);
1449 bitmap_zero(o2hb_region_bitmap, O2NM_MAX_REGIONS);
1450 bitmap_zero(o2hb_live_region_bitmap, O2NM_MAX_REGIONS);
1451 bitmap_zero(o2hb_quorum_region_bitmap, O2NM_MAX_REGIONS);
1452 bitmap_zero(o2hb_failed_region_bitmap, O2NM_MAX_REGIONS);
1453
1454 o2hb_dependent_users = 0;
1455
1456 o2hb_debug_init();
1457 }
1458
1459 /* if we're already in a callback then we're already serialized by the sem */
o2hb_fill_node_map_from_callback(unsigned long * map,unsigned int bits)1460 static void o2hb_fill_node_map_from_callback(unsigned long *map,
1461 unsigned int bits)
1462 {
1463 bitmap_copy(map, o2hb_live_node_bitmap, bits);
1464 }
1465
1466 /*
1467 * get a map of all nodes that are heartbeating in any regions
1468 */
o2hb_fill_node_map(unsigned long * map,unsigned int bits)1469 void o2hb_fill_node_map(unsigned long *map, unsigned int bits)
1470 {
1471 /* callers want to serialize this map and callbacks so that they
1472 * can trust that they don't miss nodes coming to the party */
1473 down_read(&o2hb_callback_sem);
1474 spin_lock(&o2hb_live_lock);
1475 o2hb_fill_node_map_from_callback(map, bits);
1476 spin_unlock(&o2hb_live_lock);
1477 up_read(&o2hb_callback_sem);
1478 }
1479 EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
1480
1481 /*
1482 * heartbeat configfs bits. The heartbeat set is a default set under
1483 * the cluster set in nodemanager.c.
1484 */
1485
to_o2hb_region(struct config_item * item)1486 static struct o2hb_region *to_o2hb_region(struct config_item *item)
1487 {
1488 return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
1489 }
1490
1491 /* drop_item only drops its ref after killing the thread, nothing should
1492 * be using the region anymore. this has to clean up any state that
1493 * attributes might have built up. */
o2hb_region_release(struct config_item * item)1494 static void o2hb_region_release(struct config_item *item)
1495 {
1496 int i;
1497 struct page *page;
1498 struct o2hb_region *reg = to_o2hb_region(item);
1499
1500 mlog(ML_HEARTBEAT, "hb region release (%pg)\n", reg_bdev(reg));
1501
1502 kfree(reg->hr_tmp_block);
1503
1504 if (reg->hr_slot_data) {
1505 for (i = 0; i < reg->hr_num_pages; i++) {
1506 page = reg->hr_slot_data[i];
1507 if (page)
1508 __free_page(page);
1509 }
1510 kfree(reg->hr_slot_data);
1511 }
1512
1513 if (reg->hr_bdev_file)
1514 fput(reg->hr_bdev_file);
1515
1516 kfree(reg->hr_slots);
1517
1518 debugfs_remove_recursive(reg->hr_debug_dir);
1519 kfree(reg->hr_db_livenodes);
1520 kfree(reg->hr_db_regnum);
1521 kfree(reg->hr_db_elapsed_time);
1522 kfree(reg->hr_db_pinned);
1523
1524 spin_lock(&o2hb_live_lock);
1525 list_del(®->hr_all_item);
1526 spin_unlock(&o2hb_live_lock);
1527
1528 o2net_unregister_handler_list(®->hr_handler_list);
1529 kfree(reg);
1530 }
1531
o2hb_read_block_input(struct o2hb_region * reg,const char * page,unsigned long * ret_bytes,unsigned int * ret_bits)1532 static int o2hb_read_block_input(struct o2hb_region *reg,
1533 const char *page,
1534 unsigned long *ret_bytes,
1535 unsigned int *ret_bits)
1536 {
1537 unsigned long bytes;
1538 char *p = (char *)page;
1539 int ret;
1540
1541 ret = kstrtoul(p, 0, &bytes);
1542 if (ret)
1543 return ret;
1544
1545 /* Heartbeat and fs min / max block sizes are the same. */
1546 if (bytes > 4096 || bytes < 512)
1547 return -ERANGE;
1548 if (hweight16(bytes) != 1)
1549 return -EINVAL;
1550
1551 if (ret_bytes)
1552 *ret_bytes = bytes;
1553 if (ret_bits)
1554 *ret_bits = ffs(bytes) - 1;
1555
1556 return 0;
1557 }
1558
o2hb_region_block_bytes_show(struct config_item * item,char * page)1559 static ssize_t o2hb_region_block_bytes_show(struct config_item *item,
1560 char *page)
1561 {
1562 return sprintf(page, "%u\n", to_o2hb_region(item)->hr_block_bytes);
1563 }
1564
o2hb_region_block_bytes_store(struct config_item * item,const char * page,size_t count)1565 static ssize_t o2hb_region_block_bytes_store(struct config_item *item,
1566 const char *page,
1567 size_t count)
1568 {
1569 struct o2hb_region *reg = to_o2hb_region(item);
1570 int status;
1571 unsigned long block_bytes;
1572 unsigned int block_bits;
1573
1574 if (reg->hr_bdev_file)
1575 return -EINVAL;
1576
1577 status = o2hb_read_block_input(reg, page, &block_bytes,
1578 &block_bits);
1579 if (status)
1580 return status;
1581
1582 reg->hr_block_bytes = (unsigned int)block_bytes;
1583 reg->hr_block_bits = block_bits;
1584
1585 return count;
1586 }
1587
o2hb_region_start_block_show(struct config_item * item,char * page)1588 static ssize_t o2hb_region_start_block_show(struct config_item *item,
1589 char *page)
1590 {
1591 return sprintf(page, "%llu\n", to_o2hb_region(item)->hr_start_block);
1592 }
1593
o2hb_region_start_block_store(struct config_item * item,const char * page,size_t count)1594 static ssize_t o2hb_region_start_block_store(struct config_item *item,
1595 const char *page,
1596 size_t count)
1597 {
1598 struct o2hb_region *reg = to_o2hb_region(item);
1599 unsigned long long tmp;
1600 char *p = (char *)page;
1601 ssize_t ret;
1602
1603 if (reg->hr_bdev_file)
1604 return -EINVAL;
1605
1606 ret = kstrtoull(p, 0, &tmp);
1607 if (ret)
1608 return -EINVAL;
1609
1610 reg->hr_start_block = tmp;
1611
1612 return count;
1613 }
1614
o2hb_region_blocks_show(struct config_item * item,char * page)1615 static ssize_t o2hb_region_blocks_show(struct config_item *item, char *page)
1616 {
1617 return sprintf(page, "%d\n", to_o2hb_region(item)->hr_blocks);
1618 }
1619
o2hb_region_blocks_store(struct config_item * item,const char * page,size_t count)1620 static ssize_t o2hb_region_blocks_store(struct config_item *item,
1621 const char *page,
1622 size_t count)
1623 {
1624 struct o2hb_region *reg = to_o2hb_region(item);
1625 unsigned long tmp;
1626 char *p = (char *)page;
1627 int ret;
1628
1629 if (reg->hr_bdev_file)
1630 return -EINVAL;
1631
1632 ret = kstrtoul(p, 0, &tmp);
1633 if (ret)
1634 return ret;
1635
1636 if (tmp > O2NM_MAX_NODES || tmp == 0)
1637 return -ERANGE;
1638
1639 reg->hr_blocks = (unsigned int)tmp;
1640
1641 return count;
1642 }
1643
o2hb_region_dev_show(struct config_item * item,char * page)1644 static ssize_t o2hb_region_dev_show(struct config_item *item, char *page)
1645 {
1646 unsigned int ret = 0;
1647
1648 if (to_o2hb_region(item)->hr_bdev_file)
1649 ret = sprintf(page, "%pg\n", reg_bdev(to_o2hb_region(item)));
1650
1651 return ret;
1652 }
1653
o2hb_init_region_params(struct o2hb_region * reg)1654 static void o2hb_init_region_params(struct o2hb_region *reg)
1655 {
1656 reg->hr_slots_per_page = PAGE_SIZE >> reg->hr_block_bits;
1657 reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
1658
1659 mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
1660 reg->hr_start_block, reg->hr_blocks);
1661 mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
1662 reg->hr_block_bytes, reg->hr_block_bits);
1663 mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
1664 mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
1665 }
1666
o2hb_map_slot_data(struct o2hb_region * reg)1667 static int o2hb_map_slot_data(struct o2hb_region *reg)
1668 {
1669 int i, j;
1670 unsigned int last_slot;
1671 unsigned int spp = reg->hr_slots_per_page;
1672 struct page *page;
1673 char *raw;
1674 struct o2hb_disk_slot *slot;
1675
1676 reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
1677 if (reg->hr_tmp_block == NULL)
1678 return -ENOMEM;
1679
1680 reg->hr_slots = kcalloc(reg->hr_blocks,
1681 sizeof(struct o2hb_disk_slot), GFP_KERNEL);
1682 if (reg->hr_slots == NULL)
1683 return -ENOMEM;
1684
1685 for(i = 0; i < reg->hr_blocks; i++) {
1686 slot = ®->hr_slots[i];
1687 slot->ds_node_num = i;
1688 INIT_LIST_HEAD(&slot->ds_live_item);
1689 slot->ds_raw_block = NULL;
1690 }
1691
1692 reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
1693 mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
1694 "at %u blocks per page\n",
1695 reg->hr_num_pages, reg->hr_blocks, spp);
1696
1697 reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
1698 GFP_KERNEL);
1699 if (!reg->hr_slot_data)
1700 return -ENOMEM;
1701
1702 for(i = 0; i < reg->hr_num_pages; i++) {
1703 page = alloc_page(GFP_KERNEL);
1704 if (!page)
1705 return -ENOMEM;
1706
1707 reg->hr_slot_data[i] = page;
1708
1709 last_slot = i * spp;
1710 raw = page_address(page);
1711 for (j = 0;
1712 (j < spp) && ((j + last_slot) < reg->hr_blocks);
1713 j++) {
1714 BUG_ON((j + last_slot) >= reg->hr_blocks);
1715
1716 slot = ®->hr_slots[j + last_slot];
1717 slot->ds_raw_block =
1718 (struct o2hb_disk_heartbeat_block *) raw;
1719
1720 raw += reg->hr_block_bytes;
1721 }
1722 }
1723
1724 return 0;
1725 }
1726
1727 /* Read in all the slots available and populate the tracking
1728 * structures so that we can start with a baseline idea of what's
1729 * there. */
o2hb_populate_slot_data(struct o2hb_region * reg)1730 static int o2hb_populate_slot_data(struct o2hb_region *reg)
1731 {
1732 int ret, i;
1733 struct o2hb_disk_slot *slot;
1734 struct o2hb_disk_heartbeat_block *hb_block;
1735
1736 ret = o2hb_read_slots(reg, 0, reg->hr_blocks);
1737 if (ret)
1738 goto out;
1739
1740 /* We only want to get an idea of the values initially in each
1741 * slot, so we do no verification - o2hb_check_slot will
1742 * actually determine if each configured slot is valid and
1743 * whether any values have changed. */
1744 for(i = 0; i < reg->hr_blocks; i++) {
1745 slot = ®->hr_slots[i];
1746 hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
1747
1748 /* Only fill the values that o2hb_check_slot uses to
1749 * determine changing slots */
1750 slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
1751 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
1752 }
1753
1754 out:
1755 return ret;
1756 }
1757
1758 /*
1759 * this is acting as commit; we set up all of hr_bdev_file and hr_task or
1760 * nothing
1761 */
o2hb_region_dev_store(struct config_item * item,const char * page,size_t count)1762 static ssize_t o2hb_region_dev_store(struct config_item *item,
1763 const char *page,
1764 size_t count)
1765 {
1766 struct o2hb_region *reg = to_o2hb_region(item);
1767 struct task_struct *hb_task;
1768 long fd;
1769 int sectsize;
1770 char *p = (char *)page;
1771 ssize_t ret = -EINVAL;
1772 int live_threshold;
1773
1774 if (reg->hr_bdev_file)
1775 return -EINVAL;
1776
1777 /* We can't heartbeat without having had our node number
1778 * configured yet. */
1779 if (o2nm_this_node() == O2NM_MAX_NODES)
1780 return -EINVAL;
1781
1782 ret = kstrtol(p, 0, &fd);
1783 if (ret < 0)
1784 return -EINVAL;
1785
1786 if (fd < 0 || fd >= INT_MAX)
1787 return -EINVAL;
1788
1789 CLASS(fd, f)(fd);
1790 if (fd_empty(f))
1791 return -EINVAL;
1792
1793 if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
1794 reg->hr_block_bytes == 0)
1795 return -EINVAL;
1796
1797 if (!S_ISBLK(fd_file(f)->f_mapping->host->i_mode))
1798 return -EINVAL;
1799
1800 reg->hr_bdev_file = bdev_file_open_by_dev(fd_file(f)->f_mapping->host->i_rdev,
1801 BLK_OPEN_WRITE | BLK_OPEN_READ, NULL, NULL);
1802 if (IS_ERR(reg->hr_bdev_file)) {
1803 ret = PTR_ERR(reg->hr_bdev_file);
1804 reg->hr_bdev_file = NULL;
1805 return ret;
1806 }
1807
1808 sectsize = bdev_logical_block_size(reg_bdev(reg));
1809 if (sectsize != reg->hr_block_bytes) {
1810 mlog(ML_ERROR,
1811 "blocksize %u incorrect for device, expected %d",
1812 reg->hr_block_bytes, sectsize);
1813 ret = -EINVAL;
1814 goto out3;
1815 }
1816
1817 o2hb_init_region_params(reg);
1818
1819 /* Generation of zero is invalid */
1820 do {
1821 get_random_bytes(®->hr_generation,
1822 sizeof(reg->hr_generation));
1823 } while (reg->hr_generation == 0);
1824
1825 ret = o2hb_map_slot_data(reg);
1826 if (ret) {
1827 mlog_errno(ret);
1828 goto out3;
1829 }
1830
1831 ret = o2hb_populate_slot_data(reg);
1832 if (ret) {
1833 mlog_errno(ret);
1834 goto out3;
1835 }
1836
1837 INIT_DELAYED_WORK(®->hr_write_timeout_work, o2hb_write_timeout);
1838 INIT_DELAYED_WORK(®->hr_nego_timeout_work, o2hb_nego_timeout);
1839
1840 /*
1841 * A node is considered live after it has beat LIVE_THRESHOLD
1842 * times. We're not steady until we've given them a chance
1843 * _after_ our first read.
1844 * The default threshold is bare minimum so as to limit the delay
1845 * during mounts. For global heartbeat, the threshold doubled for the
1846 * first region.
1847 */
1848 live_threshold = O2HB_LIVE_THRESHOLD;
1849 if (o2hb_global_heartbeat_active()) {
1850 spin_lock(&o2hb_live_lock);
1851 if (bitmap_weight(o2hb_region_bitmap, O2NM_MAX_REGIONS) == 1)
1852 live_threshold <<= 1;
1853 spin_unlock(&o2hb_live_lock);
1854 }
1855 ++live_threshold;
1856 atomic_set(®->hr_steady_iterations, live_threshold);
1857 /* unsteady_iterations is triple the steady_iterations */
1858 atomic_set(®->hr_unsteady_iterations, (live_threshold * 3));
1859
1860 hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
1861 reg->hr_item.ci_name);
1862 if (IS_ERR(hb_task)) {
1863 ret = PTR_ERR(hb_task);
1864 mlog_errno(ret);
1865 goto out3;
1866 }
1867
1868 spin_lock(&o2hb_live_lock);
1869 reg->hr_task = hb_task;
1870 spin_unlock(&o2hb_live_lock);
1871
1872 ret = wait_event_interruptible(o2hb_steady_queue,
1873 atomic_read(®->hr_steady_iterations) == 0 ||
1874 reg->hr_node_deleted);
1875 if (ret) {
1876 atomic_set(®->hr_steady_iterations, 0);
1877 reg->hr_aborted_start = 1;
1878 }
1879
1880 if (reg->hr_aborted_start) {
1881 ret = -EIO;
1882 goto out3;
1883 }
1884
1885 if (reg->hr_node_deleted) {
1886 ret = -EINVAL;
1887 goto out3;
1888 }
1889
1890 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1891 spin_lock(&o2hb_live_lock);
1892 hb_task = reg->hr_task;
1893 if (o2hb_global_heartbeat_active())
1894 set_bit(reg->hr_region_num, o2hb_live_region_bitmap);
1895 spin_unlock(&o2hb_live_lock);
1896
1897 if (hb_task)
1898 ret = count;
1899 else
1900 ret = -EIO;
1901
1902 if (hb_task && o2hb_global_heartbeat_active())
1903 printk(KERN_NOTICE "o2hb: Heartbeat started on region %s (%pg)\n",
1904 config_item_name(®->hr_item), reg_bdev(reg));
1905
1906 out3:
1907 if (ret < 0) {
1908 fput(reg->hr_bdev_file);
1909 reg->hr_bdev_file = NULL;
1910 }
1911 return ret;
1912 }
1913
o2hb_region_pid_show(struct config_item * item,char * page)1914 static ssize_t o2hb_region_pid_show(struct config_item *item, char *page)
1915 {
1916 struct o2hb_region *reg = to_o2hb_region(item);
1917 pid_t pid = 0;
1918
1919 spin_lock(&o2hb_live_lock);
1920 if (reg->hr_task)
1921 pid = task_pid_nr(reg->hr_task);
1922 spin_unlock(&o2hb_live_lock);
1923
1924 if (!pid)
1925 return 0;
1926
1927 return sprintf(page, "%u\n", pid);
1928 }
1929
1930 CONFIGFS_ATTR(o2hb_region_, block_bytes);
1931 CONFIGFS_ATTR(o2hb_region_, start_block);
1932 CONFIGFS_ATTR(o2hb_region_, blocks);
1933 CONFIGFS_ATTR(o2hb_region_, dev);
1934 CONFIGFS_ATTR_RO(o2hb_region_, pid);
1935
1936 static struct configfs_attribute *o2hb_region_attrs[] = {
1937 &o2hb_region_attr_block_bytes,
1938 &o2hb_region_attr_start_block,
1939 &o2hb_region_attr_blocks,
1940 &o2hb_region_attr_dev,
1941 &o2hb_region_attr_pid,
1942 NULL,
1943 };
1944
1945 static struct configfs_item_operations o2hb_region_item_ops = {
1946 .release = o2hb_region_release,
1947 };
1948
1949 static const struct config_item_type o2hb_region_type = {
1950 .ct_item_ops = &o2hb_region_item_ops,
1951 .ct_attrs = o2hb_region_attrs,
1952 .ct_owner = THIS_MODULE,
1953 };
1954
1955 /* heartbeat set */
1956
1957 struct o2hb_heartbeat_group {
1958 struct config_group hs_group;
1959 /* some stuff? */
1960 };
1961
to_o2hb_heartbeat_group(struct config_group * group)1962 static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
1963 {
1964 return group ?
1965 container_of(group, struct o2hb_heartbeat_group, hs_group)
1966 : NULL;
1967 }
1968
o2hb_debug_region_init(struct o2hb_region * reg,struct dentry * parent)1969 static void o2hb_debug_region_init(struct o2hb_region *reg,
1970 struct dentry *parent)
1971 {
1972 struct dentry *dir;
1973
1974 dir = debugfs_create_dir(config_item_name(®->hr_item), parent);
1975 reg->hr_debug_dir = dir;
1976
1977 o2hb_debug_create(O2HB_DEBUG_LIVENODES, dir, &(reg->hr_db_livenodes),
1978 sizeof(*(reg->hr_db_livenodes)),
1979 O2HB_DB_TYPE_REGION_LIVENODES,
1980 sizeof(reg->hr_live_node_bitmap), O2NM_MAX_NODES,
1981 reg);
1982
1983 o2hb_debug_create(O2HB_DEBUG_REGION_NUMBER, dir, &(reg->hr_db_regnum),
1984 sizeof(*(reg->hr_db_regnum)),
1985 O2HB_DB_TYPE_REGION_NUMBER, 0, O2NM_MAX_NODES, reg);
1986
1987 o2hb_debug_create(O2HB_DEBUG_REGION_ELAPSED_TIME, dir,
1988 &(reg->hr_db_elapsed_time),
1989 sizeof(*(reg->hr_db_elapsed_time)),
1990 O2HB_DB_TYPE_REGION_ELAPSED_TIME, 0, 0, reg);
1991
1992 o2hb_debug_create(O2HB_DEBUG_REGION_PINNED, dir, &(reg->hr_db_pinned),
1993 sizeof(*(reg->hr_db_pinned)),
1994 O2HB_DB_TYPE_REGION_PINNED, 0, 0, reg);
1995
1996 }
1997
o2hb_heartbeat_group_make_item(struct config_group * group,const char * name)1998 static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
1999 const char *name)
2000 {
2001 struct o2hb_region *reg = NULL;
2002 int ret;
2003
2004 reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
2005 if (reg == NULL)
2006 return ERR_PTR(-ENOMEM);
2007
2008 if (strlen(name) > O2HB_MAX_REGION_NAME_LEN) {
2009 ret = -ENAMETOOLONG;
2010 goto free;
2011 }
2012
2013 spin_lock(&o2hb_live_lock);
2014 reg->hr_region_num = 0;
2015 if (o2hb_global_heartbeat_active()) {
2016 reg->hr_region_num = find_first_zero_bit(o2hb_region_bitmap,
2017 O2NM_MAX_REGIONS);
2018 if (reg->hr_region_num >= O2NM_MAX_REGIONS) {
2019 spin_unlock(&o2hb_live_lock);
2020 ret = -EFBIG;
2021 goto free;
2022 }
2023 set_bit(reg->hr_region_num, o2hb_region_bitmap);
2024 }
2025 list_add_tail(®->hr_all_item, &o2hb_all_regions);
2026 spin_unlock(&o2hb_live_lock);
2027
2028 config_item_init_type_name(®->hr_item, name, &o2hb_region_type);
2029
2030 /* this is the same way to generate msg key as dlm, for local heartbeat,
2031 * name is also the same, so make initial crc value different to avoid
2032 * message key conflict.
2033 */
2034 reg->hr_key = crc32_le(reg->hr_region_num + O2NM_MAX_REGIONS,
2035 name, strlen(name));
2036 INIT_LIST_HEAD(®->hr_handler_list);
2037 ret = o2net_register_handler(O2HB_NEGO_TIMEOUT_MSG, reg->hr_key,
2038 sizeof(struct o2hb_nego_msg),
2039 o2hb_nego_timeout_handler,
2040 reg, NULL, ®->hr_handler_list);
2041 if (ret)
2042 goto remove_item;
2043
2044 ret = o2net_register_handler(O2HB_NEGO_APPROVE_MSG, reg->hr_key,
2045 sizeof(struct o2hb_nego_msg),
2046 o2hb_nego_approve_handler,
2047 reg, NULL, ®->hr_handler_list);
2048 if (ret)
2049 goto unregister_handler;
2050
2051 o2hb_debug_region_init(reg, o2hb_debug_dir);
2052
2053 return ®->hr_item;
2054
2055 unregister_handler:
2056 o2net_unregister_handler_list(®->hr_handler_list);
2057 remove_item:
2058 spin_lock(&o2hb_live_lock);
2059 list_del(®->hr_all_item);
2060 if (o2hb_global_heartbeat_active())
2061 clear_bit(reg->hr_region_num, o2hb_region_bitmap);
2062 spin_unlock(&o2hb_live_lock);
2063 free:
2064 kfree(reg);
2065 return ERR_PTR(ret);
2066 }
2067
o2hb_heartbeat_group_drop_item(struct config_group * group,struct config_item * item)2068 static void o2hb_heartbeat_group_drop_item(struct config_group *group,
2069 struct config_item *item)
2070 {
2071 struct task_struct *hb_task;
2072 struct o2hb_region *reg = to_o2hb_region(item);
2073 int quorum_region = 0;
2074
2075 /* stop the thread when the user removes the region dir */
2076 spin_lock(&o2hb_live_lock);
2077 hb_task = reg->hr_task;
2078 reg->hr_task = NULL;
2079 reg->hr_item_dropped = 1;
2080 spin_unlock(&o2hb_live_lock);
2081
2082 if (hb_task)
2083 kthread_stop(hb_task);
2084
2085 if (o2hb_global_heartbeat_active()) {
2086 spin_lock(&o2hb_live_lock);
2087 clear_bit(reg->hr_region_num, o2hb_region_bitmap);
2088 clear_bit(reg->hr_region_num, o2hb_live_region_bitmap);
2089 if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
2090 quorum_region = 1;
2091 clear_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
2092 spin_unlock(&o2hb_live_lock);
2093 printk(KERN_NOTICE "o2hb: Heartbeat %s on region %s (%pg)\n",
2094 ((atomic_read(®->hr_steady_iterations) == 0) ?
2095 "stopped" : "start aborted"), config_item_name(item),
2096 reg_bdev(reg));
2097 }
2098
2099 /*
2100 * If we're racing a dev_write(), we need to wake them. They will
2101 * check reg->hr_task
2102 */
2103 if (atomic_read(®->hr_steady_iterations) != 0) {
2104 reg->hr_aborted_start = 1;
2105 atomic_set(®->hr_steady_iterations, 0);
2106 wake_up(&o2hb_steady_queue);
2107 }
2108
2109 config_item_put(item);
2110
2111 if (!o2hb_global_heartbeat_active() || !quorum_region)
2112 return;
2113
2114 /*
2115 * If global heartbeat active and there are dependent users,
2116 * pin all regions if quorum region count <= CUT_OFF
2117 */
2118 spin_lock(&o2hb_live_lock);
2119
2120 if (!o2hb_dependent_users)
2121 goto unlock;
2122
2123 if (bitmap_weight(o2hb_quorum_region_bitmap,
2124 O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
2125 o2hb_region_pin(NULL);
2126
2127 unlock:
2128 spin_unlock(&o2hb_live_lock);
2129 }
2130
o2hb_heartbeat_group_dead_threshold_show(struct config_item * item,char * page)2131 static ssize_t o2hb_heartbeat_group_dead_threshold_show(struct config_item *item,
2132 char *page)
2133 {
2134 return sprintf(page, "%u\n", o2hb_dead_threshold);
2135 }
2136
o2hb_heartbeat_group_dead_threshold_store(struct config_item * item,const char * page,size_t count)2137 static ssize_t o2hb_heartbeat_group_dead_threshold_store(struct config_item *item,
2138 const char *page, size_t count)
2139 {
2140 unsigned long tmp;
2141 char *p = (char *)page;
2142 int ret;
2143
2144 ret = kstrtoul(p, 10, &tmp);
2145 if (ret)
2146 return ret;
2147
2148 /* this will validate ranges for us. */
2149 o2hb_dead_threshold_set((unsigned int) tmp);
2150
2151 return count;
2152 }
2153
o2hb_heartbeat_group_mode_show(struct config_item * item,char * page)2154 static ssize_t o2hb_heartbeat_group_mode_show(struct config_item *item,
2155 char *page)
2156 {
2157 return sprintf(page, "%s\n",
2158 o2hb_heartbeat_mode_desc[o2hb_heartbeat_mode]);
2159 }
2160
o2hb_heartbeat_group_mode_store(struct config_item * item,const char * page,size_t count)2161 static ssize_t o2hb_heartbeat_group_mode_store(struct config_item *item,
2162 const char *page, size_t count)
2163 {
2164 unsigned int i;
2165 int ret;
2166 size_t len;
2167
2168 len = (page[count - 1] == '\n') ? count - 1 : count;
2169 if (!len)
2170 return -EINVAL;
2171
2172 for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) {
2173 if (strncasecmp(page, o2hb_heartbeat_mode_desc[i], len))
2174 continue;
2175
2176 ret = o2hb_global_heartbeat_mode_set(i);
2177 if (!ret)
2178 printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
2179 o2hb_heartbeat_mode_desc[i]);
2180 return count;
2181 }
2182
2183 return -EINVAL;
2184
2185 }
2186
2187 CONFIGFS_ATTR(o2hb_heartbeat_group_, dead_threshold);
2188 CONFIGFS_ATTR(o2hb_heartbeat_group_, mode);
2189
2190 static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
2191 &o2hb_heartbeat_group_attr_dead_threshold,
2192 &o2hb_heartbeat_group_attr_mode,
2193 NULL,
2194 };
2195
2196 static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
2197 .make_item = o2hb_heartbeat_group_make_item,
2198 .drop_item = o2hb_heartbeat_group_drop_item,
2199 };
2200
2201 static const struct config_item_type o2hb_heartbeat_group_type = {
2202 .ct_group_ops = &o2hb_heartbeat_group_group_ops,
2203 .ct_attrs = o2hb_heartbeat_group_attrs,
2204 .ct_owner = THIS_MODULE,
2205 };
2206
2207 /* this is just here to avoid touching group in heartbeat.h which the
2208 * entire damn world #includes */
o2hb_alloc_hb_set(void)2209 struct config_group *o2hb_alloc_hb_set(void)
2210 {
2211 struct o2hb_heartbeat_group *hs = NULL;
2212 struct config_group *ret = NULL;
2213
2214 hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
2215 if (hs == NULL)
2216 goto out;
2217
2218 config_group_init_type_name(&hs->hs_group, "heartbeat",
2219 &o2hb_heartbeat_group_type);
2220
2221 ret = &hs->hs_group;
2222 out:
2223 if (ret == NULL)
2224 kfree(hs);
2225 return ret;
2226 }
2227
o2hb_free_hb_set(struct config_group * group)2228 void o2hb_free_hb_set(struct config_group *group)
2229 {
2230 struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
2231 kfree(hs);
2232 }
2233
2234 /* hb callback registration and issuing */
2235
hbcall_from_type(enum o2hb_callback_type type)2236 static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
2237 {
2238 if (type == O2HB_NUM_CB)
2239 return ERR_PTR(-EINVAL);
2240
2241 return &o2hb_callbacks[type];
2242 }
2243
o2hb_setup_callback(struct o2hb_callback_func * hc,enum o2hb_callback_type type,o2hb_cb_func * func,void * data,int priority)2244 void o2hb_setup_callback(struct o2hb_callback_func *hc,
2245 enum o2hb_callback_type type,
2246 o2hb_cb_func *func,
2247 void *data,
2248 int priority)
2249 {
2250 INIT_LIST_HEAD(&hc->hc_item);
2251 hc->hc_func = func;
2252 hc->hc_data = data;
2253 hc->hc_priority = priority;
2254 hc->hc_type = type;
2255 hc->hc_magic = O2HB_CB_MAGIC;
2256 }
2257 EXPORT_SYMBOL_GPL(o2hb_setup_callback);
2258
2259 /*
2260 * In local heartbeat mode, region_uuid passed matches the dlm domain name.
2261 * In global heartbeat mode, region_uuid passed is NULL.
2262 *
2263 * In local, we only pin the matching region. In global we pin all the active
2264 * regions.
2265 */
o2hb_region_pin(const char * region_uuid)2266 static int o2hb_region_pin(const char *region_uuid)
2267 {
2268 int ret = 0, found = 0;
2269 struct o2hb_region *reg;
2270 char *uuid;
2271
2272 assert_spin_locked(&o2hb_live_lock);
2273
2274 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2275 if (reg->hr_item_dropped)
2276 continue;
2277
2278 uuid = config_item_name(®->hr_item);
2279
2280 /* local heartbeat */
2281 if (region_uuid) {
2282 if (strcmp(region_uuid, uuid))
2283 continue;
2284 found = 1;
2285 }
2286
2287 if (reg->hr_item_pinned || reg->hr_item_dropped)
2288 goto skip_pin;
2289
2290 /* Ignore ENOENT only for local hb (userdlm domain) */
2291 ret = o2nm_depend_item(®->hr_item);
2292 if (!ret) {
2293 mlog(ML_CLUSTER, "Pin region %s\n", uuid);
2294 reg->hr_item_pinned = 1;
2295 } else {
2296 if (ret == -ENOENT && found)
2297 ret = 0;
2298 else {
2299 mlog(ML_ERROR, "Pin region %s fails with %d\n",
2300 uuid, ret);
2301 break;
2302 }
2303 }
2304 skip_pin:
2305 if (found)
2306 break;
2307 }
2308
2309 return ret;
2310 }
2311
2312 /*
2313 * In local heartbeat mode, region_uuid passed matches the dlm domain name.
2314 * In global heartbeat mode, region_uuid passed is NULL.
2315 *
2316 * In local, we only unpin the matching region. In global we unpin all the
2317 * active regions.
2318 */
o2hb_region_unpin(const char * region_uuid)2319 static void o2hb_region_unpin(const char *region_uuid)
2320 {
2321 struct o2hb_region *reg;
2322 char *uuid;
2323 int found = 0;
2324
2325 assert_spin_locked(&o2hb_live_lock);
2326
2327 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2328 if (reg->hr_item_dropped)
2329 continue;
2330
2331 uuid = config_item_name(®->hr_item);
2332 if (region_uuid) {
2333 if (strcmp(region_uuid, uuid))
2334 continue;
2335 found = 1;
2336 }
2337
2338 if (reg->hr_item_pinned) {
2339 mlog(ML_CLUSTER, "Unpin region %s\n", uuid);
2340 o2nm_undepend_item(®->hr_item);
2341 reg->hr_item_pinned = 0;
2342 }
2343 if (found)
2344 break;
2345 }
2346 }
2347
o2hb_region_inc_user(const char * region_uuid)2348 static int o2hb_region_inc_user(const char *region_uuid)
2349 {
2350 int ret = 0;
2351
2352 spin_lock(&o2hb_live_lock);
2353
2354 /* local heartbeat */
2355 if (!o2hb_global_heartbeat_active()) {
2356 ret = o2hb_region_pin(region_uuid);
2357 goto unlock;
2358 }
2359
2360 /*
2361 * if global heartbeat active and this is the first dependent user,
2362 * pin all regions if quorum region count <= CUT_OFF
2363 */
2364 o2hb_dependent_users++;
2365 if (o2hb_dependent_users > 1)
2366 goto unlock;
2367
2368 if (bitmap_weight(o2hb_quorum_region_bitmap,
2369 O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
2370 ret = o2hb_region_pin(NULL);
2371
2372 unlock:
2373 spin_unlock(&o2hb_live_lock);
2374 return ret;
2375 }
2376
o2hb_region_dec_user(const char * region_uuid)2377 static void o2hb_region_dec_user(const char *region_uuid)
2378 {
2379 spin_lock(&o2hb_live_lock);
2380
2381 /* local heartbeat */
2382 if (!o2hb_global_heartbeat_active()) {
2383 o2hb_region_unpin(region_uuid);
2384 goto unlock;
2385 }
2386
2387 /*
2388 * if global heartbeat active and there are no dependent users,
2389 * unpin all quorum regions
2390 */
2391 o2hb_dependent_users--;
2392 if (!o2hb_dependent_users)
2393 o2hb_region_unpin(NULL);
2394
2395 unlock:
2396 spin_unlock(&o2hb_live_lock);
2397 }
2398
o2hb_register_callback(const char * region_uuid,struct o2hb_callback_func * hc)2399 int o2hb_register_callback(const char *region_uuid,
2400 struct o2hb_callback_func *hc)
2401 {
2402 struct o2hb_callback_func *f;
2403 struct o2hb_callback *hbcall;
2404 int ret;
2405
2406 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2407 BUG_ON(!list_empty(&hc->hc_item));
2408
2409 hbcall = hbcall_from_type(hc->hc_type);
2410 if (IS_ERR(hbcall)) {
2411 ret = PTR_ERR(hbcall);
2412 goto out;
2413 }
2414
2415 if (region_uuid) {
2416 ret = o2hb_region_inc_user(region_uuid);
2417 if (ret) {
2418 mlog_errno(ret);
2419 goto out;
2420 }
2421 }
2422
2423 down_write(&o2hb_callback_sem);
2424
2425 list_for_each_entry(f, &hbcall->list, hc_item) {
2426 if (hc->hc_priority < f->hc_priority) {
2427 list_add_tail(&hc->hc_item, &f->hc_item);
2428 break;
2429 }
2430 }
2431 if (list_empty(&hc->hc_item))
2432 list_add_tail(&hc->hc_item, &hbcall->list);
2433
2434 up_write(&o2hb_callback_sem);
2435 ret = 0;
2436 out:
2437 mlog(ML_CLUSTER, "returning %d on behalf of %p for funcs %p\n",
2438 ret, __builtin_return_address(0), hc);
2439 return ret;
2440 }
2441 EXPORT_SYMBOL_GPL(o2hb_register_callback);
2442
o2hb_unregister_callback(const char * region_uuid,struct o2hb_callback_func * hc)2443 void o2hb_unregister_callback(const char *region_uuid,
2444 struct o2hb_callback_func *hc)
2445 {
2446 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2447
2448 mlog(ML_CLUSTER, "on behalf of %p for funcs %p\n",
2449 __builtin_return_address(0), hc);
2450
2451 /* XXX Can this happen _with_ a region reference? */
2452 if (list_empty(&hc->hc_item))
2453 return;
2454
2455 if (region_uuid)
2456 o2hb_region_dec_user(region_uuid);
2457
2458 down_write(&o2hb_callback_sem);
2459
2460 list_del_init(&hc->hc_item);
2461
2462 up_write(&o2hb_callback_sem);
2463 }
2464 EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
2465
o2hb_check_node_heartbeating_no_sem(u8 node_num)2466 int o2hb_check_node_heartbeating_no_sem(u8 node_num)
2467 {
2468 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2469
2470 spin_lock(&o2hb_live_lock);
2471 o2hb_fill_node_map_from_callback(testing_map, O2NM_MAX_NODES);
2472 spin_unlock(&o2hb_live_lock);
2473 if (!test_bit(node_num, testing_map)) {
2474 mlog(ML_HEARTBEAT,
2475 "node (%u) does not have heartbeating enabled.\n",
2476 node_num);
2477 return 0;
2478 }
2479
2480 return 1;
2481 }
2482 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_no_sem);
2483
o2hb_check_node_heartbeating_from_callback(u8 node_num)2484 int o2hb_check_node_heartbeating_from_callback(u8 node_num)
2485 {
2486 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2487
2488 o2hb_fill_node_map_from_callback(testing_map, O2NM_MAX_NODES);
2489 if (!test_bit(node_num, testing_map)) {
2490 mlog(ML_HEARTBEAT,
2491 "node (%u) does not have heartbeating enabled.\n",
2492 node_num);
2493 return 0;
2494 }
2495
2496 return 1;
2497 }
2498 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
2499
2500 /*
2501 * this is just a hack until we get the plumbing which flips file systems
2502 * read only and drops the hb ref instead of killing the node dead.
2503 */
o2hb_stop_all_regions(void)2504 void o2hb_stop_all_regions(void)
2505 {
2506 struct o2hb_region *reg;
2507
2508 mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
2509
2510 spin_lock(&o2hb_live_lock);
2511
2512 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
2513 reg->hr_unclean_stop = 1;
2514
2515 spin_unlock(&o2hb_live_lock);
2516 }
2517 EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);
2518
o2hb_get_all_regions(char * region_uuids,u8 max_regions)2519 int o2hb_get_all_regions(char *region_uuids, u8 max_regions)
2520 {
2521 struct o2hb_region *reg;
2522 int numregs = 0;
2523 char *p;
2524
2525 spin_lock(&o2hb_live_lock);
2526
2527 p = region_uuids;
2528 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2529 if (reg->hr_item_dropped)
2530 continue;
2531
2532 mlog(0, "Region: %s\n", config_item_name(®->hr_item));
2533 if (numregs < max_regions) {
2534 memcpy(p, config_item_name(®->hr_item),
2535 O2HB_MAX_REGION_NAME_LEN);
2536 p += O2HB_MAX_REGION_NAME_LEN;
2537 }
2538 numregs++;
2539 }
2540
2541 spin_unlock(&o2hb_live_lock);
2542
2543 return numregs;
2544 }
2545 EXPORT_SYMBOL_GPL(o2hb_get_all_regions);
2546
o2hb_global_heartbeat_active(void)2547 int o2hb_global_heartbeat_active(void)
2548 {
2549 return (o2hb_heartbeat_mode == O2HB_HEARTBEAT_GLOBAL);
2550 }
2551 EXPORT_SYMBOL(o2hb_global_heartbeat_active);
2552