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(0, "Queue write timeout for %u ms\n", O2HB_MAX_WRITE_TIMEOUT_MS
);
181 cancel_delayed_work(®
->hr_write_timeout_work
);
182 reg
->hr_last_timeout_start
= jiffies
;
183 schedule_delayed_work(®
->hr_write_timeout_work
,
184 msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS
));
187 static void o2hb_disarm_write_timeout(struct o2hb_region
*reg
)
189 cancel_delayed_work(®
->hr_write_timeout_work
);
190 flush_scheduled_work();
193 static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt
*wc
)
195 atomic_set(&wc
->wc_num_reqs
, 1);
196 init_completion(&wc
->wc_io_complete
);
200 /* Used in error paths too */
201 static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt
*wc
,
204 /* sadly atomic_sub_and_test() isn't available on all platforms. The
205 * good news is that the fast path only completes one at a time */
207 if (atomic_dec_and_test(&wc
->wc_num_reqs
)) {
209 complete(&wc
->wc_io_complete
);
214 static void o2hb_wait_on_io(struct o2hb_region
*reg
,
215 struct o2hb_bio_wait_ctxt
*wc
)
217 struct address_space
*mapping
= reg
->hr_bdev
->bd_inode
->i_mapping
;
219 blk_run_address_space(mapping
);
220 o2hb_bio_wait_dec(wc
, 1);
222 wait_for_completion(&wc
->wc_io_complete
);
225 static void o2hb_bio_end_io(struct bio
*bio
,
228 struct o2hb_bio_wait_ctxt
*wc
= bio
->bi_private
;
231 mlog(ML_ERROR
, "IO Error %d\n", error
);
232 wc
->wc_error
= error
;
235 o2hb_bio_wait_dec(wc
, 1);
239 /* Setup a Bio to cover I/O against num_slots slots starting at
241 static struct bio
*o2hb_setup_one_bio(struct o2hb_region
*reg
,
242 struct o2hb_bio_wait_ctxt
*wc
,
243 unsigned int *current_slot
,
244 unsigned int max_slots
)
246 int len
, current_page
;
247 unsigned int vec_len
, vec_start
;
248 unsigned int bits
= reg
->hr_block_bits
;
249 unsigned int spp
= reg
->hr_slots_per_page
;
250 unsigned int cs
= *current_slot
;
254 /* Testing has shown this allocation to take long enough under
255 * GFP_KERNEL that the local node can get fenced. It would be
256 * nicest if we could pre-allocate these bios and avoid this
258 bio
= bio_alloc(GFP_ATOMIC
, 16);
260 mlog(ML_ERROR
, "Could not alloc slots BIO!\n");
261 bio
= ERR_PTR(-ENOMEM
);
265 /* Must put everything in 512 byte sectors for the bio... */
266 bio
->bi_sector
= (reg
->hr_start_block
+ cs
) << (bits
- 9);
267 bio
->bi_bdev
= reg
->hr_bdev
;
268 bio
->bi_private
= wc
;
269 bio
->bi_end_io
= o2hb_bio_end_io
;
271 vec_start
= (cs
<< bits
) % PAGE_CACHE_SIZE
;
272 while(cs
< max_slots
) {
273 current_page
= cs
/ spp
;
274 page
= reg
->hr_slot_data
[current_page
];
276 vec_len
= min(PAGE_CACHE_SIZE
- vec_start
,
277 (max_slots
-cs
) * (PAGE_CACHE_SIZE
/spp
) );
279 mlog(ML_HB_BIO
, "page %d, vec_len = %u, vec_start = %u\n",
280 current_page
, vec_len
, vec_start
);
282 len
= bio_add_page(bio
, page
, vec_len
, vec_start
);
283 if (len
!= vec_len
) break;
285 cs
+= vec_len
/ (PAGE_CACHE_SIZE
/spp
);
294 static int o2hb_read_slots(struct o2hb_region
*reg
,
295 unsigned int max_slots
)
297 unsigned int current_slot
=0;
299 struct o2hb_bio_wait_ctxt wc
;
302 o2hb_bio_wait_init(&wc
);
304 while(current_slot
< max_slots
) {
305 bio
= o2hb_setup_one_bio(reg
, &wc
, ¤t_slot
, max_slots
);
307 status
= PTR_ERR(bio
);
312 atomic_inc(&wc
.wc_num_reqs
);
313 submit_bio(READ
, bio
);
319 o2hb_wait_on_io(reg
, &wc
);
320 if (wc
.wc_error
&& !status
)
321 status
= wc
.wc_error
;
326 static int o2hb_issue_node_write(struct o2hb_region
*reg
,
327 struct o2hb_bio_wait_ctxt
*write_wc
)
333 o2hb_bio_wait_init(write_wc
);
335 slot
= o2nm_this_node();
337 bio
= o2hb_setup_one_bio(reg
, write_wc
, &slot
, slot
+1);
339 status
= PTR_ERR(bio
);
344 atomic_inc(&write_wc
->wc_num_reqs
);
345 submit_bio(WRITE
, bio
);
352 static u32
o2hb_compute_block_crc_le(struct o2hb_region
*reg
,
353 struct o2hb_disk_heartbeat_block
*hb_block
)
358 /* We want to compute the block crc with a 0 value in the
359 * hb_cksum field. Save it off here and replace after the
361 old_cksum
= hb_block
->hb_cksum
;
362 hb_block
->hb_cksum
= 0;
364 ret
= crc32_le(0, (unsigned char *) hb_block
, reg
->hr_block_bytes
);
366 hb_block
->hb_cksum
= old_cksum
;
371 static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block
*hb_block
)
373 mlog(ML_ERROR
, "Dump slot information: seq = 0x%llx, node = %u, "
374 "cksum = 0x%x, generation 0x%llx\n",
375 (long long)le64_to_cpu(hb_block
->hb_seq
),
376 hb_block
->hb_node
, le32_to_cpu(hb_block
->hb_cksum
),
377 (long long)le64_to_cpu(hb_block
->hb_generation
));
380 static int o2hb_verify_crc(struct o2hb_region
*reg
,
381 struct o2hb_disk_heartbeat_block
*hb_block
)
385 read
= le32_to_cpu(hb_block
->hb_cksum
);
386 computed
= o2hb_compute_block_crc_le(reg
, hb_block
);
388 return read
== computed
;
391 /* We want to make sure that nobody is heartbeating on top of us --
392 * this will help detect an invalid configuration. */
393 static int o2hb_check_last_timestamp(struct o2hb_region
*reg
)
396 struct o2hb_disk_slot
*slot
;
397 struct o2hb_disk_heartbeat_block
*hb_block
;
399 node_num
= o2nm_this_node();
402 slot
= ®
->hr_slots
[node_num
];
403 /* Don't check on our 1st timestamp */
404 if (slot
->ds_last_time
) {
405 hb_block
= slot
->ds_raw_block
;
407 if (le64_to_cpu(hb_block
->hb_seq
) != slot
->ds_last_time
)
414 static inline void o2hb_prepare_block(struct o2hb_region
*reg
,
419 struct o2hb_disk_slot
*slot
;
420 struct o2hb_disk_heartbeat_block
*hb_block
;
422 node_num
= o2nm_this_node();
423 slot
= ®
->hr_slots
[node_num
];
425 hb_block
= (struct o2hb_disk_heartbeat_block
*)slot
->ds_raw_block
;
426 memset(hb_block
, 0, reg
->hr_block_bytes
);
427 /* TODO: time stuff */
428 cputime
= CURRENT_TIME
.tv_sec
;
432 hb_block
->hb_seq
= cpu_to_le64(cputime
);
433 hb_block
->hb_node
= node_num
;
434 hb_block
->hb_generation
= cpu_to_le64(generation
);
435 hb_block
->hb_dead_ms
= cpu_to_le32(o2hb_dead_threshold
* O2HB_REGION_TIMEOUT_MS
);
437 /* This step must always happen last! */
438 hb_block
->hb_cksum
= cpu_to_le32(o2hb_compute_block_crc_le(reg
,
441 mlog(ML_HB_BIO
, "our node generation = 0x%llx, cksum = 0x%x\n",
442 (long long)generation
,
443 le32_to_cpu(hb_block
->hb_cksum
));
446 static void o2hb_fire_callbacks(struct o2hb_callback
*hbcall
,
447 struct o2nm_node
*node
,
450 struct list_head
*iter
;
451 struct o2hb_callback_func
*f
;
453 list_for_each(iter
, &hbcall
->list
) {
454 f
= list_entry(iter
, struct o2hb_callback_func
, hc_item
);
455 mlog(ML_HEARTBEAT
, "calling funcs %p\n", f
);
456 (f
->hc_func
)(node
, idx
, f
->hc_data
);
460 /* Will run the list in order until we process the passed event */
461 static void o2hb_run_event_list(struct o2hb_node_event
*queued_event
)
464 struct o2hb_callback
*hbcall
;
465 struct o2hb_node_event
*event
;
467 spin_lock(&o2hb_live_lock
);
468 empty
= list_empty(&queued_event
->hn_item
);
469 spin_unlock(&o2hb_live_lock
);
473 /* Holding callback sem assures we don't alter the callback
474 * lists when doing this, and serializes ourselves with other
475 * processes wanting callbacks. */
476 down_write(&o2hb_callback_sem
);
478 spin_lock(&o2hb_live_lock
);
479 while (!list_empty(&o2hb_node_events
)
480 && !list_empty(&queued_event
->hn_item
)) {
481 event
= list_entry(o2hb_node_events
.next
,
482 struct o2hb_node_event
,
484 list_del_init(&event
->hn_item
);
485 spin_unlock(&o2hb_live_lock
);
487 mlog(ML_HEARTBEAT
, "Node %s event for %d\n",
488 event
->hn_event_type
== O2HB_NODE_UP_CB
? "UP" : "DOWN",
491 hbcall
= hbcall_from_type(event
->hn_event_type
);
493 /* We should *never* have gotten on to the list with a
494 * bad type... This isn't something that we should try
495 * to recover from. */
496 BUG_ON(IS_ERR(hbcall
));
498 o2hb_fire_callbacks(hbcall
, event
->hn_node
, event
->hn_node_num
);
500 spin_lock(&o2hb_live_lock
);
502 spin_unlock(&o2hb_live_lock
);
504 up_write(&o2hb_callback_sem
);
507 static void o2hb_queue_node_event(struct o2hb_node_event
*event
,
508 enum o2hb_callback_type type
,
509 struct o2nm_node
*node
,
512 assert_spin_locked(&o2hb_live_lock
);
514 event
->hn_event_type
= type
;
515 event
->hn_node
= node
;
516 event
->hn_node_num
= node_num
;
518 mlog(ML_HEARTBEAT
, "Queue node %s event for node %d\n",
519 type
== O2HB_NODE_UP_CB
? "UP" : "DOWN", node_num
);
521 list_add_tail(&event
->hn_item
, &o2hb_node_events
);
524 static void o2hb_shutdown_slot(struct o2hb_disk_slot
*slot
)
526 struct o2hb_node_event event
=
527 { .hn_item
= LIST_HEAD_INIT(event
.hn_item
), };
528 struct o2nm_node
*node
;
530 node
= o2nm_get_node_by_num(slot
->ds_node_num
);
534 spin_lock(&o2hb_live_lock
);
535 if (!list_empty(&slot
->ds_live_item
)) {
536 mlog(ML_HEARTBEAT
, "Shutdown, node %d leaves region\n",
539 list_del_init(&slot
->ds_live_item
);
541 if (list_empty(&o2hb_live_slots
[slot
->ds_node_num
])) {
542 clear_bit(slot
->ds_node_num
, o2hb_live_node_bitmap
);
544 o2hb_queue_node_event(&event
, O2HB_NODE_DOWN_CB
, node
,
548 spin_unlock(&o2hb_live_lock
);
550 o2hb_run_event_list(&event
);
555 static int o2hb_check_slot(struct o2hb_region
*reg
,
556 struct o2hb_disk_slot
*slot
)
558 int changed
= 0, gen_changed
= 0;
559 struct o2hb_node_event event
=
560 { .hn_item
= LIST_HEAD_INIT(event
.hn_item
), };
561 struct o2nm_node
*node
;
562 struct o2hb_disk_heartbeat_block
*hb_block
= reg
->hr_tmp_block
;
564 unsigned int dead_ms
= o2hb_dead_threshold
* O2HB_REGION_TIMEOUT_MS
;
565 unsigned int slot_dead_ms
;
567 memcpy(hb_block
, slot
->ds_raw_block
, reg
->hr_block_bytes
);
569 /* Is this correct? Do we assume that the node doesn't exist
570 * if we're not configured for him? */
571 node
= o2nm_get_node_by_num(slot
->ds_node_num
);
575 if (!o2hb_verify_crc(reg
, hb_block
)) {
576 /* all paths from here will drop o2hb_live_lock for
578 spin_lock(&o2hb_live_lock
);
580 /* Don't print an error on the console in this case -
581 * a freshly formatted heartbeat area will not have a
583 if (list_empty(&slot
->ds_live_item
))
586 /* The node is live but pushed out a bad crc. We
587 * consider it a transient miss but don't populate any
588 * other values as they may be junk. */
589 mlog(ML_ERROR
, "Node %d has written a bad crc to %s\n",
590 slot
->ds_node_num
, reg
->hr_dev_name
);
591 o2hb_dump_slot(hb_block
);
593 slot
->ds_equal_samples
++;
597 /* we don't care if these wrap.. the state transitions below
598 * clear at the right places */
599 cputime
= le64_to_cpu(hb_block
->hb_seq
);
600 if (slot
->ds_last_time
!= cputime
)
601 slot
->ds_changed_samples
++;
603 slot
->ds_equal_samples
++;
604 slot
->ds_last_time
= cputime
;
606 /* The node changed heartbeat generations. We assume this to
607 * mean it dropped off but came back before we timed out. We
608 * want to consider it down for the time being but don't want
609 * to lose any changed_samples state we might build up to
610 * considering it live again. */
611 if (slot
->ds_last_generation
!= le64_to_cpu(hb_block
->hb_generation
)) {
613 slot
->ds_equal_samples
= 0;
614 mlog(ML_HEARTBEAT
, "Node %d changed generation (0x%llx "
615 "to 0x%llx)\n", slot
->ds_node_num
,
616 (long long)slot
->ds_last_generation
,
617 (long long)le64_to_cpu(hb_block
->hb_generation
));
620 slot
->ds_last_generation
= le64_to_cpu(hb_block
->hb_generation
);
622 mlog(ML_HEARTBEAT
, "Slot %d gen 0x%llx cksum 0x%x "
623 "seq %llu last %llu changed %u equal %u\n",
624 slot
->ds_node_num
, (long long)slot
->ds_last_generation
,
625 le32_to_cpu(hb_block
->hb_cksum
),
626 (unsigned long long)le64_to_cpu(hb_block
->hb_seq
),
627 (unsigned long long)slot
->ds_last_time
, slot
->ds_changed_samples
,
628 slot
->ds_equal_samples
);
630 spin_lock(&o2hb_live_lock
);
633 /* dead nodes only come to life after some number of
634 * changes at any time during their dead time */
635 if (list_empty(&slot
->ds_live_item
) &&
636 slot
->ds_changed_samples
>= O2HB_LIVE_THRESHOLD
) {
637 mlog(ML_HEARTBEAT
, "Node %d (id 0x%llx) joined my region\n",
638 slot
->ds_node_num
, (long long)slot
->ds_last_generation
);
640 /* first on the list generates a callback */
641 if (list_empty(&o2hb_live_slots
[slot
->ds_node_num
])) {
642 set_bit(slot
->ds_node_num
, o2hb_live_node_bitmap
);
644 o2hb_queue_node_event(&event
, O2HB_NODE_UP_CB
, node
,
650 list_add_tail(&slot
->ds_live_item
,
651 &o2hb_live_slots
[slot
->ds_node_num
]);
653 slot
->ds_equal_samples
= 0;
655 /* We want to be sure that all nodes agree on the
656 * number of milliseconds before a node will be
657 * considered dead. The self-fencing timeout is
658 * computed from this value, and a discrepancy might
659 * result in heartbeat calling a node dead when it
660 * hasn't self-fenced yet. */
661 slot_dead_ms
= le32_to_cpu(hb_block
->hb_dead_ms
);
662 if (slot_dead_ms
&& slot_dead_ms
!= dead_ms
) {
663 /* TODO: Perhaps we can fail the region here. */
664 mlog(ML_ERROR
, "Node %d on device %s has a dead count "
665 "of %u ms, but our count is %u ms.\n"
666 "Please double check your configuration values "
667 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
668 slot
->ds_node_num
, reg
->hr_dev_name
, slot_dead_ms
,
674 /* if the list is dead, we're done.. */
675 if (list_empty(&slot
->ds_live_item
))
678 /* live nodes only go dead after enough consequtive missed
679 * samples.. reset the missed counter whenever we see
681 if (slot
->ds_equal_samples
>= o2hb_dead_threshold
|| gen_changed
) {
682 mlog(ML_HEARTBEAT
, "Node %d left my region\n",
685 /* last off the live_slot generates a callback */
686 list_del_init(&slot
->ds_live_item
);
687 if (list_empty(&o2hb_live_slots
[slot
->ds_node_num
])) {
688 clear_bit(slot
->ds_node_num
, o2hb_live_node_bitmap
);
690 o2hb_queue_node_event(&event
, O2HB_NODE_DOWN_CB
, node
,
696 /* We don't clear this because the node is still
697 * actually writing new blocks. */
699 slot
->ds_changed_samples
= 0;
702 if (slot
->ds_changed_samples
) {
703 slot
->ds_changed_samples
= 0;
704 slot
->ds_equal_samples
= 0;
707 spin_unlock(&o2hb_live_lock
);
709 o2hb_run_event_list(&event
);
715 /* This could be faster if we just implmented a find_last_bit, but I
716 * don't think the circumstances warrant it. */
717 static int o2hb_highest_node(unsigned long *nodes
,
724 while ((node
= find_next_bit(nodes
, numbits
, node
+ 1)) != -1) {
734 static int o2hb_do_disk_heartbeat(struct o2hb_region
*reg
)
736 int i
, ret
, highest_node
, change
= 0;
737 unsigned long configured_nodes
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
738 struct o2hb_bio_wait_ctxt write_wc
;
740 ret
= o2nm_configured_node_map(configured_nodes
,
741 sizeof(configured_nodes
));
747 highest_node
= o2hb_highest_node(configured_nodes
, O2NM_MAX_NODES
);
748 if (highest_node
>= O2NM_MAX_NODES
) {
749 mlog(ML_NOTICE
, "ocfs2_heartbeat: no configured nodes found!\n");
753 /* No sense in reading the slots of nodes that don't exist
754 * yet. Of course, if the node definitions have holes in them
755 * then we're reading an empty slot anyway... Consider this
757 ret
= o2hb_read_slots(reg
, highest_node
+ 1);
763 /* With an up to date view of the slots, we can check that no
764 * other node has been improperly configured to heartbeat in
766 if (!o2hb_check_last_timestamp(reg
))
767 mlog(ML_ERROR
, "Device \"%s\": another node is heartbeating "
768 "in our slot!\n", reg
->hr_dev_name
);
770 /* fill in the proper info for our next heartbeat */
771 o2hb_prepare_block(reg
, reg
->hr_generation
);
773 /* And fire off the write. Note that we don't wait on this I/O
775 ret
= o2hb_issue_node_write(reg
, &write_wc
);
782 while((i
= find_next_bit(configured_nodes
, O2NM_MAX_NODES
, i
+ 1)) < O2NM_MAX_NODES
) {
784 change
|= o2hb_check_slot(reg
, ®
->hr_slots
[i
]);
788 * We have to be sure we've advertised ourselves on disk
789 * before we can go to steady state. This ensures that
790 * people we find in our steady state have seen us.
792 o2hb_wait_on_io(reg
, &write_wc
);
793 if (write_wc
.wc_error
) {
794 /* Do not re-arm the write timeout on I/O error - we
795 * can't be sure that the new block ever made it to
797 mlog(ML_ERROR
, "Write error %d on device \"%s\"\n",
798 write_wc
.wc_error
, reg
->hr_dev_name
);
799 return write_wc
.wc_error
;
802 o2hb_arm_write_timeout(reg
);
804 /* let the person who launched us know when things are steady */
805 if (!change
&& (atomic_read(®
->hr_steady_iterations
) != 0)) {
806 if (atomic_dec_and_test(®
->hr_steady_iterations
))
807 wake_up(&o2hb_steady_queue
);
813 /* Subtract b from a, storing the result in a. a *must* have a larger
815 static void o2hb_tv_subtract(struct timeval
*a
,
818 /* just return 0 when a is after b */
819 if (a
->tv_sec
< b
->tv_sec
||
820 (a
->tv_sec
== b
->tv_sec
&& a
->tv_usec
< b
->tv_usec
)) {
826 a
->tv_sec
-= b
->tv_sec
;
827 a
->tv_usec
-= b
->tv_usec
;
828 while ( a
->tv_usec
< 0 ) {
830 a
->tv_usec
+= 1000000;
834 static unsigned int o2hb_elapsed_msecs(struct timeval
*start
,
837 struct timeval res
= *end
;
839 o2hb_tv_subtract(&res
, start
);
841 return res
.tv_sec
* 1000 + res
.tv_usec
/ 1000;
845 * we ride the region ref that the region dir holds. before the region
846 * dir is removed and drops it ref it will wait to tear down this
849 static int o2hb_thread(void *data
)
852 struct o2hb_region
*reg
= data
;
853 struct o2hb_bio_wait_ctxt write_wc
;
854 struct timeval before_hb
, after_hb
;
855 unsigned int elapsed_msec
;
857 mlog(ML_HEARTBEAT
|ML_KTHREAD
, "hb thread running\n");
859 set_user_nice(current
, -20);
861 while (!kthread_should_stop() && !reg
->hr_unclean_stop
) {
862 /* We track the time spent inside
863 * o2hb_do_disk_heartbeat so that we avoid more than
864 * hr_timeout_ms between disk writes. On busy systems
865 * this should result in a heartbeat which is less
866 * likely to time itself out. */
867 do_gettimeofday(&before_hb
);
871 ret
= o2hb_do_disk_heartbeat(reg
);
872 } while (ret
&& ++i
< 2);
874 do_gettimeofday(&after_hb
);
875 elapsed_msec
= o2hb_elapsed_msecs(&before_hb
, &after_hb
);
877 mlog(0, "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
878 before_hb
.tv_sec
, (unsigned long) before_hb
.tv_usec
,
879 after_hb
.tv_sec
, (unsigned long) after_hb
.tv_usec
,
882 if (elapsed_msec
< reg
->hr_timeout_ms
) {
883 /* the kthread api has blocked signals for us so no
884 * need to record the return value. */
885 msleep_interruptible(reg
->hr_timeout_ms
- elapsed_msec
);
889 o2hb_disarm_write_timeout(reg
);
891 /* unclean stop is only used in very bad situation */
892 for(i
= 0; !reg
->hr_unclean_stop
&& i
< reg
->hr_blocks
; i
++)
893 o2hb_shutdown_slot(®
->hr_slots
[i
]);
895 /* Explicit down notification - avoid forcing the other nodes
896 * to timeout on this region when we could just as easily
897 * write a clear generation - thus indicating to them that
898 * this node has left this region.
900 * XXX: Should we skip this on unclean_stop? */
901 o2hb_prepare_block(reg
, 0);
902 ret
= o2hb_issue_node_write(reg
, &write_wc
);
904 o2hb_wait_on_io(reg
, &write_wc
);
909 mlog(ML_HEARTBEAT
|ML_KTHREAD
, "hb thread exiting\n");
914 #ifdef CONFIG_DEBUG_FS
915 static int o2hb_debug_open(struct inode
*inode
, struct file
*file
)
917 unsigned long map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
922 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
926 o2hb_fill_node_map(map
, sizeof(map
));
928 while ((i
= find_next_bit(map
, O2NM_MAX_NODES
, i
+ 1)) < O2NM_MAX_NODES
)
929 out
+= snprintf(buf
+ out
, PAGE_SIZE
- out
, "%d ", i
);
930 out
+= snprintf(buf
+ out
, PAGE_SIZE
- out
, "\n");
932 i_size_write(inode
, out
);
934 file
->private_data
= buf
;
941 static int o2hb_debug_release(struct inode
*inode
, struct file
*file
)
943 kfree(file
->private_data
);
947 static ssize_t
o2hb_debug_read(struct file
*file
, char __user
*buf
,
948 size_t nbytes
, loff_t
*ppos
)
950 return simple_read_from_buffer(buf
, nbytes
, ppos
, file
->private_data
,
951 i_size_read(file
->f_mapping
->host
));
954 static int o2hb_debug_open(struct inode
*inode
, struct file
*file
)
958 static int o2hb_debug_release(struct inode
*inode
, struct file
*file
)
962 static ssize_t
o2hb_debug_read(struct file
*file
, char __user
*buf
,
963 size_t nbytes
, loff_t
*ppos
)
967 #endif /* CONFIG_DEBUG_FS */
969 static const struct file_operations o2hb_debug_fops
= {
970 .open
= o2hb_debug_open
,
971 .release
= o2hb_debug_release
,
972 .read
= o2hb_debug_read
,
973 .llseek
= generic_file_llseek
,
978 if (o2hb_debug_livenodes
)
979 debugfs_remove(o2hb_debug_livenodes
);
981 debugfs_remove(o2hb_debug_dir
);
988 for (i
= 0; i
< ARRAY_SIZE(o2hb_callbacks
); i
++)
989 INIT_LIST_HEAD(&o2hb_callbacks
[i
].list
);
991 for (i
= 0; i
< ARRAY_SIZE(o2hb_live_slots
); i
++)
992 INIT_LIST_HEAD(&o2hb_live_slots
[i
]);
994 INIT_LIST_HEAD(&o2hb_node_events
);
996 memset(o2hb_live_node_bitmap
, 0, sizeof(o2hb_live_node_bitmap
));
998 o2hb_debug_dir
= debugfs_create_dir(O2HB_DEBUG_DIR
, NULL
);
999 if (!o2hb_debug_dir
) {
1000 mlog_errno(-ENOMEM
);
1004 o2hb_debug_livenodes
= debugfs_create_file(O2HB_DEBUG_LIVENODES
,
1006 o2hb_debug_dir
, NULL
,
1008 if (!o2hb_debug_livenodes
) {
1009 mlog_errno(-ENOMEM
);
1010 debugfs_remove(o2hb_debug_dir
);
1017 /* if we're already in a callback then we're already serialized by the sem */
1018 static void o2hb_fill_node_map_from_callback(unsigned long *map
,
1021 BUG_ON(bytes
< (BITS_TO_LONGS(O2NM_MAX_NODES
) * sizeof(unsigned long)));
1023 memcpy(map
, &o2hb_live_node_bitmap
, bytes
);
1027 * get a map of all nodes that are heartbeating in any regions
1029 void o2hb_fill_node_map(unsigned long *map
, unsigned bytes
)
1031 /* callers want to serialize this map and callbacks so that they
1032 * can trust that they don't miss nodes coming to the party */
1033 down_read(&o2hb_callback_sem
);
1034 spin_lock(&o2hb_live_lock
);
1035 o2hb_fill_node_map_from_callback(map
, bytes
);
1036 spin_unlock(&o2hb_live_lock
);
1037 up_read(&o2hb_callback_sem
);
1039 EXPORT_SYMBOL_GPL(o2hb_fill_node_map
);
1042 * heartbeat configfs bits. The heartbeat set is a default set under
1043 * the cluster set in nodemanager.c.
1046 static struct o2hb_region
*to_o2hb_region(struct config_item
*item
)
1048 return item
? container_of(item
, struct o2hb_region
, hr_item
) : NULL
;
1051 /* drop_item only drops its ref after killing the thread, nothing should
1052 * be using the region anymore. this has to clean up any state that
1053 * attributes might have built up. */
1054 static void o2hb_region_release(struct config_item
*item
)
1058 struct o2hb_region
*reg
= to_o2hb_region(item
);
1060 if (reg
->hr_tmp_block
)
1061 kfree(reg
->hr_tmp_block
);
1063 if (reg
->hr_slot_data
) {
1064 for (i
= 0; i
< reg
->hr_num_pages
; i
++) {
1065 page
= reg
->hr_slot_data
[i
];
1069 kfree(reg
->hr_slot_data
);
1073 blkdev_put(reg
->hr_bdev
, FMODE_READ
|FMODE_WRITE
);
1076 kfree(reg
->hr_slots
);
1078 spin_lock(&o2hb_live_lock
);
1079 list_del(®
->hr_all_item
);
1080 spin_unlock(&o2hb_live_lock
);
1085 static int o2hb_read_block_input(struct o2hb_region
*reg
,
1088 unsigned long *ret_bytes
,
1089 unsigned int *ret_bits
)
1091 unsigned long bytes
;
1092 char *p
= (char *)page
;
1094 bytes
= simple_strtoul(p
, &p
, 0);
1095 if (!p
|| (*p
&& (*p
!= '\n')))
1098 /* Heartbeat and fs min / max block sizes are the same. */
1099 if (bytes
> 4096 || bytes
< 512)
1101 if (hweight16(bytes
) != 1)
1107 *ret_bits
= ffs(bytes
) - 1;
1112 static ssize_t
o2hb_region_block_bytes_read(struct o2hb_region
*reg
,
1115 return sprintf(page
, "%u\n", reg
->hr_block_bytes
);
1118 static ssize_t
o2hb_region_block_bytes_write(struct o2hb_region
*reg
,
1123 unsigned long block_bytes
;
1124 unsigned int block_bits
;
1129 status
= o2hb_read_block_input(reg
, page
, count
,
1130 &block_bytes
, &block_bits
);
1134 reg
->hr_block_bytes
= (unsigned int)block_bytes
;
1135 reg
->hr_block_bits
= block_bits
;
1140 static ssize_t
o2hb_region_start_block_read(struct o2hb_region
*reg
,
1143 return sprintf(page
, "%llu\n", reg
->hr_start_block
);
1146 static ssize_t
o2hb_region_start_block_write(struct o2hb_region
*reg
,
1150 unsigned long long tmp
;
1151 char *p
= (char *)page
;
1156 tmp
= simple_strtoull(p
, &p
, 0);
1157 if (!p
|| (*p
&& (*p
!= '\n')))
1160 reg
->hr_start_block
= tmp
;
1165 static ssize_t
o2hb_region_blocks_read(struct o2hb_region
*reg
,
1168 return sprintf(page
, "%d\n", reg
->hr_blocks
);
1171 static ssize_t
o2hb_region_blocks_write(struct o2hb_region
*reg
,
1176 char *p
= (char *)page
;
1181 tmp
= simple_strtoul(p
, &p
, 0);
1182 if (!p
|| (*p
&& (*p
!= '\n')))
1185 if (tmp
> O2NM_MAX_NODES
|| tmp
== 0)
1188 reg
->hr_blocks
= (unsigned int)tmp
;
1193 static ssize_t
o2hb_region_dev_read(struct o2hb_region
*reg
,
1196 unsigned int ret
= 0;
1199 ret
= sprintf(page
, "%s\n", reg
->hr_dev_name
);
1204 static void o2hb_init_region_params(struct o2hb_region
*reg
)
1206 reg
->hr_slots_per_page
= PAGE_CACHE_SIZE
>> reg
->hr_block_bits
;
1207 reg
->hr_timeout_ms
= O2HB_REGION_TIMEOUT_MS
;
1209 mlog(ML_HEARTBEAT
, "hr_start_block = %llu, hr_blocks = %u\n",
1210 reg
->hr_start_block
, reg
->hr_blocks
);
1211 mlog(ML_HEARTBEAT
, "hr_block_bytes = %u, hr_block_bits = %u\n",
1212 reg
->hr_block_bytes
, reg
->hr_block_bits
);
1213 mlog(ML_HEARTBEAT
, "hr_timeout_ms = %u\n", reg
->hr_timeout_ms
);
1214 mlog(ML_HEARTBEAT
, "dead threshold = %u\n", o2hb_dead_threshold
);
1217 static int o2hb_map_slot_data(struct o2hb_region
*reg
)
1220 unsigned int last_slot
;
1221 unsigned int spp
= reg
->hr_slots_per_page
;
1224 struct o2hb_disk_slot
*slot
;
1226 reg
->hr_tmp_block
= kmalloc(reg
->hr_block_bytes
, GFP_KERNEL
);
1227 if (reg
->hr_tmp_block
== NULL
) {
1228 mlog_errno(-ENOMEM
);
1232 reg
->hr_slots
= kcalloc(reg
->hr_blocks
,
1233 sizeof(struct o2hb_disk_slot
), GFP_KERNEL
);
1234 if (reg
->hr_slots
== NULL
) {
1235 mlog_errno(-ENOMEM
);
1239 for(i
= 0; i
< reg
->hr_blocks
; i
++) {
1240 slot
= ®
->hr_slots
[i
];
1241 slot
->ds_node_num
= i
;
1242 INIT_LIST_HEAD(&slot
->ds_live_item
);
1243 slot
->ds_raw_block
= NULL
;
1246 reg
->hr_num_pages
= (reg
->hr_blocks
+ spp
- 1) / spp
;
1247 mlog(ML_HEARTBEAT
, "Going to require %u pages to cover %u blocks "
1248 "at %u blocks per page\n",
1249 reg
->hr_num_pages
, reg
->hr_blocks
, spp
);
1251 reg
->hr_slot_data
= kcalloc(reg
->hr_num_pages
, sizeof(struct page
*),
1253 if (!reg
->hr_slot_data
) {
1254 mlog_errno(-ENOMEM
);
1258 for(i
= 0; i
< reg
->hr_num_pages
; i
++) {
1259 page
= alloc_page(GFP_KERNEL
);
1261 mlog_errno(-ENOMEM
);
1265 reg
->hr_slot_data
[i
] = page
;
1267 last_slot
= i
* spp
;
1268 raw
= page_address(page
);
1270 (j
< spp
) && ((j
+ last_slot
) < reg
->hr_blocks
);
1272 BUG_ON((j
+ last_slot
) >= reg
->hr_blocks
);
1274 slot
= ®
->hr_slots
[j
+ last_slot
];
1275 slot
->ds_raw_block
=
1276 (struct o2hb_disk_heartbeat_block
*) raw
;
1278 raw
+= reg
->hr_block_bytes
;
1285 /* Read in all the slots available and populate the tracking
1286 * structures so that we can start with a baseline idea of what's
1288 static int o2hb_populate_slot_data(struct o2hb_region
*reg
)
1291 struct o2hb_disk_slot
*slot
;
1292 struct o2hb_disk_heartbeat_block
*hb_block
;
1296 ret
= o2hb_read_slots(reg
, reg
->hr_blocks
);
1302 /* We only want to get an idea of the values initially in each
1303 * slot, so we do no verification - o2hb_check_slot will
1304 * actually determine if each configured slot is valid and
1305 * whether any values have changed. */
1306 for(i
= 0; i
< reg
->hr_blocks
; i
++) {
1307 slot
= ®
->hr_slots
[i
];
1308 hb_block
= (struct o2hb_disk_heartbeat_block
*) slot
->ds_raw_block
;
1310 /* Only fill the values that o2hb_check_slot uses to
1311 * determine changing slots */
1312 slot
->ds_last_time
= le64_to_cpu(hb_block
->hb_seq
);
1313 slot
->ds_last_generation
= le64_to_cpu(hb_block
->hb_generation
);
1321 /* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1322 static ssize_t
o2hb_region_dev_write(struct o2hb_region
*reg
,
1326 struct task_struct
*hb_task
;
1329 char *p
= (char *)page
;
1330 struct file
*filp
= NULL
;
1331 struct inode
*inode
= NULL
;
1332 ssize_t ret
= -EINVAL
;
1337 /* We can't heartbeat without having had our node number
1338 * configured yet. */
1339 if (o2nm_this_node() == O2NM_MAX_NODES
)
1342 fd
= simple_strtol(p
, &p
, 0);
1343 if (!p
|| (*p
&& (*p
!= '\n')))
1346 if (fd
< 0 || fd
>= INT_MAX
)
1353 if (reg
->hr_blocks
== 0 || reg
->hr_start_block
== 0 ||
1354 reg
->hr_block_bytes
== 0)
1357 inode
= igrab(filp
->f_mapping
->host
);
1361 if (!S_ISBLK(inode
->i_mode
))
1364 reg
->hr_bdev
= I_BDEV(filp
->f_mapping
->host
);
1365 ret
= blkdev_get(reg
->hr_bdev
, FMODE_WRITE
| FMODE_READ
);
1367 reg
->hr_bdev
= NULL
;
1372 bdevname(reg
->hr_bdev
, reg
->hr_dev_name
);
1374 sectsize
= bdev_logical_block_size(reg
->hr_bdev
);
1375 if (sectsize
!= reg
->hr_block_bytes
) {
1377 "blocksize %u incorrect for device, expected %d",
1378 reg
->hr_block_bytes
, sectsize
);
1383 o2hb_init_region_params(reg
);
1385 /* Generation of zero is invalid */
1387 get_random_bytes(®
->hr_generation
,
1388 sizeof(reg
->hr_generation
));
1389 } while (reg
->hr_generation
== 0);
1391 ret
= o2hb_map_slot_data(reg
);
1397 ret
= o2hb_populate_slot_data(reg
);
1403 INIT_DELAYED_WORK(®
->hr_write_timeout_work
, o2hb_write_timeout
);
1406 * A node is considered live after it has beat LIVE_THRESHOLD
1407 * times. We're not steady until we've given them a chance
1408 * _after_ our first read.
1410 atomic_set(®
->hr_steady_iterations
, O2HB_LIVE_THRESHOLD
+ 1);
1412 hb_task
= kthread_run(o2hb_thread
, reg
, "o2hb-%s",
1413 reg
->hr_item
.ci_name
);
1414 if (IS_ERR(hb_task
)) {
1415 ret
= PTR_ERR(hb_task
);
1420 spin_lock(&o2hb_live_lock
);
1421 reg
->hr_task
= hb_task
;
1422 spin_unlock(&o2hb_live_lock
);
1424 ret
= wait_event_interruptible(o2hb_steady_queue
,
1425 atomic_read(®
->hr_steady_iterations
) == 0);
1427 /* We got interrupted (hello ptrace!). Clean up */
1428 spin_lock(&o2hb_live_lock
);
1429 hb_task
= reg
->hr_task
;
1430 reg
->hr_task
= NULL
;
1431 spin_unlock(&o2hb_live_lock
);
1434 kthread_stop(hb_task
);
1438 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1439 spin_lock(&o2hb_live_lock
);
1440 hb_task
= reg
->hr_task
;
1441 spin_unlock(&o2hb_live_lock
);
1455 blkdev_put(reg
->hr_bdev
, FMODE_READ
|FMODE_WRITE
);
1456 reg
->hr_bdev
= NULL
;
1462 static ssize_t
o2hb_region_pid_read(struct o2hb_region
*reg
,
1467 spin_lock(&o2hb_live_lock
);
1469 pid
= task_pid_nr(reg
->hr_task
);
1470 spin_unlock(&o2hb_live_lock
);
1475 return sprintf(page
, "%u\n", pid
);
1478 struct o2hb_region_attribute
{
1479 struct configfs_attribute attr
;
1480 ssize_t (*show
)(struct o2hb_region
*, char *);
1481 ssize_t (*store
)(struct o2hb_region
*, const char *, size_t);
1484 static struct o2hb_region_attribute o2hb_region_attr_block_bytes
= {
1485 .attr
= { .ca_owner
= THIS_MODULE
,
1486 .ca_name
= "block_bytes",
1487 .ca_mode
= S_IRUGO
| S_IWUSR
},
1488 .show
= o2hb_region_block_bytes_read
,
1489 .store
= o2hb_region_block_bytes_write
,
1492 static struct o2hb_region_attribute o2hb_region_attr_start_block
= {
1493 .attr
= { .ca_owner
= THIS_MODULE
,
1494 .ca_name
= "start_block",
1495 .ca_mode
= S_IRUGO
| S_IWUSR
},
1496 .show
= o2hb_region_start_block_read
,
1497 .store
= o2hb_region_start_block_write
,
1500 static struct o2hb_region_attribute o2hb_region_attr_blocks
= {
1501 .attr
= { .ca_owner
= THIS_MODULE
,
1502 .ca_name
= "blocks",
1503 .ca_mode
= S_IRUGO
| S_IWUSR
},
1504 .show
= o2hb_region_blocks_read
,
1505 .store
= o2hb_region_blocks_write
,
1508 static struct o2hb_region_attribute o2hb_region_attr_dev
= {
1509 .attr
= { .ca_owner
= THIS_MODULE
,
1511 .ca_mode
= S_IRUGO
| S_IWUSR
},
1512 .show
= o2hb_region_dev_read
,
1513 .store
= o2hb_region_dev_write
,
1516 static struct o2hb_region_attribute o2hb_region_attr_pid
= {
1517 .attr
= { .ca_owner
= THIS_MODULE
,
1519 .ca_mode
= S_IRUGO
| S_IRUSR
},
1520 .show
= o2hb_region_pid_read
,
1523 static struct configfs_attribute
*o2hb_region_attrs
[] = {
1524 &o2hb_region_attr_block_bytes
.attr
,
1525 &o2hb_region_attr_start_block
.attr
,
1526 &o2hb_region_attr_blocks
.attr
,
1527 &o2hb_region_attr_dev
.attr
,
1528 &o2hb_region_attr_pid
.attr
,
1532 static ssize_t
o2hb_region_show(struct config_item
*item
,
1533 struct configfs_attribute
*attr
,
1536 struct o2hb_region
*reg
= to_o2hb_region(item
);
1537 struct o2hb_region_attribute
*o2hb_region_attr
=
1538 container_of(attr
, struct o2hb_region_attribute
, attr
);
1541 if (o2hb_region_attr
->show
)
1542 ret
= o2hb_region_attr
->show(reg
, page
);
1546 static ssize_t
o2hb_region_store(struct config_item
*item
,
1547 struct configfs_attribute
*attr
,
1548 const char *page
, size_t count
)
1550 struct o2hb_region
*reg
= to_o2hb_region(item
);
1551 struct o2hb_region_attribute
*o2hb_region_attr
=
1552 container_of(attr
, struct o2hb_region_attribute
, attr
);
1553 ssize_t ret
= -EINVAL
;
1555 if (o2hb_region_attr
->store
)
1556 ret
= o2hb_region_attr
->store(reg
, page
, count
);
1560 static struct configfs_item_operations o2hb_region_item_ops
= {
1561 .release
= o2hb_region_release
,
1562 .show_attribute
= o2hb_region_show
,
1563 .store_attribute
= o2hb_region_store
,
1566 static struct config_item_type o2hb_region_type
= {
1567 .ct_item_ops
= &o2hb_region_item_ops
,
1568 .ct_attrs
= o2hb_region_attrs
,
1569 .ct_owner
= THIS_MODULE
,
1574 struct o2hb_heartbeat_group
{
1575 struct config_group hs_group
;
1579 static struct o2hb_heartbeat_group
*to_o2hb_heartbeat_group(struct config_group
*group
)
1582 container_of(group
, struct o2hb_heartbeat_group
, hs_group
)
1586 static struct config_item
*o2hb_heartbeat_group_make_item(struct config_group
*group
,
1589 struct o2hb_region
*reg
= NULL
;
1591 reg
= kzalloc(sizeof(struct o2hb_region
), GFP_KERNEL
);
1593 return ERR_PTR(-ENOMEM
);
1595 config_item_init_type_name(®
->hr_item
, name
, &o2hb_region_type
);
1597 spin_lock(&o2hb_live_lock
);
1598 list_add_tail(®
->hr_all_item
, &o2hb_all_regions
);
1599 spin_unlock(&o2hb_live_lock
);
1601 return ®
->hr_item
;
1604 static void o2hb_heartbeat_group_drop_item(struct config_group
*group
,
1605 struct config_item
*item
)
1607 struct task_struct
*hb_task
;
1608 struct o2hb_region
*reg
= to_o2hb_region(item
);
1610 /* stop the thread when the user removes the region dir */
1611 spin_lock(&o2hb_live_lock
);
1612 hb_task
= reg
->hr_task
;
1613 reg
->hr_task
= NULL
;
1614 spin_unlock(&o2hb_live_lock
);
1617 kthread_stop(hb_task
);
1620 * If we're racing a dev_write(), we need to wake them. They will
1621 * check reg->hr_task
1623 if (atomic_read(®
->hr_steady_iterations
) != 0) {
1624 atomic_set(®
->hr_steady_iterations
, 0);
1625 wake_up(&o2hb_steady_queue
);
1628 config_item_put(item
);
1631 struct o2hb_heartbeat_group_attribute
{
1632 struct configfs_attribute attr
;
1633 ssize_t (*show
)(struct o2hb_heartbeat_group
*, char *);
1634 ssize_t (*store
)(struct o2hb_heartbeat_group
*, const char *, size_t);
1637 static ssize_t
o2hb_heartbeat_group_show(struct config_item
*item
,
1638 struct configfs_attribute
*attr
,
1641 struct o2hb_heartbeat_group
*reg
= to_o2hb_heartbeat_group(to_config_group(item
));
1642 struct o2hb_heartbeat_group_attribute
*o2hb_heartbeat_group_attr
=
1643 container_of(attr
, struct o2hb_heartbeat_group_attribute
, attr
);
1646 if (o2hb_heartbeat_group_attr
->show
)
1647 ret
= o2hb_heartbeat_group_attr
->show(reg
, page
);
1651 static ssize_t
o2hb_heartbeat_group_store(struct config_item
*item
,
1652 struct configfs_attribute
*attr
,
1653 const char *page
, size_t count
)
1655 struct o2hb_heartbeat_group
*reg
= to_o2hb_heartbeat_group(to_config_group(item
));
1656 struct o2hb_heartbeat_group_attribute
*o2hb_heartbeat_group_attr
=
1657 container_of(attr
, struct o2hb_heartbeat_group_attribute
, attr
);
1658 ssize_t ret
= -EINVAL
;
1660 if (o2hb_heartbeat_group_attr
->store
)
1661 ret
= o2hb_heartbeat_group_attr
->store(reg
, page
, count
);
1665 static ssize_t
o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group
*group
,
1668 return sprintf(page
, "%u\n", o2hb_dead_threshold
);
1671 static ssize_t
o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group
*group
,
1676 char *p
= (char *)page
;
1678 tmp
= simple_strtoul(p
, &p
, 10);
1679 if (!p
|| (*p
&& (*p
!= '\n')))
1682 /* this will validate ranges for us. */
1683 o2hb_dead_threshold_set((unsigned int) tmp
);
1688 static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold
= {
1689 .attr
= { .ca_owner
= THIS_MODULE
,
1690 .ca_name
= "dead_threshold",
1691 .ca_mode
= S_IRUGO
| S_IWUSR
},
1692 .show
= o2hb_heartbeat_group_threshold_show
,
1693 .store
= o2hb_heartbeat_group_threshold_store
,
1696 static struct configfs_attribute
*o2hb_heartbeat_group_attrs
[] = {
1697 &o2hb_heartbeat_group_attr_threshold
.attr
,
1701 static struct configfs_item_operations o2hb_hearbeat_group_item_ops
= {
1702 .show_attribute
= o2hb_heartbeat_group_show
,
1703 .store_attribute
= o2hb_heartbeat_group_store
,
1706 static struct configfs_group_operations o2hb_heartbeat_group_group_ops
= {
1707 .make_item
= o2hb_heartbeat_group_make_item
,
1708 .drop_item
= o2hb_heartbeat_group_drop_item
,
1711 static struct config_item_type o2hb_heartbeat_group_type
= {
1712 .ct_group_ops
= &o2hb_heartbeat_group_group_ops
,
1713 .ct_item_ops
= &o2hb_hearbeat_group_item_ops
,
1714 .ct_attrs
= o2hb_heartbeat_group_attrs
,
1715 .ct_owner
= THIS_MODULE
,
1718 /* this is just here to avoid touching group in heartbeat.h which the
1719 * entire damn world #includes */
1720 struct config_group
*o2hb_alloc_hb_set(void)
1722 struct o2hb_heartbeat_group
*hs
= NULL
;
1723 struct config_group
*ret
= NULL
;
1725 hs
= kzalloc(sizeof(struct o2hb_heartbeat_group
), GFP_KERNEL
);
1729 config_group_init_type_name(&hs
->hs_group
, "heartbeat",
1730 &o2hb_heartbeat_group_type
);
1732 ret
= &hs
->hs_group
;
1739 void o2hb_free_hb_set(struct config_group
*group
)
1741 struct o2hb_heartbeat_group
*hs
= to_o2hb_heartbeat_group(group
);
1745 /* hb callback registration and issueing */
1747 static struct o2hb_callback
*hbcall_from_type(enum o2hb_callback_type type
)
1749 if (type
== O2HB_NUM_CB
)
1750 return ERR_PTR(-EINVAL
);
1752 return &o2hb_callbacks
[type
];
1755 void o2hb_setup_callback(struct o2hb_callback_func
*hc
,
1756 enum o2hb_callback_type type
,
1761 INIT_LIST_HEAD(&hc
->hc_item
);
1764 hc
->hc_priority
= priority
;
1766 hc
->hc_magic
= O2HB_CB_MAGIC
;
1768 EXPORT_SYMBOL_GPL(o2hb_setup_callback
);
1770 static struct o2hb_region
*o2hb_find_region(const char *region_uuid
)
1772 struct o2hb_region
*p
, *reg
= NULL
;
1774 assert_spin_locked(&o2hb_live_lock
);
1776 list_for_each_entry(p
, &o2hb_all_regions
, hr_all_item
) {
1777 if (!strcmp(region_uuid
, config_item_name(&p
->hr_item
))) {
1786 static int o2hb_region_get(const char *region_uuid
)
1789 struct o2hb_region
*reg
;
1791 spin_lock(&o2hb_live_lock
);
1793 reg
= o2hb_find_region(region_uuid
);
1796 spin_unlock(&o2hb_live_lock
);
1801 ret
= o2nm_depend_this_node();
1805 ret
= o2nm_depend_item(®
->hr_item
);
1807 o2nm_undepend_this_node();
1813 static void o2hb_region_put(const char *region_uuid
)
1815 struct o2hb_region
*reg
;
1817 spin_lock(&o2hb_live_lock
);
1819 reg
= o2hb_find_region(region_uuid
);
1821 spin_unlock(&o2hb_live_lock
);
1824 o2nm_undepend_item(®
->hr_item
);
1825 o2nm_undepend_this_node();
1829 int o2hb_register_callback(const char *region_uuid
,
1830 struct o2hb_callback_func
*hc
)
1832 struct o2hb_callback_func
*tmp
;
1833 struct list_head
*iter
;
1834 struct o2hb_callback
*hbcall
;
1837 BUG_ON(hc
->hc_magic
!= O2HB_CB_MAGIC
);
1838 BUG_ON(!list_empty(&hc
->hc_item
));
1840 hbcall
= hbcall_from_type(hc
->hc_type
);
1841 if (IS_ERR(hbcall
)) {
1842 ret
= PTR_ERR(hbcall
);
1847 ret
= o2hb_region_get(region_uuid
);
1852 down_write(&o2hb_callback_sem
);
1854 list_for_each(iter
, &hbcall
->list
) {
1855 tmp
= list_entry(iter
, struct o2hb_callback_func
, hc_item
);
1856 if (hc
->hc_priority
< tmp
->hc_priority
) {
1857 list_add_tail(&hc
->hc_item
, iter
);
1861 if (list_empty(&hc
->hc_item
))
1862 list_add_tail(&hc
->hc_item
, &hbcall
->list
);
1864 up_write(&o2hb_callback_sem
);
1867 mlog(ML_HEARTBEAT
, "returning %d on behalf of %p for funcs %p\n",
1868 ret
, __builtin_return_address(0), hc
);
1871 EXPORT_SYMBOL_GPL(o2hb_register_callback
);
1873 void o2hb_unregister_callback(const char *region_uuid
,
1874 struct o2hb_callback_func
*hc
)
1876 BUG_ON(hc
->hc_magic
!= O2HB_CB_MAGIC
);
1878 mlog(ML_HEARTBEAT
, "on behalf of %p for funcs %p\n",
1879 __builtin_return_address(0), hc
);
1881 /* XXX Can this happen _with_ a region reference? */
1882 if (list_empty(&hc
->hc_item
))
1886 o2hb_region_put(region_uuid
);
1888 down_write(&o2hb_callback_sem
);
1890 list_del_init(&hc
->hc_item
);
1892 up_write(&o2hb_callback_sem
);
1894 EXPORT_SYMBOL_GPL(o2hb_unregister_callback
);
1896 int o2hb_check_node_heartbeating(u8 node_num
)
1898 unsigned long testing_map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
1900 o2hb_fill_node_map(testing_map
, sizeof(testing_map
));
1901 if (!test_bit(node_num
, testing_map
)) {
1903 "node (%u) does not have heartbeating enabled.\n",
1910 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating
);
1912 int o2hb_check_node_heartbeating_from_callback(u8 node_num
)
1914 unsigned long testing_map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
1916 o2hb_fill_node_map_from_callback(testing_map
, sizeof(testing_map
));
1917 if (!test_bit(node_num
, testing_map
)) {
1919 "node (%u) does not have heartbeating enabled.\n",
1926 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback
);
1928 /* Makes sure our local node is configured with a node number, and is
1930 int o2hb_check_local_node_heartbeating(void)
1934 /* if this node was set then we have networking */
1935 node_num
= o2nm_this_node();
1936 if (node_num
== O2NM_MAX_NODES
) {
1937 mlog(ML_HEARTBEAT
, "this node has not been configured.\n");
1941 return o2hb_check_node_heartbeating(node_num
);
1943 EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating
);
1946 * this is just a hack until we get the plumbing which flips file systems
1947 * read only and drops the hb ref instead of killing the node dead.
1949 void o2hb_stop_all_regions(void)
1951 struct o2hb_region
*reg
;
1953 mlog(ML_ERROR
, "stopping heartbeat on all active regions.\n");
1955 spin_lock(&o2hb_live_lock
);
1957 list_for_each_entry(reg
, &o2hb_all_regions
, hr_all_item
)
1958 reg
->hr_unclean_stop
= 1;
1960 spin_unlock(&o2hb_live_lock
);
1962 EXPORT_SYMBOL_GPL(o2hb_stop_all_regions
);