/* * The first heartbeat pass had one global thread that would serialize all hb * callback calls. This global serializing sem should only be removed once * we've made sure that all callees can deal with being called concurrently * from multiple hb region threads.
*/ static DECLARE_RWSEM(o2hb_callback_sem);
/* * multiple hb threads are watching multiple regions. A node is live * whenever any of the threads sees activity from the node in its region.
*/ static DEFINE_SPINLOCK(o2hb_live_lock); staticstruct list_head o2hb_live_slots[O2NM_MAX_NODES]; staticunsignedlong o2hb_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)]; static LIST_HEAD(o2hb_node_events); static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue);
/* * In global heartbeat, we maintain a series of region bitmaps. * - o2hb_region_bitmap allows us to limit the region number to max region. * - o2hb_live_region_bitmap tracks live regions (seen steady iterations). * - o2hb_quorum_region_bitmap tracks live regions that have seen all nodes * heartbeat on it. * - o2hb_failed_region_bitmap tracks the regions that have seen io timeouts.
*/ staticunsignedlong o2hb_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)]; staticunsignedlong o2hb_live_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)]; staticunsignedlong o2hb_quorum_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)]; staticunsignedlong o2hb_failed_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
/* * o2hb_dependent_users tracks the number of registered callbacks that depend * on heartbeat. o2net and o2dlm are two entities that register this callback. * However only o2dlm depends on the heartbeat. It does not want the heartbeat * to stop while a dlm domain is still active.
*/ staticunsignedint o2hb_dependent_users;
/* * In global heartbeat mode, all regions are pinned if there are one or more * dependent users and the quorum region count is <= O2HB_PIN_CUT_OFF. All * regions are unpinned if the region count exceeds the cut off or the number * of dependent users falls to zero.
*/ #define O2HB_PIN_CUT_OFF 3
/* * In local heartbeat mode, we assume the dlm domain name to be the same as * region uuid. This is true for domains created for the file system but not * necessarily true for userdlm domains. This is a known limitation. * * In global heartbeat mode, we pin/unpin all o2hb regions. This solution * works for both file system and userdlm domains.
*/ staticint o2hb_region_pin(constchar *region_uuid); staticvoid o2hb_region_unpin(constchar *region_uuid);
/* Only sets a new threshold if there are no active regions. * * No locking or otherwise interesting code is required for reading * o2hb_dead_threshold as it can't change once regions are active and
* it's not interesting to anyone until then anyway. */ staticvoid o2hb_dead_threshold_set(unsignedint threshold)
{ if (threshold > O2HB_MIN_DEAD_THRESHOLD) {
spin_lock(&o2hb_live_lock); if (list_empty(&o2hb_all_regions))
o2hb_dead_threshold = threshold;
spin_unlock(&o2hb_live_lock);
}
}
staticint o2hb_global_heartbeat_mode_set(unsignedint hb_mode)
{ int ret = -1;
if (hb_mode < O2HB_HEARTBEAT_NUM_MODES) {
spin_lock(&o2hb_live_lock); if (list_empty(&o2hb_all_regions)) {
o2hb_heartbeat_mode = hb_mode;
ret = 0;
}
spin_unlock(&o2hb_live_lock);
}
/* each thread owns a region.. when we're asked to tear down the region
* we ask the thread to stop, who cleans up the region */ struct o2hb_region { struct config_item hr_item;
/* let the person setting up hb wait for it to return until it * has reached a 'steady' state. This will be fixed when we have
* a more complete api that doesn't lead to this sort of fragility. */
atomic_t hr_steady_iterations;
/* terminate o2hb thread if it does not reach steady state
* (hr_steady_iterations == 0) within hr_unsteady_iterations */
atomic_t hr_unsteady_iterations;
unsignedint hr_timeout_ms;
/* randomized as the region goes up and down so that a node
* recognizes a node going up and down in one iteration */
u64 hr_generation;
/* negotiate timer, used to negotiate extending hb timeout. */ struct delayed_work hr_nego_timeout_work; unsignedlong hr_nego_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
/* Used during o2hb_check_slot to hold a copy of the block * being checked because we temporarily have to zero out the
* crc field. */ struct o2hb_disk_heartbeat_block *hr_tmp_block;
mlog(ML_ERROR, "Heartbeat write timeout to device %pg after %u " "milliseconds\n", reg_bdev(reg),
jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
if (o2hb_global_heartbeat_active()) {
spin_lock(&o2hb_live_lock); if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
set_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
failed = bitmap_weight(o2hb_failed_region_bitmap,
O2NM_MAX_REGIONS);
quorum = bitmap_weight(o2hb_quorum_region_bitmap,
O2NM_MAX_REGIONS);
spin_unlock(&o2hb_live_lock);
mlog(ML_HEARTBEAT, "Number of regions %d, failed regions %d\n",
quorum, failed);
/* * Fence if the number of failed regions >= half the number * of quorum regions
*/ if ((failed << 1) < quorum) return;
}
o2quo_disk_timeout();
}
staticvoid o2hb_arm_timeout(struct o2hb_region *reg)
{ /* Arm writeout only after thread reaches steady state */ if (atomic_read(®->hr_steady_iterations) != 0) return;
mlog(ML_HEARTBEAT, "Queue write timeout for %u ms\n",
O2HB_MAX_WRITE_TIMEOUT_MS);
cancel_delayed_work(®->hr_nego_timeout_work); /* negotiate timeout must be less than write timeout. */
schedule_delayed_work(®->hr_nego_timeout_work,
msecs_to_jiffies(O2HB_NEGO_TIMEOUT_MS));
bitmap_zero(reg->hr_nego_node_bitmap, O2NM_MAX_NODES);
}
if (ret == -EAGAIN || ret == -ENOMEM) {
msleep(100); goto again;
}
return ret;
}
staticvoid o2hb_nego_timeout(struct work_struct *work)
{ unsignedlong live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)]; int master_node, i, ret; struct o2hb_region *reg;
reg = container_of(work, struct o2hb_region, hr_nego_timeout_work.work); /* don't negotiate timeout if last hb failed since it is very * possible io failed. Should let write timeout fence self.
*/ if (reg->hr_last_hb_status) return;
o2hb_fill_node_map(live_node_bitmap, O2NM_MAX_NODES); /* lowest node as master node to make negotiate decision. */
master_node = find_first_bit(live_node_bitmap, O2NM_MAX_NODES);
if (master_node == o2nm_this_node()) { if (!test_bit(master_node, reg->hr_nego_node_bitmap)) {
printk(KERN_NOTICE "o2hb: node %d hb write hung for %ds on region %s (%pg).\n",
o2nm_this_node(), O2HB_NEGO_TIMEOUT_MS/1000,
config_item_name(®->hr_item), reg_bdev(reg));
set_bit(master_node, reg->hr_nego_node_bitmap);
} if (!bitmap_equal(reg->hr_nego_node_bitmap, live_node_bitmap,
O2NM_MAX_NODES)) { /* check negotiate bitmap every second to do timeout * approve decision.
*/
schedule_delayed_work(®->hr_nego_timeout_work,
msecs_to_jiffies(1000));
return;
}
printk(KERN_NOTICE "o2hb: all nodes hb write hung, maybe region %s (%pg) is down.\n",
config_item_name(®->hr_item),
reg_bdev(reg)); /* approve negotiate timeout request. */
o2hb_arm_timeout(reg);
i = -1; while ((i = find_next_bit(live_node_bitmap,
O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) { if (i == master_node) continue;
mlog(ML_HEARTBEAT, "send NEGO_APPROVE msg to node %d\n", i);
ret = o2hb_send_nego_msg(reg->hr_key,
O2HB_NEGO_APPROVE_MSG, i); if (ret)
mlog(ML_ERROR, "send NEGO_APPROVE msg to node %d fail %d\n",
i, ret);
}
} else { /* negotiate timeout with master node. */
printk(KERN_NOTICE "o2hb: node %d hb write hung for %ds on region %s (%pg), negotiate timeout with node %d.\n",
o2nm_this_node(), O2HB_NEGO_TIMEOUT_MS/1000, config_item_name(®->hr_item),
reg_bdev(reg), master_node);
ret = o2hb_send_nego_msg(reg->hr_key, O2HB_NEGO_TIMEOUT_MSG,
master_node); if (ret)
mlog(ML_ERROR, "send NEGO_TIMEOUT msg to node %d fail %d\n",
master_node, ret);
}
}
/* Used in error paths too */ staticinlinevoid o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt *wc, unsignedint num)
{ /* sadly atomic_sub_and_test() isn't available on all platforms. The
* good news is that the fast path only completes one at a time */ while(num--) { if (atomic_dec_and_test(&wc->wc_num_reqs)) {
BUG_ON(num > 0);
complete(&wc->wc_io_complete);
}
}
}
/* Setup a Bio to cover I/O against num_slots slots starting at
* start_slot. */ staticstruct bio *o2hb_setup_one_bio(struct o2hb_region *reg, struct o2hb_bio_wait_ctxt *wc, unsignedint *current_slot, unsignedint max_slots, blk_opf_t opf)
{ int len, current_page; unsignedint vec_len, vec_start; unsignedint bits = reg->hr_block_bits; unsignedint spp = reg->hr_slots_per_page; unsignedint cs = *current_slot; struct bio *bio; struct page *page;
/* Testing has shown this allocation to take long enough under * GFP_KERNEL that the local node can get fenced. It would be * nicest if we could pre-allocate these bios and avoid this
* all together. */
bio = bio_alloc(reg_bdev(reg), 16, opf, GFP_ATOMIC); if (!bio) {
mlog(ML_ERROR, "Could not alloc slots BIO!\n");
bio = ERR_PTR(-ENOMEM); goto bail;
}
/* Must put everything in 512 byte sectors for the bio... */
bio->bi_iter.bi_sector = (reg->hr_start_block + cs) << (bits - 9);
bio->bi_private = wc;
bio->bi_end_io = o2hb_bio_end_io;
/* We want to compute the block crc with a 0 value in the * hb_cksum field. Save it off here and replace after the
* crc. */
old_cksum = hb_block->hb_cksum;
hb_block->hb_cksum = 0;
ret = crc32_le(0, (unsignedchar *) hb_block, reg->hr_block_bytes);
/* * Compare the slot data with what we wrote in the last iteration. * If the match fails, print an appropriate error message. This is to * detect errors like... another node hearting on the same slot, * flaky device that is losing writes, etc. * Returns 1 if check succeeds, 0 otherwise.
*/ staticint o2hb_check_own_slot(struct o2hb_region *reg)
{ struct o2hb_disk_slot *slot; struct o2hb_disk_heartbeat_block *hb_block; char *errstr;
slot = ®->hr_slots[o2nm_this_node()]; /* Don't check on our 1st timestamp */ if (!slot->ds_last_time) return 0;
/* Will run the list in order until we process the passed event */ staticvoid o2hb_run_event_list(struct o2hb_node_event *queued_event)
{ struct o2hb_callback *hbcall; struct o2hb_node_event *event;
/* Holding callback sem assures we don't alter the callback * lists when doing this, and serializes ourselves with other
* processes wanting callbacks. */
down_write(&o2hb_callback_sem);
/* We should *never* have gotten on to the list with a * bad type... This isn't something that we should try
* to recover from. */
BUG_ON(IS_ERR(hbcall));
staticvoid o2hb_set_quorum_device(struct o2hb_region *reg)
{ if (!o2hb_global_heartbeat_active()) return;
/* Prevent race with o2hb_heartbeat_group_drop_item() */ if (kthread_should_stop()) return;
/* Tag region as quorum only after thread reaches steady state */ if (atomic_read(®->hr_steady_iterations) != 0) return;
spin_lock(&o2hb_live_lock);
if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap)) goto unlock;
/* * A region can be added to the quorum only when it sees all * live nodes heartbeat on it. In other words, the region has been * added to all nodes.
*/ if (!bitmap_equal(reg->hr_live_node_bitmap, o2hb_live_node_bitmap,
O2NM_MAX_NODES)) goto unlock;
printk(KERN_NOTICE "o2hb: Region %s (%pg) is now a quorum device\n",
config_item_name(®->hr_item), reg_bdev(reg));
/* * If global heartbeat active, unpin all regions if the * region count > CUT_OFF
*/ if (bitmap_weight(o2hb_quorum_region_bitmap,
O2NM_MAX_REGIONS) > O2HB_PIN_CUT_OFF)
o2hb_region_unpin(NULL);
unlock:
spin_unlock(&o2hb_live_lock);
}
/* * If a node is no longer configured but is still in the livemap, we * may need to clear that bit from the livemap.
*/
node = o2nm_get_node_by_num(slot->ds_node_num); if (!node) {
spin_lock(&o2hb_live_lock);
tmp = test_bit(slot->ds_node_num, o2hb_live_node_bitmap);
spin_unlock(&o2hb_live_lock); if (!tmp) return 0;
}
if (!o2hb_verify_crc(reg, hb_block)) { /* all paths from here will drop o2hb_live_lock for
* us. */
spin_lock(&o2hb_live_lock);
/* Don't print an error on the console in this case - * a freshly formatted heartbeat area will not have a
* crc set on it. */ if (list_empty(&slot->ds_live_item)) goto out;
/* The node is live but pushed out a bad crc. We * consider it a transient miss but don't populate any
* other values as they may be junk. */
mlog(ML_ERROR, "Node %d has written a bad crc to %pg\n",
slot->ds_node_num, reg_bdev(reg));
o2hb_dump_slot(hb_block);
slot->ds_equal_samples++; goto fire_callbacks;
}
/* we don't care if these wrap.. the state transitions below
* clear at the right places */
cputime = le64_to_cpu(hb_block->hb_seq); if (slot->ds_last_time != cputime)
slot->ds_changed_samples++; else
slot->ds_equal_samples++;
slot->ds_last_time = cputime;
/* The node changed heartbeat generations. We assume this to * mean it dropped off but came back before we timed out. We * want to consider it down for the time being but don't want * to lose any changed_samples state we might build up to
* considering it live again. */ if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
gen_changed = 1;
slot->ds_equal_samples = 0;
mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx " "to 0x%llx)\n", slot->ds_node_num,
(longlong)slot->ds_last_generation,
(longlong)le64_to_cpu(hb_block->hb_generation));
}
fire_callbacks: /* dead nodes only come to life after some number of
* changes at any time during their dead time */ if (list_empty(&slot->ds_live_item) &&
slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
slot->ds_node_num, (longlong)slot->ds_last_generation);
/* first on the list generates a callback */ if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
mlog(ML_HEARTBEAT, "o2hb: Add node %d to live nodes " "bitmap\n", slot->ds_node_num);
set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
/* We want to be sure that all nodes agree on the * number of milliseconds before a node will be * considered dead. The self-fencing timeout is * computed from this value, and a discrepancy might * result in heartbeat calling a node dead when it
* hasn't self-fenced yet. */
slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms); if (slot_dead_ms && slot_dead_ms != dead_ms) { /* TODO: Perhaps we can fail the region here. */
mlog(ML_ERROR, "Node %d on device %pg has a dead count " "of %u ms, but our count is %u ms.\n" "Please double check your configuration values " "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
slot->ds_node_num, reg_bdev(reg),
slot_dead_ms, dead_ms);
} goto out;
}
/* if the list is dead, we're done.. */ if (list_empty(&slot->ds_live_item)) goto out;
/* live nodes only go dead after enough consecutive missed * samples.. reset the missed counter whenever we see
* activity */ if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
mlog(ML_HEARTBEAT, "Node %d left my region\n",
slot->ds_node_num);
/* last off the live_slot generates a callback */
list_del_init(&slot->ds_live_item); if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
mlog(ML_HEARTBEAT, "o2hb: Remove node %d from live " "nodes bitmap\n", slot->ds_node_num);
clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
/* node can be null */
o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
node, slot->ds_node_num);
changed = 1;
queued = 1;
}
/* We don't clear this because the node is still
* actually writing new blocks. */ if (!gen_changed)
slot->ds_changed_samples = 0; goto out;
} if (slot->ds_changed_samples) {
slot->ds_changed_samples = 0;
slot->ds_equal_samples = 0;
}
out:
spin_unlock(&o2hb_live_lock);
if (queued)
o2hb_run_event_list(&event);
if (node)
o2nm_node_put(node); return changed;
}
staticint o2hb_highest_node(unsignedlong *nodes, int numbits)
{ return find_last_bit(nodes, numbits);
}
staticint o2hb_lowest_node(unsignedlong *nodes, int numbits)
{ return find_first_bit(nodes, numbits);
}
staticint o2hb_do_disk_heartbeat(struct o2hb_region *reg)
{ int i, ret, highest_node, lowest_node; int membership_change = 0, own_slot_ok = 0; unsignedlong configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)]; unsignedlong live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)]; struct o2hb_bio_wait_ctxt write_wc;
ret = o2nm_configured_node_map(configured_nodes, sizeof(configured_nodes)); if (ret) {
mlog_errno(ret); goto bail;
}
/* * If a node is not configured but is in the livemap, we still need * to read the slot so as to be able to remove it from the livemap.
*/
o2hb_fill_node_map(live_node_bitmap, O2NM_MAX_NODES);
i = -1; while ((i = find_next_bit(live_node_bitmap,
O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
set_bit(i, configured_nodes);
}
highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
lowest_node = o2hb_lowest_node(configured_nodes, O2NM_MAX_NODES); if (highest_node >= O2NM_MAX_NODES || lowest_node >= O2NM_MAX_NODES) {
mlog(ML_NOTICE, "o2hb: No configured nodes found!\n");
ret = -EINVAL; goto bail;
}
/* No sense in reading the slots of nodes that don't exist * yet. Of course, if the node definitions have holes in them * then we're reading an empty slot anyway... Consider this
* best-effort. */
ret = o2hb_read_slots(reg, lowest_node, highest_node + 1); if (ret < 0) {
mlog_errno(ret); goto bail;
}
/* With an up to date view of the slots, we can check that no * other node has been improperly configured to heartbeat in
* our slot. */
own_slot_ok = o2hb_check_own_slot(reg);
/* fill in the proper info for our next heartbeat */
o2hb_prepare_block(reg, reg->hr_generation);
ret = o2hb_issue_node_write(reg, &write_wc); if (ret < 0) {
mlog_errno(ret); goto bail;
}
i = -1; while((i = find_next_bit(configured_nodes,
O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
membership_change |= o2hb_check_slot(reg, ®->hr_slots[i]);
}
/* * We have to be sure we've advertised ourselves on disk * before we can go to steady state. This ensures that * people we find in our steady state have seen us.
*/
o2hb_wait_on_io(&write_wc); if (write_wc.wc_error) { /* Do not re-arm the write timeout on I/O error - we * can't be sure that the new block ever made it to
* disk */
mlog(ML_ERROR, "Write error %d on device \"%pg\"\n",
write_wc.wc_error, reg_bdev(reg));
ret = write_wc.wc_error; goto bail;
}
/* Skip disarming the timeout if own slot has stale/bad data */ if (own_slot_ok) {
o2hb_set_quorum_device(reg);
o2hb_arm_timeout(reg);
reg->hr_last_timeout_start = jiffies;
}
bail: /* let the person who launched us know when things are steady */ if (atomic_read(®->hr_steady_iterations) != 0) { if (!ret && own_slot_ok && !membership_change) { if (atomic_dec_and_test(®->hr_steady_iterations))
wake_up(&o2hb_steady_queue);
}
}
if (atomic_read(®->hr_steady_iterations) != 0) { if (atomic_dec_and_test(®->hr_unsteady_iterations)) {
printk(KERN_NOTICE "o2hb: Unable to stabilize " "heartbeat on region %s (%pg)\n",
config_item_name(®->hr_item),
reg_bdev(reg));
atomic_set(®->hr_steady_iterations, 0);
reg->hr_aborted_start = 1;
wake_up(&o2hb_steady_queue);
ret = -EIO;
}
}
return ret;
}
/* * we ride the region ref that the region dir holds. before the region * dir is removed and drops it ref it will wait to tear down this * thread.
*/ staticint o2hb_thread(void *data)
{ int i, ret; struct o2hb_region *reg = data; struct o2hb_bio_wait_ctxt write_wc;
ktime_t before_hb, after_hb; unsignedint elapsed_msec;
/* Pin node */
ret = o2nm_depend_this_node(); if (ret) {
mlog(ML_ERROR, "Node has been deleted, ret = %d\n", ret);
reg->hr_node_deleted = 1;
wake_up(&o2hb_steady_queue); return 0;
}
while (!kthread_should_stop() &&
!reg->hr_unclean_stop && !reg->hr_aborted_start) { /* We track the time spent inside * o2hb_do_disk_heartbeat so that we avoid more than * hr_timeout_ms between disk writes. On busy systems * this should result in a heartbeat which is less
* likely to time itself out. */
before_hb = ktime_get_real();
ret = o2hb_do_disk_heartbeat(reg);
reg->hr_last_hb_status = ret;
mlog(ML_HEARTBEAT, "start = %lld, end = %lld, msec = %u, ret = %d\n",
before_hb, after_hb, elapsed_msec, ret);
if (!kthread_should_stop() &&
elapsed_msec < reg->hr_timeout_ms) { /* the kthread api has blocked signals for us so no
* need to record the return value. */
msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
}
}
o2hb_disarm_timeout(reg);
/* unclean stop is only used in very bad situation */ for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
o2hb_shutdown_slot(®->hr_slots[i]);
/* Explicit down notification - avoid forcing the other nodes * to timeout on this region when we could just as easily * write a clear generation - thus indicating to them that * this node has left this region.
*/ if (!reg->hr_unclean_stop && !reg->hr_aborted_start) {
o2hb_prepare_block(reg, 0);
ret = o2hb_issue_node_write(reg, &write_wc); if (ret == 0)
o2hb_wait_on_io(&write_wc); else
mlog_errno(ret);
}
#ifdef CONFIG_DEBUG_FS staticint o2hb_debug_open(struct inode *inode, struct file *file)
{ struct o2hb_debug_buf *db = inode->i_private; struct o2hb_region *reg; unsignedlong map[BITS_TO_LONGS(O2NM_MAX_NODES)]; unsignedlong lts; char *buf = NULL; int i = -1; int out = 0;
/* max_nodes should be the largest bitmap we pass here */
BUG_ON(sizeof(map) < db->db_size);
buf = kmalloc(PAGE_SIZE, GFP_KERNEL); if (!buf) goto bail;
switch (db->db_type) { case O2HB_DB_TYPE_LIVENODES: case O2HB_DB_TYPE_LIVEREGIONS: case O2HB_DB_TYPE_QUORUMREGIONS: case O2HB_DB_TYPE_FAILEDREGIONS:
spin_lock(&o2hb_live_lock);
memcpy(map, db->db_data, db->db_size);
spin_unlock(&o2hb_live_lock); break;
case O2HB_DB_TYPE_REGION_NUMBER:
reg = (struct o2hb_region *)db->db_data;
out += scnprintf(buf + out, PAGE_SIZE - out, "%d\n",
reg->hr_region_num); goto done;
case O2HB_DB_TYPE_REGION_ELAPSED_TIME:
reg = (struct o2hb_region *)db->db_data;
lts = reg->hr_last_timeout_start; /* If 0, it has never been set before */ if (lts)
lts = jiffies_to_msecs(jiffies - lts);
out += scnprintf(buf + out, PAGE_SIZE - out, "%lu\n", lts); goto done;
case O2HB_DB_TYPE_REGION_PINNED:
reg = (struct o2hb_region *)db->db_data;
out += scnprintf(buf + out, PAGE_SIZE - out, "%u\n",
!!reg->hr_item_pinned); goto done;
default: goto done;
}
while ((i = find_next_bit(map, db->db_len, i + 1)) < db->db_len)
out += scnprintf(buf + out, PAGE_SIZE - out, "%d ", i);
out += scnprintf(buf + out, PAGE_SIZE - out, "\n");
/* if we're already in a callback then we're already serialized by the sem */ staticvoid o2hb_fill_node_map_from_callback(unsignedlong *map, unsignedint bits)
{
bitmap_copy(map, o2hb_live_node_bitmap, bits);
}
/* * get a map of all nodes that are heartbeating in any regions
*/ void o2hb_fill_node_map(unsignedlong *map, unsignedint bits)
{ /* callers want to serialize this map and callbacks so that they
* can trust that they don't miss nodes coming to the party */
down_read(&o2hb_callback_sem);
spin_lock(&o2hb_live_lock);
o2hb_fill_node_map_from_callback(map, bits);
spin_unlock(&o2hb_live_lock);
up_read(&o2hb_callback_sem);
}
EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
/* * heartbeat configfs bits. The heartbeat set is a default set under * the cluster set in nodemanager.c.
*/
/* drop_item only drops its ref after killing the thread, nothing should * be using the region anymore. this has to clean up any state that
* attributes might have built up. */ staticvoid o2hb_region_release(struct config_item *item)
{ int i; struct page *page; struct o2hb_region *reg = to_o2hb_region(item);
mlog(ML_HEARTBEAT, "hb region release (%pg)\n", reg_bdev(reg));
kfree(reg->hr_tmp_block);
if (reg->hr_slot_data) { for (i = 0; i < reg->hr_num_pages; i++) {
page = reg->hr_slot_data[i]; if (page)
__free_page(page);
}
kfree(reg->hr_slot_data);
}
/* Read in all the slots available and populate the tracking * structures so that we can start with a baseline idea of what's
* there. */ staticint o2hb_populate_slot_data(struct o2hb_region *reg)
{ int ret, i; struct o2hb_disk_slot *slot; struct o2hb_disk_heartbeat_block *hb_block;
ret = o2hb_read_slots(reg, 0, reg->hr_blocks); if (ret) goto out;
/* We only want to get an idea of the values initially in each * slot, so we do no verification - o2hb_check_slot will * actually determine if each configured slot is valid and
* whether any values have changed. */ for(i = 0; i < reg->hr_blocks; i++) {
slot = ®->hr_slots[i];
hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
/* Only fill the values that o2hb_check_slot uses to
* determine changing slots */
slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
}
out: return ret;
}
/* * this is acting as commit; we set up all of hr_bdev_file and hr_task or * nothing
*/ static ssize_t o2hb_region_dev_store(struct config_item *item, constchar *page,
size_t count)
{ struct o2hb_region *reg = to_o2hb_region(item); struct task_struct *hb_task; long fd; int sectsize; char *p = (char *)page;
ssize_t ret = -EINVAL; int live_threshold;
if (reg->hr_bdev_file) return -EINVAL;
/* We can't heartbeat without having had our node number
* configured yet. */ if (o2nm_this_node() == O2NM_MAX_NODES) return -EINVAL;
ret = kstrtol(p, 0, &fd); if (ret < 0) return -EINVAL;
if (fd < 0 || fd >= INT_MAX) return -EINVAL;
CLASS(fd, f)(fd); if (fd_empty(f)) return -EINVAL;
/* * A node is considered live after it has beat LIVE_THRESHOLD * times. We're not steady until we've given them a chance * _after_ our first read. * The default threshold is bare minimum so as to limit the delay * during mounts. For global heartbeat, the threshold doubled for the * first region.
*/
live_threshold = O2HB_LIVE_THRESHOLD; if (o2hb_global_heartbeat_active()) {
spin_lock(&o2hb_live_lock); if (bitmap_weight(o2hb_region_bitmap, O2NM_MAX_REGIONS) == 1)
live_threshold <<= 1;
spin_unlock(&o2hb_live_lock);
}
++live_threshold;
atomic_set(®->hr_steady_iterations, live_threshold); /* unsteady_iterations is triple the steady_iterations */
atomic_set(®->hr_unsteady_iterations, (live_threshold * 3));
hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
reg->hr_item.ci_name); if (IS_ERR(hb_task)) {
ret = PTR_ERR(hb_task);
mlog_errno(ret); goto out3;
}
ret = wait_event_interruptible(o2hb_steady_queue,
atomic_read(®->hr_steady_iterations) == 0 ||
reg->hr_node_deleted); if (ret) {
atomic_set(®->hr_steady_iterations, 0);
reg->hr_aborted_start = 1;
}
if (reg->hr_aborted_start) {
ret = -EIO; goto out3;
}
if (reg->hr_node_deleted) {
ret = -EINVAL; goto out3;
}
/* Ok, we were woken. Make sure it wasn't by drop_item() */
spin_lock(&o2hb_live_lock);
hb_task = reg->hr_task; if (o2hb_global_heartbeat_active())
set_bit(reg->hr_region_num, o2hb_live_region_bitmap);
spin_unlock(&o2hb_live_lock);
if (hb_task)
ret = count; else
ret = -EIO;
if (hb_task && o2hb_global_heartbeat_active())
printk(KERN_NOTICE "o2hb: Heartbeat started on region %s (%pg)\n",
config_item_name(®->hr_item), reg_bdev(reg));
/* this is the same way to generate msg key as dlm, for local heartbeat, * name is also the same, so make initial crc value different to avoid * message key conflict.
*/
reg->hr_key = crc32_le(reg->hr_region_num + O2NM_MAX_REGIONS,
name, strlen(name));
INIT_LIST_HEAD(®->hr_handler_list);
ret = o2net_register_handler(O2HB_NEGO_TIMEOUT_MSG, reg->hr_key, sizeof(struct o2hb_nego_msg),
o2hb_nego_timeout_handler,
reg, NULL, ®->hr_handler_list); if (ret) goto remove_item;
ret = o2net_register_handler(O2HB_NEGO_APPROVE_MSG, reg->hr_key, sizeof(struct o2hb_nego_msg),
o2hb_nego_approve_handler,
reg, NULL, ®->hr_handler_list); if (ret) goto unregister_handler;
/* stop the thread when the user removes the region dir */
spin_lock(&o2hb_live_lock);
hb_task = reg->hr_task;
reg->hr_task = NULL;
reg->hr_item_dropped = 1;
spin_unlock(&o2hb_live_lock);
if (hb_task)
kthread_stop(hb_task);
if (o2hb_global_heartbeat_active()) {
spin_lock(&o2hb_live_lock);
clear_bit(reg->hr_region_num, o2hb_region_bitmap);
clear_bit(reg->hr_region_num, o2hb_live_region_bitmap); if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
quorum_region = 1;
clear_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
spin_unlock(&o2hb_live_lock);
printk(KERN_NOTICE "o2hb: Heartbeat %s on region %s (%pg)\n",
((atomic_read(®->hr_steady_iterations) == 0) ? "stopped" : "start aborted"), config_item_name(item),
reg_bdev(reg));
}
/* * If we're racing a dev_write(), we need to wake them. They will * check reg->hr_task
*/ if (atomic_read(®->hr_steady_iterations) != 0) {
reg->hr_aborted_start = 1;
atomic_set(®->hr_steady_iterations, 0);
wake_up(&o2hb_steady_queue);
}
config_item_put(item);
if (!o2hb_global_heartbeat_active() || !quorum_region) return;
/* * If global heartbeat active and there are dependent users, * pin all regions if quorum region count <= CUT_OFF
*/
spin_lock(&o2hb_live_lock);
if (!o2hb_dependent_users) goto unlock;
if (bitmap_weight(o2hb_quorum_region_bitmap,
O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
o2hb_region_pin(NULL);
len = (page[count - 1] == '\n') ? count - 1 : count; if (!len) return -EINVAL;
for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) { if (strncasecmp(page, o2hb_heartbeat_mode_desc[i], len)) continue;
ret = o2hb_global_heartbeat_mode_set(i); if (!ret)
printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
o2hb_heartbeat_mode_desc[i]); return count;
}
/* this is just here to avoid touching group in heartbeat.h which the
* entire damn world #includes */ struct config_group *o2hb_alloc_hb_set(void)
{ struct o2hb_heartbeat_group *hs = NULL; struct config_group *ret = NULL;
/* * In local heartbeat mode, region_uuid passed matches the dlm domain name. * In global heartbeat mode, region_uuid passed is NULL. * * In local, we only pin the matching region. In global we pin all the active * regions.
*/ staticint o2hb_region_pin(constchar *region_uuid)
{ int ret = 0, found = 0; struct o2hb_region *reg; char *uuid;
assert_spin_locked(&o2hb_live_lock);
list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) { if (reg->hr_item_dropped) continue;
uuid = config_item_name(®->hr_item);
/* local heartbeat */ if (region_uuid) { if (strcmp(region_uuid, uuid)) continue;
found = 1;
}
if (reg->hr_item_pinned || reg->hr_item_dropped) goto skip_pin;
/* Ignore ENOENT only for local hb (userdlm domain) */
ret = o2nm_depend_item(®->hr_item); if (!ret) {
mlog(ML_CLUSTER, "Pin region %s\n", uuid);
reg->hr_item_pinned = 1;
} else { if (ret == -ENOENT && found)
ret = 0; else {
mlog(ML_ERROR, "Pin region %s fails with %d\n",
uuid, ret); break;
}
}
skip_pin: if (found) break;
}
return ret;
}
/* * In local heartbeat mode, region_uuid passed matches the dlm domain name. * In global heartbeat mode, region_uuid passed is NULL. * * In local, we only unpin the matching region. In global we unpin all the * active regions.
*/ staticvoid o2hb_region_unpin(constchar *region_uuid)
{ struct o2hb_region *reg; char *uuid; int found = 0;
assert_spin_locked(&o2hb_live_lock);
list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) { if (reg->hr_item_dropped) continue;
uuid = config_item_name(®->hr_item); if (region_uuid) { if (strcmp(region_uuid, uuid)) continue;
found = 1;
}
if (reg->hr_item_pinned) {
mlog(ML_CLUSTER, "Unpin region %s\n", uuid);
o2nm_undepend_item(®->hr_item);
reg->hr_item_pinned = 0;
} if (found) break;
}
}
staticint o2hb_region_inc_user(constchar *region_uuid)
{ int ret = 0;
spin_lock(&o2hb_live_lock);
/* local heartbeat */ if (!o2hb_global_heartbeat_active()) {
ret = o2hb_region_pin(region_uuid); goto unlock;
}
/* * if global heartbeat active and this is the first dependent user, * pin all regions if quorum region count <= CUT_OFF
*/
o2hb_dependent_users++; if (o2hb_dependent_users > 1) goto unlock;
if (bitmap_weight(o2hb_quorum_region_bitmap,
O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
ret = o2hb_region_pin(NULL);
/* local heartbeat */ if (!o2hb_global_heartbeat_active()) {
o2hb_region_unpin(region_uuid); goto unlock;
}
/* * if global heartbeat active and there are no dependent users, * unpin all quorum regions
*/
o2hb_dependent_users--; if (!o2hb_dependent_users)
o2hb_region_unpin(NULL);
unlock:
spin_unlock(&o2hb_live_lock);
}
int o2hb_register_callback(constchar *region_uuid, struct o2hb_callback_func *hc)
{ struct o2hb_callback_func *f; struct o2hb_callback *hbcall; int ret;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.