1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * Copyright (C) 2004, 2005 Oracle. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/jiffies.h>
25 #include <linux/module.h>
27 #include <linux/bio.h>
28 #include <linux/blkdev.h>
29 #include <linux/delay.h>
30 #include <linux/file.h>
31 #include <linux/kthread.h>
32 #include <linux/configfs.h>
33 #include <linux/random.h>
34 #include <linux/crc32.h>
35 #include <linux/time.h>
36 #include <linux/debugfs.h>
38 #include "heartbeat.h"
40 #include "nodemanager.h"
47 * The first heartbeat pass had one global thread that would serialize all hb
48 * callback calls. This global serializing sem should only be removed once
49 * we've made sure that all callees can deal with being called concurrently
50 * from multiple hb region threads.
52 static DECLARE_RWSEM(o2hb_callback_sem
);
55 * multiple hb threads are watching multiple regions. A node is live
56 * whenever any of the threads sees activity from the node in its region.
58 static DEFINE_SPINLOCK(o2hb_live_lock
);
59 static struct list_head o2hb_live_slots
[O2NM_MAX_NODES
];
60 static unsigned long o2hb_live_node_bitmap
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
61 static LIST_HEAD(o2hb_node_events
);
62 static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue
);
64 #define O2HB_DEBUG_DIR "o2hb"
65 #define O2HB_DEBUG_LIVENODES "livenodes"
66 static struct dentry
*o2hb_debug_dir
;
67 static struct dentry
*o2hb_debug_livenodes
;
69 static LIST_HEAD(o2hb_all_regions
);
71 static struct o2hb_callback
{
72 struct list_head list
;
73 } o2hb_callbacks
[O2HB_NUM_CB
];
75 static struct o2hb_callback
*hbcall_from_type(enum o2hb_callback_type type
);
77 #define O2HB_DEFAULT_BLOCK_BITS 9
79 unsigned int o2hb_dead_threshold
= O2HB_DEFAULT_DEAD_THRESHOLD
;
81 /* Only sets a new threshold if there are no active regions.
83 * No locking or otherwise interesting code is required for reading
84 * o2hb_dead_threshold as it can't change once regions are active and
85 * it's not interesting to anyone until then anyway. */
86 static void o2hb_dead_threshold_set(unsigned int threshold
)
88 if (threshold
> O2HB_MIN_DEAD_THRESHOLD
) {
89 spin_lock(&o2hb_live_lock
);
90 if (list_empty(&o2hb_all_regions
))
91 o2hb_dead_threshold
= threshold
;
92 spin_unlock(&o2hb_live_lock
);
96 struct o2hb_node_event
{
97 struct list_head hn_item
;
98 enum o2hb_callback_type hn_event_type
;
99 struct o2nm_node
*hn_node
;
103 struct o2hb_disk_slot
{
104 struct o2hb_disk_heartbeat_block
*ds_raw_block
;
107 u64 ds_last_generation
;
108 u16 ds_equal_samples
;
109 u16 ds_changed_samples
;
110 struct list_head ds_live_item
;
113 /* each thread owns a region.. when we're asked to tear down the region
114 * we ask the thread to stop, who cleans up the region */
116 struct config_item hr_item
;
118 struct list_head hr_all_item
;
119 unsigned hr_unclean_stop
:1;
121 /* protected by the hr_callback_sem */
122 struct task_struct
*hr_task
;
124 unsigned int hr_blocks
;
125 unsigned long long hr_start_block
;
127 unsigned int hr_block_bits
;
128 unsigned int hr_block_bytes
;
130 unsigned int hr_slots_per_page
;
131 unsigned int hr_num_pages
;
133 struct page
**hr_slot_data
;
134 struct block_device
*hr_bdev
;
135 struct o2hb_disk_slot
*hr_slots
;
137 /* let the person setting up hb wait for it to return until it
138 * has reached a 'steady' state. This will be fixed when we have
139 * a more complete api that doesn't lead to this sort of fragility. */
140 atomic_t hr_steady_iterations
;
142 char hr_dev_name
[BDEVNAME_SIZE
];
144 unsigned int hr_timeout_ms
;
146 /* randomized as the region goes up and down so that a node
147 * recognizes a node going up and down in one iteration */
150 struct delayed_work hr_write_timeout_work
;
151 unsigned long hr_last_timeout_start
;
153 /* Used during o2hb_check_slot to hold a copy of the block
154 * being checked because we temporarily have to zero out the
156 struct o2hb_disk_heartbeat_block
*hr_tmp_block
;
159 struct o2hb_bio_wait_ctxt
{
160 atomic_t wc_num_reqs
;
161 struct completion wc_io_complete
;
165 static void o2hb_write_timeout(struct work_struct
*work
)
167 struct o2hb_region
*reg
=
168 container_of(work
, struct o2hb_region
,
169 hr_write_timeout_work
.work
);
171 mlog(ML_ERROR
, "Heartbeat write timeout to device %s after %u "
172 "milliseconds\n", reg
->hr_dev_name
,
173 jiffies_to_msecs(jiffies
- reg
->hr_last_timeout_start
));
174 o2quo_disk_timeout();
177 static void o2hb_arm_write_timeout(struct o2hb_region
*reg
)
179 mlog(ML_HEARTBEAT
, "Queue write timeout for %u ms\n",
180 O2HB_MAX_WRITE_TIMEOUT_MS
);
182 cancel_delayed_work(®
->hr_write_timeout_work
);
183 reg
->hr_last_timeout_start
= jiffies
;
184 schedule_delayed_work(®
->hr_write_timeout_work
,
185 msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS
));
188 static void o2hb_disarm_write_timeout(struct o2hb_region
*reg
)
190 cancel_delayed_work(®
->hr_write_timeout_work
);
191 flush_scheduled_work();
194 static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt
*wc
)
196 atomic_set(&wc
->wc_num_reqs
, 1);
197 init_completion(&wc
->wc_io_complete
);
201 /* Used in error paths too */
202 static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt
*wc
,
205 /* sadly atomic_sub_and_test() isn't available on all platforms. The
206 * good news is that the fast path only completes one at a time */
208 if (atomic_dec_and_test(&wc
->wc_num_reqs
)) {
210 complete(&wc
->wc_io_complete
);
215 static void o2hb_wait_on_io(struct o2hb_region
*reg
,
216 struct o2hb_bio_wait_ctxt
*wc
)
218 struct address_space
*mapping
= reg
->hr_bdev
->bd_inode
->i_mapping
;
220 blk_run_address_space(mapping
);
221 o2hb_bio_wait_dec(wc
, 1);
223 wait_for_completion(&wc
->wc_io_complete
);
226 static void o2hb_bio_end_io(struct bio
*bio
,
229 struct o2hb_bio_wait_ctxt
*wc
= bio
->bi_private
;
232 mlog(ML_ERROR
, "IO Error %d\n", error
);
233 wc
->wc_error
= error
;
236 o2hb_bio_wait_dec(wc
, 1);
240 /* Setup a Bio to cover I/O against num_slots slots starting at
242 static struct bio
*o2hb_setup_one_bio(struct o2hb_region
*reg
,
243 struct o2hb_bio_wait_ctxt
*wc
,
244 unsigned int *current_slot
,
245 unsigned int max_slots
)
247 int len
, current_page
;
248 unsigned int vec_len
, vec_start
;
249 unsigned int bits
= reg
->hr_block_bits
;
250 unsigned int spp
= reg
->hr_slots_per_page
;
251 unsigned int cs
= *current_slot
;
255 /* Testing has shown this allocation to take long enough under
256 * GFP_KERNEL that the local node can get fenced. It would be
257 * nicest if we could pre-allocate these bios and avoid this
259 bio
= bio_alloc(GFP_ATOMIC
, 16);
261 mlog(ML_ERROR
, "Could not alloc slots BIO!\n");
262 bio
= ERR_PTR(-ENOMEM
);
266 /* Must put everything in 512 byte sectors for the bio... */
267 bio
->bi_sector
= (reg
->hr_start_block
+ cs
) << (bits
- 9);
268 bio
->bi_bdev
= reg
->hr_bdev
;
269 bio
->bi_private
= wc
;
270 bio
->bi_end_io
= o2hb_bio_end_io
;
272 vec_start
= (cs
<< bits
) % PAGE_CACHE_SIZE
;
273 while(cs
< max_slots
) {
274 current_page
= cs
/ spp
;
275 page
= reg
->hr_slot_data
[current_page
];
277 vec_len
= min(PAGE_CACHE_SIZE
- vec_start
,
278 (max_slots
-cs
) * (PAGE_CACHE_SIZE
/spp
) );
280 mlog(ML_HB_BIO
, "page %d, vec_len = %u, vec_start = %u\n",
281 current_page
, vec_len
, vec_start
);
283 len
= bio_add_page(bio
, page
, vec_len
, vec_start
);
284 if (len
!= vec_len
) break;
286 cs
+= vec_len
/ (PAGE_CACHE_SIZE
/spp
);
295 static int o2hb_read_slots(struct o2hb_region
*reg
,
296 unsigned int max_slots
)
298 unsigned int current_slot
=0;
300 struct o2hb_bio_wait_ctxt wc
;
303 o2hb_bio_wait_init(&wc
);
305 while(current_slot
< max_slots
) {
306 bio
= o2hb_setup_one_bio(reg
, &wc
, ¤t_slot
, max_slots
);
308 status
= PTR_ERR(bio
);
313 atomic_inc(&wc
.wc_num_reqs
);
314 submit_bio(READ
, bio
);
320 o2hb_wait_on_io(reg
, &wc
);
321 if (wc
.wc_error
&& !status
)
322 status
= wc
.wc_error
;
327 static int o2hb_issue_node_write(struct o2hb_region
*reg
,
328 struct o2hb_bio_wait_ctxt
*write_wc
)
334 o2hb_bio_wait_init(write_wc
);
336 slot
= o2nm_this_node();
338 bio
= o2hb_setup_one_bio(reg
, write_wc
, &slot
, slot
+1);
340 status
= PTR_ERR(bio
);
345 atomic_inc(&write_wc
->wc_num_reqs
);
346 submit_bio(WRITE
, bio
);
353 static u32
o2hb_compute_block_crc_le(struct o2hb_region
*reg
,
354 struct o2hb_disk_heartbeat_block
*hb_block
)
359 /* We want to compute the block crc with a 0 value in the
360 * hb_cksum field. Save it off here and replace after the
362 old_cksum
= hb_block
->hb_cksum
;
363 hb_block
->hb_cksum
= 0;
365 ret
= crc32_le(0, (unsigned char *) hb_block
, reg
->hr_block_bytes
);
367 hb_block
->hb_cksum
= old_cksum
;
372 static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block
*hb_block
)
374 mlog(ML_ERROR
, "Dump slot information: seq = 0x%llx, node = %u, "
375 "cksum = 0x%x, generation 0x%llx\n",
376 (long long)le64_to_cpu(hb_block
->hb_seq
),
377 hb_block
->hb_node
, le32_to_cpu(hb_block
->hb_cksum
),
378 (long long)le64_to_cpu(hb_block
->hb_generation
));
381 static int o2hb_verify_crc(struct o2hb_region
*reg
,
382 struct o2hb_disk_heartbeat_block
*hb_block
)
386 read
= le32_to_cpu(hb_block
->hb_cksum
);
387 computed
= o2hb_compute_block_crc_le(reg
, hb_block
);
389 return read
== computed
;
392 /* We want to make sure that nobody is heartbeating on top of us --
393 * this will help detect an invalid configuration. */
394 static int o2hb_check_last_timestamp(struct o2hb_region
*reg
)
397 struct o2hb_disk_slot
*slot
;
398 struct o2hb_disk_heartbeat_block
*hb_block
;
400 node_num
= o2nm_this_node();
403 slot
= ®
->hr_slots
[node_num
];
404 /* Don't check on our 1st timestamp */
405 if (slot
->ds_last_time
) {
406 hb_block
= slot
->ds_raw_block
;
408 if (le64_to_cpu(hb_block
->hb_seq
) != slot
->ds_last_time
)
415 static inline void o2hb_prepare_block(struct o2hb_region
*reg
,
420 struct o2hb_disk_slot
*slot
;
421 struct o2hb_disk_heartbeat_block
*hb_block
;
423 node_num
= o2nm_this_node();
424 slot
= ®
->hr_slots
[node_num
];
426 hb_block
= (struct o2hb_disk_heartbeat_block
*)slot
->ds_raw_block
;
427 memset(hb_block
, 0, reg
->hr_block_bytes
);
428 /* TODO: time stuff */
429 cputime
= CURRENT_TIME
.tv_sec
;
433 hb_block
->hb_seq
= cpu_to_le64(cputime
);
434 hb_block
->hb_node
= node_num
;
435 hb_block
->hb_generation
= cpu_to_le64(generation
);
436 hb_block
->hb_dead_ms
= cpu_to_le32(o2hb_dead_threshold
* O2HB_REGION_TIMEOUT_MS
);
438 /* This step must always happen last! */
439 hb_block
->hb_cksum
= cpu_to_le32(o2hb_compute_block_crc_le(reg
,
442 mlog(ML_HB_BIO
, "our node generation = 0x%llx, cksum = 0x%x\n",
443 (long long)generation
,
444 le32_to_cpu(hb_block
->hb_cksum
));
447 static void o2hb_fire_callbacks(struct o2hb_callback
*hbcall
,
448 struct o2nm_node
*node
,
451 struct list_head
*iter
;
452 struct o2hb_callback_func
*f
;
454 list_for_each(iter
, &hbcall
->list
) {
455 f
= list_entry(iter
, struct o2hb_callback_func
, hc_item
);
456 mlog(ML_HEARTBEAT
, "calling funcs %p\n", f
);
457 (f
->hc_func
)(node
, idx
, f
->hc_data
);
461 /* Will run the list in order until we process the passed event */
462 static void o2hb_run_event_list(struct o2hb_node_event
*queued_event
)
465 struct o2hb_callback
*hbcall
;
466 struct o2hb_node_event
*event
;
468 spin_lock(&o2hb_live_lock
);
469 empty
= list_empty(&queued_event
->hn_item
);
470 spin_unlock(&o2hb_live_lock
);
474 /* Holding callback sem assures we don't alter the callback
475 * lists when doing this, and serializes ourselves with other
476 * processes wanting callbacks. */
477 down_write(&o2hb_callback_sem
);
479 spin_lock(&o2hb_live_lock
);
480 while (!list_empty(&o2hb_node_events
)
481 && !list_empty(&queued_event
->hn_item
)) {
482 event
= list_entry(o2hb_node_events
.next
,
483 struct o2hb_node_event
,
485 list_del_init(&event
->hn_item
);
486 spin_unlock(&o2hb_live_lock
);
488 mlog(ML_HEARTBEAT
, "Node %s event for %d\n",
489 event
->hn_event_type
== O2HB_NODE_UP_CB
? "UP" : "DOWN",
492 hbcall
= hbcall_from_type(event
->hn_event_type
);
494 /* We should *never* have gotten on to the list with a
495 * bad type... This isn't something that we should try
496 * to recover from. */
497 BUG_ON(IS_ERR(hbcall
));
499 o2hb_fire_callbacks(hbcall
, event
->hn_node
, event
->hn_node_num
);
501 spin_lock(&o2hb_live_lock
);
503 spin_unlock(&o2hb_live_lock
);
505 up_write(&o2hb_callback_sem
);
508 static void o2hb_queue_node_event(struct o2hb_node_event
*event
,
509 enum o2hb_callback_type type
,
510 struct o2nm_node
*node
,
513 assert_spin_locked(&o2hb_live_lock
);
515 event
->hn_event_type
= type
;
516 event
->hn_node
= node
;
517 event
->hn_node_num
= node_num
;
519 mlog(ML_HEARTBEAT
, "Queue node %s event for node %d\n",
520 type
== O2HB_NODE_UP_CB
? "UP" : "DOWN", node_num
);
522 list_add_tail(&event
->hn_item
, &o2hb_node_events
);
525 static void o2hb_shutdown_slot(struct o2hb_disk_slot
*slot
)
527 struct o2hb_node_event event
=
528 { .hn_item
= LIST_HEAD_INIT(event
.hn_item
), };
529 struct o2nm_node
*node
;
531 node
= o2nm_get_node_by_num(slot
->ds_node_num
);
535 spin_lock(&o2hb_live_lock
);
536 if (!list_empty(&slot
->ds_live_item
)) {
537 mlog(ML_HEARTBEAT
, "Shutdown, node %d leaves region\n",
540 list_del_init(&slot
->ds_live_item
);
542 if (list_empty(&o2hb_live_slots
[slot
->ds_node_num
])) {
543 clear_bit(slot
->ds_node_num
, o2hb_live_node_bitmap
);
545 o2hb_queue_node_event(&event
, O2HB_NODE_DOWN_CB
, node
,
549 spin_unlock(&o2hb_live_lock
);
551 o2hb_run_event_list(&event
);
556 static int o2hb_check_slot(struct o2hb_region
*reg
,
557 struct o2hb_disk_slot
*slot
)
559 int changed
= 0, gen_changed
= 0;
560 struct o2hb_node_event event
=
561 { .hn_item
= LIST_HEAD_INIT(event
.hn_item
), };
562 struct o2nm_node
*node
;
563 struct o2hb_disk_heartbeat_block
*hb_block
= reg
->hr_tmp_block
;
565 unsigned int dead_ms
= o2hb_dead_threshold
* O2HB_REGION_TIMEOUT_MS
;
566 unsigned int slot_dead_ms
;
568 memcpy(hb_block
, slot
->ds_raw_block
, reg
->hr_block_bytes
);
570 /* Is this correct? Do we assume that the node doesn't exist
571 * if we're not configured for him? */
572 node
= o2nm_get_node_by_num(slot
->ds_node_num
);
576 if (!o2hb_verify_crc(reg
, hb_block
)) {
577 /* all paths from here will drop o2hb_live_lock for
579 spin_lock(&o2hb_live_lock
);
581 /* Don't print an error on the console in this case -
582 * a freshly formatted heartbeat area will not have a
584 if (list_empty(&slot
->ds_live_item
))
587 /* The node is live but pushed out a bad crc. We
588 * consider it a transient miss but don't populate any
589 * other values as they may be junk. */
590 mlog(ML_ERROR
, "Node %d has written a bad crc to %s\n",
591 slot
->ds_node_num
, reg
->hr_dev_name
);
592 o2hb_dump_slot(hb_block
);
594 slot
->ds_equal_samples
++;
598 /* we don't care if these wrap.. the state transitions below
599 * clear at the right places */
600 cputime
= le64_to_cpu(hb_block
->hb_seq
);
601 if (slot
->ds_last_time
!= cputime
)
602 slot
->ds_changed_samples
++;
604 slot
->ds_equal_samples
++;
605 slot
->ds_last_time
= cputime
;
607 /* The node changed heartbeat generations. We assume this to
608 * mean it dropped off but came back before we timed out. We
609 * want to consider it down for the time being but don't want
610 * to lose any changed_samples state we might build up to
611 * considering it live again. */
612 if (slot
->ds_last_generation
!= le64_to_cpu(hb_block
->hb_generation
)) {
614 slot
->ds_equal_samples
= 0;
615 mlog(ML_HEARTBEAT
, "Node %d changed generation (0x%llx "
616 "to 0x%llx)\n", slot
->ds_node_num
,
617 (long long)slot
->ds_last_generation
,
618 (long long)le64_to_cpu(hb_block
->hb_generation
));
621 slot
->ds_last_generation
= le64_to_cpu(hb_block
->hb_generation
);
623 mlog(ML_HEARTBEAT
, "Slot %d gen 0x%llx cksum 0x%x "
624 "seq %llu last %llu changed %u equal %u\n",
625 slot
->ds_node_num
, (long long)slot
->ds_last_generation
,
626 le32_to_cpu(hb_block
->hb_cksum
),
627 (unsigned long long)le64_to_cpu(hb_block
->hb_seq
),
628 (unsigned long long)slot
->ds_last_time
, slot
->ds_changed_samples
,
629 slot
->ds_equal_samples
);
631 spin_lock(&o2hb_live_lock
);
634 /* dead nodes only come to life after some number of
635 * changes at any time during their dead time */
636 if (list_empty(&slot
->ds_live_item
) &&
637 slot
->ds_changed_samples
>= O2HB_LIVE_THRESHOLD
) {
638 mlog(ML_HEARTBEAT
, "Node %d (id 0x%llx) joined my region\n",
639 slot
->ds_node_num
, (long long)slot
->ds_last_generation
);
641 /* first on the list generates a callback */
642 if (list_empty(&o2hb_live_slots
[slot
->ds_node_num
])) {
643 set_bit(slot
->ds_node_num
, o2hb_live_node_bitmap
);
645 o2hb_queue_node_event(&event
, O2HB_NODE_UP_CB
, node
,
651 list_add_tail(&slot
->ds_live_item
,
652 &o2hb_live_slots
[slot
->ds_node_num
]);
654 slot
->ds_equal_samples
= 0;
656 /* We want to be sure that all nodes agree on the
657 * number of milliseconds before a node will be
658 * considered dead. The self-fencing timeout is
659 * computed from this value, and a discrepancy might
660 * result in heartbeat calling a node dead when it
661 * hasn't self-fenced yet. */
662 slot_dead_ms
= le32_to_cpu(hb_block
->hb_dead_ms
);
663 if (slot_dead_ms
&& slot_dead_ms
!= dead_ms
) {
664 /* TODO: Perhaps we can fail the region here. */
665 mlog(ML_ERROR
, "Node %d on device %s has a dead count "
666 "of %u ms, but our count is %u ms.\n"
667 "Please double check your configuration values "
668 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
669 slot
->ds_node_num
, reg
->hr_dev_name
, slot_dead_ms
,
675 /* if the list is dead, we're done.. */
676 if (list_empty(&slot
->ds_live_item
))
679 /* live nodes only go dead after enough consequtive missed
680 * samples.. reset the missed counter whenever we see
682 if (slot
->ds_equal_samples
>= o2hb_dead_threshold
|| gen_changed
) {
683 mlog(ML_HEARTBEAT
, "Node %d left my region\n",
686 /* last off the live_slot generates a callback */
687 list_del_init(&slot
->ds_live_item
);
688 if (list_empty(&o2hb_live_slots
[slot
->ds_node_num
])) {
689 clear_bit(slot
->ds_node_num
, o2hb_live_node_bitmap
);
691 o2hb_queue_node_event(&event
, O2HB_NODE_DOWN_CB
, node
,
697 /* We don't clear this because the node is still
698 * actually writing new blocks. */
700 slot
->ds_changed_samples
= 0;
703 if (slot
->ds_changed_samples
) {
704 slot
->ds_changed_samples
= 0;
705 slot
->ds_equal_samples
= 0;
708 spin_unlock(&o2hb_live_lock
);
710 o2hb_run_event_list(&event
);
716 /* This could be faster if we just implmented a find_last_bit, but I
717 * don't think the circumstances warrant it. */
718 static int o2hb_highest_node(unsigned long *nodes
,
725 while ((node
= find_next_bit(nodes
, numbits
, node
+ 1)) != -1) {
735 static int o2hb_do_disk_heartbeat(struct o2hb_region
*reg
)
737 int i
, ret
, highest_node
, change
= 0;
738 unsigned long configured_nodes
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
739 struct o2hb_bio_wait_ctxt write_wc
;
741 ret
= o2nm_configured_node_map(configured_nodes
,
742 sizeof(configured_nodes
));
748 highest_node
= o2hb_highest_node(configured_nodes
, O2NM_MAX_NODES
);
749 if (highest_node
>= O2NM_MAX_NODES
) {
750 mlog(ML_NOTICE
, "ocfs2_heartbeat: no configured nodes found!\n");
754 /* No sense in reading the slots of nodes that don't exist
755 * yet. Of course, if the node definitions have holes in them
756 * then we're reading an empty slot anyway... Consider this
758 ret
= o2hb_read_slots(reg
, highest_node
+ 1);
764 /* With an up to date view of the slots, we can check that no
765 * other node has been improperly configured to heartbeat in
767 if (!o2hb_check_last_timestamp(reg
))
768 mlog(ML_ERROR
, "Device \"%s\": another node is heartbeating "
769 "in our slot!\n", reg
->hr_dev_name
);
771 /* fill in the proper info for our next heartbeat */
772 o2hb_prepare_block(reg
, reg
->hr_generation
);
774 /* And fire off the write. Note that we don't wait on this I/O
776 ret
= o2hb_issue_node_write(reg
, &write_wc
);
783 while((i
= find_next_bit(configured_nodes
, O2NM_MAX_NODES
, i
+ 1)) < O2NM_MAX_NODES
) {
785 change
|= o2hb_check_slot(reg
, ®
->hr_slots
[i
]);
789 * We have to be sure we've advertised ourselves on disk
790 * before we can go to steady state. This ensures that
791 * people we find in our steady state have seen us.
793 o2hb_wait_on_io(reg
, &write_wc
);
794 if (write_wc
.wc_error
) {
795 /* Do not re-arm the write timeout on I/O error - we
796 * can't be sure that the new block ever made it to
798 mlog(ML_ERROR
, "Write error %d on device \"%s\"\n",
799 write_wc
.wc_error
, reg
->hr_dev_name
);
800 return write_wc
.wc_error
;
803 o2hb_arm_write_timeout(reg
);
805 /* let the person who launched us know when things are steady */
806 if (!change
&& (atomic_read(®
->hr_steady_iterations
) != 0)) {
807 if (atomic_dec_and_test(®
->hr_steady_iterations
))
808 wake_up(&o2hb_steady_queue
);
814 /* Subtract b from a, storing the result in a. a *must* have a larger
816 static void o2hb_tv_subtract(struct timeval
*a
,
819 /* just return 0 when a is after b */
820 if (a
->tv_sec
< b
->tv_sec
||
821 (a
->tv_sec
== b
->tv_sec
&& a
->tv_usec
< b
->tv_usec
)) {
827 a
->tv_sec
-= b
->tv_sec
;
828 a
->tv_usec
-= b
->tv_usec
;
829 while ( a
->tv_usec
< 0 ) {
831 a
->tv_usec
+= 1000000;
835 static unsigned int o2hb_elapsed_msecs(struct timeval
*start
,
838 struct timeval res
= *end
;
840 o2hb_tv_subtract(&res
, start
);
842 return res
.tv_sec
* 1000 + res
.tv_usec
/ 1000;
846 * we ride the region ref that the region dir holds. before the region
847 * dir is removed and drops it ref it will wait to tear down this
850 static int o2hb_thread(void *data
)
853 struct o2hb_region
*reg
= data
;
854 struct o2hb_bio_wait_ctxt write_wc
;
855 struct timeval before_hb
, after_hb
;
856 unsigned int elapsed_msec
;
858 mlog(ML_HEARTBEAT
|ML_KTHREAD
, "hb thread running\n");
860 set_user_nice(current
, -20);
862 while (!kthread_should_stop() && !reg
->hr_unclean_stop
) {
863 /* We track the time spent inside
864 * o2hb_do_disk_heartbeat so that we avoid more than
865 * hr_timeout_ms between disk writes. On busy systems
866 * this should result in a heartbeat which is less
867 * likely to time itself out. */
868 do_gettimeofday(&before_hb
);
872 ret
= o2hb_do_disk_heartbeat(reg
);
873 } while (ret
&& ++i
< 2);
875 do_gettimeofday(&after_hb
);
876 elapsed_msec
= o2hb_elapsed_msecs(&before_hb
, &after_hb
);
879 "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
880 before_hb
.tv_sec
, (unsigned long) before_hb
.tv_usec
,
881 after_hb
.tv_sec
, (unsigned long) after_hb
.tv_usec
,
884 if (elapsed_msec
< reg
->hr_timeout_ms
) {
885 /* the kthread api has blocked signals for us so no
886 * need to record the return value. */
887 msleep_interruptible(reg
->hr_timeout_ms
- elapsed_msec
);
891 o2hb_disarm_write_timeout(reg
);
893 /* unclean stop is only used in very bad situation */
894 for(i
= 0; !reg
->hr_unclean_stop
&& i
< reg
->hr_blocks
; i
++)
895 o2hb_shutdown_slot(®
->hr_slots
[i
]);
897 /* Explicit down notification - avoid forcing the other nodes
898 * to timeout on this region when we could just as easily
899 * write a clear generation - thus indicating to them that
900 * this node has left this region.
902 * XXX: Should we skip this on unclean_stop? */
903 o2hb_prepare_block(reg
, 0);
904 ret
= o2hb_issue_node_write(reg
, &write_wc
);
906 o2hb_wait_on_io(reg
, &write_wc
);
911 mlog(ML_HEARTBEAT
|ML_KTHREAD
, "hb thread exiting\n");
916 #ifdef CONFIG_DEBUG_FS
917 static int o2hb_debug_open(struct inode
*inode
, struct file
*file
)
919 unsigned long map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
924 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
928 o2hb_fill_node_map(map
, sizeof(map
));
930 while ((i
= find_next_bit(map
, O2NM_MAX_NODES
, i
+ 1)) < O2NM_MAX_NODES
)
931 out
+= snprintf(buf
+ out
, PAGE_SIZE
- out
, "%d ", i
);
932 out
+= snprintf(buf
+ out
, PAGE_SIZE
- out
, "\n");
934 i_size_write(inode
, out
);
936 file
->private_data
= buf
;
943 static int o2hb_debug_release(struct inode
*inode
, struct file
*file
)
945 kfree(file
->private_data
);
949 static ssize_t
o2hb_debug_read(struct file
*file
, char __user
*buf
,
950 size_t nbytes
, loff_t
*ppos
)
952 return simple_read_from_buffer(buf
, nbytes
, ppos
, file
->private_data
,
953 i_size_read(file
->f_mapping
->host
));
956 static int o2hb_debug_open(struct inode
*inode
, struct file
*file
)
960 static int o2hb_debug_release(struct inode
*inode
, struct file
*file
)
964 static ssize_t
o2hb_debug_read(struct file
*file
, char __user
*buf
,
965 size_t nbytes
, loff_t
*ppos
)
969 #endif /* CONFIG_DEBUG_FS */
971 static const struct file_operations o2hb_debug_fops
= {
972 .open
= o2hb_debug_open
,
973 .release
= o2hb_debug_release
,
974 .read
= o2hb_debug_read
,
975 .llseek
= generic_file_llseek
,
980 if (o2hb_debug_livenodes
)
981 debugfs_remove(o2hb_debug_livenodes
);
983 debugfs_remove(o2hb_debug_dir
);
990 for (i
= 0; i
< ARRAY_SIZE(o2hb_callbacks
); i
++)
991 INIT_LIST_HEAD(&o2hb_callbacks
[i
].list
);
993 for (i
= 0; i
< ARRAY_SIZE(o2hb_live_slots
); i
++)
994 INIT_LIST_HEAD(&o2hb_live_slots
[i
]);
996 INIT_LIST_HEAD(&o2hb_node_events
);
998 memset(o2hb_live_node_bitmap
, 0, sizeof(o2hb_live_node_bitmap
));
1000 o2hb_debug_dir
= debugfs_create_dir(O2HB_DEBUG_DIR
, NULL
);
1001 if (!o2hb_debug_dir
) {
1002 mlog_errno(-ENOMEM
);
1006 o2hb_debug_livenodes
= debugfs_create_file(O2HB_DEBUG_LIVENODES
,
1008 o2hb_debug_dir
, NULL
,
1010 if (!o2hb_debug_livenodes
) {
1011 mlog_errno(-ENOMEM
);
1012 debugfs_remove(o2hb_debug_dir
);
1019 /* if we're already in a callback then we're already serialized by the sem */
1020 static void o2hb_fill_node_map_from_callback(unsigned long *map
,
1023 BUG_ON(bytes
< (BITS_TO_LONGS(O2NM_MAX_NODES
) * sizeof(unsigned long)));
1025 memcpy(map
, &o2hb_live_node_bitmap
, bytes
);
1029 * get a map of all nodes that are heartbeating in any regions
1031 void o2hb_fill_node_map(unsigned long *map
, unsigned bytes
)
1033 /* callers want to serialize this map and callbacks so that they
1034 * can trust that they don't miss nodes coming to the party */
1035 down_read(&o2hb_callback_sem
);
1036 spin_lock(&o2hb_live_lock
);
1037 o2hb_fill_node_map_from_callback(map
, bytes
);
1038 spin_unlock(&o2hb_live_lock
);
1039 up_read(&o2hb_callback_sem
);
1041 EXPORT_SYMBOL_GPL(o2hb_fill_node_map
);
1044 * heartbeat configfs bits. The heartbeat set is a default set under
1045 * the cluster set in nodemanager.c.
1048 static struct o2hb_region
*to_o2hb_region(struct config_item
*item
)
1050 return item
? container_of(item
, struct o2hb_region
, hr_item
) : NULL
;
1053 /* drop_item only drops its ref after killing the thread, nothing should
1054 * be using the region anymore. this has to clean up any state that
1055 * attributes might have built up. */
1056 static void o2hb_region_release(struct config_item
*item
)
1060 struct o2hb_region
*reg
= to_o2hb_region(item
);
1062 if (reg
->hr_tmp_block
)
1063 kfree(reg
->hr_tmp_block
);
1065 if (reg
->hr_slot_data
) {
1066 for (i
= 0; i
< reg
->hr_num_pages
; i
++) {
1067 page
= reg
->hr_slot_data
[i
];
1071 kfree(reg
->hr_slot_data
);
1075 blkdev_put(reg
->hr_bdev
, FMODE_READ
|FMODE_WRITE
);
1078 kfree(reg
->hr_slots
);
1080 spin_lock(&o2hb_live_lock
);
1081 list_del(®
->hr_all_item
);
1082 spin_unlock(&o2hb_live_lock
);
1087 static int o2hb_read_block_input(struct o2hb_region
*reg
,
1090 unsigned long *ret_bytes
,
1091 unsigned int *ret_bits
)
1093 unsigned long bytes
;
1094 char *p
= (char *)page
;
1096 bytes
= simple_strtoul(p
, &p
, 0);
1097 if (!p
|| (*p
&& (*p
!= '\n')))
1100 /* Heartbeat and fs min / max block sizes are the same. */
1101 if (bytes
> 4096 || bytes
< 512)
1103 if (hweight16(bytes
) != 1)
1109 *ret_bits
= ffs(bytes
) - 1;
1114 static ssize_t
o2hb_region_block_bytes_read(struct o2hb_region
*reg
,
1117 return sprintf(page
, "%u\n", reg
->hr_block_bytes
);
1120 static ssize_t
o2hb_region_block_bytes_write(struct o2hb_region
*reg
,
1125 unsigned long block_bytes
;
1126 unsigned int block_bits
;
1131 status
= o2hb_read_block_input(reg
, page
, count
,
1132 &block_bytes
, &block_bits
);
1136 reg
->hr_block_bytes
= (unsigned int)block_bytes
;
1137 reg
->hr_block_bits
= block_bits
;
1142 static ssize_t
o2hb_region_start_block_read(struct o2hb_region
*reg
,
1145 return sprintf(page
, "%llu\n", reg
->hr_start_block
);
1148 static ssize_t
o2hb_region_start_block_write(struct o2hb_region
*reg
,
1152 unsigned long long tmp
;
1153 char *p
= (char *)page
;
1158 tmp
= simple_strtoull(p
, &p
, 0);
1159 if (!p
|| (*p
&& (*p
!= '\n')))
1162 reg
->hr_start_block
= tmp
;
1167 static ssize_t
o2hb_region_blocks_read(struct o2hb_region
*reg
,
1170 return sprintf(page
, "%d\n", reg
->hr_blocks
);
1173 static ssize_t
o2hb_region_blocks_write(struct o2hb_region
*reg
,
1178 char *p
= (char *)page
;
1183 tmp
= simple_strtoul(p
, &p
, 0);
1184 if (!p
|| (*p
&& (*p
!= '\n')))
1187 if (tmp
> O2NM_MAX_NODES
|| tmp
== 0)
1190 reg
->hr_blocks
= (unsigned int)tmp
;
1195 static ssize_t
o2hb_region_dev_read(struct o2hb_region
*reg
,
1198 unsigned int ret
= 0;
1201 ret
= sprintf(page
, "%s\n", reg
->hr_dev_name
);
1206 static void o2hb_init_region_params(struct o2hb_region
*reg
)
1208 reg
->hr_slots_per_page
= PAGE_CACHE_SIZE
>> reg
->hr_block_bits
;
1209 reg
->hr_timeout_ms
= O2HB_REGION_TIMEOUT_MS
;
1211 mlog(ML_HEARTBEAT
, "hr_start_block = %llu, hr_blocks = %u\n",
1212 reg
->hr_start_block
, reg
->hr_blocks
);
1213 mlog(ML_HEARTBEAT
, "hr_block_bytes = %u, hr_block_bits = %u\n",
1214 reg
->hr_block_bytes
, reg
->hr_block_bits
);
1215 mlog(ML_HEARTBEAT
, "hr_timeout_ms = %u\n", reg
->hr_timeout_ms
);
1216 mlog(ML_HEARTBEAT
, "dead threshold = %u\n", o2hb_dead_threshold
);
1219 static int o2hb_map_slot_data(struct o2hb_region
*reg
)
1222 unsigned int last_slot
;
1223 unsigned int spp
= reg
->hr_slots_per_page
;
1226 struct o2hb_disk_slot
*slot
;
1228 reg
->hr_tmp_block
= kmalloc(reg
->hr_block_bytes
, GFP_KERNEL
);
1229 if (reg
->hr_tmp_block
== NULL
) {
1230 mlog_errno(-ENOMEM
);
1234 reg
->hr_slots
= kcalloc(reg
->hr_blocks
,
1235 sizeof(struct o2hb_disk_slot
), GFP_KERNEL
);
1236 if (reg
->hr_slots
== NULL
) {
1237 mlog_errno(-ENOMEM
);
1241 for(i
= 0; i
< reg
->hr_blocks
; i
++) {
1242 slot
= ®
->hr_slots
[i
];
1243 slot
->ds_node_num
= i
;
1244 INIT_LIST_HEAD(&slot
->ds_live_item
);
1245 slot
->ds_raw_block
= NULL
;
1248 reg
->hr_num_pages
= (reg
->hr_blocks
+ spp
- 1) / spp
;
1249 mlog(ML_HEARTBEAT
, "Going to require %u pages to cover %u blocks "
1250 "at %u blocks per page\n",
1251 reg
->hr_num_pages
, reg
->hr_blocks
, spp
);
1253 reg
->hr_slot_data
= kcalloc(reg
->hr_num_pages
, sizeof(struct page
*),
1255 if (!reg
->hr_slot_data
) {
1256 mlog_errno(-ENOMEM
);
1260 for(i
= 0; i
< reg
->hr_num_pages
; i
++) {
1261 page
= alloc_page(GFP_KERNEL
);
1263 mlog_errno(-ENOMEM
);
1267 reg
->hr_slot_data
[i
] = page
;
1269 last_slot
= i
* spp
;
1270 raw
= page_address(page
);
1272 (j
< spp
) && ((j
+ last_slot
) < reg
->hr_blocks
);
1274 BUG_ON((j
+ last_slot
) >= reg
->hr_blocks
);
1276 slot
= ®
->hr_slots
[j
+ last_slot
];
1277 slot
->ds_raw_block
=
1278 (struct o2hb_disk_heartbeat_block
*) raw
;
1280 raw
+= reg
->hr_block_bytes
;
1287 /* Read in all the slots available and populate the tracking
1288 * structures so that we can start with a baseline idea of what's
1290 static int o2hb_populate_slot_data(struct o2hb_region
*reg
)
1293 struct o2hb_disk_slot
*slot
;
1294 struct o2hb_disk_heartbeat_block
*hb_block
;
1298 ret
= o2hb_read_slots(reg
, reg
->hr_blocks
);
1304 /* We only want to get an idea of the values initially in each
1305 * slot, so we do no verification - o2hb_check_slot will
1306 * actually determine if each configured slot is valid and
1307 * whether any values have changed. */
1308 for(i
= 0; i
< reg
->hr_blocks
; i
++) {
1309 slot
= ®
->hr_slots
[i
];
1310 hb_block
= (struct o2hb_disk_heartbeat_block
*) slot
->ds_raw_block
;
1312 /* Only fill the values that o2hb_check_slot uses to
1313 * determine changing slots */
1314 slot
->ds_last_time
= le64_to_cpu(hb_block
->hb_seq
);
1315 slot
->ds_last_generation
= le64_to_cpu(hb_block
->hb_generation
);
1323 /* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1324 static ssize_t
o2hb_region_dev_write(struct o2hb_region
*reg
,
1328 struct task_struct
*hb_task
;
1331 char *p
= (char *)page
;
1332 struct file
*filp
= NULL
;
1333 struct inode
*inode
= NULL
;
1334 ssize_t ret
= -EINVAL
;
1339 /* We can't heartbeat without having had our node number
1340 * configured yet. */
1341 if (o2nm_this_node() == O2NM_MAX_NODES
)
1344 fd
= simple_strtol(p
, &p
, 0);
1345 if (!p
|| (*p
&& (*p
!= '\n')))
1348 if (fd
< 0 || fd
>= INT_MAX
)
1355 if (reg
->hr_blocks
== 0 || reg
->hr_start_block
== 0 ||
1356 reg
->hr_block_bytes
== 0)
1359 inode
= igrab(filp
->f_mapping
->host
);
1363 if (!S_ISBLK(inode
->i_mode
))
1366 reg
->hr_bdev
= I_BDEV(filp
->f_mapping
->host
);
1367 ret
= blkdev_get(reg
->hr_bdev
, FMODE_WRITE
| FMODE_READ
);
1369 reg
->hr_bdev
= NULL
;
1374 bdevname(reg
->hr_bdev
, reg
->hr_dev_name
);
1376 sectsize
= bdev_logical_block_size(reg
->hr_bdev
);
1377 if (sectsize
!= reg
->hr_block_bytes
) {
1379 "blocksize %u incorrect for device, expected %d",
1380 reg
->hr_block_bytes
, sectsize
);
1385 o2hb_init_region_params(reg
);
1387 /* Generation of zero is invalid */
1389 get_random_bytes(®
->hr_generation
,
1390 sizeof(reg
->hr_generation
));
1391 } while (reg
->hr_generation
== 0);
1393 ret
= o2hb_map_slot_data(reg
);
1399 ret
= o2hb_populate_slot_data(reg
);
1405 INIT_DELAYED_WORK(®
->hr_write_timeout_work
, o2hb_write_timeout
);
1408 * A node is considered live after it has beat LIVE_THRESHOLD
1409 * times. We're not steady until we've given them a chance
1410 * _after_ our first read.
1412 atomic_set(®
->hr_steady_iterations
, O2HB_LIVE_THRESHOLD
+ 1);
1414 hb_task
= kthread_run(o2hb_thread
, reg
, "o2hb-%s",
1415 reg
->hr_item
.ci_name
);
1416 if (IS_ERR(hb_task
)) {
1417 ret
= PTR_ERR(hb_task
);
1422 spin_lock(&o2hb_live_lock
);
1423 reg
->hr_task
= hb_task
;
1424 spin_unlock(&o2hb_live_lock
);
1426 ret
= wait_event_interruptible(o2hb_steady_queue
,
1427 atomic_read(®
->hr_steady_iterations
) == 0);
1429 /* We got interrupted (hello ptrace!). Clean up */
1430 spin_lock(&o2hb_live_lock
);
1431 hb_task
= reg
->hr_task
;
1432 reg
->hr_task
= NULL
;
1433 spin_unlock(&o2hb_live_lock
);
1436 kthread_stop(hb_task
);
1440 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1441 spin_lock(&o2hb_live_lock
);
1442 hb_task
= reg
->hr_task
;
1443 spin_unlock(&o2hb_live_lock
);
1457 blkdev_put(reg
->hr_bdev
, FMODE_READ
|FMODE_WRITE
);
1458 reg
->hr_bdev
= NULL
;
1464 static ssize_t
o2hb_region_pid_read(struct o2hb_region
*reg
,
1469 spin_lock(&o2hb_live_lock
);
1471 pid
= task_pid_nr(reg
->hr_task
);
1472 spin_unlock(&o2hb_live_lock
);
1477 return sprintf(page
, "%u\n", pid
);
1480 struct o2hb_region_attribute
{
1481 struct configfs_attribute attr
;
1482 ssize_t (*show
)(struct o2hb_region
*, char *);
1483 ssize_t (*store
)(struct o2hb_region
*, const char *, size_t);
1486 static struct o2hb_region_attribute o2hb_region_attr_block_bytes
= {
1487 .attr
= { .ca_owner
= THIS_MODULE
,
1488 .ca_name
= "block_bytes",
1489 .ca_mode
= S_IRUGO
| S_IWUSR
},
1490 .show
= o2hb_region_block_bytes_read
,
1491 .store
= o2hb_region_block_bytes_write
,
1494 static struct o2hb_region_attribute o2hb_region_attr_start_block
= {
1495 .attr
= { .ca_owner
= THIS_MODULE
,
1496 .ca_name
= "start_block",
1497 .ca_mode
= S_IRUGO
| S_IWUSR
},
1498 .show
= o2hb_region_start_block_read
,
1499 .store
= o2hb_region_start_block_write
,
1502 static struct o2hb_region_attribute o2hb_region_attr_blocks
= {
1503 .attr
= { .ca_owner
= THIS_MODULE
,
1504 .ca_name
= "blocks",
1505 .ca_mode
= S_IRUGO
| S_IWUSR
},
1506 .show
= o2hb_region_blocks_read
,
1507 .store
= o2hb_region_blocks_write
,
1510 static struct o2hb_region_attribute o2hb_region_attr_dev
= {
1511 .attr
= { .ca_owner
= THIS_MODULE
,
1513 .ca_mode
= S_IRUGO
| S_IWUSR
},
1514 .show
= o2hb_region_dev_read
,
1515 .store
= o2hb_region_dev_write
,
1518 static struct o2hb_region_attribute o2hb_region_attr_pid
= {
1519 .attr
= { .ca_owner
= THIS_MODULE
,
1521 .ca_mode
= S_IRUGO
| S_IRUSR
},
1522 .show
= o2hb_region_pid_read
,
1525 static struct configfs_attribute
*o2hb_region_attrs
[] = {
1526 &o2hb_region_attr_block_bytes
.attr
,
1527 &o2hb_region_attr_start_block
.attr
,
1528 &o2hb_region_attr_blocks
.attr
,
1529 &o2hb_region_attr_dev
.attr
,
1530 &o2hb_region_attr_pid
.attr
,
1534 static ssize_t
o2hb_region_show(struct config_item
*item
,
1535 struct configfs_attribute
*attr
,
1538 struct o2hb_region
*reg
= to_o2hb_region(item
);
1539 struct o2hb_region_attribute
*o2hb_region_attr
=
1540 container_of(attr
, struct o2hb_region_attribute
, attr
);
1543 if (o2hb_region_attr
->show
)
1544 ret
= o2hb_region_attr
->show(reg
, page
);
1548 static ssize_t
o2hb_region_store(struct config_item
*item
,
1549 struct configfs_attribute
*attr
,
1550 const char *page
, size_t count
)
1552 struct o2hb_region
*reg
= to_o2hb_region(item
);
1553 struct o2hb_region_attribute
*o2hb_region_attr
=
1554 container_of(attr
, struct o2hb_region_attribute
, attr
);
1555 ssize_t ret
= -EINVAL
;
1557 if (o2hb_region_attr
->store
)
1558 ret
= o2hb_region_attr
->store(reg
, page
, count
);
1562 static struct configfs_item_operations o2hb_region_item_ops
= {
1563 .release
= o2hb_region_release
,
1564 .show_attribute
= o2hb_region_show
,
1565 .store_attribute
= o2hb_region_store
,
1568 static struct config_item_type o2hb_region_type
= {
1569 .ct_item_ops
= &o2hb_region_item_ops
,
1570 .ct_attrs
= o2hb_region_attrs
,
1571 .ct_owner
= THIS_MODULE
,
1576 struct o2hb_heartbeat_group
{
1577 struct config_group hs_group
;
1581 static struct o2hb_heartbeat_group
*to_o2hb_heartbeat_group(struct config_group
*group
)
1584 container_of(group
, struct o2hb_heartbeat_group
, hs_group
)
1588 static struct config_item
*o2hb_heartbeat_group_make_item(struct config_group
*group
,
1591 struct o2hb_region
*reg
= NULL
;
1593 reg
= kzalloc(sizeof(struct o2hb_region
), GFP_KERNEL
);
1595 return ERR_PTR(-ENOMEM
);
1597 config_item_init_type_name(®
->hr_item
, name
, &o2hb_region_type
);
1599 spin_lock(&o2hb_live_lock
);
1600 list_add_tail(®
->hr_all_item
, &o2hb_all_regions
);
1601 spin_unlock(&o2hb_live_lock
);
1603 return ®
->hr_item
;
1606 static void o2hb_heartbeat_group_drop_item(struct config_group
*group
,
1607 struct config_item
*item
)
1609 struct task_struct
*hb_task
;
1610 struct o2hb_region
*reg
= to_o2hb_region(item
);
1612 /* stop the thread when the user removes the region dir */
1613 spin_lock(&o2hb_live_lock
);
1614 hb_task
= reg
->hr_task
;
1615 reg
->hr_task
= NULL
;
1616 spin_unlock(&o2hb_live_lock
);
1619 kthread_stop(hb_task
);
1622 * If we're racing a dev_write(), we need to wake them. They will
1623 * check reg->hr_task
1625 if (atomic_read(®
->hr_steady_iterations
) != 0) {
1626 atomic_set(®
->hr_steady_iterations
, 0);
1627 wake_up(&o2hb_steady_queue
);
1630 config_item_put(item
);
1633 struct o2hb_heartbeat_group_attribute
{
1634 struct configfs_attribute attr
;
1635 ssize_t (*show
)(struct o2hb_heartbeat_group
*, char *);
1636 ssize_t (*store
)(struct o2hb_heartbeat_group
*, const char *, size_t);
1639 static ssize_t
o2hb_heartbeat_group_show(struct config_item
*item
,
1640 struct configfs_attribute
*attr
,
1643 struct o2hb_heartbeat_group
*reg
= to_o2hb_heartbeat_group(to_config_group(item
));
1644 struct o2hb_heartbeat_group_attribute
*o2hb_heartbeat_group_attr
=
1645 container_of(attr
, struct o2hb_heartbeat_group_attribute
, attr
);
1648 if (o2hb_heartbeat_group_attr
->show
)
1649 ret
= o2hb_heartbeat_group_attr
->show(reg
, page
);
1653 static ssize_t
o2hb_heartbeat_group_store(struct config_item
*item
,
1654 struct configfs_attribute
*attr
,
1655 const char *page
, size_t count
)
1657 struct o2hb_heartbeat_group
*reg
= to_o2hb_heartbeat_group(to_config_group(item
));
1658 struct o2hb_heartbeat_group_attribute
*o2hb_heartbeat_group_attr
=
1659 container_of(attr
, struct o2hb_heartbeat_group_attribute
, attr
);
1660 ssize_t ret
= -EINVAL
;
1662 if (o2hb_heartbeat_group_attr
->store
)
1663 ret
= o2hb_heartbeat_group_attr
->store(reg
, page
, count
);
1667 static ssize_t
o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group
*group
,
1670 return sprintf(page
, "%u\n", o2hb_dead_threshold
);
1673 static ssize_t
o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group
*group
,
1678 char *p
= (char *)page
;
1680 tmp
= simple_strtoul(p
, &p
, 10);
1681 if (!p
|| (*p
&& (*p
!= '\n')))
1684 /* this will validate ranges for us. */
1685 o2hb_dead_threshold_set((unsigned int) tmp
);
1690 static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold
= {
1691 .attr
= { .ca_owner
= THIS_MODULE
,
1692 .ca_name
= "dead_threshold",
1693 .ca_mode
= S_IRUGO
| S_IWUSR
},
1694 .show
= o2hb_heartbeat_group_threshold_show
,
1695 .store
= o2hb_heartbeat_group_threshold_store
,
1698 static struct configfs_attribute
*o2hb_heartbeat_group_attrs
[] = {
1699 &o2hb_heartbeat_group_attr_threshold
.attr
,
1703 static struct configfs_item_operations o2hb_hearbeat_group_item_ops
= {
1704 .show_attribute
= o2hb_heartbeat_group_show
,
1705 .store_attribute
= o2hb_heartbeat_group_store
,
1708 static struct configfs_group_operations o2hb_heartbeat_group_group_ops
= {
1709 .make_item
= o2hb_heartbeat_group_make_item
,
1710 .drop_item
= o2hb_heartbeat_group_drop_item
,
1713 static struct config_item_type o2hb_heartbeat_group_type
= {
1714 .ct_group_ops
= &o2hb_heartbeat_group_group_ops
,
1715 .ct_item_ops
= &o2hb_hearbeat_group_item_ops
,
1716 .ct_attrs
= o2hb_heartbeat_group_attrs
,
1717 .ct_owner
= THIS_MODULE
,
1720 /* this is just here to avoid touching group in heartbeat.h which the
1721 * entire damn world #includes */
1722 struct config_group
*o2hb_alloc_hb_set(void)
1724 struct o2hb_heartbeat_group
*hs
= NULL
;
1725 struct config_group
*ret
= NULL
;
1727 hs
= kzalloc(sizeof(struct o2hb_heartbeat_group
), GFP_KERNEL
);
1731 config_group_init_type_name(&hs
->hs_group
, "heartbeat",
1732 &o2hb_heartbeat_group_type
);
1734 ret
= &hs
->hs_group
;
1741 void o2hb_free_hb_set(struct config_group
*group
)
1743 struct o2hb_heartbeat_group
*hs
= to_o2hb_heartbeat_group(group
);
1747 /* hb callback registration and issueing */
1749 static struct o2hb_callback
*hbcall_from_type(enum o2hb_callback_type type
)
1751 if (type
== O2HB_NUM_CB
)
1752 return ERR_PTR(-EINVAL
);
1754 return &o2hb_callbacks
[type
];
1757 void o2hb_setup_callback(struct o2hb_callback_func
*hc
,
1758 enum o2hb_callback_type type
,
1763 INIT_LIST_HEAD(&hc
->hc_item
);
1766 hc
->hc_priority
= priority
;
1768 hc
->hc_magic
= O2HB_CB_MAGIC
;
1770 EXPORT_SYMBOL_GPL(o2hb_setup_callback
);
1772 static struct o2hb_region
*o2hb_find_region(const char *region_uuid
)
1774 struct o2hb_region
*p
, *reg
= NULL
;
1776 assert_spin_locked(&o2hb_live_lock
);
1778 list_for_each_entry(p
, &o2hb_all_regions
, hr_all_item
) {
1779 if (!strcmp(region_uuid
, config_item_name(&p
->hr_item
))) {
1788 static int o2hb_region_get(const char *region_uuid
)
1791 struct o2hb_region
*reg
;
1793 spin_lock(&o2hb_live_lock
);
1795 reg
= o2hb_find_region(region_uuid
);
1798 spin_unlock(&o2hb_live_lock
);
1803 ret
= o2nm_depend_this_node();
1807 ret
= o2nm_depend_item(®
->hr_item
);
1809 o2nm_undepend_this_node();
1815 static void o2hb_region_put(const char *region_uuid
)
1817 struct o2hb_region
*reg
;
1819 spin_lock(&o2hb_live_lock
);
1821 reg
= o2hb_find_region(region_uuid
);
1823 spin_unlock(&o2hb_live_lock
);
1826 o2nm_undepend_item(®
->hr_item
);
1827 o2nm_undepend_this_node();
1831 int o2hb_register_callback(const char *region_uuid
,
1832 struct o2hb_callback_func
*hc
)
1834 struct o2hb_callback_func
*tmp
;
1835 struct list_head
*iter
;
1836 struct o2hb_callback
*hbcall
;
1839 BUG_ON(hc
->hc_magic
!= O2HB_CB_MAGIC
);
1840 BUG_ON(!list_empty(&hc
->hc_item
));
1842 hbcall
= hbcall_from_type(hc
->hc_type
);
1843 if (IS_ERR(hbcall
)) {
1844 ret
= PTR_ERR(hbcall
);
1849 ret
= o2hb_region_get(region_uuid
);
1854 down_write(&o2hb_callback_sem
);
1856 list_for_each(iter
, &hbcall
->list
) {
1857 tmp
= list_entry(iter
, struct o2hb_callback_func
, hc_item
);
1858 if (hc
->hc_priority
< tmp
->hc_priority
) {
1859 list_add_tail(&hc
->hc_item
, iter
);
1863 if (list_empty(&hc
->hc_item
))
1864 list_add_tail(&hc
->hc_item
, &hbcall
->list
);
1866 up_write(&o2hb_callback_sem
);
1869 mlog(ML_HEARTBEAT
, "returning %d on behalf of %p for funcs %p\n",
1870 ret
, __builtin_return_address(0), hc
);
1873 EXPORT_SYMBOL_GPL(o2hb_register_callback
);
1875 void o2hb_unregister_callback(const char *region_uuid
,
1876 struct o2hb_callback_func
*hc
)
1878 BUG_ON(hc
->hc_magic
!= O2HB_CB_MAGIC
);
1880 mlog(ML_HEARTBEAT
, "on behalf of %p for funcs %p\n",
1881 __builtin_return_address(0), hc
);
1883 /* XXX Can this happen _with_ a region reference? */
1884 if (list_empty(&hc
->hc_item
))
1888 o2hb_region_put(region_uuid
);
1890 down_write(&o2hb_callback_sem
);
1892 list_del_init(&hc
->hc_item
);
1894 up_write(&o2hb_callback_sem
);
1896 EXPORT_SYMBOL_GPL(o2hb_unregister_callback
);
1898 int o2hb_check_node_heartbeating(u8 node_num
)
1900 unsigned long testing_map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
1902 o2hb_fill_node_map(testing_map
, sizeof(testing_map
));
1903 if (!test_bit(node_num
, testing_map
)) {
1905 "node (%u) does not have heartbeating enabled.\n",
1912 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating
);
1914 int o2hb_check_node_heartbeating_from_callback(u8 node_num
)
1916 unsigned long testing_map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
1918 o2hb_fill_node_map_from_callback(testing_map
, sizeof(testing_map
));
1919 if (!test_bit(node_num
, testing_map
)) {
1921 "node (%u) does not have heartbeating enabled.\n",
1928 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback
);
1930 /* Makes sure our local node is configured with a node number, and is
1932 int o2hb_check_local_node_heartbeating(void)
1936 /* if this node was set then we have networking */
1937 node_num
= o2nm_this_node();
1938 if (node_num
== O2NM_MAX_NODES
) {
1939 mlog(ML_HEARTBEAT
, "this node has not been configured.\n");
1943 return o2hb_check_node_heartbeating(node_num
);
1945 EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating
);
1948 * this is just a hack until we get the plumbing which flips file systems
1949 * read only and drops the hb ref instead of killing the node dead.
1951 void o2hb_stop_all_regions(void)
1953 struct o2hb_region
*reg
;
1955 mlog(ML_ERROR
, "stopping heartbeat on all active regions.\n");
1957 spin_lock(&o2hb_live_lock
);
1959 list_for_each_entry(reg
, &o2hb_all_regions
, hr_all_item
)
1960 reg
->hr_unclean_stop
= 1;
1962 spin_unlock(&o2hb_live_lock
);
1964 EXPORT_SYMBOL_GPL(o2hb_stop_all_regions
);