1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * Copyright (C) 2004, 2005 Oracle. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/jiffies.h>
25 #include <linux/module.h>
27 #include <linux/bio.h>
28 #include <linux/blkdev.h>
29 #include <linux/delay.h>
30 #include <linux/file.h>
31 #include <linux/kthread.h>
32 #include <linux/configfs.h>
33 #include <linux/random.h>
34 #include <linux/crc32.h>
35 #include <linux/time.h>
36 #include <linux/debugfs.h>
37 #include <linux/slab.h>
39 #include "heartbeat.h"
41 #include "nodemanager.h"
48 * The first heartbeat pass had one global thread that would serialize all hb
49 * callback calls. This global serializing sem should only be removed once
50 * we've made sure that all callees can deal with being called concurrently
51 * from multiple hb region threads.
53 static DECLARE_RWSEM(o2hb_callback_sem);
56 * multiple hb threads are watching multiple regions. A node is live
57 * whenever any of the threads sees activity from the node in its region.
59 static DEFINE_SPINLOCK(o2hb_live_lock);
60 static struct list_head o2hb_live_slots[O2NM_MAX_NODES];
61 static unsigned long o2hb_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
62 static LIST_HEAD(o2hb_node_events);
63 static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue);
66 * In global heartbeat, we maintain a series of region bitmaps.
67 * - o2hb_region_bitmap allows us to limit the region number to max region.
68 * - o2hb_live_region_bitmap tracks live regions (seen steady iterations).
69 * - o2hb_quorum_region_bitmap tracks live regions that have seen all nodes
71 * - o2hb_failed_region_bitmap tracks the regions that have seen io timeouts.
73 static unsigned long o2hb_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
74 static unsigned long o2hb_live_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
75 static unsigned long o2hb_quorum_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
76 static unsigned long o2hb_failed_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
78 #define O2HB_DB_TYPE_LIVENODES 0
79 #define O2HB_DB_TYPE_LIVEREGIONS 1
80 #define O2HB_DB_TYPE_QUORUMREGIONS 2
81 #define O2HB_DB_TYPE_FAILEDREGIONS 3
82 #define O2HB_DB_TYPE_REGION_LIVENODES 4
83 #define O2HB_DB_TYPE_REGION_NUMBER 5
84 #define O2HB_DB_TYPE_REGION_ELAPSED_TIME 6
85 struct o2hb_debug_buf {
92 static struct o2hb_debug_buf *o2hb_db_livenodes;
93 static struct o2hb_debug_buf *o2hb_db_liveregions;
94 static struct o2hb_debug_buf *o2hb_db_quorumregions;
95 static struct o2hb_debug_buf *o2hb_db_failedregions;
97 #define O2HB_DEBUG_DIR "o2hb"
98 #define O2HB_DEBUG_LIVENODES "livenodes"
99 #define O2HB_DEBUG_LIVEREGIONS "live_regions"
100 #define O2HB_DEBUG_QUORUMREGIONS "quorum_regions"
101 #define O2HB_DEBUG_FAILEDREGIONS "failed_regions"
102 #define O2HB_DEBUG_REGION_NUMBER "num"
103 #define O2HB_DEBUG_REGION_ELAPSED_TIME "elapsed_time_in_ms"
105 static struct dentry *o2hb_debug_dir;
106 static struct dentry *o2hb_debug_livenodes;
107 static struct dentry *o2hb_debug_liveregions;
108 static struct dentry *o2hb_debug_quorumregions;
109 static struct dentry *o2hb_debug_failedregions;
111 static LIST_HEAD(o2hb_all_regions);
113 static struct o2hb_callback {
114 struct list_head list;
115 } o2hb_callbacks[O2HB_NUM_CB];
117 static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type);
119 #define O2HB_DEFAULT_BLOCK_BITS 9
121 enum o2hb_heartbeat_modes {
122 O2HB_HEARTBEAT_LOCAL = 0,
123 O2HB_HEARTBEAT_GLOBAL,
124 O2HB_HEARTBEAT_NUM_MODES,
127 char *o2hb_heartbeat_mode_desc[O2HB_HEARTBEAT_NUM_MODES] = {
128 "local", /* O2HB_HEARTBEAT_LOCAL */
129 "global", /* O2HB_HEARTBEAT_GLOBAL */
132 unsigned int o2hb_dead_threshold = O2HB_DEFAULT_DEAD_THRESHOLD;
133 unsigned int o2hb_heartbeat_mode = O2HB_HEARTBEAT_LOCAL;
136 * o2hb_dependent_users tracks the number of registered callbacks that depend
137 * on heartbeat. o2net and o2dlm are two entities that register this callback.
138 * However only o2dlm depends on the heartbeat. It does not want the heartbeat
139 * to stop while a dlm domain is still active.
141 unsigned int o2hb_dependent_users;
144 * In global heartbeat mode, all regions are pinned if there are one or more
145 * dependent users and the quorum region count is <= O2HB_PIN_CUT_OFF. All
146 * regions are unpinned if the region count exceeds the cut off or the number
147 * of dependent users falls to zero.
149 #define O2HB_PIN_CUT_OFF 3
152 * In local heartbeat mode, we assume the dlm domain name to be the same as
153 * region uuid. This is true for domains created for the file system but not
154 * necessarily true for userdlm domains. This is a known limitation.
156 * In global heartbeat mode, we pin/unpin all o2hb regions. This solution
157 * works for both file system and userdlm domains.
159 static int o2hb_region_pin(const char *region_uuid);
160 static void o2hb_region_unpin(const char *region_uuid);
162 /* Only sets a new threshold if there are no active regions.
164 * No locking or otherwise interesting code is required for reading
165 * o2hb_dead_threshold as it can't change once regions are active and
166 * it's not interesting to anyone until then anyway. */
167 static void o2hb_dead_threshold_set(unsigned int threshold)
169 if (threshold > O2HB_MIN_DEAD_THRESHOLD) {
170 spin_lock(&o2hb_live_lock);
171 if (list_empty(&o2hb_all_regions))
172 o2hb_dead_threshold = threshold;
173 spin_unlock(&o2hb_live_lock);
177 static int o2hb_global_hearbeat_mode_set(unsigned int hb_mode)
181 if (hb_mode < O2HB_HEARTBEAT_NUM_MODES) {
182 spin_lock(&o2hb_live_lock);
183 if (list_empty(&o2hb_all_regions)) {
184 o2hb_heartbeat_mode = hb_mode;
187 spin_unlock(&o2hb_live_lock);
193 struct o2hb_node_event {
194 struct list_head hn_item;
195 enum o2hb_callback_type hn_event_type;
196 struct o2nm_node *hn_node;
200 struct o2hb_disk_slot {
201 struct o2hb_disk_heartbeat_block *ds_raw_block;
204 u64 ds_last_generation;
205 u16 ds_equal_samples;
206 u16 ds_changed_samples;
207 struct list_head ds_live_item;
210 /* each thread owns a region.. when we're asked to tear down the region
211 * we ask the thread to stop, who cleans up the region */
213 struct config_item hr_item;
215 struct list_head hr_all_item;
216 unsigned hr_unclean_stop:1,
220 /* protected by the hr_callback_sem */
221 struct task_struct *hr_task;
223 unsigned int hr_blocks;
224 unsigned long long hr_start_block;
226 unsigned int hr_block_bits;
227 unsigned int hr_block_bytes;
229 unsigned int hr_slots_per_page;
230 unsigned int hr_num_pages;
232 struct page **hr_slot_data;
233 struct block_device *hr_bdev;
234 struct o2hb_disk_slot *hr_slots;
236 /* live node map of this region */
237 unsigned long hr_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
238 unsigned int hr_region_num;
240 struct dentry *hr_debug_dir;
241 struct dentry *hr_debug_livenodes;
242 struct dentry *hr_debug_regnum;
243 struct dentry *hr_debug_elapsed_time;
244 struct o2hb_debug_buf *hr_db_livenodes;
245 struct o2hb_debug_buf *hr_db_regnum;
246 struct o2hb_debug_buf *hr_db_elapsed_time;
248 /* let the person setting up hb wait for it to return until it
249 * has reached a 'steady' state. This will be fixed when we have
250 * a more complete api that doesn't lead to this sort of fragility. */
251 atomic_t hr_steady_iterations;
253 char hr_dev_name[BDEVNAME_SIZE];
255 unsigned int hr_timeout_ms;
257 /* randomized as the region goes up and down so that a node
258 * recognizes a node going up and down in one iteration */
261 struct delayed_work hr_write_timeout_work;
262 unsigned long hr_last_timeout_start;
264 /* Used during o2hb_check_slot to hold a copy of the block
265 * being checked because we temporarily have to zero out the
267 struct o2hb_disk_heartbeat_block *hr_tmp_block;
270 struct o2hb_bio_wait_ctxt {
271 atomic_t wc_num_reqs;
272 struct completion wc_io_complete;
276 static int o2hb_pop_count(void *map, int count)
280 while ((i = find_next_bit(map, count, i + 1)) < count)
285 static void o2hb_write_timeout(struct work_struct *work)
289 struct o2hb_region *reg =
290 container_of(work, struct o2hb_region,
291 hr_write_timeout_work.work);
293 mlog(ML_ERROR, "Heartbeat write timeout to device %s after %u "
294 "milliseconds\n", reg->hr_dev_name,
295 jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
297 if (o2hb_global_heartbeat_active()) {
298 spin_lock_irqsave(&o2hb_live_lock, flags);
299 if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
300 set_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
301 failed = o2hb_pop_count(&o2hb_failed_region_bitmap,
303 quorum = o2hb_pop_count(&o2hb_quorum_region_bitmap,
305 spin_unlock_irqrestore(&o2hb_live_lock, flags);
307 mlog(ML_HEARTBEAT, "Number of regions %d, failed regions %d\n",
311 * Fence if the number of failed regions >= half the number
314 if ((failed << 1) < quorum)
318 o2quo_disk_timeout();
321 static void o2hb_arm_write_timeout(struct o2hb_region *reg)
323 mlog(ML_HEARTBEAT, "Queue write timeout for %u ms\n",
324 O2HB_MAX_WRITE_TIMEOUT_MS);
326 if (o2hb_global_heartbeat_active()) {
327 spin_lock(&o2hb_live_lock);
328 clear_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
329 spin_unlock(&o2hb_live_lock);
331 cancel_delayed_work(®->hr_write_timeout_work);
332 reg->hr_last_timeout_start = jiffies;
333 schedule_delayed_work(®->hr_write_timeout_work,
334 msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS));
337 static void o2hb_disarm_write_timeout(struct o2hb_region *reg)
339 cancel_delayed_work(®->hr_write_timeout_work);
340 flush_scheduled_work();
343 static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt *wc)
345 atomic_set(&wc->wc_num_reqs, 1);
346 init_completion(&wc->wc_io_complete);
350 /* Used in error paths too */
351 static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt *wc,
354 /* sadly atomic_sub_and_test() isn't available on all platforms. The
355 * good news is that the fast path only completes one at a time */
357 if (atomic_dec_and_test(&wc->wc_num_reqs)) {
359 complete(&wc->wc_io_complete);
364 static void o2hb_wait_on_io(struct o2hb_region *reg,
365 struct o2hb_bio_wait_ctxt *wc)
367 struct address_space *mapping = reg->hr_bdev->bd_inode->i_mapping;
369 blk_run_address_space(mapping);
370 o2hb_bio_wait_dec(wc, 1);
372 wait_for_completion(&wc->wc_io_complete);
375 static void o2hb_bio_end_io(struct bio *bio,
378 struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
381 mlog(ML_ERROR, "IO Error %d\n", error);
382 wc->wc_error = error;
385 o2hb_bio_wait_dec(wc, 1);
389 /* Setup a Bio to cover I/O against num_slots slots starting at
391 static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
392 struct o2hb_bio_wait_ctxt *wc,
393 unsigned int *current_slot,
394 unsigned int max_slots)
396 int len, current_page;
397 unsigned int vec_len, vec_start;
398 unsigned int bits = reg->hr_block_bits;
399 unsigned int spp = reg->hr_slots_per_page;
400 unsigned int cs = *current_slot;
404 /* Testing has shown this allocation to take long enough under
405 * GFP_KERNEL that the local node can get fenced. It would be
406 * nicest if we could pre-allocate these bios and avoid this
408 bio = bio_alloc(GFP_ATOMIC, 16);
410 mlog(ML_ERROR, "Could not alloc slots BIO!\n");
411 bio = ERR_PTR(-ENOMEM);
415 /* Must put everything in 512 byte sectors for the bio... */
416 bio->bi_sector = (reg->hr_start_block + cs) << (bits - 9);
417 bio->bi_bdev = reg->hr_bdev;
418 bio->bi_private = wc;
419 bio->bi_end_io = o2hb_bio_end_io;
421 vec_start = (cs << bits) % PAGE_CACHE_SIZE;
422 while(cs < max_slots) {
423 current_page = cs / spp;
424 page = reg->hr_slot_data[current_page];
426 vec_len = min(PAGE_CACHE_SIZE - vec_start,
427 (max_slots-cs) * (PAGE_CACHE_SIZE/spp) );
429 mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
430 current_page, vec_len, vec_start);
432 len = bio_add_page(bio, page, vec_len, vec_start);
433 if (len != vec_len) break;
435 cs += vec_len / (PAGE_CACHE_SIZE/spp);
444 static int o2hb_read_slots(struct o2hb_region *reg,
445 unsigned int max_slots)
447 unsigned int current_slot=0;
449 struct o2hb_bio_wait_ctxt wc;
452 o2hb_bio_wait_init(&wc);
454 while(current_slot < max_slots) {
455 bio = o2hb_setup_one_bio(reg, &wc, ¤t_slot, max_slots);
457 status = PTR_ERR(bio);
462 atomic_inc(&wc.wc_num_reqs);
463 submit_bio(READ, bio);
469 o2hb_wait_on_io(reg, &wc);
470 if (wc.wc_error && !status)
471 status = wc.wc_error;
476 static int o2hb_issue_node_write(struct o2hb_region *reg,
477 struct o2hb_bio_wait_ctxt *write_wc)
483 o2hb_bio_wait_init(write_wc);
485 slot = o2nm_this_node();
487 bio = o2hb_setup_one_bio(reg, write_wc, &slot, slot+1);
489 status = PTR_ERR(bio);
494 atomic_inc(&write_wc->wc_num_reqs);
495 submit_bio(WRITE, bio);
502 static u32 o2hb_compute_block_crc_le(struct o2hb_region *reg,
503 struct o2hb_disk_heartbeat_block *hb_block)
508 /* We want to compute the block crc with a 0 value in the
509 * hb_cksum field. Save it off here and replace after the
511 old_cksum = hb_block->hb_cksum;
512 hb_block->hb_cksum = 0;
514 ret = crc32_le(0, (unsigned char *) hb_block, reg->hr_block_bytes);
516 hb_block->hb_cksum = old_cksum;
521 static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
523 mlog(ML_ERROR, "Dump slot information: seq = 0x%llx, node = %u, "
524 "cksum = 0x%x, generation 0x%llx\n",
525 (long long)le64_to_cpu(hb_block->hb_seq),
526 hb_block->hb_node, le32_to_cpu(hb_block->hb_cksum),
527 (long long)le64_to_cpu(hb_block->hb_generation));
530 static int o2hb_verify_crc(struct o2hb_region *reg,
531 struct o2hb_disk_heartbeat_block *hb_block)
535 read = le32_to_cpu(hb_block->hb_cksum);
536 computed = o2hb_compute_block_crc_le(reg, hb_block);
538 return read == computed;
541 /* We want to make sure that nobody is heartbeating on top of us --
542 * this will help detect an invalid configuration. */
543 static int o2hb_check_last_timestamp(struct o2hb_region *reg)
546 struct o2hb_disk_slot *slot;
547 struct o2hb_disk_heartbeat_block *hb_block;
549 node_num = o2nm_this_node();
552 slot = ®->hr_slots[node_num];
553 /* Don't check on our 1st timestamp */
554 if (slot->ds_last_time) {
555 hb_block = slot->ds_raw_block;
557 if (le64_to_cpu(hb_block->hb_seq) != slot->ds_last_time)
564 static inline void o2hb_prepare_block(struct o2hb_region *reg,
569 struct o2hb_disk_slot *slot;
570 struct o2hb_disk_heartbeat_block *hb_block;
572 node_num = o2nm_this_node();
573 slot = ®->hr_slots[node_num];
575 hb_block = (struct o2hb_disk_heartbeat_block *)slot->ds_raw_block;
576 memset(hb_block, 0, reg->hr_block_bytes);
577 /* TODO: time stuff */
578 cputime = CURRENT_TIME.tv_sec;
582 hb_block->hb_seq = cpu_to_le64(cputime);
583 hb_block->hb_node = node_num;
584 hb_block->hb_generation = cpu_to_le64(generation);
585 hb_block->hb_dead_ms = cpu_to_le32(o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS);
587 /* This step must always happen last! */
588 hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
591 mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x\n",
592 (long long)generation,
593 le32_to_cpu(hb_block->hb_cksum));
596 static void o2hb_fire_callbacks(struct o2hb_callback *hbcall,
597 struct o2nm_node *node,
600 struct list_head *iter;
601 struct o2hb_callback_func *f;
603 list_for_each(iter, &hbcall->list) {
604 f = list_entry(iter, struct o2hb_callback_func, hc_item);
605 mlog(ML_HEARTBEAT, "calling funcs %p\n", f);
606 (f->hc_func)(node, idx, f->hc_data);
610 /* Will run the list in order until we process the passed event */
611 static void o2hb_run_event_list(struct o2hb_node_event *queued_event)
614 struct o2hb_callback *hbcall;
615 struct o2hb_node_event *event;
617 spin_lock(&o2hb_live_lock);
618 empty = list_empty(&queued_event->hn_item);
619 spin_unlock(&o2hb_live_lock);
623 /* Holding callback sem assures we don't alter the callback
624 * lists when doing this, and serializes ourselves with other
625 * processes wanting callbacks. */
626 down_write(&o2hb_callback_sem);
628 spin_lock(&o2hb_live_lock);
629 while (!list_empty(&o2hb_node_events)
630 && !list_empty(&queued_event->hn_item)) {
631 event = list_entry(o2hb_node_events.next,
632 struct o2hb_node_event,
634 list_del_init(&event->hn_item);
635 spin_unlock(&o2hb_live_lock);
637 mlog(ML_HEARTBEAT, "Node %s event for %d\n",
638 event->hn_event_type == O2HB_NODE_UP_CB ? "UP" : "DOWN",
641 hbcall = hbcall_from_type(event->hn_event_type);
643 /* We should *never* have gotten on to the list with a
644 * bad type... This isn't something that we should try
645 * to recover from. */
646 BUG_ON(IS_ERR(hbcall));
648 o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num);
650 spin_lock(&o2hb_live_lock);
652 spin_unlock(&o2hb_live_lock);
654 up_write(&o2hb_callback_sem);
657 static void o2hb_queue_node_event(struct o2hb_node_event *event,
658 enum o2hb_callback_type type,
659 struct o2nm_node *node,
662 assert_spin_locked(&o2hb_live_lock);
664 BUG_ON((!node) && (type != O2HB_NODE_DOWN_CB));
666 event->hn_event_type = type;
667 event->hn_node = node;
668 event->hn_node_num = node_num;
670 mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
671 type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
673 list_add_tail(&event->hn_item, &o2hb_node_events);
676 static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
678 struct o2hb_node_event event =
679 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
680 struct o2nm_node *node;
682 node = o2nm_get_node_by_num(slot->ds_node_num);
686 spin_lock(&o2hb_live_lock);
687 if (!list_empty(&slot->ds_live_item)) {
688 mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
691 list_del_init(&slot->ds_live_item);
693 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
694 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
696 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
700 spin_unlock(&o2hb_live_lock);
702 o2hb_run_event_list(&event);
707 static void o2hb_set_quorum_device(struct o2hb_region *reg,
708 struct o2hb_disk_slot *slot)
710 assert_spin_locked(&o2hb_live_lock);
712 if (!o2hb_global_heartbeat_active())
715 if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
719 * A region can be added to the quorum only when it sees all
720 * live nodes heartbeat on it. In other words, the region has been
721 * added to all nodes.
723 if (memcmp(reg->hr_live_node_bitmap, o2hb_live_node_bitmap,
724 sizeof(o2hb_live_node_bitmap)))
727 if (slot->ds_changed_samples < O2HB_LIVE_THRESHOLD)
730 printk(KERN_NOTICE "o2hb: Region %s is now a quorum device\n",
731 config_item_name(®->hr_item));
733 set_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
736 * If global heartbeat active, unpin all regions if the
737 * region count > CUT_OFF
739 if (o2hb_pop_count(&o2hb_quorum_region_bitmap,
740 O2NM_MAX_REGIONS) > O2HB_PIN_CUT_OFF)
741 o2hb_region_unpin(NULL);
744 static int o2hb_check_slot(struct o2hb_region *reg,
745 struct o2hb_disk_slot *slot)
747 int changed = 0, gen_changed = 0;
748 struct o2hb_node_event event =
749 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
750 struct o2nm_node *node;
751 struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
753 unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
754 unsigned int slot_dead_ms;
757 memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
760 * If a node is no longer configured but is still in the livemap, we
761 * may need to clear that bit from the livemap.
763 node = o2nm_get_node_by_num(slot->ds_node_num);
765 spin_lock(&o2hb_live_lock);
766 tmp = test_bit(slot->ds_node_num, o2hb_live_node_bitmap);
767 spin_unlock(&o2hb_live_lock);
772 if (!o2hb_verify_crc(reg, hb_block)) {
773 /* all paths from here will drop o2hb_live_lock for
775 spin_lock(&o2hb_live_lock);
777 /* Don't print an error on the console in this case -
778 * a freshly formatted heartbeat area will not have a
780 if (list_empty(&slot->ds_live_item))
783 /* The node is live but pushed out a bad crc. We
784 * consider it a transient miss but don't populate any
785 * other values as they may be junk. */
786 mlog(ML_ERROR, "Node %d has written a bad crc to %s\n",
787 slot->ds_node_num, reg->hr_dev_name);
788 o2hb_dump_slot(hb_block);
790 slot->ds_equal_samples++;
794 /* we don't care if these wrap.. the state transitions below
795 * clear at the right places */
796 cputime = le64_to_cpu(hb_block->hb_seq);
797 if (slot->ds_last_time != cputime)
798 slot->ds_changed_samples++;
800 slot->ds_equal_samples++;
801 slot->ds_last_time = cputime;
803 /* The node changed heartbeat generations. We assume this to
804 * mean it dropped off but came back before we timed out. We
805 * want to consider it down for the time being but don't want
806 * to lose any changed_samples state we might build up to
807 * considering it live again. */
808 if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
810 slot->ds_equal_samples = 0;
811 mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
812 "to 0x%llx)\n", slot->ds_node_num,
813 (long long)slot->ds_last_generation,
814 (long long)le64_to_cpu(hb_block->hb_generation));
817 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
819 mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
820 "seq %llu last %llu changed %u equal %u\n",
821 slot->ds_node_num, (long long)slot->ds_last_generation,
822 le32_to_cpu(hb_block->hb_cksum),
823 (unsigned long long)le64_to_cpu(hb_block->hb_seq),
824 (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
825 slot->ds_equal_samples);
827 spin_lock(&o2hb_live_lock);
830 /* dead nodes only come to life after some number of
831 * changes at any time during their dead time */
832 if (list_empty(&slot->ds_live_item) &&
833 slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
834 mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
835 slot->ds_node_num, (long long)slot->ds_last_generation);
837 set_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
839 /* first on the list generates a callback */
840 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
841 mlog(ML_HEARTBEAT, "o2hb: Add node %d to live nodes "
842 "bitmap\n", slot->ds_node_num);
843 set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
845 o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
851 list_add_tail(&slot->ds_live_item,
852 &o2hb_live_slots[slot->ds_node_num]);
854 slot->ds_equal_samples = 0;
856 /* We want to be sure that all nodes agree on the
857 * number of milliseconds before a node will be
858 * considered dead. The self-fencing timeout is
859 * computed from this value, and a discrepancy might
860 * result in heartbeat calling a node dead when it
861 * hasn't self-fenced yet. */
862 slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
863 if (slot_dead_ms && slot_dead_ms != dead_ms) {
864 /* TODO: Perhaps we can fail the region here. */
865 mlog(ML_ERROR, "Node %d on device %s has a dead count "
866 "of %u ms, but our count is %u ms.\n"
867 "Please double check your configuration values "
868 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
869 slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
875 /* if the list is dead, we're done.. */
876 if (list_empty(&slot->ds_live_item))
879 /* live nodes only go dead after enough consequtive missed
880 * samples.. reset the missed counter whenever we see
882 if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
883 mlog(ML_HEARTBEAT, "Node %d left my region\n",
886 clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
888 /* last off the live_slot generates a callback */
889 list_del_init(&slot->ds_live_item);
890 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
891 mlog(ML_HEARTBEAT, "o2hb: Remove node %d from live "
892 "nodes bitmap\n", slot->ds_node_num);
893 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
895 /* node can be null */
896 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
897 node, slot->ds_node_num);
902 /* We don't clear this because the node is still
903 * actually writing new blocks. */
905 slot->ds_changed_samples = 0;
908 if (slot->ds_changed_samples) {
909 slot->ds_changed_samples = 0;
910 slot->ds_equal_samples = 0;
913 o2hb_set_quorum_device(reg, slot);
915 spin_unlock(&o2hb_live_lock);
917 o2hb_run_event_list(&event);
924 /* This could be faster if we just implmented a find_last_bit, but I
925 * don't think the circumstances warrant it. */
926 static int o2hb_highest_node(unsigned long *nodes,
933 while ((node = find_next_bit(nodes, numbits, node + 1)) != -1) {
943 static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
945 int i, ret, highest_node, change = 0;
946 unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
947 unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
948 struct o2hb_bio_wait_ctxt write_wc;
950 ret = o2nm_configured_node_map(configured_nodes,
951 sizeof(configured_nodes));
958 * If a node is not configured but is in the livemap, we still need
959 * to read the slot so as to be able to remove it from the livemap.
961 o2hb_fill_node_map(live_node_bitmap, sizeof(live_node_bitmap));
963 while ((i = find_next_bit(live_node_bitmap,
964 O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
965 set_bit(i, configured_nodes);
968 highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
969 if (highest_node >= O2NM_MAX_NODES) {
970 mlog(ML_NOTICE, "ocfs2_heartbeat: no configured nodes found!\n");
974 /* No sense in reading the slots of nodes that don't exist
975 * yet. Of course, if the node definitions have holes in them
976 * then we're reading an empty slot anyway... Consider this
978 ret = o2hb_read_slots(reg, highest_node + 1);
984 /* With an up to date view of the slots, we can check that no
985 * other node has been improperly configured to heartbeat in
987 if (!o2hb_check_last_timestamp(reg))
988 mlog(ML_ERROR, "Device \"%s\": another node is heartbeating "
989 "in our slot!\n", reg->hr_dev_name);
991 /* fill in the proper info for our next heartbeat */
992 o2hb_prepare_block(reg, reg->hr_generation);
994 /* And fire off the write. Note that we don't wait on this I/O
996 ret = o2hb_issue_node_write(reg, &write_wc);
1003 while((i = find_next_bit(configured_nodes, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
1005 change |= o2hb_check_slot(reg, ®->hr_slots[i]);
1009 * We have to be sure we've advertised ourselves on disk
1010 * before we can go to steady state. This ensures that
1011 * people we find in our steady state have seen us.
1013 o2hb_wait_on_io(reg, &write_wc);
1014 if (write_wc.wc_error) {
1015 /* Do not re-arm the write timeout on I/O error - we
1016 * can't be sure that the new block ever made it to
1018 mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
1019 write_wc.wc_error, reg->hr_dev_name);
1020 return write_wc.wc_error;
1023 o2hb_arm_write_timeout(reg);
1025 /* let the person who launched us know when things are steady */
1026 if (!change && (atomic_read(®->hr_steady_iterations) != 0)) {
1027 if (atomic_dec_and_test(®->hr_steady_iterations))
1028 wake_up(&o2hb_steady_queue);
1034 /* Subtract b from a, storing the result in a. a *must* have a larger
1036 static void o2hb_tv_subtract(struct timeval *a,
1039 /* just return 0 when a is after b */
1040 if (a->tv_sec < b->tv_sec ||
1041 (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) {
1047 a->tv_sec -= b->tv_sec;
1048 a->tv_usec -= b->tv_usec;
1049 while ( a->tv_usec < 0 ) {
1051 a->tv_usec += 1000000;
1055 static unsigned int o2hb_elapsed_msecs(struct timeval *start,
1056 struct timeval *end)
1058 struct timeval res = *end;
1060 o2hb_tv_subtract(&res, start);
1062 return res.tv_sec * 1000 + res.tv_usec / 1000;
1066 * we ride the region ref that the region dir holds. before the region
1067 * dir is removed and drops it ref it will wait to tear down this
1070 static int o2hb_thread(void *data)
1073 struct o2hb_region *reg = data;
1074 struct o2hb_bio_wait_ctxt write_wc;
1075 struct timeval before_hb, after_hb;
1076 unsigned int elapsed_msec;
1078 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
1080 set_user_nice(current, -20);
1082 while (!kthread_should_stop() && !reg->hr_unclean_stop) {
1083 /* We track the time spent inside
1084 * o2hb_do_disk_heartbeat so that we avoid more than
1085 * hr_timeout_ms between disk writes. On busy systems
1086 * this should result in a heartbeat which is less
1087 * likely to time itself out. */
1088 do_gettimeofday(&before_hb);
1092 ret = o2hb_do_disk_heartbeat(reg);
1093 } while (ret && ++i < 2);
1095 do_gettimeofday(&after_hb);
1096 elapsed_msec = o2hb_elapsed_msecs(&before_hb, &after_hb);
1099 "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
1100 before_hb.tv_sec, (unsigned long) before_hb.tv_usec,
1101 after_hb.tv_sec, (unsigned long) after_hb.tv_usec,
1104 if (elapsed_msec < reg->hr_timeout_ms) {
1105 /* the kthread api has blocked signals for us so no
1106 * need to record the return value. */
1107 msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
1111 o2hb_disarm_write_timeout(reg);
1113 /* unclean stop is only used in very bad situation */
1114 for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
1115 o2hb_shutdown_slot(®->hr_slots[i]);
1117 /* Explicit down notification - avoid forcing the other nodes
1118 * to timeout on this region when we could just as easily
1119 * write a clear generation - thus indicating to them that
1120 * this node has left this region.
1122 * XXX: Should we skip this on unclean_stop? */
1123 o2hb_prepare_block(reg, 0);
1124 ret = o2hb_issue_node_write(reg, &write_wc);
1126 o2hb_wait_on_io(reg, &write_wc);
1131 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread exiting\n");
1136 #ifdef CONFIG_DEBUG_FS
1137 static int o2hb_debug_open(struct inode *inode, struct file *file)
1139 struct o2hb_debug_buf *db = inode->i_private;
1140 struct o2hb_region *reg;
1141 unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1146 /* max_nodes should be the largest bitmap we pass here */
1147 BUG_ON(sizeof(map) < db->db_size);
1149 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1153 switch (db->db_type) {
1154 case O2HB_DB_TYPE_LIVENODES:
1155 case O2HB_DB_TYPE_LIVEREGIONS:
1156 case O2HB_DB_TYPE_QUORUMREGIONS:
1157 case O2HB_DB_TYPE_FAILEDREGIONS:
1158 spin_lock(&o2hb_live_lock);
1159 memcpy(map, db->db_data, db->db_size);
1160 spin_unlock(&o2hb_live_lock);
1163 case O2HB_DB_TYPE_REGION_LIVENODES:
1164 spin_lock(&o2hb_live_lock);
1165 reg = (struct o2hb_region *)db->db_data;
1166 memcpy(map, reg->hr_live_node_bitmap, db->db_size);
1167 spin_unlock(&o2hb_live_lock);
1170 case O2HB_DB_TYPE_REGION_NUMBER:
1171 reg = (struct o2hb_region *)db->db_data;
1172 out += snprintf(buf + out, PAGE_SIZE - out, "%d\n",
1173 reg->hr_region_num);
1176 case O2HB_DB_TYPE_REGION_ELAPSED_TIME:
1177 reg = (struct o2hb_region *)db->db_data;
1178 out += snprintf(buf + out, PAGE_SIZE - out, "%u\n",
1179 jiffies_to_msecs(jiffies -
1180 reg->hr_last_timeout_start));
1187 while ((i = find_next_bit(map, db->db_len, i + 1)) < db->db_len)
1188 out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
1189 out += snprintf(buf + out, PAGE_SIZE - out, "\n");
1192 i_size_write(inode, out);
1194 file->private_data = buf;
1201 static int o2hb_debug_release(struct inode *inode, struct file *file)
1203 kfree(file->private_data);
1207 static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1208 size_t nbytes, loff_t *ppos)
1210 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
1211 i_size_read(file->f_mapping->host));
1214 static int o2hb_debug_open(struct inode *inode, struct file *file)
1218 static int o2hb_debug_release(struct inode *inode, struct file *file)
1222 static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1223 size_t nbytes, loff_t *ppos)
1227 #endif /* CONFIG_DEBUG_FS */
1229 static const struct file_operations o2hb_debug_fops = {
1230 .open = o2hb_debug_open,
1231 .release = o2hb_debug_release,
1232 .read = o2hb_debug_read,
1233 .llseek = generic_file_llseek,
1236 void o2hb_exit(void)
1238 kfree(o2hb_db_livenodes);
1239 kfree(o2hb_db_liveregions);
1240 kfree(o2hb_db_quorumregions);
1241 kfree(o2hb_db_failedregions);
1242 debugfs_remove(o2hb_debug_failedregions);
1243 debugfs_remove(o2hb_debug_quorumregions);
1244 debugfs_remove(o2hb_debug_liveregions);
1245 debugfs_remove(o2hb_debug_livenodes);
1246 debugfs_remove(o2hb_debug_dir);
1249 static struct dentry *o2hb_debug_create(const char *name, struct dentry *dir,
1250 struct o2hb_debug_buf **db, int db_len,
1251 int type, int size, int len, void *data)
1253 *db = kmalloc(db_len, GFP_KERNEL);
1257 (*db)->db_type = type;
1258 (*db)->db_size = size;
1259 (*db)->db_len = len;
1260 (*db)->db_data = data;
1262 return debugfs_create_file(name, S_IFREG|S_IRUSR, dir, *db,
1266 static int o2hb_debug_init(void)
1270 o2hb_debug_dir = debugfs_create_dir(O2HB_DEBUG_DIR, NULL);
1271 if (!o2hb_debug_dir) {
1276 o2hb_debug_livenodes = o2hb_debug_create(O2HB_DEBUG_LIVENODES,
1279 sizeof(*o2hb_db_livenodes),
1280 O2HB_DB_TYPE_LIVENODES,
1281 sizeof(o2hb_live_node_bitmap),
1283 o2hb_live_node_bitmap);
1284 if (!o2hb_debug_livenodes) {
1289 o2hb_debug_liveregions = o2hb_debug_create(O2HB_DEBUG_LIVEREGIONS,
1291 &o2hb_db_liveregions,
1292 sizeof(*o2hb_db_liveregions),
1293 O2HB_DB_TYPE_LIVEREGIONS,
1294 sizeof(o2hb_live_region_bitmap),
1296 o2hb_live_region_bitmap);
1297 if (!o2hb_debug_liveregions) {
1302 o2hb_debug_quorumregions =
1303 o2hb_debug_create(O2HB_DEBUG_QUORUMREGIONS,
1305 &o2hb_db_quorumregions,
1306 sizeof(*o2hb_db_quorumregions),
1307 O2HB_DB_TYPE_QUORUMREGIONS,
1308 sizeof(o2hb_quorum_region_bitmap),
1310 o2hb_quorum_region_bitmap);
1311 if (!o2hb_debug_quorumregions) {
1316 o2hb_debug_failedregions =
1317 o2hb_debug_create(O2HB_DEBUG_FAILEDREGIONS,
1319 &o2hb_db_failedregions,
1320 sizeof(*o2hb_db_failedregions),
1321 O2HB_DB_TYPE_FAILEDREGIONS,
1322 sizeof(o2hb_failed_region_bitmap),
1324 o2hb_failed_region_bitmap);
1325 if (!o2hb_debug_failedregions) {
1342 for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
1343 INIT_LIST_HEAD(&o2hb_callbacks[i].list);
1345 for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
1346 INIT_LIST_HEAD(&o2hb_live_slots[i]);
1348 INIT_LIST_HEAD(&o2hb_node_events);
1350 memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
1351 memset(o2hb_region_bitmap, 0, sizeof(o2hb_region_bitmap));
1352 memset(o2hb_live_region_bitmap, 0, sizeof(o2hb_live_region_bitmap));
1353 memset(o2hb_quorum_region_bitmap, 0, sizeof(o2hb_quorum_region_bitmap));
1354 memset(o2hb_failed_region_bitmap, 0, sizeof(o2hb_failed_region_bitmap));
1356 o2hb_dependent_users = 0;
1358 return o2hb_debug_init();
1361 /* if we're already in a callback then we're already serialized by the sem */
1362 static void o2hb_fill_node_map_from_callback(unsigned long *map,
1365 BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
1367 memcpy(map, &o2hb_live_node_bitmap, bytes);
1371 * get a map of all nodes that are heartbeating in any regions
1373 void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
1375 /* callers want to serialize this map and callbacks so that they
1376 * can trust that they don't miss nodes coming to the party */
1377 down_read(&o2hb_callback_sem);
1378 spin_lock(&o2hb_live_lock);
1379 o2hb_fill_node_map_from_callback(map, bytes);
1380 spin_unlock(&o2hb_live_lock);
1381 up_read(&o2hb_callback_sem);
1383 EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
1386 * heartbeat configfs bits. The heartbeat set is a default set under
1387 * the cluster set in nodemanager.c.
1390 static struct o2hb_region *to_o2hb_region(struct config_item *item)
1392 return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
1395 /* drop_item only drops its ref after killing the thread, nothing should
1396 * be using the region anymore. this has to clean up any state that
1397 * attributes might have built up. */
1398 static void o2hb_region_release(struct config_item *item)
1402 struct o2hb_region *reg = to_o2hb_region(item);
1404 if (reg->hr_tmp_block)
1405 kfree(reg->hr_tmp_block);
1407 if (reg->hr_slot_data) {
1408 for (i = 0; i < reg->hr_num_pages; i++) {
1409 page = reg->hr_slot_data[i];
1413 kfree(reg->hr_slot_data);
1417 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
1420 kfree(reg->hr_slots);
1422 kfree(reg->hr_db_regnum);
1423 kfree(reg->hr_db_livenodes);
1424 debugfs_remove(reg->hr_debug_livenodes);
1425 debugfs_remove(reg->hr_debug_regnum);
1426 debugfs_remove(reg->hr_debug_elapsed_time);
1427 debugfs_remove(reg->hr_debug_dir);
1429 spin_lock(&o2hb_live_lock);
1430 list_del(®->hr_all_item);
1431 spin_unlock(&o2hb_live_lock);
1436 static int o2hb_read_block_input(struct o2hb_region *reg,
1439 unsigned long *ret_bytes,
1440 unsigned int *ret_bits)
1442 unsigned long bytes;
1443 char *p = (char *)page;
1445 bytes = simple_strtoul(p, &p, 0);
1446 if (!p || (*p && (*p != '\n')))
1449 /* Heartbeat and fs min / max block sizes are the same. */
1450 if (bytes > 4096 || bytes < 512)
1452 if (hweight16(bytes) != 1)
1458 *ret_bits = ffs(bytes) - 1;
1463 static ssize_t o2hb_region_block_bytes_read(struct o2hb_region *reg,
1466 return sprintf(page, "%u\n", reg->hr_block_bytes);
1469 static ssize_t o2hb_region_block_bytes_write(struct o2hb_region *reg,
1474 unsigned long block_bytes;
1475 unsigned int block_bits;
1480 status = o2hb_read_block_input(reg, page, count,
1481 &block_bytes, &block_bits);
1485 reg->hr_block_bytes = (unsigned int)block_bytes;
1486 reg->hr_block_bits = block_bits;
1491 static ssize_t o2hb_region_start_block_read(struct o2hb_region *reg,
1494 return sprintf(page, "%llu\n", reg->hr_start_block);
1497 static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
1501 unsigned long long tmp;
1502 char *p = (char *)page;
1507 tmp = simple_strtoull(p, &p, 0);
1508 if (!p || (*p && (*p != '\n')))
1511 reg->hr_start_block = tmp;
1516 static ssize_t o2hb_region_blocks_read(struct o2hb_region *reg,
1519 return sprintf(page, "%d\n", reg->hr_blocks);
1522 static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
1527 char *p = (char *)page;
1532 tmp = simple_strtoul(p, &p, 0);
1533 if (!p || (*p && (*p != '\n')))
1536 if (tmp > O2NM_MAX_NODES || tmp == 0)
1539 reg->hr_blocks = (unsigned int)tmp;
1544 static ssize_t o2hb_region_dev_read(struct o2hb_region *reg,
1547 unsigned int ret = 0;
1550 ret = sprintf(page, "%s\n", reg->hr_dev_name);
1555 static void o2hb_init_region_params(struct o2hb_region *reg)
1557 reg->hr_slots_per_page = PAGE_CACHE_SIZE >> reg->hr_block_bits;
1558 reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
1560 mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
1561 reg->hr_start_block, reg->hr_blocks);
1562 mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
1563 reg->hr_block_bytes, reg->hr_block_bits);
1564 mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
1565 mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
1568 static int o2hb_map_slot_data(struct o2hb_region *reg)
1571 unsigned int last_slot;
1572 unsigned int spp = reg->hr_slots_per_page;
1575 struct o2hb_disk_slot *slot;
1577 reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
1578 if (reg->hr_tmp_block == NULL) {
1579 mlog_errno(-ENOMEM);
1583 reg->hr_slots = kcalloc(reg->hr_blocks,
1584 sizeof(struct o2hb_disk_slot), GFP_KERNEL);
1585 if (reg->hr_slots == NULL) {
1586 mlog_errno(-ENOMEM);
1590 for(i = 0; i < reg->hr_blocks; i++) {
1591 slot = ®->hr_slots[i];
1592 slot->ds_node_num = i;
1593 INIT_LIST_HEAD(&slot->ds_live_item);
1594 slot->ds_raw_block = NULL;
1597 reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
1598 mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
1599 "at %u blocks per page\n",
1600 reg->hr_num_pages, reg->hr_blocks, spp);
1602 reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
1604 if (!reg->hr_slot_data) {
1605 mlog_errno(-ENOMEM);
1609 for(i = 0; i < reg->hr_num_pages; i++) {
1610 page = alloc_page(GFP_KERNEL);
1612 mlog_errno(-ENOMEM);
1616 reg->hr_slot_data[i] = page;
1618 last_slot = i * spp;
1619 raw = page_address(page);
1621 (j < spp) && ((j + last_slot) < reg->hr_blocks);
1623 BUG_ON((j + last_slot) >= reg->hr_blocks);
1625 slot = ®->hr_slots[j + last_slot];
1626 slot->ds_raw_block =
1627 (struct o2hb_disk_heartbeat_block *) raw;
1629 raw += reg->hr_block_bytes;
1636 /* Read in all the slots available and populate the tracking
1637 * structures so that we can start with a baseline idea of what's
1639 static int o2hb_populate_slot_data(struct o2hb_region *reg)
1642 struct o2hb_disk_slot *slot;
1643 struct o2hb_disk_heartbeat_block *hb_block;
1647 ret = o2hb_read_slots(reg, reg->hr_blocks);
1653 /* We only want to get an idea of the values initially in each
1654 * slot, so we do no verification - o2hb_check_slot will
1655 * actually determine if each configured slot is valid and
1656 * whether any values have changed. */
1657 for(i = 0; i < reg->hr_blocks; i++) {
1658 slot = ®->hr_slots[i];
1659 hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
1661 /* Only fill the values that o2hb_check_slot uses to
1662 * determine changing slots */
1663 slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
1664 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
1672 /* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1673 static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
1677 struct task_struct *hb_task;
1680 char *p = (char *)page;
1681 struct file *filp = NULL;
1682 struct inode *inode = NULL;
1683 ssize_t ret = -EINVAL;
1688 /* We can't heartbeat without having had our node number
1689 * configured yet. */
1690 if (o2nm_this_node() == O2NM_MAX_NODES)
1693 fd = simple_strtol(p, &p, 0);
1694 if (!p || (*p && (*p != '\n')))
1697 if (fd < 0 || fd >= INT_MAX)
1704 if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
1705 reg->hr_block_bytes == 0)
1708 inode = igrab(filp->f_mapping->host);
1712 if (!S_ISBLK(inode->i_mode))
1715 reg->hr_bdev = I_BDEV(filp->f_mapping->host);
1716 ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ);
1718 reg->hr_bdev = NULL;
1723 bdevname(reg->hr_bdev, reg->hr_dev_name);
1725 sectsize = bdev_logical_block_size(reg->hr_bdev);
1726 if (sectsize != reg->hr_block_bytes) {
1728 "blocksize %u incorrect for device, expected %d",
1729 reg->hr_block_bytes, sectsize);
1734 o2hb_init_region_params(reg);
1736 /* Generation of zero is invalid */
1738 get_random_bytes(®->hr_generation,
1739 sizeof(reg->hr_generation));
1740 } while (reg->hr_generation == 0);
1742 ret = o2hb_map_slot_data(reg);
1748 ret = o2hb_populate_slot_data(reg);
1754 INIT_DELAYED_WORK(®->hr_write_timeout_work, o2hb_write_timeout);
1757 * A node is considered live after it has beat LIVE_THRESHOLD
1758 * times. We're not steady until we've given them a chance
1759 * _after_ our first read.
1761 atomic_set(®->hr_steady_iterations, O2HB_LIVE_THRESHOLD + 1);
1763 hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
1764 reg->hr_item.ci_name);
1765 if (IS_ERR(hb_task)) {
1766 ret = PTR_ERR(hb_task);
1771 spin_lock(&o2hb_live_lock);
1772 reg->hr_task = hb_task;
1773 spin_unlock(&o2hb_live_lock);
1775 ret = wait_event_interruptible(o2hb_steady_queue,
1776 atomic_read(®->hr_steady_iterations) == 0);
1778 /* We got interrupted (hello ptrace!). Clean up */
1779 spin_lock(&o2hb_live_lock);
1780 hb_task = reg->hr_task;
1781 reg->hr_task = NULL;
1782 spin_unlock(&o2hb_live_lock);
1785 kthread_stop(hb_task);
1789 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1790 spin_lock(&o2hb_live_lock);
1791 hb_task = reg->hr_task;
1792 if (o2hb_global_heartbeat_active())
1793 set_bit(reg->hr_region_num, o2hb_live_region_bitmap);
1794 spin_unlock(&o2hb_live_lock);
1801 if (hb_task && o2hb_global_heartbeat_active())
1802 printk(KERN_NOTICE "o2hb: Heartbeat started on region %s\n",
1803 config_item_name(®->hr_item));
1812 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
1813 reg->hr_bdev = NULL;
1819 static ssize_t o2hb_region_pid_read(struct o2hb_region *reg,
1824 spin_lock(&o2hb_live_lock);
1826 pid = task_pid_nr(reg->hr_task);
1827 spin_unlock(&o2hb_live_lock);
1832 return sprintf(page, "%u\n", pid);
1835 struct o2hb_region_attribute {
1836 struct configfs_attribute attr;
1837 ssize_t (*show)(struct o2hb_region *, char *);
1838 ssize_t (*store)(struct o2hb_region *, const char *, size_t);
1841 static struct o2hb_region_attribute o2hb_region_attr_block_bytes = {
1842 .attr = { .ca_owner = THIS_MODULE,
1843 .ca_name = "block_bytes",
1844 .ca_mode = S_IRUGO | S_IWUSR },
1845 .show = o2hb_region_block_bytes_read,
1846 .store = o2hb_region_block_bytes_write,
1849 static struct o2hb_region_attribute o2hb_region_attr_start_block = {
1850 .attr = { .ca_owner = THIS_MODULE,
1851 .ca_name = "start_block",
1852 .ca_mode = S_IRUGO | S_IWUSR },
1853 .show = o2hb_region_start_block_read,
1854 .store = o2hb_region_start_block_write,
1857 static struct o2hb_region_attribute o2hb_region_attr_blocks = {
1858 .attr = { .ca_owner = THIS_MODULE,
1859 .ca_name = "blocks",
1860 .ca_mode = S_IRUGO | S_IWUSR },
1861 .show = o2hb_region_blocks_read,
1862 .store = o2hb_region_blocks_write,
1865 static struct o2hb_region_attribute o2hb_region_attr_dev = {
1866 .attr = { .ca_owner = THIS_MODULE,
1868 .ca_mode = S_IRUGO | S_IWUSR },
1869 .show = o2hb_region_dev_read,
1870 .store = o2hb_region_dev_write,
1873 static struct o2hb_region_attribute o2hb_region_attr_pid = {
1874 .attr = { .ca_owner = THIS_MODULE,
1876 .ca_mode = S_IRUGO | S_IRUSR },
1877 .show = o2hb_region_pid_read,
1880 static struct configfs_attribute *o2hb_region_attrs[] = {
1881 &o2hb_region_attr_block_bytes.attr,
1882 &o2hb_region_attr_start_block.attr,
1883 &o2hb_region_attr_blocks.attr,
1884 &o2hb_region_attr_dev.attr,
1885 &o2hb_region_attr_pid.attr,
1889 static ssize_t o2hb_region_show(struct config_item *item,
1890 struct configfs_attribute *attr,
1893 struct o2hb_region *reg = to_o2hb_region(item);
1894 struct o2hb_region_attribute *o2hb_region_attr =
1895 container_of(attr, struct o2hb_region_attribute, attr);
1898 if (o2hb_region_attr->show)
1899 ret = o2hb_region_attr->show(reg, page);
1903 static ssize_t o2hb_region_store(struct config_item *item,
1904 struct configfs_attribute *attr,
1905 const char *page, size_t count)
1907 struct o2hb_region *reg = to_o2hb_region(item);
1908 struct o2hb_region_attribute *o2hb_region_attr =
1909 container_of(attr, struct o2hb_region_attribute, attr);
1910 ssize_t ret = -EINVAL;
1912 if (o2hb_region_attr->store)
1913 ret = o2hb_region_attr->store(reg, page, count);
1917 static struct configfs_item_operations o2hb_region_item_ops = {
1918 .release = o2hb_region_release,
1919 .show_attribute = o2hb_region_show,
1920 .store_attribute = o2hb_region_store,
1923 static struct config_item_type o2hb_region_type = {
1924 .ct_item_ops = &o2hb_region_item_ops,
1925 .ct_attrs = o2hb_region_attrs,
1926 .ct_owner = THIS_MODULE,
1931 struct o2hb_heartbeat_group {
1932 struct config_group hs_group;
1936 static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
1939 container_of(group, struct o2hb_heartbeat_group, hs_group)
1943 static int o2hb_debug_region_init(struct o2hb_region *reg, struct dentry *dir)
1948 debugfs_create_dir(config_item_name(®->hr_item), dir);
1949 if (!reg->hr_debug_dir) {
1954 reg->hr_debug_livenodes =
1955 o2hb_debug_create(O2HB_DEBUG_LIVENODES,
1957 &(reg->hr_db_livenodes),
1958 sizeof(*(reg->hr_db_livenodes)),
1959 O2HB_DB_TYPE_REGION_LIVENODES,
1960 sizeof(reg->hr_live_node_bitmap),
1961 O2NM_MAX_NODES, reg);
1962 if (!reg->hr_debug_livenodes) {
1967 reg->hr_debug_regnum =
1968 o2hb_debug_create(O2HB_DEBUG_REGION_NUMBER,
1970 &(reg->hr_db_regnum),
1971 sizeof(*(reg->hr_db_regnum)),
1972 O2HB_DB_TYPE_REGION_NUMBER,
1973 0, O2NM_MAX_NODES, reg);
1974 if (!reg->hr_debug_regnum) {
1979 reg->hr_debug_elapsed_time =
1980 o2hb_debug_create(O2HB_DEBUG_REGION_ELAPSED_TIME,
1982 &(reg->hr_db_elapsed_time),
1983 sizeof(*(reg->hr_db_elapsed_time)),
1984 O2HB_DB_TYPE_REGION_ELAPSED_TIME,
1986 if (!reg->hr_debug_elapsed_time) {
1996 static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
1999 struct o2hb_region *reg = NULL;
2002 reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
2004 return ERR_PTR(-ENOMEM);
2006 if (strlen(name) > O2HB_MAX_REGION_NAME_LEN) {
2007 ret = -ENAMETOOLONG;
2011 spin_lock(&o2hb_live_lock);
2012 reg->hr_region_num = 0;
2013 if (o2hb_global_heartbeat_active()) {
2014 reg->hr_region_num = find_first_zero_bit(o2hb_region_bitmap,
2016 if (reg->hr_region_num >= O2NM_MAX_REGIONS) {
2017 spin_unlock(&o2hb_live_lock);
2021 set_bit(reg->hr_region_num, o2hb_region_bitmap);
2023 list_add_tail(®->hr_all_item, &o2hb_all_regions);
2024 spin_unlock(&o2hb_live_lock);
2026 config_item_init_type_name(®->hr_item, name, &o2hb_region_type);
2028 ret = o2hb_debug_region_init(reg, o2hb_debug_dir);
2030 config_item_put(®->hr_item);
2034 return ®->hr_item;
2037 return ERR_PTR(ret);
2040 static void o2hb_heartbeat_group_drop_item(struct config_group *group,
2041 struct config_item *item)
2043 struct task_struct *hb_task;
2044 struct o2hb_region *reg = to_o2hb_region(item);
2045 int quorum_region = 0;
2047 /* stop the thread when the user removes the region dir */
2048 spin_lock(&o2hb_live_lock);
2049 if (o2hb_global_heartbeat_active()) {
2050 clear_bit(reg->hr_region_num, o2hb_region_bitmap);
2051 clear_bit(reg->hr_region_num, o2hb_live_region_bitmap);
2052 if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
2054 clear_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
2056 hb_task = reg->hr_task;
2057 reg->hr_task = NULL;
2058 reg->hr_item_dropped = 1;
2059 spin_unlock(&o2hb_live_lock);
2062 kthread_stop(hb_task);
2065 * If we're racing a dev_write(), we need to wake them. They will
2066 * check reg->hr_task
2068 if (atomic_read(®->hr_steady_iterations) != 0) {
2069 atomic_set(®->hr_steady_iterations, 0);
2070 wake_up(&o2hb_steady_queue);
2073 if (o2hb_global_heartbeat_active())
2074 printk(KERN_NOTICE "o2hb: Heartbeat stopped on region %s\n",
2075 config_item_name(®->hr_item));
2077 config_item_put(item);
2079 if (!o2hb_global_heartbeat_active() || !quorum_region)
2083 * If global heartbeat active and there are dependent users,
2084 * pin all regions if quorum region count <= CUT_OFF
2086 spin_lock(&o2hb_live_lock);
2088 if (!o2hb_dependent_users)
2091 if (o2hb_pop_count(&o2hb_quorum_region_bitmap,
2092 O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
2093 o2hb_region_pin(NULL);
2096 spin_unlock(&o2hb_live_lock);
2099 struct o2hb_heartbeat_group_attribute {
2100 struct configfs_attribute attr;
2101 ssize_t (*show)(struct o2hb_heartbeat_group *, char *);
2102 ssize_t (*store)(struct o2hb_heartbeat_group *, const char *, size_t);
2105 static ssize_t o2hb_heartbeat_group_show(struct config_item *item,
2106 struct configfs_attribute *attr,
2109 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
2110 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
2111 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
2114 if (o2hb_heartbeat_group_attr->show)
2115 ret = o2hb_heartbeat_group_attr->show(reg, page);
2119 static ssize_t o2hb_heartbeat_group_store(struct config_item *item,
2120 struct configfs_attribute *attr,
2121 const char *page, size_t count)
2123 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
2124 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
2125 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
2126 ssize_t ret = -EINVAL;
2128 if (o2hb_heartbeat_group_attr->store)
2129 ret = o2hb_heartbeat_group_attr->store(reg, page, count);
2133 static ssize_t o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group *group,
2136 return sprintf(page, "%u\n", o2hb_dead_threshold);
2139 static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group *group,
2144 char *p = (char *)page;
2146 tmp = simple_strtoul(p, &p, 10);
2147 if (!p || (*p && (*p != '\n')))
2150 /* this will validate ranges for us. */
2151 o2hb_dead_threshold_set((unsigned int) tmp);
2157 ssize_t o2hb_heartbeat_group_mode_show(struct o2hb_heartbeat_group *group,
2160 return sprintf(page, "%s\n",
2161 o2hb_heartbeat_mode_desc[o2hb_heartbeat_mode]);
2165 ssize_t o2hb_heartbeat_group_mode_store(struct o2hb_heartbeat_group *group,
2166 const char *page, size_t count)
2172 len = (page[count - 1] == '\n') ? count - 1 : count;
2176 for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) {
2177 if (strnicmp(page, o2hb_heartbeat_mode_desc[i], len))
2180 ret = o2hb_global_hearbeat_mode_set(i);
2182 printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
2183 o2hb_heartbeat_mode_desc[i]);
2191 static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold = {
2192 .attr = { .ca_owner = THIS_MODULE,
2193 .ca_name = "dead_threshold",
2194 .ca_mode = S_IRUGO | S_IWUSR },
2195 .show = o2hb_heartbeat_group_threshold_show,
2196 .store = o2hb_heartbeat_group_threshold_store,
2199 static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_mode = {
2200 .attr = { .ca_owner = THIS_MODULE,
2202 .ca_mode = S_IRUGO | S_IWUSR },
2203 .show = o2hb_heartbeat_group_mode_show,
2204 .store = o2hb_heartbeat_group_mode_store,
2207 static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
2208 &o2hb_heartbeat_group_attr_threshold.attr,
2209 &o2hb_heartbeat_group_attr_mode.attr,
2213 static struct configfs_item_operations o2hb_hearbeat_group_item_ops = {
2214 .show_attribute = o2hb_heartbeat_group_show,
2215 .store_attribute = o2hb_heartbeat_group_store,
2218 static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
2219 .make_item = o2hb_heartbeat_group_make_item,
2220 .drop_item = o2hb_heartbeat_group_drop_item,
2223 static struct config_item_type o2hb_heartbeat_group_type = {
2224 .ct_group_ops = &o2hb_heartbeat_group_group_ops,
2225 .ct_item_ops = &o2hb_hearbeat_group_item_ops,
2226 .ct_attrs = o2hb_heartbeat_group_attrs,
2227 .ct_owner = THIS_MODULE,
2230 /* this is just here to avoid touching group in heartbeat.h which the
2231 * entire damn world #includes */
2232 struct config_group *o2hb_alloc_hb_set(void)
2234 struct o2hb_heartbeat_group *hs = NULL;
2235 struct config_group *ret = NULL;
2237 hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
2241 config_group_init_type_name(&hs->hs_group, "heartbeat",
2242 &o2hb_heartbeat_group_type);
2244 ret = &hs->hs_group;
2251 void o2hb_free_hb_set(struct config_group *group)
2253 struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
2257 /* hb callback registration and issueing */
2259 static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
2261 if (type == O2HB_NUM_CB)
2262 return ERR_PTR(-EINVAL);
2264 return &o2hb_callbacks[type];
2267 void o2hb_setup_callback(struct o2hb_callback_func *hc,
2268 enum o2hb_callback_type type,
2273 INIT_LIST_HEAD(&hc->hc_item);
2276 hc->hc_priority = priority;
2278 hc->hc_magic = O2HB_CB_MAGIC;
2280 EXPORT_SYMBOL_GPL(o2hb_setup_callback);
2283 * In local heartbeat mode, region_uuid passed matches the dlm domain name.
2284 * In global heartbeat mode, region_uuid passed is NULL.
2286 * In local, we only pin the matching region. In global we pin all the active
2289 static int o2hb_region_pin(const char *region_uuid)
2291 int ret = 0, found = 0;
2292 struct o2hb_region *reg;
2295 assert_spin_locked(&o2hb_live_lock);
2297 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2298 uuid = config_item_name(®->hr_item);
2300 /* local heartbeat */
2302 if (strcmp(region_uuid, uuid))
2307 if (reg->hr_item_pinned || reg->hr_item_dropped)
2310 /* Ignore ENOENT only for local hb (userdlm domain) */
2311 ret = o2nm_depend_item(®->hr_item);
2313 mlog(ML_CLUSTER, "Pin region %s\n", uuid);
2314 reg->hr_item_pinned = 1;
2316 if (ret == -ENOENT && found)
2319 mlog(ML_ERROR, "Pin region %s fails with %d\n",
2333 * In local heartbeat mode, region_uuid passed matches the dlm domain name.
2334 * In global heartbeat mode, region_uuid passed is NULL.
2336 * In local, we only unpin the matching region. In global we unpin all the
2339 static void o2hb_region_unpin(const char *region_uuid)
2341 struct o2hb_region *reg;
2345 assert_spin_locked(&o2hb_live_lock);
2347 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2348 uuid = config_item_name(®->hr_item);
2350 if (strcmp(region_uuid, uuid))
2355 if (reg->hr_item_pinned) {
2356 mlog(ML_CLUSTER, "Unpin region %s\n", uuid);
2357 o2nm_undepend_item(®->hr_item);
2358 reg->hr_item_pinned = 0;
2365 static int o2hb_region_inc_user(const char *region_uuid)
2369 spin_lock(&o2hb_live_lock);
2371 /* local heartbeat */
2372 if (!o2hb_global_heartbeat_active()) {
2373 ret = o2hb_region_pin(region_uuid);
2378 * if global heartbeat active and this is the first dependent user,
2379 * pin all regions if quorum region count <= CUT_OFF
2381 o2hb_dependent_users++;
2382 if (o2hb_dependent_users > 1)
2385 if (o2hb_pop_count(&o2hb_quorum_region_bitmap,
2386 O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
2387 ret = o2hb_region_pin(NULL);
2390 spin_unlock(&o2hb_live_lock);
2394 void o2hb_region_dec_user(const char *region_uuid)
2396 spin_lock(&o2hb_live_lock);
2398 /* local heartbeat */
2399 if (!o2hb_global_heartbeat_active()) {
2400 o2hb_region_unpin(region_uuid);
2405 * if global heartbeat active and there are no dependent users,
2406 * unpin all quorum regions
2408 o2hb_dependent_users--;
2409 if (!o2hb_dependent_users)
2410 o2hb_region_unpin(NULL);
2413 spin_unlock(&o2hb_live_lock);
2416 int o2hb_register_callback(const char *region_uuid,
2417 struct o2hb_callback_func *hc)
2419 struct o2hb_callback_func *tmp;
2420 struct list_head *iter;
2421 struct o2hb_callback *hbcall;
2424 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2425 BUG_ON(!list_empty(&hc->hc_item));
2427 hbcall = hbcall_from_type(hc->hc_type);
2428 if (IS_ERR(hbcall)) {
2429 ret = PTR_ERR(hbcall);
2434 ret = o2hb_region_inc_user(region_uuid);
2441 down_write(&o2hb_callback_sem);
2443 list_for_each(iter, &hbcall->list) {
2444 tmp = list_entry(iter, struct o2hb_callback_func, hc_item);
2445 if (hc->hc_priority < tmp->hc_priority) {
2446 list_add_tail(&hc->hc_item, iter);
2450 if (list_empty(&hc->hc_item))
2451 list_add_tail(&hc->hc_item, &hbcall->list);
2453 up_write(&o2hb_callback_sem);
2456 mlog(ML_CLUSTER, "returning %d on behalf of %p for funcs %p\n",
2457 ret, __builtin_return_address(0), hc);
2460 EXPORT_SYMBOL_GPL(o2hb_register_callback);
2462 void o2hb_unregister_callback(const char *region_uuid,
2463 struct o2hb_callback_func *hc)
2465 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2467 mlog(ML_CLUSTER, "on behalf of %p for funcs %p\n",
2468 __builtin_return_address(0), hc);
2470 /* XXX Can this happen _with_ a region reference? */
2471 if (list_empty(&hc->hc_item))
2475 o2hb_region_dec_user(region_uuid);
2477 down_write(&o2hb_callback_sem);
2479 list_del_init(&hc->hc_item);
2481 up_write(&o2hb_callback_sem);
2483 EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
2485 int o2hb_check_node_heartbeating(u8 node_num)
2487 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2489 o2hb_fill_node_map(testing_map, sizeof(testing_map));
2490 if (!test_bit(node_num, testing_map)) {
2492 "node (%u) does not have heartbeating enabled.\n",
2499 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating);
2501 int o2hb_check_node_heartbeating_from_callback(u8 node_num)
2503 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2505 o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
2506 if (!test_bit(node_num, testing_map)) {
2508 "node (%u) does not have heartbeating enabled.\n",
2515 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
2517 /* Makes sure our local node is configured with a node number, and is
2519 int o2hb_check_local_node_heartbeating(void)
2523 /* if this node was set then we have networking */
2524 node_num = o2nm_this_node();
2525 if (node_num == O2NM_MAX_NODES) {
2526 mlog(ML_HEARTBEAT, "this node has not been configured.\n");
2530 return o2hb_check_node_heartbeating(node_num);
2532 EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating);
2535 * this is just a hack until we get the plumbing which flips file systems
2536 * read only and drops the hb ref instead of killing the node dead.
2538 void o2hb_stop_all_regions(void)
2540 struct o2hb_region *reg;
2542 mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
2544 spin_lock(&o2hb_live_lock);
2546 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
2547 reg->hr_unclean_stop = 1;
2549 spin_unlock(&o2hb_live_lock);
2551 EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);
2553 int o2hb_get_all_regions(char *region_uuids, u8 max_regions)
2555 struct o2hb_region *reg;
2559 spin_lock(&o2hb_live_lock);
2562 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2563 mlog(0, "Region: %s\n", config_item_name(®->hr_item));
2564 if (numregs < max_regions) {
2565 memcpy(p, config_item_name(®->hr_item),
2566 O2HB_MAX_REGION_NAME_LEN);
2567 p += O2HB_MAX_REGION_NAME_LEN;
2572 spin_unlock(&o2hb_live_lock);
2576 EXPORT_SYMBOL_GPL(o2hb_get_all_regions);
2578 int o2hb_global_heartbeat_active(void)
2580 return (o2hb_heartbeat_mode == O2HB_HEARTBEAT_GLOBAL);
2582 EXPORT_SYMBOL(o2hb_global_heartbeat_active);