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>
37 #include "heartbeat.h"
39 #include "nodemanager.h"
46 * The first heartbeat pass had one global thread that would serialize all hb
47 * callback calls. This global serializing sem should only be removed once
48 * we've made sure that all callees can deal with being called concurrently
49 * from multiple hb region threads.
51 static DECLARE_RWSEM(o2hb_callback_sem);
54 * multiple hb threads are watching multiple regions. A node is live
55 * whenever any of the threads sees activity from the node in its region.
57 static spinlock_t o2hb_live_lock = SPIN_LOCK_UNLOCKED;
58 static struct list_head o2hb_live_slots[O2NM_MAX_NODES];
59 static unsigned long o2hb_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
60 static LIST_HEAD(o2hb_node_events);
61 static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue);
63 static LIST_HEAD(o2hb_all_regions);
65 static struct o2hb_callback {
66 struct list_head list;
67 } o2hb_callbacks[O2HB_NUM_CB];
69 static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type);
71 #define O2HB_DEFAULT_BLOCK_BITS 9
73 unsigned int o2hb_dead_threshold = O2HB_DEFAULT_DEAD_THRESHOLD;
75 /* Only sets a new threshold if there are no active regions.
77 * No locking or otherwise interesting code is required for reading
78 * o2hb_dead_threshold as it can't change once regions are active and
79 * it's not interesting to anyone until then anyway. */
80 static void o2hb_dead_threshold_set(unsigned int threshold)
82 if (threshold > O2HB_MIN_DEAD_THRESHOLD) {
83 spin_lock(&o2hb_live_lock);
84 if (list_empty(&o2hb_all_regions))
85 o2hb_dead_threshold = threshold;
86 spin_unlock(&o2hb_live_lock);
90 struct o2hb_node_event {
91 struct list_head hn_item;
92 enum o2hb_callback_type hn_event_type;
93 struct o2nm_node *hn_node;
97 struct o2hb_disk_slot {
98 struct o2hb_disk_heartbeat_block *ds_raw_block;
101 u64 ds_last_generation;
102 u16 ds_equal_samples;
103 u16 ds_changed_samples;
104 struct list_head ds_live_item;
107 /* each thread owns a region.. when we're asked to tear down the region
108 * we ask the thread to stop, who cleans up the region */
110 struct config_item hr_item;
112 struct list_head hr_all_item;
113 unsigned hr_unclean_stop:1;
115 /* protected by the hr_callback_sem */
116 struct task_struct *hr_task;
118 unsigned int hr_blocks;
119 unsigned long long hr_start_block;
121 unsigned int hr_block_bits;
122 unsigned int hr_block_bytes;
124 unsigned int hr_slots_per_page;
125 unsigned int hr_num_pages;
127 struct page **hr_slot_data;
128 struct block_device *hr_bdev;
129 struct o2hb_disk_slot *hr_slots;
131 /* let the person setting up hb wait for it to return until it
132 * has reached a 'steady' state. This will be fixed when we have
133 * a more complete api that doesn't lead to this sort of fragility. */
134 atomic_t hr_steady_iterations;
136 char hr_dev_name[BDEVNAME_SIZE];
138 unsigned int hr_timeout_ms;
140 /* randomized as the region goes up and down so that a node
141 * recognizes a node going up and down in one iteration */
144 struct work_struct hr_write_timeout_work;
145 unsigned long hr_last_timeout_start;
147 /* Used during o2hb_check_slot to hold a copy of the block
148 * being checked because we temporarily have to zero out the
150 struct o2hb_disk_heartbeat_block *hr_tmp_block;
153 struct o2hb_bio_wait_ctxt {
154 atomic_t wc_num_reqs;
155 struct completion wc_io_complete;
159 static void o2hb_write_timeout(void *arg)
161 struct o2hb_region *reg = arg;
163 mlog(ML_ERROR, "Heartbeat write timeout to device %s after %u "
164 "milliseconds\n", reg->hr_dev_name,
165 jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
166 o2quo_disk_timeout();
169 static void o2hb_arm_write_timeout(struct o2hb_region *reg)
171 mlog(0, "Queue write timeout for %u ms\n", O2HB_MAX_WRITE_TIMEOUT_MS);
173 cancel_delayed_work(®->hr_write_timeout_work);
174 reg->hr_last_timeout_start = jiffies;
175 schedule_delayed_work(®->hr_write_timeout_work,
176 msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS));
179 static void o2hb_disarm_write_timeout(struct o2hb_region *reg)
181 cancel_delayed_work(®->hr_write_timeout_work);
182 flush_scheduled_work();
185 static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt *wc,
186 unsigned int num_ios)
188 atomic_set(&wc->wc_num_reqs, num_ios);
189 init_completion(&wc->wc_io_complete);
193 /* Used in error paths too */
194 static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt *wc,
197 /* sadly atomic_sub_and_test() isn't available on all platforms. The
198 * good news is that the fast path only completes one at a time */
200 if (atomic_dec_and_test(&wc->wc_num_reqs)) {
202 complete(&wc->wc_io_complete);
207 static void o2hb_wait_on_io(struct o2hb_region *reg,
208 struct o2hb_bio_wait_ctxt *wc)
210 struct address_space *mapping = reg->hr_bdev->bd_inode->i_mapping;
212 blk_run_address_space(mapping);
214 wait_for_completion(&wc->wc_io_complete);
217 static int o2hb_bio_end_io(struct bio *bio,
218 unsigned int bytes_done,
221 struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
224 mlog(ML_ERROR, "IO Error %d\n", error);
225 wc->wc_error = error;
231 o2hb_bio_wait_dec(wc, 1);
235 /* Setup a Bio to cover I/O against num_slots slots starting at
237 static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
238 struct o2hb_bio_wait_ctxt *wc,
239 unsigned int start_slot,
240 unsigned int num_slots)
242 int i, nr_vecs, len, first_page, last_page;
243 unsigned int vec_len, vec_start;
244 unsigned int bits = reg->hr_block_bits;
245 unsigned int spp = reg->hr_slots_per_page;
249 nr_vecs = (num_slots + spp - 1) / spp;
251 /* Testing has shown this allocation to take long enough under
252 * GFP_KERNEL that the local node can get fenced. It would be
253 * nicest if we could pre-allocate these bios and avoid this
255 bio = bio_alloc(GFP_ATOMIC, nr_vecs);
257 mlog(ML_ERROR, "Could not alloc slots BIO!\n");
258 bio = ERR_PTR(-ENOMEM);
262 /* Must put everything in 512 byte sectors for the bio... */
263 bio->bi_sector = (reg->hr_start_block + start_slot) << (bits - 9);
264 bio->bi_bdev = reg->hr_bdev;
265 bio->bi_private = wc;
266 bio->bi_end_io = o2hb_bio_end_io;
268 first_page = start_slot / spp;
269 last_page = first_page + nr_vecs;
270 vec_start = (start_slot << bits) % PAGE_CACHE_SIZE;
271 for(i = first_page; i < last_page; i++) {
272 page = reg->hr_slot_data[i];
274 vec_len = PAGE_CACHE_SIZE;
275 /* last page might be short */
276 if (((i + 1) * spp) > (start_slot + num_slots))
277 vec_len = ((num_slots + start_slot) % spp) << bits;
278 vec_len -= vec_start;
280 mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
281 i, vec_len, vec_start);
283 len = bio_add_page(bio, page, vec_len, vec_start);
284 if (len != vec_len) {
288 mlog(ML_ERROR, "Error adding page to bio i = %d, "
289 "vec_len = %u, len = %d\n, start = %u\n",
290 i, vec_len, len, vec_start);
302 * Compute the maximum number of sectors the bdev can handle in one bio,
305 * Stolen from oracleasm, thanks Joel!
307 static int compute_max_sectors(struct block_device *bdev)
309 int max_pages, max_sectors, pow_two_sectors;
311 struct request_queue *q;
313 q = bdev_get_queue(bdev);
314 max_pages = q->max_sectors >> (PAGE_SHIFT - 9);
315 if (max_pages > BIO_MAX_PAGES)
316 max_pages = BIO_MAX_PAGES;
317 if (max_pages > q->max_phys_segments)
318 max_pages = q->max_phys_segments;
319 if (max_pages > q->max_hw_segments)
320 max_pages = q->max_hw_segments;
321 max_pages--; /* Handle I/Os that straddle a page */
323 max_sectors = max_pages << (PAGE_SHIFT - 9);
325 /* Why is fls() 1-based???? */
326 pow_two_sectors = 1 << (fls(max_sectors) - 1);
328 return pow_two_sectors;
331 static inline void o2hb_compute_request_limits(struct o2hb_region *reg,
332 unsigned int num_slots,
333 unsigned int *num_bios,
334 unsigned int *slots_per_bio)
336 unsigned int max_sectors, io_sectors;
338 max_sectors = compute_max_sectors(reg->hr_bdev);
340 io_sectors = num_slots << (reg->hr_block_bits - 9);
342 *num_bios = (io_sectors + max_sectors - 1) / max_sectors;
343 *slots_per_bio = max_sectors >> (reg->hr_block_bits - 9);
345 mlog(ML_HB_BIO, "My io size is %u sectors for %u slots. This "
346 "device can handle %u sectors of I/O\n", io_sectors, num_slots,
348 mlog(ML_HB_BIO, "Will need %u bios holding %u slots each\n",
349 *num_bios, *slots_per_bio);
352 static int o2hb_read_slots(struct o2hb_region *reg,
353 unsigned int max_slots)
355 unsigned int num_bios, slots_per_bio, start_slot, num_slots;
357 struct o2hb_bio_wait_ctxt wc;
361 o2hb_compute_request_limits(reg, max_slots, &num_bios, &slots_per_bio);
363 bios = kcalloc(num_bios, sizeof(struct bio *), GFP_KERNEL);
370 o2hb_bio_wait_init(&wc, num_bios);
372 num_slots = slots_per_bio;
373 for(i = 0; i < num_bios; i++) {
374 start_slot = i * slots_per_bio;
376 /* adjust num_slots at last bio */
377 if (max_slots < (start_slot + num_slots))
378 num_slots = max_slots - start_slot;
380 bio = o2hb_setup_one_bio(reg, &wc, start_slot, num_slots);
382 o2hb_bio_wait_dec(&wc, num_bios - i);
384 status = PTR_ERR(bio);
390 submit_bio(READ, bio);
396 o2hb_wait_on_io(reg, &wc);
397 if (wc.wc_error && !status)
398 status = wc.wc_error;
401 for(i = 0; i < num_bios; i++)
410 static int o2hb_issue_node_write(struct o2hb_region *reg,
411 struct bio **write_bio,
412 struct o2hb_bio_wait_ctxt *write_wc)
418 o2hb_bio_wait_init(write_wc, 1);
420 slot = o2nm_this_node();
422 bio = o2hb_setup_one_bio(reg, write_wc, slot, 1);
424 status = PTR_ERR(bio);
429 submit_bio(WRITE, bio);
437 static u32 o2hb_compute_block_crc_le(struct o2hb_region *reg,
438 struct o2hb_disk_heartbeat_block *hb_block)
443 /* We want to compute the block crc with a 0 value in the
444 * hb_cksum field. Save it off here and replace after the
446 old_cksum = hb_block->hb_cksum;
447 hb_block->hb_cksum = 0;
449 ret = crc32_le(0, (unsigned char *) hb_block, reg->hr_block_bytes);
451 hb_block->hb_cksum = old_cksum;
456 static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
458 mlog(ML_ERROR, "Dump slot information: seq = 0x%llx, node = %u, "
459 "cksum = 0x%x, generation 0x%llx\n",
460 (long long)le64_to_cpu(hb_block->hb_seq),
461 hb_block->hb_node, le32_to_cpu(hb_block->hb_cksum),
462 (long long)le64_to_cpu(hb_block->hb_generation));
465 static int o2hb_verify_crc(struct o2hb_region *reg,
466 struct o2hb_disk_heartbeat_block *hb_block)
470 read = le32_to_cpu(hb_block->hb_cksum);
471 computed = o2hb_compute_block_crc_le(reg, hb_block);
473 return read == computed;
476 /* We want to make sure that nobody is heartbeating on top of us --
477 * this will help detect an invalid configuration. */
478 static int o2hb_check_last_timestamp(struct o2hb_region *reg)
481 struct o2hb_disk_slot *slot;
482 struct o2hb_disk_heartbeat_block *hb_block;
484 node_num = o2nm_this_node();
487 slot = ®->hr_slots[node_num];
488 /* Don't check on our 1st timestamp */
489 if (slot->ds_last_time) {
490 hb_block = slot->ds_raw_block;
492 if (le64_to_cpu(hb_block->hb_seq) != slot->ds_last_time)
499 static inline void o2hb_prepare_block(struct o2hb_region *reg,
504 struct o2hb_disk_slot *slot;
505 struct o2hb_disk_heartbeat_block *hb_block;
507 node_num = o2nm_this_node();
508 slot = ®->hr_slots[node_num];
510 hb_block = (struct o2hb_disk_heartbeat_block *)slot->ds_raw_block;
511 memset(hb_block, 0, reg->hr_block_bytes);
512 /* TODO: time stuff */
513 cputime = CURRENT_TIME.tv_sec;
517 hb_block->hb_seq = cpu_to_le64(cputime);
518 hb_block->hb_node = node_num;
519 hb_block->hb_generation = cpu_to_le64(generation);
521 /* This step must always happen last! */
522 hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
525 mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x\n",
526 (long long)cpu_to_le64(generation),
527 le32_to_cpu(hb_block->hb_cksum));
530 static void o2hb_fire_callbacks(struct o2hb_callback *hbcall,
531 struct o2nm_node *node,
534 struct list_head *iter;
535 struct o2hb_callback_func *f;
537 list_for_each(iter, &hbcall->list) {
538 f = list_entry(iter, struct o2hb_callback_func, hc_item);
539 mlog(ML_HEARTBEAT, "calling funcs %p\n", f);
540 (f->hc_func)(node, idx, f->hc_data);
544 /* Will run the list in order until we process the passed event */
545 static void o2hb_run_event_list(struct o2hb_node_event *queued_event)
548 struct o2hb_callback *hbcall;
549 struct o2hb_node_event *event;
551 spin_lock(&o2hb_live_lock);
552 empty = list_empty(&queued_event->hn_item);
553 spin_unlock(&o2hb_live_lock);
557 /* Holding callback sem assures we don't alter the callback
558 * lists when doing this, and serializes ourselves with other
559 * processes wanting callbacks. */
560 down_write(&o2hb_callback_sem);
562 spin_lock(&o2hb_live_lock);
563 while (!list_empty(&o2hb_node_events)
564 && !list_empty(&queued_event->hn_item)) {
565 event = list_entry(o2hb_node_events.next,
566 struct o2hb_node_event,
568 list_del_init(&event->hn_item);
569 spin_unlock(&o2hb_live_lock);
571 mlog(ML_HEARTBEAT, "Node %s event for %d\n",
572 event->hn_event_type == O2HB_NODE_UP_CB ? "UP" : "DOWN",
575 hbcall = hbcall_from_type(event->hn_event_type);
577 /* We should *never* have gotten on to the list with a
578 * bad type... This isn't something that we should try
579 * to recover from. */
580 BUG_ON(IS_ERR(hbcall));
582 o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num);
584 spin_lock(&o2hb_live_lock);
586 spin_unlock(&o2hb_live_lock);
588 up_write(&o2hb_callback_sem);
591 static void o2hb_queue_node_event(struct o2hb_node_event *event,
592 enum o2hb_callback_type type,
593 struct o2nm_node *node,
596 assert_spin_locked(&o2hb_live_lock);
598 event->hn_event_type = type;
599 event->hn_node = node;
600 event->hn_node_num = node_num;
602 mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
603 type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
605 list_add_tail(&event->hn_item, &o2hb_node_events);
608 static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
610 struct o2hb_node_event event =
611 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
612 struct o2nm_node *node;
614 node = o2nm_get_node_by_num(slot->ds_node_num);
618 spin_lock(&o2hb_live_lock);
619 if (!list_empty(&slot->ds_live_item)) {
620 mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
623 list_del_init(&slot->ds_live_item);
625 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
626 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
628 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
632 spin_unlock(&o2hb_live_lock);
634 o2hb_run_event_list(&event);
639 static int o2hb_check_slot(struct o2hb_region *reg,
640 struct o2hb_disk_slot *slot)
642 int changed = 0, gen_changed = 0;
643 struct o2hb_node_event event =
644 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
645 struct o2nm_node *node;
646 struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
649 memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
651 /* Is this correct? Do we assume that the node doesn't exist
652 * if we're not configured for him? */
653 node = o2nm_get_node_by_num(slot->ds_node_num);
657 if (!o2hb_verify_crc(reg, hb_block)) {
658 /* all paths from here will drop o2hb_live_lock for
660 spin_lock(&o2hb_live_lock);
662 /* Don't print an error on the console in this case -
663 * a freshly formatted heartbeat area will not have a
665 if (list_empty(&slot->ds_live_item))
668 /* The node is live but pushed out a bad crc. We
669 * consider it a transient miss but don't populate any
670 * other values as they may be junk. */
671 mlog(ML_ERROR, "Node %d has written a bad crc to %s\n",
672 slot->ds_node_num, reg->hr_dev_name);
673 o2hb_dump_slot(hb_block);
675 slot->ds_equal_samples++;
679 /* we don't care if these wrap.. the state transitions below
680 * clear at the right places */
681 cputime = le64_to_cpu(hb_block->hb_seq);
682 if (slot->ds_last_time != cputime)
683 slot->ds_changed_samples++;
685 slot->ds_equal_samples++;
686 slot->ds_last_time = cputime;
688 /* The node changed heartbeat generations. We assume this to
689 * mean it dropped off but came back before we timed out. We
690 * want to consider it down for the time being but don't want
691 * to lose any changed_samples state we might build up to
692 * considering it live again. */
693 if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
695 slot->ds_equal_samples = 0;
696 mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
697 "to 0x%llx)\n", slot->ds_node_num,
698 (long long)slot->ds_last_generation,
699 (long long)le64_to_cpu(hb_block->hb_generation));
702 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
704 mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
705 "seq %llu last %llu changed %u equal %u\n",
706 slot->ds_node_num, (long long)slot->ds_last_generation,
707 le32_to_cpu(hb_block->hb_cksum),
708 (unsigned long long)le64_to_cpu(hb_block->hb_seq),
709 (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
710 slot->ds_equal_samples);
712 spin_lock(&o2hb_live_lock);
715 /* dead nodes only come to life after some number of
716 * changes at any time during their dead time */
717 if (list_empty(&slot->ds_live_item) &&
718 slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
719 mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
720 slot->ds_node_num, (long long)slot->ds_last_generation);
722 /* first on the list generates a callback */
723 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
724 set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
726 o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
732 list_add_tail(&slot->ds_live_item,
733 &o2hb_live_slots[slot->ds_node_num]);
735 slot->ds_equal_samples = 0;
739 /* if the list is dead, we're done.. */
740 if (list_empty(&slot->ds_live_item))
743 /* live nodes only go dead after enough consequtive missed
744 * samples.. reset the missed counter whenever we see
746 if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
747 mlog(ML_HEARTBEAT, "Node %d left my region\n",
750 /* last off the live_slot generates a callback */
751 list_del_init(&slot->ds_live_item);
752 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
753 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
755 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
761 /* We don't clear this because the node is still
762 * actually writing new blocks. */
764 slot->ds_changed_samples = 0;
767 if (slot->ds_changed_samples) {
768 slot->ds_changed_samples = 0;
769 slot->ds_equal_samples = 0;
772 spin_unlock(&o2hb_live_lock);
774 o2hb_run_event_list(&event);
780 /* This could be faster if we just implmented a find_last_bit, but I
781 * don't think the circumstances warrant it. */
782 static int o2hb_highest_node(unsigned long *nodes,
789 while ((node = find_next_bit(nodes, numbits, node + 1)) != -1) {
799 static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
801 int i, ret, highest_node, change = 0;
802 unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
803 struct bio *write_bio;
804 struct o2hb_bio_wait_ctxt write_wc;
806 ret = o2nm_configured_node_map(configured_nodes,
807 sizeof(configured_nodes));
813 highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
814 if (highest_node >= O2NM_MAX_NODES) {
815 mlog(ML_NOTICE, "ocfs2_heartbeat: no configured nodes found!\n");
819 /* No sense in reading the slots of nodes that don't exist
820 * yet. Of course, if the node definitions have holes in them
821 * then we're reading an empty slot anyway... Consider this
823 ret = o2hb_read_slots(reg, highest_node + 1);
829 /* With an up to date view of the slots, we can check that no
830 * other node has been improperly configured to heartbeat in
832 if (!o2hb_check_last_timestamp(reg))
833 mlog(ML_ERROR, "Device \"%s\": another node is heartbeating "
834 "in our slot!\n", reg->hr_dev_name);
836 /* fill in the proper info for our next heartbeat */
837 o2hb_prepare_block(reg, reg->hr_generation);
839 /* And fire off the write. Note that we don't wait on this I/O
841 ret = o2hb_issue_node_write(reg, &write_bio, &write_wc);
848 while((i = find_next_bit(configured_nodes, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
850 change |= o2hb_check_slot(reg, ®->hr_slots[i]);
854 * We have to be sure we've advertised ourselves on disk
855 * before we can go to steady state. This ensures that
856 * people we find in our steady state have seen us.
858 o2hb_wait_on_io(reg, &write_wc);
860 if (write_wc.wc_error) {
861 /* Do not re-arm the write timeout on I/O error - we
862 * can't be sure that the new block ever made it to
864 mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
865 write_wc.wc_error, reg->hr_dev_name);
866 return write_wc.wc_error;
869 o2hb_arm_write_timeout(reg);
871 /* let the person who launched us know when things are steady */
872 if (!change && (atomic_read(®->hr_steady_iterations) != 0)) {
873 if (atomic_dec_and_test(®->hr_steady_iterations))
874 wake_up(&o2hb_steady_queue);
880 /* Subtract b from a, storing the result in a. a *must* have a larger
882 static void o2hb_tv_subtract(struct timeval *a,
885 /* just return 0 when a is after b */
886 if (a->tv_sec < b->tv_sec ||
887 (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) {
893 a->tv_sec -= b->tv_sec;
894 a->tv_usec -= b->tv_usec;
895 while ( a->tv_usec < 0 ) {
897 a->tv_usec += 1000000;
901 static unsigned int o2hb_elapsed_msecs(struct timeval *start,
904 struct timeval res = *end;
906 o2hb_tv_subtract(&res, start);
908 return res.tv_sec * 1000 + res.tv_usec / 1000;
912 * we ride the region ref that the region dir holds. before the region
913 * dir is removed and drops it ref it will wait to tear down this
916 static int o2hb_thread(void *data)
919 struct o2hb_region *reg = data;
920 struct bio *write_bio;
921 struct o2hb_bio_wait_ctxt write_wc;
922 struct timeval before_hb, after_hb;
923 unsigned int elapsed_msec;
925 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
927 set_user_nice(current, -20);
929 while (!kthread_should_stop() && !reg->hr_unclean_stop) {
930 /* We track the time spent inside
931 * o2hb_do_disk_heartbeat so that we avoid more then
932 * hr_timeout_ms between disk writes. On busy systems
933 * this should result in a heartbeat which is less
934 * likely to time itself out. */
935 do_gettimeofday(&before_hb);
939 ret = o2hb_do_disk_heartbeat(reg);
940 } while (ret && ++i < 2);
942 do_gettimeofday(&after_hb);
943 elapsed_msec = o2hb_elapsed_msecs(&before_hb, &after_hb);
945 mlog(0, "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
946 before_hb.tv_sec, (unsigned long) before_hb.tv_usec,
947 after_hb.tv_sec, (unsigned long) after_hb.tv_usec,
950 if (elapsed_msec < reg->hr_timeout_ms) {
951 /* the kthread api has blocked signals for us so no
952 * need to record the return value. */
953 msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
957 o2hb_disarm_write_timeout(reg);
959 /* unclean stop is only used in very bad situation */
960 for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
961 o2hb_shutdown_slot(®->hr_slots[i]);
963 /* Explicit down notification - avoid forcing the other nodes
964 * to timeout on this region when we could just as easily
965 * write a clear generation - thus indicating to them that
966 * this node has left this region.
968 * XXX: Should we skip this on unclean_stop? */
969 o2hb_prepare_block(reg, 0);
970 ret = o2hb_issue_node_write(reg, &write_bio, &write_wc);
972 o2hb_wait_on_io(reg, &write_wc);
978 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread exiting\n");
987 for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
988 INIT_LIST_HEAD(&o2hb_callbacks[i].list);
990 for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
991 INIT_LIST_HEAD(&o2hb_live_slots[i]);
993 INIT_LIST_HEAD(&o2hb_node_events);
995 memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
998 /* if we're already in a callback then we're already serialized by the sem */
999 static void o2hb_fill_node_map_from_callback(unsigned long *map,
1002 BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
1004 memcpy(map, &o2hb_live_node_bitmap, bytes);
1008 * get a map of all nodes that are heartbeating in any regions
1010 void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
1012 /* callers want to serialize this map and callbacks so that they
1013 * can trust that they don't miss nodes coming to the party */
1014 down_read(&o2hb_callback_sem);
1015 spin_lock(&o2hb_live_lock);
1016 o2hb_fill_node_map_from_callback(map, bytes);
1017 spin_unlock(&o2hb_live_lock);
1018 up_read(&o2hb_callback_sem);
1020 EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
1023 * heartbeat configfs bits. The heartbeat set is a default set under
1024 * the cluster set in nodemanager.c.
1027 static struct o2hb_region *to_o2hb_region(struct config_item *item)
1029 return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
1032 /* drop_item only drops its ref after killing the thread, nothing should
1033 * be using the region anymore. this has to clean up any state that
1034 * attributes might have built up. */
1035 static void o2hb_region_release(struct config_item *item)
1039 struct o2hb_region *reg = to_o2hb_region(item);
1041 if (reg->hr_tmp_block)
1042 kfree(reg->hr_tmp_block);
1044 if (reg->hr_slot_data) {
1045 for (i = 0; i < reg->hr_num_pages; i++) {
1046 page = reg->hr_slot_data[i];
1050 kfree(reg->hr_slot_data);
1054 blkdev_put(reg->hr_bdev);
1057 kfree(reg->hr_slots);
1059 spin_lock(&o2hb_live_lock);
1060 list_del(®->hr_all_item);
1061 spin_unlock(&o2hb_live_lock);
1066 static int o2hb_read_block_input(struct o2hb_region *reg,
1069 unsigned long *ret_bytes,
1070 unsigned int *ret_bits)
1072 unsigned long bytes;
1073 char *p = (char *)page;
1075 bytes = simple_strtoul(p, &p, 0);
1076 if (!p || (*p && (*p != '\n')))
1079 /* Heartbeat and fs min / max block sizes are the same. */
1080 if (bytes > 4096 || bytes < 512)
1082 if (hweight16(bytes) != 1)
1088 *ret_bits = ffs(bytes) - 1;
1093 static ssize_t o2hb_region_block_bytes_read(struct o2hb_region *reg,
1096 return sprintf(page, "%u\n", reg->hr_block_bytes);
1099 static ssize_t o2hb_region_block_bytes_write(struct o2hb_region *reg,
1104 unsigned long block_bytes;
1105 unsigned int block_bits;
1110 status = o2hb_read_block_input(reg, page, count,
1111 &block_bytes, &block_bits);
1115 reg->hr_block_bytes = (unsigned int)block_bytes;
1116 reg->hr_block_bits = block_bits;
1121 static ssize_t o2hb_region_start_block_read(struct o2hb_region *reg,
1124 return sprintf(page, "%llu\n", reg->hr_start_block);
1127 static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
1131 unsigned long long tmp;
1132 char *p = (char *)page;
1137 tmp = simple_strtoull(p, &p, 0);
1138 if (!p || (*p && (*p != '\n')))
1141 reg->hr_start_block = tmp;
1146 static ssize_t o2hb_region_blocks_read(struct o2hb_region *reg,
1149 return sprintf(page, "%d\n", reg->hr_blocks);
1152 static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
1157 char *p = (char *)page;
1162 tmp = simple_strtoul(p, &p, 0);
1163 if (!p || (*p && (*p != '\n')))
1166 if (tmp > O2NM_MAX_NODES || tmp == 0)
1169 reg->hr_blocks = (unsigned int)tmp;
1174 static ssize_t o2hb_region_dev_read(struct o2hb_region *reg,
1177 unsigned int ret = 0;
1180 ret = sprintf(page, "%s\n", reg->hr_dev_name);
1185 static void o2hb_init_region_params(struct o2hb_region *reg)
1187 reg->hr_slots_per_page = PAGE_CACHE_SIZE >> reg->hr_block_bits;
1188 reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
1190 mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
1191 reg->hr_start_block, reg->hr_blocks);
1192 mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
1193 reg->hr_block_bytes, reg->hr_block_bits);
1194 mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
1195 mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
1198 static int o2hb_map_slot_data(struct o2hb_region *reg)
1201 unsigned int last_slot;
1202 unsigned int spp = reg->hr_slots_per_page;
1205 struct o2hb_disk_slot *slot;
1207 reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
1208 if (reg->hr_tmp_block == NULL) {
1209 mlog_errno(-ENOMEM);
1213 reg->hr_slots = kcalloc(reg->hr_blocks,
1214 sizeof(struct o2hb_disk_slot), GFP_KERNEL);
1215 if (reg->hr_slots == NULL) {
1216 mlog_errno(-ENOMEM);
1220 for(i = 0; i < reg->hr_blocks; i++) {
1221 slot = ®->hr_slots[i];
1222 slot->ds_node_num = i;
1223 INIT_LIST_HEAD(&slot->ds_live_item);
1224 slot->ds_raw_block = NULL;
1227 reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
1228 mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
1229 "at %u blocks per page\n",
1230 reg->hr_num_pages, reg->hr_blocks, spp);
1232 reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
1234 if (!reg->hr_slot_data) {
1235 mlog_errno(-ENOMEM);
1239 for(i = 0; i < reg->hr_num_pages; i++) {
1240 page = alloc_page(GFP_KERNEL);
1242 mlog_errno(-ENOMEM);
1246 reg->hr_slot_data[i] = page;
1248 last_slot = i * spp;
1249 raw = page_address(page);
1251 (j < spp) && ((j + last_slot) < reg->hr_blocks);
1253 BUG_ON((j + last_slot) >= reg->hr_blocks);
1255 slot = ®->hr_slots[j + last_slot];
1256 slot->ds_raw_block =
1257 (struct o2hb_disk_heartbeat_block *) raw;
1259 raw += reg->hr_block_bytes;
1266 /* Read in all the slots available and populate the tracking
1267 * structures so that we can start with a baseline idea of what's
1269 static int o2hb_populate_slot_data(struct o2hb_region *reg)
1272 struct o2hb_disk_slot *slot;
1273 struct o2hb_disk_heartbeat_block *hb_block;
1277 ret = o2hb_read_slots(reg, reg->hr_blocks);
1283 /* We only want to get an idea of the values initially in each
1284 * slot, so we do no verification - o2hb_check_slot will
1285 * actually determine if each configured slot is valid and
1286 * whether any values have changed. */
1287 for(i = 0; i < reg->hr_blocks; i++) {
1288 slot = ®->hr_slots[i];
1289 hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
1291 /* Only fill the values that o2hb_check_slot uses to
1292 * determine changing slots */
1293 slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
1294 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
1302 /* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1303 static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
1309 char *p = (char *)page;
1310 struct file *filp = NULL;
1311 struct inode *inode = NULL;
1312 ssize_t ret = -EINVAL;
1317 /* We can't heartbeat without having had our node number
1318 * configured yet. */
1319 if (o2nm_this_node() == O2NM_MAX_NODES)
1322 fd = simple_strtol(p, &p, 0);
1323 if (!p || (*p && (*p != '\n')))
1326 if (fd < 0 || fd >= INT_MAX)
1333 if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
1334 reg->hr_block_bytes == 0)
1337 inode = igrab(filp->f_mapping->host);
1341 if (!S_ISBLK(inode->i_mode))
1344 reg->hr_bdev = I_BDEV(filp->f_mapping->host);
1345 ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ, 0);
1347 reg->hr_bdev = NULL;
1352 bdevname(reg->hr_bdev, reg->hr_dev_name);
1354 sectsize = bdev_hardsect_size(reg->hr_bdev);
1355 if (sectsize != reg->hr_block_bytes) {
1357 "blocksize %u incorrect for device, expected %d",
1358 reg->hr_block_bytes, sectsize);
1363 o2hb_init_region_params(reg);
1365 /* Generation of zero is invalid */
1367 get_random_bytes(®->hr_generation,
1368 sizeof(reg->hr_generation));
1369 } while (reg->hr_generation == 0);
1371 ret = o2hb_map_slot_data(reg);
1377 ret = o2hb_populate_slot_data(reg);
1383 INIT_WORK(®->hr_write_timeout_work, o2hb_write_timeout, reg);
1386 * A node is considered live after it has beat LIVE_THRESHOLD
1387 * times. We're not steady until we've given them a chance
1388 * _after_ our first read.
1390 atomic_set(®->hr_steady_iterations, O2HB_LIVE_THRESHOLD + 1);
1392 reg->hr_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
1393 reg->hr_item.ci_name);
1394 if (IS_ERR(reg->hr_task)) {
1395 ret = PTR_ERR(reg->hr_task);
1397 reg->hr_task = NULL;
1401 ret = wait_event_interruptible(o2hb_steady_queue,
1402 atomic_read(®->hr_steady_iterations) == 0);
1404 kthread_stop(reg->hr_task);
1405 reg->hr_task = NULL;
1417 blkdev_put(reg->hr_bdev);
1418 reg->hr_bdev = NULL;
1424 struct o2hb_region_attribute {
1425 struct configfs_attribute attr;
1426 ssize_t (*show)(struct o2hb_region *, char *);
1427 ssize_t (*store)(struct o2hb_region *, const char *, size_t);
1430 static struct o2hb_region_attribute o2hb_region_attr_block_bytes = {
1431 .attr = { .ca_owner = THIS_MODULE,
1432 .ca_name = "block_bytes",
1433 .ca_mode = S_IRUGO | S_IWUSR },
1434 .show = o2hb_region_block_bytes_read,
1435 .store = o2hb_region_block_bytes_write,
1438 static struct o2hb_region_attribute o2hb_region_attr_start_block = {
1439 .attr = { .ca_owner = THIS_MODULE,
1440 .ca_name = "start_block",
1441 .ca_mode = S_IRUGO | S_IWUSR },
1442 .show = o2hb_region_start_block_read,
1443 .store = o2hb_region_start_block_write,
1446 static struct o2hb_region_attribute o2hb_region_attr_blocks = {
1447 .attr = { .ca_owner = THIS_MODULE,
1448 .ca_name = "blocks",
1449 .ca_mode = S_IRUGO | S_IWUSR },
1450 .show = o2hb_region_blocks_read,
1451 .store = o2hb_region_blocks_write,
1454 static struct o2hb_region_attribute o2hb_region_attr_dev = {
1455 .attr = { .ca_owner = THIS_MODULE,
1457 .ca_mode = S_IRUGO | S_IWUSR },
1458 .show = o2hb_region_dev_read,
1459 .store = o2hb_region_dev_write,
1462 static struct configfs_attribute *o2hb_region_attrs[] = {
1463 &o2hb_region_attr_block_bytes.attr,
1464 &o2hb_region_attr_start_block.attr,
1465 &o2hb_region_attr_blocks.attr,
1466 &o2hb_region_attr_dev.attr,
1470 static ssize_t o2hb_region_show(struct config_item *item,
1471 struct configfs_attribute *attr,
1474 struct o2hb_region *reg = to_o2hb_region(item);
1475 struct o2hb_region_attribute *o2hb_region_attr =
1476 container_of(attr, struct o2hb_region_attribute, attr);
1479 if (o2hb_region_attr->show)
1480 ret = o2hb_region_attr->show(reg, page);
1484 static ssize_t o2hb_region_store(struct config_item *item,
1485 struct configfs_attribute *attr,
1486 const char *page, size_t count)
1488 struct o2hb_region *reg = to_o2hb_region(item);
1489 struct o2hb_region_attribute *o2hb_region_attr =
1490 container_of(attr, struct o2hb_region_attribute, attr);
1491 ssize_t ret = -EINVAL;
1493 if (o2hb_region_attr->store)
1494 ret = o2hb_region_attr->store(reg, page, count);
1498 static struct configfs_item_operations o2hb_region_item_ops = {
1499 .release = o2hb_region_release,
1500 .show_attribute = o2hb_region_show,
1501 .store_attribute = o2hb_region_store,
1504 static struct config_item_type o2hb_region_type = {
1505 .ct_item_ops = &o2hb_region_item_ops,
1506 .ct_attrs = o2hb_region_attrs,
1507 .ct_owner = THIS_MODULE,
1512 struct o2hb_heartbeat_group {
1513 struct config_group hs_group;
1517 static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
1520 container_of(group, struct o2hb_heartbeat_group, hs_group)
1524 static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
1527 struct o2hb_region *reg = NULL;
1528 struct config_item *ret = NULL;
1530 reg = kcalloc(1, sizeof(struct o2hb_region), GFP_KERNEL);
1532 goto out; /* ENOMEM */
1534 config_item_init_type_name(®->hr_item, name, &o2hb_region_type);
1536 ret = ®->hr_item;
1538 spin_lock(&o2hb_live_lock);
1539 list_add_tail(®->hr_all_item, &o2hb_all_regions);
1540 spin_unlock(&o2hb_live_lock);
1548 static void o2hb_heartbeat_group_drop_item(struct config_group *group,
1549 struct config_item *item)
1551 struct o2hb_region *reg = to_o2hb_region(item);
1553 /* stop the thread when the user removes the region dir */
1555 kthread_stop(reg->hr_task);
1556 reg->hr_task = NULL;
1559 config_item_put(item);
1562 struct o2hb_heartbeat_group_attribute {
1563 struct configfs_attribute attr;
1564 ssize_t (*show)(struct o2hb_heartbeat_group *, char *);
1565 ssize_t (*store)(struct o2hb_heartbeat_group *, const char *, size_t);
1568 static ssize_t o2hb_heartbeat_group_show(struct config_item *item,
1569 struct configfs_attribute *attr,
1572 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1573 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1574 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1577 if (o2hb_heartbeat_group_attr->show)
1578 ret = o2hb_heartbeat_group_attr->show(reg, page);
1582 static ssize_t o2hb_heartbeat_group_store(struct config_item *item,
1583 struct configfs_attribute *attr,
1584 const char *page, size_t count)
1586 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1587 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1588 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1589 ssize_t ret = -EINVAL;
1591 if (o2hb_heartbeat_group_attr->store)
1592 ret = o2hb_heartbeat_group_attr->store(reg, page, count);
1596 static ssize_t o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group *group,
1599 return sprintf(page, "%u\n", o2hb_dead_threshold);
1602 static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group *group,
1607 char *p = (char *)page;
1609 tmp = simple_strtoul(p, &p, 10);
1610 if (!p || (*p && (*p != '\n')))
1613 /* this will validate ranges for us. */
1614 o2hb_dead_threshold_set((unsigned int) tmp);
1619 static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold = {
1620 .attr = { .ca_owner = THIS_MODULE,
1621 .ca_name = "dead_threshold",
1622 .ca_mode = S_IRUGO | S_IWUSR },
1623 .show = o2hb_heartbeat_group_threshold_show,
1624 .store = o2hb_heartbeat_group_threshold_store,
1627 static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
1628 &o2hb_heartbeat_group_attr_threshold.attr,
1632 static struct configfs_item_operations o2hb_hearbeat_group_item_ops = {
1633 .show_attribute = o2hb_heartbeat_group_show,
1634 .store_attribute = o2hb_heartbeat_group_store,
1637 static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
1638 .make_item = o2hb_heartbeat_group_make_item,
1639 .drop_item = o2hb_heartbeat_group_drop_item,
1642 static struct config_item_type o2hb_heartbeat_group_type = {
1643 .ct_group_ops = &o2hb_heartbeat_group_group_ops,
1644 .ct_item_ops = &o2hb_hearbeat_group_item_ops,
1645 .ct_attrs = o2hb_heartbeat_group_attrs,
1646 .ct_owner = THIS_MODULE,
1649 /* this is just here to avoid touching group in heartbeat.h which the
1650 * entire damn world #includes */
1651 struct config_group *o2hb_alloc_hb_set(void)
1653 struct o2hb_heartbeat_group *hs = NULL;
1654 struct config_group *ret = NULL;
1656 hs = kcalloc(1, sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
1660 config_group_init_type_name(&hs->hs_group, "heartbeat",
1661 &o2hb_heartbeat_group_type);
1663 ret = &hs->hs_group;
1670 void o2hb_free_hb_set(struct config_group *group)
1672 struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
1676 /* hb callback registration and issueing */
1678 static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
1680 if (type == O2HB_NUM_CB)
1681 return ERR_PTR(-EINVAL);
1683 return &o2hb_callbacks[type];
1686 void o2hb_setup_callback(struct o2hb_callback_func *hc,
1687 enum o2hb_callback_type type,
1692 INIT_LIST_HEAD(&hc->hc_item);
1695 hc->hc_priority = priority;
1697 hc->hc_magic = O2HB_CB_MAGIC;
1699 EXPORT_SYMBOL_GPL(o2hb_setup_callback);
1701 int o2hb_register_callback(struct o2hb_callback_func *hc)
1703 struct o2hb_callback_func *tmp;
1704 struct list_head *iter;
1705 struct o2hb_callback *hbcall;
1708 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
1709 BUG_ON(!list_empty(&hc->hc_item));
1711 hbcall = hbcall_from_type(hc->hc_type);
1712 if (IS_ERR(hbcall)) {
1713 ret = PTR_ERR(hbcall);
1717 down_write(&o2hb_callback_sem);
1719 list_for_each(iter, &hbcall->list) {
1720 tmp = list_entry(iter, struct o2hb_callback_func, hc_item);
1721 if (hc->hc_priority < tmp->hc_priority) {
1722 list_add_tail(&hc->hc_item, iter);
1726 if (list_empty(&hc->hc_item))
1727 list_add_tail(&hc->hc_item, &hbcall->list);
1729 up_write(&o2hb_callback_sem);
1732 mlog(ML_HEARTBEAT, "returning %d on behalf of %p for funcs %p\n",
1733 ret, __builtin_return_address(0), hc);
1736 EXPORT_SYMBOL_GPL(o2hb_register_callback);
1738 int o2hb_unregister_callback(struct o2hb_callback_func *hc)
1740 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
1742 mlog(ML_HEARTBEAT, "on behalf of %p for funcs %p\n",
1743 __builtin_return_address(0), hc);
1745 if (list_empty(&hc->hc_item))
1748 down_write(&o2hb_callback_sem);
1750 list_del_init(&hc->hc_item);
1752 up_write(&o2hb_callback_sem);
1756 EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
1758 int o2hb_check_node_heartbeating(u8 node_num)
1760 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1762 o2hb_fill_node_map(testing_map, sizeof(testing_map));
1763 if (!test_bit(node_num, testing_map)) {
1765 "node (%u) does not have heartbeating enabled.\n",
1772 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating);
1774 int o2hb_check_node_heartbeating_from_callback(u8 node_num)
1776 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1778 o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
1779 if (!test_bit(node_num, testing_map)) {
1781 "node (%u) does not have heartbeating enabled.\n",
1788 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
1790 /* Makes sure our local node is configured with a node number, and is
1792 int o2hb_check_local_node_heartbeating(void)
1796 /* if this node was set then we have networking */
1797 node_num = o2nm_this_node();
1798 if (node_num == O2NM_MAX_NODES) {
1799 mlog(ML_HEARTBEAT, "this node has not been configured.\n");
1803 return o2hb_check_node_heartbeating(node_num);
1805 EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating);
1808 * this is just a hack until we get the plumbing which flips file systems
1809 * read only and drops the hb ref instead of killing the node dead.
1811 void o2hb_stop_all_regions(void)
1813 struct o2hb_region *reg;
1815 mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
1817 spin_lock(&o2hb_live_lock);
1819 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
1820 reg->hr_unclean_stop = 1;
1822 spin_unlock(&o2hb_live_lock);
1824 EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);