Drop 'size' argument from bio_endio and bi_end_io
[linux-2.6/mini2440.git] / fs / ocfs2 / cluster / heartbeat.c
blobda2c2b442b4913e768812fa644dd78823979edee
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>
26 #include <linux/fs.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"
38 #include "tcp.h"
39 #include "nodemanager.h"
40 #include "quorum.h"
42 #include "masklog.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 DEFINE_SPINLOCK(o2hb_live_lock);
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;
94 int hn_node_num;
97 struct o2hb_disk_slot {
98 struct o2hb_disk_heartbeat_block *ds_raw_block;
99 u8 ds_node_num;
100 u64 ds_last_time;
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 */
109 struct o2hb_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 */
142 u64 hr_generation;
144 struct delayed_work 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
149 * crc field. */
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;
156 int wc_error;
159 static void o2hb_write_timeout(struct work_struct *work)
161 struct o2hb_region *reg =
162 container_of(work, struct o2hb_region,
163 hr_write_timeout_work.work);
165 mlog(ML_ERROR, "Heartbeat write timeout to device %s after %u "
166 "milliseconds\n", reg->hr_dev_name,
167 jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
168 o2quo_disk_timeout();
171 static void o2hb_arm_write_timeout(struct o2hb_region *reg)
173 mlog(0, "Queue write timeout for %u ms\n", O2HB_MAX_WRITE_TIMEOUT_MS);
175 cancel_delayed_work(&reg->hr_write_timeout_work);
176 reg->hr_last_timeout_start = jiffies;
177 schedule_delayed_work(&reg->hr_write_timeout_work,
178 msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS));
181 static void o2hb_disarm_write_timeout(struct o2hb_region *reg)
183 cancel_delayed_work(&reg->hr_write_timeout_work);
184 flush_scheduled_work();
187 static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt *wc)
189 atomic_set(&wc->wc_num_reqs, 1);
190 init_completion(&wc->wc_io_complete);
191 wc->wc_error = 0;
194 /* Used in error paths too */
195 static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt *wc,
196 unsigned int num)
198 /* sadly atomic_sub_and_test() isn't available on all platforms. The
199 * good news is that the fast path only completes one at a time */
200 while(num--) {
201 if (atomic_dec_and_test(&wc->wc_num_reqs)) {
202 BUG_ON(num > 0);
203 complete(&wc->wc_io_complete);
208 static void o2hb_wait_on_io(struct o2hb_region *reg,
209 struct o2hb_bio_wait_ctxt *wc)
211 struct address_space *mapping = reg->hr_bdev->bd_inode->i_mapping;
213 blk_run_address_space(mapping);
214 o2hb_bio_wait_dec(wc, 1);
216 wait_for_completion(&wc->wc_io_complete);
219 static int o2hb_bio_end_io(struct bio *bio,
220 int error)
222 struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
224 if (error) {
225 mlog(ML_ERROR, "IO Error %d\n", error);
226 wc->wc_error = error;
229 o2hb_bio_wait_dec(wc, 1);
230 bio_put(bio);
231 return 0;
234 /* Setup a Bio to cover I/O against num_slots slots starting at
235 * start_slot. */
236 static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
237 struct o2hb_bio_wait_ctxt *wc,
238 unsigned int *current_slot,
239 unsigned int max_slots)
241 int len, current_page;
242 unsigned int vec_len, vec_start;
243 unsigned int bits = reg->hr_block_bits;
244 unsigned int spp = reg->hr_slots_per_page;
245 unsigned int cs = *current_slot;
246 struct bio *bio;
247 struct page *page;
249 /* Testing has shown this allocation to take long enough under
250 * GFP_KERNEL that the local node can get fenced. It would be
251 * nicest if we could pre-allocate these bios and avoid this
252 * all together. */
253 bio = bio_alloc(GFP_ATOMIC, 16);
254 if (!bio) {
255 mlog(ML_ERROR, "Could not alloc slots BIO!\n");
256 bio = ERR_PTR(-ENOMEM);
257 goto bail;
260 /* Must put everything in 512 byte sectors for the bio... */
261 bio->bi_sector = (reg->hr_start_block + cs) << (bits - 9);
262 bio->bi_bdev = reg->hr_bdev;
263 bio->bi_private = wc;
264 bio->bi_end_io = o2hb_bio_end_io;
266 vec_start = (cs << bits) % PAGE_CACHE_SIZE;
267 while(cs < max_slots) {
268 current_page = cs / spp;
269 page = reg->hr_slot_data[current_page];
271 vec_len = min(PAGE_CACHE_SIZE,
272 (max_slots-cs) * (PAGE_CACHE_SIZE/spp) );
274 mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
275 current_page, vec_len, vec_start);
277 len = bio_add_page(bio, page, vec_len, vec_start);
278 if (len != vec_len) break;
280 cs += vec_len / (PAGE_CACHE_SIZE/spp);
281 vec_start = 0;
284 bail:
285 *current_slot = cs;
286 return bio;
289 static int o2hb_read_slots(struct o2hb_region *reg,
290 unsigned int max_slots)
292 unsigned int current_slot=0;
293 int status;
294 struct o2hb_bio_wait_ctxt wc;
295 struct bio *bio;
297 o2hb_bio_wait_init(&wc);
299 while(current_slot < max_slots) {
300 bio = o2hb_setup_one_bio(reg, &wc, &current_slot, max_slots);
301 if (IS_ERR(bio)) {
302 status = PTR_ERR(bio);
303 mlog_errno(status);
304 goto bail_and_wait;
307 atomic_inc(&wc.wc_num_reqs);
308 submit_bio(READ, bio);
311 status = 0;
313 bail_and_wait:
314 o2hb_wait_on_io(reg, &wc);
315 if (wc.wc_error && !status)
316 status = wc.wc_error;
318 return status;
321 static int o2hb_issue_node_write(struct o2hb_region *reg,
322 struct o2hb_bio_wait_ctxt *write_wc)
324 int status;
325 unsigned int slot;
326 struct bio *bio;
328 o2hb_bio_wait_init(write_wc);
330 slot = o2nm_this_node();
332 bio = o2hb_setup_one_bio(reg, write_wc, &slot, slot+1);
333 if (IS_ERR(bio)) {
334 status = PTR_ERR(bio);
335 mlog_errno(status);
336 goto bail;
339 atomic_inc(&write_wc->wc_num_reqs);
340 submit_bio(WRITE, bio);
342 status = 0;
343 bail:
344 return status;
347 static u32 o2hb_compute_block_crc_le(struct o2hb_region *reg,
348 struct o2hb_disk_heartbeat_block *hb_block)
350 __le32 old_cksum;
351 u32 ret;
353 /* We want to compute the block crc with a 0 value in the
354 * hb_cksum field. Save it off here and replace after the
355 * crc. */
356 old_cksum = hb_block->hb_cksum;
357 hb_block->hb_cksum = 0;
359 ret = crc32_le(0, (unsigned char *) hb_block, reg->hr_block_bytes);
361 hb_block->hb_cksum = old_cksum;
363 return ret;
366 static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
368 mlog(ML_ERROR, "Dump slot information: seq = 0x%llx, node = %u, "
369 "cksum = 0x%x, generation 0x%llx\n",
370 (long long)le64_to_cpu(hb_block->hb_seq),
371 hb_block->hb_node, le32_to_cpu(hb_block->hb_cksum),
372 (long long)le64_to_cpu(hb_block->hb_generation));
375 static int o2hb_verify_crc(struct o2hb_region *reg,
376 struct o2hb_disk_heartbeat_block *hb_block)
378 u32 read, computed;
380 read = le32_to_cpu(hb_block->hb_cksum);
381 computed = o2hb_compute_block_crc_le(reg, hb_block);
383 return read == computed;
386 /* We want to make sure that nobody is heartbeating on top of us --
387 * this will help detect an invalid configuration. */
388 static int o2hb_check_last_timestamp(struct o2hb_region *reg)
390 int node_num, ret;
391 struct o2hb_disk_slot *slot;
392 struct o2hb_disk_heartbeat_block *hb_block;
394 node_num = o2nm_this_node();
396 ret = 1;
397 slot = &reg->hr_slots[node_num];
398 /* Don't check on our 1st timestamp */
399 if (slot->ds_last_time) {
400 hb_block = slot->ds_raw_block;
402 if (le64_to_cpu(hb_block->hb_seq) != slot->ds_last_time)
403 ret = 0;
406 return ret;
409 static inline void o2hb_prepare_block(struct o2hb_region *reg,
410 u64 generation)
412 int node_num;
413 u64 cputime;
414 struct o2hb_disk_slot *slot;
415 struct o2hb_disk_heartbeat_block *hb_block;
417 node_num = o2nm_this_node();
418 slot = &reg->hr_slots[node_num];
420 hb_block = (struct o2hb_disk_heartbeat_block *)slot->ds_raw_block;
421 memset(hb_block, 0, reg->hr_block_bytes);
422 /* TODO: time stuff */
423 cputime = CURRENT_TIME.tv_sec;
424 if (!cputime)
425 cputime = 1;
427 hb_block->hb_seq = cpu_to_le64(cputime);
428 hb_block->hb_node = node_num;
429 hb_block->hb_generation = cpu_to_le64(generation);
430 hb_block->hb_dead_ms = cpu_to_le32(o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS);
432 /* This step must always happen last! */
433 hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
434 hb_block));
436 mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x\n",
437 (long long)generation,
438 le32_to_cpu(hb_block->hb_cksum));
441 static void o2hb_fire_callbacks(struct o2hb_callback *hbcall,
442 struct o2nm_node *node,
443 int idx)
445 struct list_head *iter;
446 struct o2hb_callback_func *f;
448 list_for_each(iter, &hbcall->list) {
449 f = list_entry(iter, struct o2hb_callback_func, hc_item);
450 mlog(ML_HEARTBEAT, "calling funcs %p\n", f);
451 (f->hc_func)(node, idx, f->hc_data);
455 /* Will run the list in order until we process the passed event */
456 static void o2hb_run_event_list(struct o2hb_node_event *queued_event)
458 int empty;
459 struct o2hb_callback *hbcall;
460 struct o2hb_node_event *event;
462 spin_lock(&o2hb_live_lock);
463 empty = list_empty(&queued_event->hn_item);
464 spin_unlock(&o2hb_live_lock);
465 if (empty)
466 return;
468 /* Holding callback sem assures we don't alter the callback
469 * lists when doing this, and serializes ourselves with other
470 * processes wanting callbacks. */
471 down_write(&o2hb_callback_sem);
473 spin_lock(&o2hb_live_lock);
474 while (!list_empty(&o2hb_node_events)
475 && !list_empty(&queued_event->hn_item)) {
476 event = list_entry(o2hb_node_events.next,
477 struct o2hb_node_event,
478 hn_item);
479 list_del_init(&event->hn_item);
480 spin_unlock(&o2hb_live_lock);
482 mlog(ML_HEARTBEAT, "Node %s event for %d\n",
483 event->hn_event_type == O2HB_NODE_UP_CB ? "UP" : "DOWN",
484 event->hn_node_num);
486 hbcall = hbcall_from_type(event->hn_event_type);
488 /* We should *never* have gotten on to the list with a
489 * bad type... This isn't something that we should try
490 * to recover from. */
491 BUG_ON(IS_ERR(hbcall));
493 o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num);
495 spin_lock(&o2hb_live_lock);
497 spin_unlock(&o2hb_live_lock);
499 up_write(&o2hb_callback_sem);
502 static void o2hb_queue_node_event(struct o2hb_node_event *event,
503 enum o2hb_callback_type type,
504 struct o2nm_node *node,
505 int node_num)
507 assert_spin_locked(&o2hb_live_lock);
509 event->hn_event_type = type;
510 event->hn_node = node;
511 event->hn_node_num = node_num;
513 mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
514 type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
516 list_add_tail(&event->hn_item, &o2hb_node_events);
519 static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
521 struct o2hb_node_event event =
522 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
523 struct o2nm_node *node;
525 node = o2nm_get_node_by_num(slot->ds_node_num);
526 if (!node)
527 return;
529 spin_lock(&o2hb_live_lock);
530 if (!list_empty(&slot->ds_live_item)) {
531 mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
532 slot->ds_node_num);
534 list_del_init(&slot->ds_live_item);
536 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
537 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
539 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
540 slot->ds_node_num);
543 spin_unlock(&o2hb_live_lock);
545 o2hb_run_event_list(&event);
547 o2nm_node_put(node);
550 static int o2hb_check_slot(struct o2hb_region *reg,
551 struct o2hb_disk_slot *slot)
553 int changed = 0, gen_changed = 0;
554 struct o2hb_node_event event =
555 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
556 struct o2nm_node *node;
557 struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
558 u64 cputime;
559 unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
560 unsigned int slot_dead_ms;
562 memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
564 /* Is this correct? Do we assume that the node doesn't exist
565 * if we're not configured for him? */
566 node = o2nm_get_node_by_num(slot->ds_node_num);
567 if (!node)
568 return 0;
570 if (!o2hb_verify_crc(reg, hb_block)) {
571 /* all paths from here will drop o2hb_live_lock for
572 * us. */
573 spin_lock(&o2hb_live_lock);
575 /* Don't print an error on the console in this case -
576 * a freshly formatted heartbeat area will not have a
577 * crc set on it. */
578 if (list_empty(&slot->ds_live_item))
579 goto out;
581 /* The node is live but pushed out a bad crc. We
582 * consider it a transient miss but don't populate any
583 * other values as they may be junk. */
584 mlog(ML_ERROR, "Node %d has written a bad crc to %s\n",
585 slot->ds_node_num, reg->hr_dev_name);
586 o2hb_dump_slot(hb_block);
588 slot->ds_equal_samples++;
589 goto fire_callbacks;
592 /* we don't care if these wrap.. the state transitions below
593 * clear at the right places */
594 cputime = le64_to_cpu(hb_block->hb_seq);
595 if (slot->ds_last_time != cputime)
596 slot->ds_changed_samples++;
597 else
598 slot->ds_equal_samples++;
599 slot->ds_last_time = cputime;
601 /* The node changed heartbeat generations. We assume this to
602 * mean it dropped off but came back before we timed out. We
603 * want to consider it down for the time being but don't want
604 * to lose any changed_samples state we might build up to
605 * considering it live again. */
606 if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
607 gen_changed = 1;
608 slot->ds_equal_samples = 0;
609 mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
610 "to 0x%llx)\n", slot->ds_node_num,
611 (long long)slot->ds_last_generation,
612 (long long)le64_to_cpu(hb_block->hb_generation));
615 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
617 mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
618 "seq %llu last %llu changed %u equal %u\n",
619 slot->ds_node_num, (long long)slot->ds_last_generation,
620 le32_to_cpu(hb_block->hb_cksum),
621 (unsigned long long)le64_to_cpu(hb_block->hb_seq),
622 (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
623 slot->ds_equal_samples);
625 spin_lock(&o2hb_live_lock);
627 fire_callbacks:
628 /* dead nodes only come to life after some number of
629 * changes at any time during their dead time */
630 if (list_empty(&slot->ds_live_item) &&
631 slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
632 mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
633 slot->ds_node_num, (long long)slot->ds_last_generation);
635 /* first on the list generates a callback */
636 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
637 set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
639 o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
640 slot->ds_node_num);
642 changed = 1;
645 list_add_tail(&slot->ds_live_item,
646 &o2hb_live_slots[slot->ds_node_num]);
648 slot->ds_equal_samples = 0;
650 /* We want to be sure that all nodes agree on the
651 * number of milliseconds before a node will be
652 * considered dead. The self-fencing timeout is
653 * computed from this value, and a discrepancy might
654 * result in heartbeat calling a node dead when it
655 * hasn't self-fenced yet. */
656 slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
657 if (slot_dead_ms && slot_dead_ms != dead_ms) {
658 /* TODO: Perhaps we can fail the region here. */
659 mlog(ML_ERROR, "Node %d on device %s has a dead count "
660 "of %u ms, but our count is %u ms.\n"
661 "Please double check your configuration values "
662 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
663 slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
664 dead_ms);
666 goto out;
669 /* if the list is dead, we're done.. */
670 if (list_empty(&slot->ds_live_item))
671 goto out;
673 /* live nodes only go dead after enough consequtive missed
674 * samples.. reset the missed counter whenever we see
675 * activity */
676 if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
677 mlog(ML_HEARTBEAT, "Node %d left my region\n",
678 slot->ds_node_num);
680 /* last off the live_slot generates a callback */
681 list_del_init(&slot->ds_live_item);
682 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
683 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
685 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
686 slot->ds_node_num);
688 changed = 1;
691 /* We don't clear this because the node is still
692 * actually writing new blocks. */
693 if (!gen_changed)
694 slot->ds_changed_samples = 0;
695 goto out;
697 if (slot->ds_changed_samples) {
698 slot->ds_changed_samples = 0;
699 slot->ds_equal_samples = 0;
701 out:
702 spin_unlock(&o2hb_live_lock);
704 o2hb_run_event_list(&event);
706 o2nm_node_put(node);
707 return changed;
710 /* This could be faster if we just implmented a find_last_bit, but I
711 * don't think the circumstances warrant it. */
712 static int o2hb_highest_node(unsigned long *nodes,
713 int numbits)
715 int highest, node;
717 highest = numbits;
718 node = -1;
719 while ((node = find_next_bit(nodes, numbits, node + 1)) != -1) {
720 if (node >= numbits)
721 break;
723 highest = node;
726 return highest;
729 static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
731 int i, ret, highest_node, change = 0;
732 unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
733 struct o2hb_bio_wait_ctxt write_wc;
735 ret = o2nm_configured_node_map(configured_nodes,
736 sizeof(configured_nodes));
737 if (ret) {
738 mlog_errno(ret);
739 return ret;
742 highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
743 if (highest_node >= O2NM_MAX_NODES) {
744 mlog(ML_NOTICE, "ocfs2_heartbeat: no configured nodes found!\n");
745 return -EINVAL;
748 /* No sense in reading the slots of nodes that don't exist
749 * yet. Of course, if the node definitions have holes in them
750 * then we're reading an empty slot anyway... Consider this
751 * best-effort. */
752 ret = o2hb_read_slots(reg, highest_node + 1);
753 if (ret < 0) {
754 mlog_errno(ret);
755 return ret;
758 /* With an up to date view of the slots, we can check that no
759 * other node has been improperly configured to heartbeat in
760 * our slot. */
761 if (!o2hb_check_last_timestamp(reg))
762 mlog(ML_ERROR, "Device \"%s\": another node is heartbeating "
763 "in our slot!\n", reg->hr_dev_name);
765 /* fill in the proper info for our next heartbeat */
766 o2hb_prepare_block(reg, reg->hr_generation);
768 /* And fire off the write. Note that we don't wait on this I/O
769 * until later. */
770 ret = o2hb_issue_node_write(reg, &write_wc);
771 if (ret < 0) {
772 mlog_errno(ret);
773 return ret;
776 i = -1;
777 while((i = find_next_bit(configured_nodes, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
779 change |= o2hb_check_slot(reg, &reg->hr_slots[i]);
783 * We have to be sure we've advertised ourselves on disk
784 * before we can go to steady state. This ensures that
785 * people we find in our steady state have seen us.
787 o2hb_wait_on_io(reg, &write_wc);
788 if (write_wc.wc_error) {
789 /* Do not re-arm the write timeout on I/O error - we
790 * can't be sure that the new block ever made it to
791 * disk */
792 mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
793 write_wc.wc_error, reg->hr_dev_name);
794 return write_wc.wc_error;
797 o2hb_arm_write_timeout(reg);
799 /* let the person who launched us know when things are steady */
800 if (!change && (atomic_read(&reg->hr_steady_iterations) != 0)) {
801 if (atomic_dec_and_test(&reg->hr_steady_iterations))
802 wake_up(&o2hb_steady_queue);
805 return 0;
808 /* Subtract b from a, storing the result in a. a *must* have a larger
809 * value than b. */
810 static void o2hb_tv_subtract(struct timeval *a,
811 struct timeval *b)
813 /* just return 0 when a is after b */
814 if (a->tv_sec < b->tv_sec ||
815 (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) {
816 a->tv_sec = 0;
817 a->tv_usec = 0;
818 return;
821 a->tv_sec -= b->tv_sec;
822 a->tv_usec -= b->tv_usec;
823 while ( a->tv_usec < 0 ) {
824 a->tv_sec--;
825 a->tv_usec += 1000000;
829 static unsigned int o2hb_elapsed_msecs(struct timeval *start,
830 struct timeval *end)
832 struct timeval res = *end;
834 o2hb_tv_subtract(&res, start);
836 return res.tv_sec * 1000 + res.tv_usec / 1000;
840 * we ride the region ref that the region dir holds. before the region
841 * dir is removed and drops it ref it will wait to tear down this
842 * thread.
844 static int o2hb_thread(void *data)
846 int i, ret;
847 struct o2hb_region *reg = data;
848 struct o2hb_bio_wait_ctxt write_wc;
849 struct timeval before_hb, after_hb;
850 unsigned int elapsed_msec;
852 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
854 set_user_nice(current, -20);
856 while (!kthread_should_stop() && !reg->hr_unclean_stop) {
857 /* We track the time spent inside
858 * o2hb_do_disk_heartbeat so that we avoid more then
859 * hr_timeout_ms between disk writes. On busy systems
860 * this should result in a heartbeat which is less
861 * likely to time itself out. */
862 do_gettimeofday(&before_hb);
864 i = 0;
865 do {
866 ret = o2hb_do_disk_heartbeat(reg);
867 } while (ret && ++i < 2);
869 do_gettimeofday(&after_hb);
870 elapsed_msec = o2hb_elapsed_msecs(&before_hb, &after_hb);
872 mlog(0, "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
873 before_hb.tv_sec, (unsigned long) before_hb.tv_usec,
874 after_hb.tv_sec, (unsigned long) after_hb.tv_usec,
875 elapsed_msec);
877 if (elapsed_msec < reg->hr_timeout_ms) {
878 /* the kthread api has blocked signals for us so no
879 * need to record the return value. */
880 msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
884 o2hb_disarm_write_timeout(reg);
886 /* unclean stop is only used in very bad situation */
887 for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
888 o2hb_shutdown_slot(&reg->hr_slots[i]);
890 /* Explicit down notification - avoid forcing the other nodes
891 * to timeout on this region when we could just as easily
892 * write a clear generation - thus indicating to them that
893 * this node has left this region.
895 * XXX: Should we skip this on unclean_stop? */
896 o2hb_prepare_block(reg, 0);
897 ret = o2hb_issue_node_write(reg, &write_wc);
898 if (ret == 0) {
899 o2hb_wait_on_io(reg, &write_wc);
900 } else {
901 mlog_errno(ret);
904 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread exiting\n");
906 return 0;
909 void o2hb_init(void)
911 int i;
913 for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
914 INIT_LIST_HEAD(&o2hb_callbacks[i].list);
916 for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
917 INIT_LIST_HEAD(&o2hb_live_slots[i]);
919 INIT_LIST_HEAD(&o2hb_node_events);
921 memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
924 /* if we're already in a callback then we're already serialized by the sem */
925 static void o2hb_fill_node_map_from_callback(unsigned long *map,
926 unsigned bytes)
928 BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
930 memcpy(map, &o2hb_live_node_bitmap, bytes);
934 * get a map of all nodes that are heartbeating in any regions
936 void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
938 /* callers want to serialize this map and callbacks so that they
939 * can trust that they don't miss nodes coming to the party */
940 down_read(&o2hb_callback_sem);
941 spin_lock(&o2hb_live_lock);
942 o2hb_fill_node_map_from_callback(map, bytes);
943 spin_unlock(&o2hb_live_lock);
944 up_read(&o2hb_callback_sem);
946 EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
949 * heartbeat configfs bits. The heartbeat set is a default set under
950 * the cluster set in nodemanager.c.
953 static struct o2hb_region *to_o2hb_region(struct config_item *item)
955 return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
958 /* drop_item only drops its ref after killing the thread, nothing should
959 * be using the region anymore. this has to clean up any state that
960 * attributes might have built up. */
961 static void o2hb_region_release(struct config_item *item)
963 int i;
964 struct page *page;
965 struct o2hb_region *reg = to_o2hb_region(item);
967 if (reg->hr_tmp_block)
968 kfree(reg->hr_tmp_block);
970 if (reg->hr_slot_data) {
971 for (i = 0; i < reg->hr_num_pages; i++) {
972 page = reg->hr_slot_data[i];
973 if (page)
974 __free_page(page);
976 kfree(reg->hr_slot_data);
979 if (reg->hr_bdev)
980 blkdev_put(reg->hr_bdev);
982 if (reg->hr_slots)
983 kfree(reg->hr_slots);
985 spin_lock(&o2hb_live_lock);
986 list_del(&reg->hr_all_item);
987 spin_unlock(&o2hb_live_lock);
989 kfree(reg);
992 static int o2hb_read_block_input(struct o2hb_region *reg,
993 const char *page,
994 size_t count,
995 unsigned long *ret_bytes,
996 unsigned int *ret_bits)
998 unsigned long bytes;
999 char *p = (char *)page;
1001 bytes = simple_strtoul(p, &p, 0);
1002 if (!p || (*p && (*p != '\n')))
1003 return -EINVAL;
1005 /* Heartbeat and fs min / max block sizes are the same. */
1006 if (bytes > 4096 || bytes < 512)
1007 return -ERANGE;
1008 if (hweight16(bytes) != 1)
1009 return -EINVAL;
1011 if (ret_bytes)
1012 *ret_bytes = bytes;
1013 if (ret_bits)
1014 *ret_bits = ffs(bytes) - 1;
1016 return 0;
1019 static ssize_t o2hb_region_block_bytes_read(struct o2hb_region *reg,
1020 char *page)
1022 return sprintf(page, "%u\n", reg->hr_block_bytes);
1025 static ssize_t o2hb_region_block_bytes_write(struct o2hb_region *reg,
1026 const char *page,
1027 size_t count)
1029 int status;
1030 unsigned long block_bytes;
1031 unsigned int block_bits;
1033 if (reg->hr_bdev)
1034 return -EINVAL;
1036 status = o2hb_read_block_input(reg, page, count,
1037 &block_bytes, &block_bits);
1038 if (status)
1039 return status;
1041 reg->hr_block_bytes = (unsigned int)block_bytes;
1042 reg->hr_block_bits = block_bits;
1044 return count;
1047 static ssize_t o2hb_region_start_block_read(struct o2hb_region *reg,
1048 char *page)
1050 return sprintf(page, "%llu\n", reg->hr_start_block);
1053 static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
1054 const char *page,
1055 size_t count)
1057 unsigned long long tmp;
1058 char *p = (char *)page;
1060 if (reg->hr_bdev)
1061 return -EINVAL;
1063 tmp = simple_strtoull(p, &p, 0);
1064 if (!p || (*p && (*p != '\n')))
1065 return -EINVAL;
1067 reg->hr_start_block = tmp;
1069 return count;
1072 static ssize_t o2hb_region_blocks_read(struct o2hb_region *reg,
1073 char *page)
1075 return sprintf(page, "%d\n", reg->hr_blocks);
1078 static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
1079 const char *page,
1080 size_t count)
1082 unsigned long tmp;
1083 char *p = (char *)page;
1085 if (reg->hr_bdev)
1086 return -EINVAL;
1088 tmp = simple_strtoul(p, &p, 0);
1089 if (!p || (*p && (*p != '\n')))
1090 return -EINVAL;
1092 if (tmp > O2NM_MAX_NODES || tmp == 0)
1093 return -ERANGE;
1095 reg->hr_blocks = (unsigned int)tmp;
1097 return count;
1100 static ssize_t o2hb_region_dev_read(struct o2hb_region *reg,
1101 char *page)
1103 unsigned int ret = 0;
1105 if (reg->hr_bdev)
1106 ret = sprintf(page, "%s\n", reg->hr_dev_name);
1108 return ret;
1111 static void o2hb_init_region_params(struct o2hb_region *reg)
1113 reg->hr_slots_per_page = PAGE_CACHE_SIZE >> reg->hr_block_bits;
1114 reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
1116 mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
1117 reg->hr_start_block, reg->hr_blocks);
1118 mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
1119 reg->hr_block_bytes, reg->hr_block_bits);
1120 mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
1121 mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
1124 static int o2hb_map_slot_data(struct o2hb_region *reg)
1126 int i, j;
1127 unsigned int last_slot;
1128 unsigned int spp = reg->hr_slots_per_page;
1129 struct page *page;
1130 char *raw;
1131 struct o2hb_disk_slot *slot;
1133 reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
1134 if (reg->hr_tmp_block == NULL) {
1135 mlog_errno(-ENOMEM);
1136 return -ENOMEM;
1139 reg->hr_slots = kcalloc(reg->hr_blocks,
1140 sizeof(struct o2hb_disk_slot), GFP_KERNEL);
1141 if (reg->hr_slots == NULL) {
1142 mlog_errno(-ENOMEM);
1143 return -ENOMEM;
1146 for(i = 0; i < reg->hr_blocks; i++) {
1147 slot = &reg->hr_slots[i];
1148 slot->ds_node_num = i;
1149 INIT_LIST_HEAD(&slot->ds_live_item);
1150 slot->ds_raw_block = NULL;
1153 reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
1154 mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
1155 "at %u blocks per page\n",
1156 reg->hr_num_pages, reg->hr_blocks, spp);
1158 reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
1159 GFP_KERNEL);
1160 if (!reg->hr_slot_data) {
1161 mlog_errno(-ENOMEM);
1162 return -ENOMEM;
1165 for(i = 0; i < reg->hr_num_pages; i++) {
1166 page = alloc_page(GFP_KERNEL);
1167 if (!page) {
1168 mlog_errno(-ENOMEM);
1169 return -ENOMEM;
1172 reg->hr_slot_data[i] = page;
1174 last_slot = i * spp;
1175 raw = page_address(page);
1176 for (j = 0;
1177 (j < spp) && ((j + last_slot) < reg->hr_blocks);
1178 j++) {
1179 BUG_ON((j + last_slot) >= reg->hr_blocks);
1181 slot = &reg->hr_slots[j + last_slot];
1182 slot->ds_raw_block =
1183 (struct o2hb_disk_heartbeat_block *) raw;
1185 raw += reg->hr_block_bytes;
1189 return 0;
1192 /* Read in all the slots available and populate the tracking
1193 * structures so that we can start with a baseline idea of what's
1194 * there. */
1195 static int o2hb_populate_slot_data(struct o2hb_region *reg)
1197 int ret, i;
1198 struct o2hb_disk_slot *slot;
1199 struct o2hb_disk_heartbeat_block *hb_block;
1201 mlog_entry_void();
1203 ret = o2hb_read_slots(reg, reg->hr_blocks);
1204 if (ret) {
1205 mlog_errno(ret);
1206 goto out;
1209 /* We only want to get an idea of the values initially in each
1210 * slot, so we do no verification - o2hb_check_slot will
1211 * actually determine if each configured slot is valid and
1212 * whether any values have changed. */
1213 for(i = 0; i < reg->hr_blocks; i++) {
1214 slot = &reg->hr_slots[i];
1215 hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
1217 /* Only fill the values that o2hb_check_slot uses to
1218 * determine changing slots */
1219 slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
1220 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
1223 out:
1224 mlog_exit(ret);
1225 return ret;
1228 /* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1229 static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
1230 const char *page,
1231 size_t count)
1233 struct task_struct *hb_task;
1234 long fd;
1235 int sectsize;
1236 char *p = (char *)page;
1237 struct file *filp = NULL;
1238 struct inode *inode = NULL;
1239 ssize_t ret = -EINVAL;
1241 if (reg->hr_bdev)
1242 goto out;
1244 /* We can't heartbeat without having had our node number
1245 * configured yet. */
1246 if (o2nm_this_node() == O2NM_MAX_NODES)
1247 goto out;
1249 fd = simple_strtol(p, &p, 0);
1250 if (!p || (*p && (*p != '\n')))
1251 goto out;
1253 if (fd < 0 || fd >= INT_MAX)
1254 goto out;
1256 filp = fget(fd);
1257 if (filp == NULL)
1258 goto out;
1260 if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
1261 reg->hr_block_bytes == 0)
1262 goto out;
1264 inode = igrab(filp->f_mapping->host);
1265 if (inode == NULL)
1266 goto out;
1268 if (!S_ISBLK(inode->i_mode))
1269 goto out;
1271 reg->hr_bdev = I_BDEV(filp->f_mapping->host);
1272 ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ, 0);
1273 if (ret) {
1274 reg->hr_bdev = NULL;
1275 goto out;
1277 inode = NULL;
1279 bdevname(reg->hr_bdev, reg->hr_dev_name);
1281 sectsize = bdev_hardsect_size(reg->hr_bdev);
1282 if (sectsize != reg->hr_block_bytes) {
1283 mlog(ML_ERROR,
1284 "blocksize %u incorrect for device, expected %d",
1285 reg->hr_block_bytes, sectsize);
1286 ret = -EINVAL;
1287 goto out;
1290 o2hb_init_region_params(reg);
1292 /* Generation of zero is invalid */
1293 do {
1294 get_random_bytes(&reg->hr_generation,
1295 sizeof(reg->hr_generation));
1296 } while (reg->hr_generation == 0);
1298 ret = o2hb_map_slot_data(reg);
1299 if (ret) {
1300 mlog_errno(ret);
1301 goto out;
1304 ret = o2hb_populate_slot_data(reg);
1305 if (ret) {
1306 mlog_errno(ret);
1307 goto out;
1310 INIT_DELAYED_WORK(&reg->hr_write_timeout_work, o2hb_write_timeout);
1313 * A node is considered live after it has beat LIVE_THRESHOLD
1314 * times. We're not steady until we've given them a chance
1315 * _after_ our first read.
1317 atomic_set(&reg->hr_steady_iterations, O2HB_LIVE_THRESHOLD + 1);
1319 hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
1320 reg->hr_item.ci_name);
1321 if (IS_ERR(hb_task)) {
1322 ret = PTR_ERR(hb_task);
1323 mlog_errno(ret);
1324 goto out;
1327 spin_lock(&o2hb_live_lock);
1328 reg->hr_task = hb_task;
1329 spin_unlock(&o2hb_live_lock);
1331 ret = wait_event_interruptible(o2hb_steady_queue,
1332 atomic_read(&reg->hr_steady_iterations) == 0);
1333 if (ret) {
1334 /* We got interrupted (hello ptrace!). Clean up */
1335 spin_lock(&o2hb_live_lock);
1336 hb_task = reg->hr_task;
1337 reg->hr_task = NULL;
1338 spin_unlock(&o2hb_live_lock);
1340 if (hb_task)
1341 kthread_stop(hb_task);
1342 goto out;
1345 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1346 spin_lock(&o2hb_live_lock);
1347 hb_task = reg->hr_task;
1348 spin_unlock(&o2hb_live_lock);
1350 if (hb_task)
1351 ret = count;
1352 else
1353 ret = -EIO;
1355 out:
1356 if (filp)
1357 fput(filp);
1358 if (inode)
1359 iput(inode);
1360 if (ret < 0) {
1361 if (reg->hr_bdev) {
1362 blkdev_put(reg->hr_bdev);
1363 reg->hr_bdev = NULL;
1366 return ret;
1369 static ssize_t o2hb_region_pid_read(struct o2hb_region *reg,
1370 char *page)
1372 pid_t pid = 0;
1374 spin_lock(&o2hb_live_lock);
1375 if (reg->hr_task)
1376 pid = reg->hr_task->pid;
1377 spin_unlock(&o2hb_live_lock);
1379 if (!pid)
1380 return 0;
1382 return sprintf(page, "%u\n", pid);
1385 struct o2hb_region_attribute {
1386 struct configfs_attribute attr;
1387 ssize_t (*show)(struct o2hb_region *, char *);
1388 ssize_t (*store)(struct o2hb_region *, const char *, size_t);
1391 static struct o2hb_region_attribute o2hb_region_attr_block_bytes = {
1392 .attr = { .ca_owner = THIS_MODULE,
1393 .ca_name = "block_bytes",
1394 .ca_mode = S_IRUGO | S_IWUSR },
1395 .show = o2hb_region_block_bytes_read,
1396 .store = o2hb_region_block_bytes_write,
1399 static struct o2hb_region_attribute o2hb_region_attr_start_block = {
1400 .attr = { .ca_owner = THIS_MODULE,
1401 .ca_name = "start_block",
1402 .ca_mode = S_IRUGO | S_IWUSR },
1403 .show = o2hb_region_start_block_read,
1404 .store = o2hb_region_start_block_write,
1407 static struct o2hb_region_attribute o2hb_region_attr_blocks = {
1408 .attr = { .ca_owner = THIS_MODULE,
1409 .ca_name = "blocks",
1410 .ca_mode = S_IRUGO | S_IWUSR },
1411 .show = o2hb_region_blocks_read,
1412 .store = o2hb_region_blocks_write,
1415 static struct o2hb_region_attribute o2hb_region_attr_dev = {
1416 .attr = { .ca_owner = THIS_MODULE,
1417 .ca_name = "dev",
1418 .ca_mode = S_IRUGO | S_IWUSR },
1419 .show = o2hb_region_dev_read,
1420 .store = o2hb_region_dev_write,
1423 static struct o2hb_region_attribute o2hb_region_attr_pid = {
1424 .attr = { .ca_owner = THIS_MODULE,
1425 .ca_name = "pid",
1426 .ca_mode = S_IRUGO | S_IRUSR },
1427 .show = o2hb_region_pid_read,
1430 static struct configfs_attribute *o2hb_region_attrs[] = {
1431 &o2hb_region_attr_block_bytes.attr,
1432 &o2hb_region_attr_start_block.attr,
1433 &o2hb_region_attr_blocks.attr,
1434 &o2hb_region_attr_dev.attr,
1435 &o2hb_region_attr_pid.attr,
1436 NULL,
1439 static ssize_t o2hb_region_show(struct config_item *item,
1440 struct configfs_attribute *attr,
1441 char *page)
1443 struct o2hb_region *reg = to_o2hb_region(item);
1444 struct o2hb_region_attribute *o2hb_region_attr =
1445 container_of(attr, struct o2hb_region_attribute, attr);
1446 ssize_t ret = 0;
1448 if (o2hb_region_attr->show)
1449 ret = o2hb_region_attr->show(reg, page);
1450 return ret;
1453 static ssize_t o2hb_region_store(struct config_item *item,
1454 struct configfs_attribute *attr,
1455 const char *page, size_t count)
1457 struct o2hb_region *reg = to_o2hb_region(item);
1458 struct o2hb_region_attribute *o2hb_region_attr =
1459 container_of(attr, struct o2hb_region_attribute, attr);
1460 ssize_t ret = -EINVAL;
1462 if (o2hb_region_attr->store)
1463 ret = o2hb_region_attr->store(reg, page, count);
1464 return ret;
1467 static struct configfs_item_operations o2hb_region_item_ops = {
1468 .release = o2hb_region_release,
1469 .show_attribute = o2hb_region_show,
1470 .store_attribute = o2hb_region_store,
1473 static struct config_item_type o2hb_region_type = {
1474 .ct_item_ops = &o2hb_region_item_ops,
1475 .ct_attrs = o2hb_region_attrs,
1476 .ct_owner = THIS_MODULE,
1479 /* heartbeat set */
1481 struct o2hb_heartbeat_group {
1482 struct config_group hs_group;
1483 /* some stuff? */
1486 static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
1488 return group ?
1489 container_of(group, struct o2hb_heartbeat_group, hs_group)
1490 : NULL;
1493 static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
1494 const char *name)
1496 struct o2hb_region *reg = NULL;
1497 struct config_item *ret = NULL;
1499 reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
1500 if (reg == NULL)
1501 goto out; /* ENOMEM */
1503 config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
1505 ret = &reg->hr_item;
1507 spin_lock(&o2hb_live_lock);
1508 list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
1509 spin_unlock(&o2hb_live_lock);
1510 out:
1511 if (ret == NULL)
1512 kfree(reg);
1514 return ret;
1517 static void o2hb_heartbeat_group_drop_item(struct config_group *group,
1518 struct config_item *item)
1520 struct task_struct *hb_task;
1521 struct o2hb_region *reg = to_o2hb_region(item);
1523 /* stop the thread when the user removes the region dir */
1524 spin_lock(&o2hb_live_lock);
1525 hb_task = reg->hr_task;
1526 reg->hr_task = NULL;
1527 spin_unlock(&o2hb_live_lock);
1529 if (hb_task)
1530 kthread_stop(hb_task);
1533 * If we're racing a dev_write(), we need to wake them. They will
1534 * check reg->hr_task
1536 if (atomic_read(&reg->hr_steady_iterations) != 0) {
1537 atomic_set(&reg->hr_steady_iterations, 0);
1538 wake_up(&o2hb_steady_queue);
1541 config_item_put(item);
1544 struct o2hb_heartbeat_group_attribute {
1545 struct configfs_attribute attr;
1546 ssize_t (*show)(struct o2hb_heartbeat_group *, char *);
1547 ssize_t (*store)(struct o2hb_heartbeat_group *, const char *, size_t);
1550 static ssize_t o2hb_heartbeat_group_show(struct config_item *item,
1551 struct configfs_attribute *attr,
1552 char *page)
1554 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1555 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1556 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1557 ssize_t ret = 0;
1559 if (o2hb_heartbeat_group_attr->show)
1560 ret = o2hb_heartbeat_group_attr->show(reg, page);
1561 return ret;
1564 static ssize_t o2hb_heartbeat_group_store(struct config_item *item,
1565 struct configfs_attribute *attr,
1566 const char *page, size_t count)
1568 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1569 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1570 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1571 ssize_t ret = -EINVAL;
1573 if (o2hb_heartbeat_group_attr->store)
1574 ret = o2hb_heartbeat_group_attr->store(reg, page, count);
1575 return ret;
1578 static ssize_t o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group *group,
1579 char *page)
1581 return sprintf(page, "%u\n", o2hb_dead_threshold);
1584 static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group *group,
1585 const char *page,
1586 size_t count)
1588 unsigned long tmp;
1589 char *p = (char *)page;
1591 tmp = simple_strtoul(p, &p, 10);
1592 if (!p || (*p && (*p != '\n')))
1593 return -EINVAL;
1595 /* this will validate ranges for us. */
1596 o2hb_dead_threshold_set((unsigned int) tmp);
1598 return count;
1601 static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold = {
1602 .attr = { .ca_owner = THIS_MODULE,
1603 .ca_name = "dead_threshold",
1604 .ca_mode = S_IRUGO | S_IWUSR },
1605 .show = o2hb_heartbeat_group_threshold_show,
1606 .store = o2hb_heartbeat_group_threshold_store,
1609 static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
1610 &o2hb_heartbeat_group_attr_threshold.attr,
1611 NULL,
1614 static struct configfs_item_operations o2hb_hearbeat_group_item_ops = {
1615 .show_attribute = o2hb_heartbeat_group_show,
1616 .store_attribute = o2hb_heartbeat_group_store,
1619 static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
1620 .make_item = o2hb_heartbeat_group_make_item,
1621 .drop_item = o2hb_heartbeat_group_drop_item,
1624 static struct config_item_type o2hb_heartbeat_group_type = {
1625 .ct_group_ops = &o2hb_heartbeat_group_group_ops,
1626 .ct_item_ops = &o2hb_hearbeat_group_item_ops,
1627 .ct_attrs = o2hb_heartbeat_group_attrs,
1628 .ct_owner = THIS_MODULE,
1631 /* this is just here to avoid touching group in heartbeat.h which the
1632 * entire damn world #includes */
1633 struct config_group *o2hb_alloc_hb_set(void)
1635 struct o2hb_heartbeat_group *hs = NULL;
1636 struct config_group *ret = NULL;
1638 hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
1639 if (hs == NULL)
1640 goto out;
1642 config_group_init_type_name(&hs->hs_group, "heartbeat",
1643 &o2hb_heartbeat_group_type);
1645 ret = &hs->hs_group;
1646 out:
1647 if (ret == NULL)
1648 kfree(hs);
1649 return ret;
1652 void o2hb_free_hb_set(struct config_group *group)
1654 struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
1655 kfree(hs);
1658 /* hb callback registration and issueing */
1660 static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
1662 if (type == O2HB_NUM_CB)
1663 return ERR_PTR(-EINVAL);
1665 return &o2hb_callbacks[type];
1668 void o2hb_setup_callback(struct o2hb_callback_func *hc,
1669 enum o2hb_callback_type type,
1670 o2hb_cb_func *func,
1671 void *data,
1672 int priority)
1674 INIT_LIST_HEAD(&hc->hc_item);
1675 hc->hc_func = func;
1676 hc->hc_data = data;
1677 hc->hc_priority = priority;
1678 hc->hc_type = type;
1679 hc->hc_magic = O2HB_CB_MAGIC;
1681 EXPORT_SYMBOL_GPL(o2hb_setup_callback);
1683 static struct o2hb_region *o2hb_find_region(const char *region_uuid)
1685 struct o2hb_region *p, *reg = NULL;
1687 assert_spin_locked(&o2hb_live_lock);
1689 list_for_each_entry(p, &o2hb_all_regions, hr_all_item) {
1690 if (!strcmp(region_uuid, config_item_name(&p->hr_item))) {
1691 reg = p;
1692 break;
1696 return reg;
1699 static int o2hb_region_get(const char *region_uuid)
1701 int ret = 0;
1702 struct o2hb_region *reg;
1704 spin_lock(&o2hb_live_lock);
1706 reg = o2hb_find_region(region_uuid);
1707 if (!reg)
1708 ret = -ENOENT;
1709 spin_unlock(&o2hb_live_lock);
1711 if (ret)
1712 goto out;
1714 ret = o2nm_depend_this_node();
1715 if (ret)
1716 goto out;
1718 ret = o2nm_depend_item(&reg->hr_item);
1719 if (ret)
1720 o2nm_undepend_this_node();
1722 out:
1723 return ret;
1726 static void o2hb_region_put(const char *region_uuid)
1728 struct o2hb_region *reg;
1730 spin_lock(&o2hb_live_lock);
1732 reg = o2hb_find_region(region_uuid);
1734 spin_unlock(&o2hb_live_lock);
1736 if (reg) {
1737 o2nm_undepend_item(&reg->hr_item);
1738 o2nm_undepend_this_node();
1742 int o2hb_register_callback(const char *region_uuid,
1743 struct o2hb_callback_func *hc)
1745 struct o2hb_callback_func *tmp;
1746 struct list_head *iter;
1747 struct o2hb_callback *hbcall;
1748 int ret;
1750 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
1751 BUG_ON(!list_empty(&hc->hc_item));
1753 hbcall = hbcall_from_type(hc->hc_type);
1754 if (IS_ERR(hbcall)) {
1755 ret = PTR_ERR(hbcall);
1756 goto out;
1759 if (region_uuid) {
1760 ret = o2hb_region_get(region_uuid);
1761 if (ret)
1762 goto out;
1765 down_write(&o2hb_callback_sem);
1767 list_for_each(iter, &hbcall->list) {
1768 tmp = list_entry(iter, struct o2hb_callback_func, hc_item);
1769 if (hc->hc_priority < tmp->hc_priority) {
1770 list_add_tail(&hc->hc_item, iter);
1771 break;
1774 if (list_empty(&hc->hc_item))
1775 list_add_tail(&hc->hc_item, &hbcall->list);
1777 up_write(&o2hb_callback_sem);
1778 ret = 0;
1779 out:
1780 mlog(ML_HEARTBEAT, "returning %d on behalf of %p for funcs %p\n",
1781 ret, __builtin_return_address(0), hc);
1782 return ret;
1784 EXPORT_SYMBOL_GPL(o2hb_register_callback);
1786 void o2hb_unregister_callback(const char *region_uuid,
1787 struct o2hb_callback_func *hc)
1789 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
1791 mlog(ML_HEARTBEAT, "on behalf of %p for funcs %p\n",
1792 __builtin_return_address(0), hc);
1794 /* XXX Can this happen _with_ a region reference? */
1795 if (list_empty(&hc->hc_item))
1796 return;
1798 if (region_uuid)
1799 o2hb_region_put(region_uuid);
1801 down_write(&o2hb_callback_sem);
1803 list_del_init(&hc->hc_item);
1805 up_write(&o2hb_callback_sem);
1807 EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
1809 int o2hb_check_node_heartbeating(u8 node_num)
1811 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1813 o2hb_fill_node_map(testing_map, sizeof(testing_map));
1814 if (!test_bit(node_num, testing_map)) {
1815 mlog(ML_HEARTBEAT,
1816 "node (%u) does not have heartbeating enabled.\n",
1817 node_num);
1818 return 0;
1821 return 1;
1823 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating);
1825 int o2hb_check_node_heartbeating_from_callback(u8 node_num)
1827 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1829 o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
1830 if (!test_bit(node_num, testing_map)) {
1831 mlog(ML_HEARTBEAT,
1832 "node (%u) does not have heartbeating enabled.\n",
1833 node_num);
1834 return 0;
1837 return 1;
1839 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
1841 /* Makes sure our local node is configured with a node number, and is
1842 * heartbeating. */
1843 int o2hb_check_local_node_heartbeating(void)
1845 u8 node_num;
1847 /* if this node was set then we have networking */
1848 node_num = o2nm_this_node();
1849 if (node_num == O2NM_MAX_NODES) {
1850 mlog(ML_HEARTBEAT, "this node has not been configured.\n");
1851 return 0;
1854 return o2hb_check_node_heartbeating(node_num);
1856 EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating);
1859 * this is just a hack until we get the plumbing which flips file systems
1860 * read only and drops the hb ref instead of killing the node dead.
1862 void o2hb_stop_all_regions(void)
1864 struct o2hb_region *reg;
1866 mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
1868 spin_lock(&o2hb_live_lock);
1870 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
1871 reg->hr_unclean_stop = 1;
1873 spin_unlock(&o2hb_live_lock);
1875 EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);