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>
37 #include <linux/slab.h>
39 #include "heartbeat.h"
41 #include "nodemanager.h"
48 * The first heartbeat pass had one global thread that would serialize all hb
49 * callback calls. This global serializing sem should only be removed once
50 * we've made sure that all callees can deal with being called concurrently
51 * from multiple hb region threads.
53 static DECLARE_RWSEM(o2hb_callback_sem
);
56 * multiple hb threads are watching multiple regions. A node is live
57 * whenever any of the threads sees activity from the node in its region.
59 static DEFINE_SPINLOCK(o2hb_live_lock
);
60 static struct list_head o2hb_live_slots
[O2NM_MAX_NODES
];
61 static unsigned long o2hb_live_node_bitmap
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
62 static LIST_HEAD(o2hb_node_events
);
63 static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue
);
65 #define O2HB_DEBUG_DIR "o2hb"
66 #define O2HB_DEBUG_LIVENODES "livenodes"
67 static struct dentry
*o2hb_debug_dir
;
68 static struct dentry
*o2hb_debug_livenodes
;
70 static LIST_HEAD(o2hb_all_regions
);
72 static struct o2hb_callback
{
73 struct list_head list
;
74 } o2hb_callbacks
[O2HB_NUM_CB
];
76 static struct o2hb_callback
*hbcall_from_type(enum o2hb_callback_type type
);
78 #define O2HB_DEFAULT_BLOCK_BITS 9
80 unsigned int o2hb_dead_threshold
= O2HB_DEFAULT_DEAD_THRESHOLD
;
82 /* Only sets a new threshold if there are no active regions.
84 * No locking or otherwise interesting code is required for reading
85 * o2hb_dead_threshold as it can't change once regions are active and
86 * it's not interesting to anyone until then anyway. */
87 static void o2hb_dead_threshold_set(unsigned int threshold
)
89 if (threshold
> O2HB_MIN_DEAD_THRESHOLD
) {
90 spin_lock(&o2hb_live_lock
);
91 if (list_empty(&o2hb_all_regions
))
92 o2hb_dead_threshold
= threshold
;
93 spin_unlock(&o2hb_live_lock
);
97 struct o2hb_node_event
{
98 struct list_head hn_item
;
99 enum o2hb_callback_type hn_event_type
;
100 struct o2nm_node
*hn_node
;
104 struct o2hb_disk_slot
{
105 struct o2hb_disk_heartbeat_block
*ds_raw_block
;
108 u64 ds_last_generation
;
109 u16 ds_equal_samples
;
110 u16 ds_changed_samples
;
111 struct list_head ds_live_item
;
114 /* each thread owns a region.. when we're asked to tear down the region
115 * we ask the thread to stop, who cleans up the region */
117 struct config_item hr_item
;
119 struct list_head hr_all_item
;
120 unsigned hr_unclean_stop
:1;
122 /* protected by the hr_callback_sem */
123 struct task_struct
*hr_task
;
125 unsigned int hr_blocks
;
126 unsigned long long hr_start_block
;
128 unsigned int hr_block_bits
;
129 unsigned int hr_block_bytes
;
131 unsigned int hr_slots_per_page
;
132 unsigned int hr_num_pages
;
134 struct page
**hr_slot_data
;
135 struct block_device
*hr_bdev
;
136 struct o2hb_disk_slot
*hr_slots
;
138 /* let the person setting up hb wait for it to return until it
139 * has reached a 'steady' state. This will be fixed when we have
140 * a more complete api that doesn't lead to this sort of fragility. */
141 atomic_t hr_steady_iterations
;
143 char hr_dev_name
[BDEVNAME_SIZE
];
145 unsigned int hr_timeout_ms
;
147 /* randomized as the region goes up and down so that a node
148 * recognizes a node going up and down in one iteration */
151 struct delayed_work hr_write_timeout_work
;
152 unsigned long hr_last_timeout_start
;
154 /* Used during o2hb_check_slot to hold a copy of the block
155 * being checked because we temporarily have to zero out the
157 struct o2hb_disk_heartbeat_block
*hr_tmp_block
;
160 struct o2hb_bio_wait_ctxt
{
161 atomic_t wc_num_reqs
;
162 struct completion wc_io_complete
;
166 static void o2hb_write_timeout(struct work_struct
*work
)
168 struct o2hb_region
*reg
=
169 container_of(work
, struct o2hb_region
,
170 hr_write_timeout_work
.work
);
172 mlog(ML_ERROR
, "Heartbeat write timeout to device %s after %u "
173 "milliseconds\n", reg
->hr_dev_name
,
174 jiffies_to_msecs(jiffies
- reg
->hr_last_timeout_start
));
175 o2quo_disk_timeout();
178 static void o2hb_arm_write_timeout(struct o2hb_region
*reg
)
180 mlog(ML_HEARTBEAT
, "Queue write timeout for %u ms\n",
181 O2HB_MAX_WRITE_TIMEOUT_MS
);
183 cancel_delayed_work(®
->hr_write_timeout_work
);
184 reg
->hr_last_timeout_start
= jiffies
;
185 schedule_delayed_work(®
->hr_write_timeout_work
,
186 msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS
));
189 static void o2hb_disarm_write_timeout(struct o2hb_region
*reg
)
191 cancel_delayed_work(®
->hr_write_timeout_work
);
192 flush_scheduled_work();
195 static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt
*wc
)
197 atomic_set(&wc
->wc_num_reqs
, 1);
198 init_completion(&wc
->wc_io_complete
);
202 /* Used in error paths too */
203 static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt
*wc
,
206 /* sadly atomic_sub_and_test() isn't available on all platforms. The
207 * good news is that the fast path only completes one at a time */
209 if (atomic_dec_and_test(&wc
->wc_num_reqs
)) {
211 complete(&wc
->wc_io_complete
);
216 static void o2hb_wait_on_io(struct o2hb_region
*reg
,
217 struct o2hb_bio_wait_ctxt
*wc
)
219 struct address_space
*mapping
= reg
->hr_bdev
->bd_inode
->i_mapping
;
221 blk_run_address_space(mapping
);
222 o2hb_bio_wait_dec(wc
, 1);
224 wait_for_completion(&wc
->wc_io_complete
);
227 static void o2hb_bio_end_io(struct bio
*bio
,
230 struct o2hb_bio_wait_ctxt
*wc
= bio
->bi_private
;
233 mlog(ML_ERROR
, "IO Error %d\n", error
);
234 wc
->wc_error
= error
;
237 o2hb_bio_wait_dec(wc
, 1);
241 /* Setup a Bio to cover I/O against num_slots slots starting at
243 static struct bio
*o2hb_setup_one_bio(struct o2hb_region
*reg
,
244 struct o2hb_bio_wait_ctxt
*wc
,
245 unsigned int *current_slot
,
246 unsigned int max_slots
)
248 int len
, current_page
;
249 unsigned int vec_len
, vec_start
;
250 unsigned int bits
= reg
->hr_block_bits
;
251 unsigned int spp
= reg
->hr_slots_per_page
;
252 unsigned int cs
= *current_slot
;
256 /* Testing has shown this allocation to take long enough under
257 * GFP_KERNEL that the local node can get fenced. It would be
258 * nicest if we could pre-allocate these bios and avoid this
260 bio
= bio_alloc(GFP_ATOMIC
, 16);
262 mlog(ML_ERROR
, "Could not alloc slots BIO!\n");
263 bio
= ERR_PTR(-ENOMEM
);
267 /* Must put everything in 512 byte sectors for the bio... */
268 bio
->bi_sector
= (reg
->hr_start_block
+ cs
) << (bits
- 9);
269 bio
->bi_bdev
= reg
->hr_bdev
;
270 bio
->bi_private
= wc
;
271 bio
->bi_end_io
= o2hb_bio_end_io
;
273 vec_start
= (cs
<< bits
) % PAGE_CACHE_SIZE
;
274 while(cs
< max_slots
) {
275 current_page
= cs
/ spp
;
276 page
= reg
->hr_slot_data
[current_page
];
278 vec_len
= min(PAGE_CACHE_SIZE
- vec_start
,
279 (max_slots
-cs
) * (PAGE_CACHE_SIZE
/spp
) );
281 mlog(ML_HB_BIO
, "page %d, vec_len = %u, vec_start = %u\n",
282 current_page
, vec_len
, vec_start
);
284 len
= bio_add_page(bio
, page
, vec_len
, vec_start
);
285 if (len
!= vec_len
) break;
287 cs
+= vec_len
/ (PAGE_CACHE_SIZE
/spp
);
296 static int o2hb_read_slots(struct o2hb_region
*reg
,
297 unsigned int max_slots
)
299 unsigned int current_slot
=0;
301 struct o2hb_bio_wait_ctxt wc
;
304 o2hb_bio_wait_init(&wc
);
306 while(current_slot
< max_slots
) {
307 bio
= o2hb_setup_one_bio(reg
, &wc
, ¤t_slot
, max_slots
);
309 status
= PTR_ERR(bio
);
314 atomic_inc(&wc
.wc_num_reqs
);
315 submit_bio(READ
, bio
);
321 o2hb_wait_on_io(reg
, &wc
);
322 if (wc
.wc_error
&& !status
)
323 status
= wc
.wc_error
;
328 static int o2hb_issue_node_write(struct o2hb_region
*reg
,
329 struct o2hb_bio_wait_ctxt
*write_wc
)
335 o2hb_bio_wait_init(write_wc
);
337 slot
= o2nm_this_node();
339 bio
= o2hb_setup_one_bio(reg
, write_wc
, &slot
, slot
+1);
341 status
= PTR_ERR(bio
);
346 atomic_inc(&write_wc
->wc_num_reqs
);
347 submit_bio(WRITE
, bio
);
354 static u32
o2hb_compute_block_crc_le(struct o2hb_region
*reg
,
355 struct o2hb_disk_heartbeat_block
*hb_block
)
360 /* We want to compute the block crc with a 0 value in the
361 * hb_cksum field. Save it off here and replace after the
363 old_cksum
= hb_block
->hb_cksum
;
364 hb_block
->hb_cksum
= 0;
366 ret
= crc32_le(0, (unsigned char *) hb_block
, reg
->hr_block_bytes
);
368 hb_block
->hb_cksum
= old_cksum
;
373 static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block
*hb_block
)
375 mlog(ML_ERROR
, "Dump slot information: seq = 0x%llx, node = %u, "
376 "cksum = 0x%x, generation 0x%llx\n",
377 (long long)le64_to_cpu(hb_block
->hb_seq
),
378 hb_block
->hb_node
, le32_to_cpu(hb_block
->hb_cksum
),
379 (long long)le64_to_cpu(hb_block
->hb_generation
));
382 static int o2hb_verify_crc(struct o2hb_region
*reg
,
383 struct o2hb_disk_heartbeat_block
*hb_block
)
387 read
= le32_to_cpu(hb_block
->hb_cksum
);
388 computed
= o2hb_compute_block_crc_le(reg
, hb_block
);
390 return read
== computed
;
393 /* We want to make sure that nobody is heartbeating on top of us --
394 * this will help detect an invalid configuration. */
395 static int o2hb_check_last_timestamp(struct o2hb_region
*reg
)
398 struct o2hb_disk_slot
*slot
;
399 struct o2hb_disk_heartbeat_block
*hb_block
;
401 node_num
= o2nm_this_node();
404 slot
= ®
->hr_slots
[node_num
];
405 /* Don't check on our 1st timestamp */
406 if (slot
->ds_last_time
) {
407 hb_block
= slot
->ds_raw_block
;
409 if (le64_to_cpu(hb_block
->hb_seq
) != slot
->ds_last_time
)
416 static inline void o2hb_prepare_block(struct o2hb_region
*reg
,
421 struct o2hb_disk_slot
*slot
;
422 struct o2hb_disk_heartbeat_block
*hb_block
;
424 node_num
= o2nm_this_node();
425 slot
= ®
->hr_slots
[node_num
];
427 hb_block
= (struct o2hb_disk_heartbeat_block
*)slot
->ds_raw_block
;
428 memset(hb_block
, 0, reg
->hr_block_bytes
);
429 /* TODO: time stuff */
430 cputime
= CURRENT_TIME
.tv_sec
;
434 hb_block
->hb_seq
= cpu_to_le64(cputime
);
435 hb_block
->hb_node
= node_num
;
436 hb_block
->hb_generation
= cpu_to_le64(generation
);
437 hb_block
->hb_dead_ms
= cpu_to_le32(o2hb_dead_threshold
* O2HB_REGION_TIMEOUT_MS
);
439 /* This step must always happen last! */
440 hb_block
->hb_cksum
= cpu_to_le32(o2hb_compute_block_crc_le(reg
,
443 mlog(ML_HB_BIO
, "our node generation = 0x%llx, cksum = 0x%x\n",
444 (long long)generation
,
445 le32_to_cpu(hb_block
->hb_cksum
));
448 static void o2hb_fire_callbacks(struct o2hb_callback
*hbcall
,
449 struct o2nm_node
*node
,
452 struct list_head
*iter
;
453 struct o2hb_callback_func
*f
;
455 list_for_each(iter
, &hbcall
->list
) {
456 f
= list_entry(iter
, struct o2hb_callback_func
, hc_item
);
457 mlog(ML_HEARTBEAT
, "calling funcs %p\n", f
);
458 (f
->hc_func
)(node
, idx
, f
->hc_data
);
462 /* Will run the list in order until we process the passed event */
463 static void o2hb_run_event_list(struct o2hb_node_event
*queued_event
)
466 struct o2hb_callback
*hbcall
;
467 struct o2hb_node_event
*event
;
469 spin_lock(&o2hb_live_lock
);
470 empty
= list_empty(&queued_event
->hn_item
);
471 spin_unlock(&o2hb_live_lock
);
475 /* Holding callback sem assures we don't alter the callback
476 * lists when doing this, and serializes ourselves with other
477 * processes wanting callbacks. */
478 down_write(&o2hb_callback_sem
);
480 spin_lock(&o2hb_live_lock
);
481 while (!list_empty(&o2hb_node_events
)
482 && !list_empty(&queued_event
->hn_item
)) {
483 event
= list_entry(o2hb_node_events
.next
,
484 struct o2hb_node_event
,
486 list_del_init(&event
->hn_item
);
487 spin_unlock(&o2hb_live_lock
);
489 mlog(ML_HEARTBEAT
, "Node %s event for %d\n",
490 event
->hn_event_type
== O2HB_NODE_UP_CB
? "UP" : "DOWN",
493 hbcall
= hbcall_from_type(event
->hn_event_type
);
495 /* We should *never* have gotten on to the list with a
496 * bad type... This isn't something that we should try
497 * to recover from. */
498 BUG_ON(IS_ERR(hbcall
));
500 o2hb_fire_callbacks(hbcall
, event
->hn_node
, event
->hn_node_num
);
502 spin_lock(&o2hb_live_lock
);
504 spin_unlock(&o2hb_live_lock
);
506 up_write(&o2hb_callback_sem
);
509 static void o2hb_queue_node_event(struct o2hb_node_event
*event
,
510 enum o2hb_callback_type type
,
511 struct o2nm_node
*node
,
514 assert_spin_locked(&o2hb_live_lock
);
516 event
->hn_event_type
= type
;
517 event
->hn_node
= node
;
518 event
->hn_node_num
= node_num
;
520 mlog(ML_HEARTBEAT
, "Queue node %s event for node %d\n",
521 type
== O2HB_NODE_UP_CB
? "UP" : "DOWN", node_num
);
523 list_add_tail(&event
->hn_item
, &o2hb_node_events
);
526 static void o2hb_shutdown_slot(struct o2hb_disk_slot
*slot
)
528 struct o2hb_node_event event
=
529 { .hn_item
= LIST_HEAD_INIT(event
.hn_item
), };
530 struct o2nm_node
*node
;
532 node
= o2nm_get_node_by_num(slot
->ds_node_num
);
536 spin_lock(&o2hb_live_lock
);
537 if (!list_empty(&slot
->ds_live_item
)) {
538 mlog(ML_HEARTBEAT
, "Shutdown, node %d leaves region\n",
541 list_del_init(&slot
->ds_live_item
);
543 if (list_empty(&o2hb_live_slots
[slot
->ds_node_num
])) {
544 clear_bit(slot
->ds_node_num
, o2hb_live_node_bitmap
);
546 o2hb_queue_node_event(&event
, O2HB_NODE_DOWN_CB
, node
,
550 spin_unlock(&o2hb_live_lock
);
552 o2hb_run_event_list(&event
);
557 static int o2hb_check_slot(struct o2hb_region
*reg
,
558 struct o2hb_disk_slot
*slot
)
560 int changed
= 0, gen_changed
= 0;
561 struct o2hb_node_event event
=
562 { .hn_item
= LIST_HEAD_INIT(event
.hn_item
), };
563 struct o2nm_node
*node
;
564 struct o2hb_disk_heartbeat_block
*hb_block
= reg
->hr_tmp_block
;
566 unsigned int dead_ms
= o2hb_dead_threshold
* O2HB_REGION_TIMEOUT_MS
;
567 unsigned int slot_dead_ms
;
569 memcpy(hb_block
, slot
->ds_raw_block
, reg
->hr_block_bytes
);
571 /* Is this correct? Do we assume that the node doesn't exist
572 * if we're not configured for him? */
573 node
= o2nm_get_node_by_num(slot
->ds_node_num
);
577 if (!o2hb_verify_crc(reg
, hb_block
)) {
578 /* all paths from here will drop o2hb_live_lock for
580 spin_lock(&o2hb_live_lock
);
582 /* Don't print an error on the console in this case -
583 * a freshly formatted heartbeat area will not have a
585 if (list_empty(&slot
->ds_live_item
))
588 /* The node is live but pushed out a bad crc. We
589 * consider it a transient miss but don't populate any
590 * other values as they may be junk. */
591 mlog(ML_ERROR
, "Node %d has written a bad crc to %s\n",
592 slot
->ds_node_num
, reg
->hr_dev_name
);
593 o2hb_dump_slot(hb_block
);
595 slot
->ds_equal_samples
++;
599 /* we don't care if these wrap.. the state transitions below
600 * clear at the right places */
601 cputime
= le64_to_cpu(hb_block
->hb_seq
);
602 if (slot
->ds_last_time
!= cputime
)
603 slot
->ds_changed_samples
++;
605 slot
->ds_equal_samples
++;
606 slot
->ds_last_time
= cputime
;
608 /* The node changed heartbeat generations. We assume this to
609 * mean it dropped off but came back before we timed out. We
610 * want to consider it down for the time being but don't want
611 * to lose any changed_samples state we might build up to
612 * considering it live again. */
613 if (slot
->ds_last_generation
!= le64_to_cpu(hb_block
->hb_generation
)) {
615 slot
->ds_equal_samples
= 0;
616 mlog(ML_HEARTBEAT
, "Node %d changed generation (0x%llx "
617 "to 0x%llx)\n", slot
->ds_node_num
,
618 (long long)slot
->ds_last_generation
,
619 (long long)le64_to_cpu(hb_block
->hb_generation
));
622 slot
->ds_last_generation
= le64_to_cpu(hb_block
->hb_generation
);
624 mlog(ML_HEARTBEAT
, "Slot %d gen 0x%llx cksum 0x%x "
625 "seq %llu last %llu changed %u equal %u\n",
626 slot
->ds_node_num
, (long long)slot
->ds_last_generation
,
627 le32_to_cpu(hb_block
->hb_cksum
),
628 (unsigned long long)le64_to_cpu(hb_block
->hb_seq
),
629 (unsigned long long)slot
->ds_last_time
, slot
->ds_changed_samples
,
630 slot
->ds_equal_samples
);
632 spin_lock(&o2hb_live_lock
);
635 /* dead nodes only come to life after some number of
636 * changes at any time during their dead time */
637 if (list_empty(&slot
->ds_live_item
) &&
638 slot
->ds_changed_samples
>= O2HB_LIVE_THRESHOLD
) {
639 mlog(ML_HEARTBEAT
, "Node %d (id 0x%llx) joined my region\n",
640 slot
->ds_node_num
, (long long)slot
->ds_last_generation
);
642 /* first on the list generates a callback */
643 if (list_empty(&o2hb_live_slots
[slot
->ds_node_num
])) {
644 set_bit(slot
->ds_node_num
, o2hb_live_node_bitmap
);
646 o2hb_queue_node_event(&event
, O2HB_NODE_UP_CB
, node
,
652 list_add_tail(&slot
->ds_live_item
,
653 &o2hb_live_slots
[slot
->ds_node_num
]);
655 slot
->ds_equal_samples
= 0;
657 /* We want to be sure that all nodes agree on the
658 * number of milliseconds before a node will be
659 * considered dead. The self-fencing timeout is
660 * computed from this value, and a discrepancy might
661 * result in heartbeat calling a node dead when it
662 * hasn't self-fenced yet. */
663 slot_dead_ms
= le32_to_cpu(hb_block
->hb_dead_ms
);
664 if (slot_dead_ms
&& slot_dead_ms
!= dead_ms
) {
665 /* TODO: Perhaps we can fail the region here. */
666 mlog(ML_ERROR
, "Node %d on device %s has a dead count "
667 "of %u ms, but our count is %u ms.\n"
668 "Please double check your configuration values "
669 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
670 slot
->ds_node_num
, reg
->hr_dev_name
, slot_dead_ms
,
676 /* if the list is dead, we're done.. */
677 if (list_empty(&slot
->ds_live_item
))
680 /* live nodes only go dead after enough consequtive missed
681 * samples.. reset the missed counter whenever we see
683 if (slot
->ds_equal_samples
>= o2hb_dead_threshold
|| gen_changed
) {
684 mlog(ML_HEARTBEAT
, "Node %d left my region\n",
687 /* last off the live_slot generates a callback */
688 list_del_init(&slot
->ds_live_item
);
689 if (list_empty(&o2hb_live_slots
[slot
->ds_node_num
])) {
690 clear_bit(slot
->ds_node_num
, o2hb_live_node_bitmap
);
692 o2hb_queue_node_event(&event
, O2HB_NODE_DOWN_CB
, node
,
698 /* We don't clear this because the node is still
699 * actually writing new blocks. */
701 slot
->ds_changed_samples
= 0;
704 if (slot
->ds_changed_samples
) {
705 slot
->ds_changed_samples
= 0;
706 slot
->ds_equal_samples
= 0;
709 spin_unlock(&o2hb_live_lock
);
711 o2hb_run_event_list(&event
);
717 /* This could be faster if we just implmented a find_last_bit, but I
718 * don't think the circumstances warrant it. */
719 static int o2hb_highest_node(unsigned long *nodes
,
726 while ((node
= find_next_bit(nodes
, numbits
, node
+ 1)) != -1) {
736 static int o2hb_do_disk_heartbeat(struct o2hb_region
*reg
)
738 int i
, ret
, highest_node
, change
= 0;
739 unsigned long configured_nodes
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
740 struct o2hb_bio_wait_ctxt write_wc
;
742 ret
= o2nm_configured_node_map(configured_nodes
,
743 sizeof(configured_nodes
));
749 highest_node
= o2hb_highest_node(configured_nodes
, O2NM_MAX_NODES
);
750 if (highest_node
>= O2NM_MAX_NODES
) {
751 mlog(ML_NOTICE
, "ocfs2_heartbeat: no configured nodes found!\n");
755 /* No sense in reading the slots of nodes that don't exist
756 * yet. Of course, if the node definitions have holes in them
757 * then we're reading an empty slot anyway... Consider this
759 ret
= o2hb_read_slots(reg
, highest_node
+ 1);
765 /* With an up to date view of the slots, we can check that no
766 * other node has been improperly configured to heartbeat in
768 if (!o2hb_check_last_timestamp(reg
))
769 mlog(ML_ERROR
, "Device \"%s\": another node is heartbeating "
770 "in our slot!\n", reg
->hr_dev_name
);
772 /* fill in the proper info for our next heartbeat */
773 o2hb_prepare_block(reg
, reg
->hr_generation
);
775 /* And fire off the write. Note that we don't wait on this I/O
777 ret
= o2hb_issue_node_write(reg
, &write_wc
);
784 while((i
= find_next_bit(configured_nodes
, O2NM_MAX_NODES
, i
+ 1)) < O2NM_MAX_NODES
) {
786 change
|= o2hb_check_slot(reg
, ®
->hr_slots
[i
]);
790 * We have to be sure we've advertised ourselves on disk
791 * before we can go to steady state. This ensures that
792 * people we find in our steady state have seen us.
794 o2hb_wait_on_io(reg
, &write_wc
);
795 if (write_wc
.wc_error
) {
796 /* Do not re-arm the write timeout on I/O error - we
797 * can't be sure that the new block ever made it to
799 mlog(ML_ERROR
, "Write error %d on device \"%s\"\n",
800 write_wc
.wc_error
, reg
->hr_dev_name
);
801 return write_wc
.wc_error
;
804 o2hb_arm_write_timeout(reg
);
806 /* let the person who launched us know when things are steady */
807 if (!change
&& (atomic_read(®
->hr_steady_iterations
) != 0)) {
808 if (atomic_dec_and_test(®
->hr_steady_iterations
))
809 wake_up(&o2hb_steady_queue
);
815 /* Subtract b from a, storing the result in a. a *must* have a larger
817 static void o2hb_tv_subtract(struct timeval
*a
,
820 /* just return 0 when a is after b */
821 if (a
->tv_sec
< b
->tv_sec
||
822 (a
->tv_sec
== b
->tv_sec
&& a
->tv_usec
< b
->tv_usec
)) {
828 a
->tv_sec
-= b
->tv_sec
;
829 a
->tv_usec
-= b
->tv_usec
;
830 while ( a
->tv_usec
< 0 ) {
832 a
->tv_usec
+= 1000000;
836 static unsigned int o2hb_elapsed_msecs(struct timeval
*start
,
839 struct timeval res
= *end
;
841 o2hb_tv_subtract(&res
, start
);
843 return res
.tv_sec
* 1000 + res
.tv_usec
/ 1000;
847 * we ride the region ref that the region dir holds. before the region
848 * dir is removed and drops it ref it will wait to tear down this
851 static int o2hb_thread(void *data
)
854 struct o2hb_region
*reg
= data
;
855 struct o2hb_bio_wait_ctxt write_wc
;
856 struct timeval before_hb
, after_hb
;
857 unsigned int elapsed_msec
;
859 mlog(ML_HEARTBEAT
|ML_KTHREAD
, "hb thread running\n");
861 set_user_nice(current
, -20);
863 while (!kthread_should_stop() && !reg
->hr_unclean_stop
) {
864 /* We track the time spent inside
865 * o2hb_do_disk_heartbeat so that we avoid more than
866 * hr_timeout_ms between disk writes. On busy systems
867 * this should result in a heartbeat which is less
868 * likely to time itself out. */
869 do_gettimeofday(&before_hb
);
873 ret
= o2hb_do_disk_heartbeat(reg
);
874 } while (ret
&& ++i
< 2);
876 do_gettimeofday(&after_hb
);
877 elapsed_msec
= o2hb_elapsed_msecs(&before_hb
, &after_hb
);
880 "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
881 before_hb
.tv_sec
, (unsigned long) before_hb
.tv_usec
,
882 after_hb
.tv_sec
, (unsigned long) after_hb
.tv_usec
,
885 if (elapsed_msec
< reg
->hr_timeout_ms
) {
886 /* the kthread api has blocked signals for us so no
887 * need to record the return value. */
888 msleep_interruptible(reg
->hr_timeout_ms
- elapsed_msec
);
892 o2hb_disarm_write_timeout(reg
);
894 /* unclean stop is only used in very bad situation */
895 for(i
= 0; !reg
->hr_unclean_stop
&& i
< reg
->hr_blocks
; i
++)
896 o2hb_shutdown_slot(®
->hr_slots
[i
]);
898 o2hb_prepare_block(reg
, 0);
899 ret
= o2hb_issue_node_write(reg
, &write_wc
);
901 o2hb_wait_on_io(reg
, &write_wc
);
906 mlog(ML_HEARTBEAT
|ML_KTHREAD
, "hb thread exiting\n");
911 #ifdef CONFIG_DEBUG_FS
912 static int o2hb_debug_open(struct inode
*inode
, struct file
*file
)
914 unsigned long map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
919 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
923 o2hb_fill_node_map(map
, sizeof(map
));
925 while ((i
= find_next_bit(map
, O2NM_MAX_NODES
, i
+ 1)) < O2NM_MAX_NODES
)
926 out
+= snprintf(buf
+ out
, PAGE_SIZE
- out
, "%d ", i
);
927 out
+= snprintf(buf
+ out
, PAGE_SIZE
- out
, "\n");
929 i_size_write(inode
, out
);
931 file
->private_data
= buf
;
938 static int o2hb_debug_release(struct inode
*inode
, struct file
*file
)
940 kfree(file
->private_data
);
944 static ssize_t
o2hb_debug_read(struct file
*file
, char __user
*buf
,
945 size_t nbytes
, loff_t
*ppos
)
947 return simple_read_from_buffer(buf
, nbytes
, ppos
, file
->private_data
,
948 i_size_read(file
->f_mapping
->host
));
951 static int o2hb_debug_open(struct inode
*inode
, struct file
*file
)
955 static int o2hb_debug_release(struct inode
*inode
, struct file
*file
)
959 static ssize_t
o2hb_debug_read(struct file
*file
, char __user
*buf
,
960 size_t nbytes
, loff_t
*ppos
)
964 #endif /* CONFIG_DEBUG_FS */
966 static const struct file_operations o2hb_debug_fops
= {
967 .open
= o2hb_debug_open
,
968 .release
= o2hb_debug_release
,
969 .read
= o2hb_debug_read
,
970 .llseek
= generic_file_llseek
,
975 if (o2hb_debug_livenodes
)
976 debugfs_remove(o2hb_debug_livenodes
);
978 debugfs_remove(o2hb_debug_dir
);
985 for (i
= 0; i
< ARRAY_SIZE(o2hb_callbacks
); i
++)
986 INIT_LIST_HEAD(&o2hb_callbacks
[i
].list
);
988 for (i
= 0; i
< ARRAY_SIZE(o2hb_live_slots
); i
++)
989 INIT_LIST_HEAD(&o2hb_live_slots
[i
]);
991 INIT_LIST_HEAD(&o2hb_node_events
);
993 memset(o2hb_live_node_bitmap
, 0, sizeof(o2hb_live_node_bitmap
));
995 o2hb_debug_dir
= debugfs_create_dir(O2HB_DEBUG_DIR
, NULL
);
996 if (!o2hb_debug_dir
) {
1001 o2hb_debug_livenodes
= debugfs_create_file(O2HB_DEBUG_LIVENODES
,
1003 o2hb_debug_dir
, NULL
,
1005 if (!o2hb_debug_livenodes
) {
1006 mlog_errno(-ENOMEM
);
1007 debugfs_remove(o2hb_debug_dir
);
1014 /* if we're already in a callback then we're already serialized by the sem */
1015 static void o2hb_fill_node_map_from_callback(unsigned long *map
,
1018 BUG_ON(bytes
< (BITS_TO_LONGS(O2NM_MAX_NODES
) * sizeof(unsigned long)));
1020 memcpy(map
, &o2hb_live_node_bitmap
, bytes
);
1024 * get a map of all nodes that are heartbeating in any regions
1026 void o2hb_fill_node_map(unsigned long *map
, unsigned bytes
)
1028 /* callers want to serialize this map and callbacks so that they
1029 * can trust that they don't miss nodes coming to the party */
1030 down_read(&o2hb_callback_sem
);
1031 spin_lock(&o2hb_live_lock
);
1032 o2hb_fill_node_map_from_callback(map
, bytes
);
1033 spin_unlock(&o2hb_live_lock
);
1034 up_read(&o2hb_callback_sem
);
1036 EXPORT_SYMBOL_GPL(o2hb_fill_node_map
);
1039 * heartbeat configfs bits. The heartbeat set is a default set under
1040 * the cluster set in nodemanager.c.
1043 static struct o2hb_region
*to_o2hb_region(struct config_item
*item
)
1045 return item
? container_of(item
, struct o2hb_region
, hr_item
) : NULL
;
1048 /* drop_item only drops its ref after killing the thread, nothing should
1049 * be using the region anymore. this has to clean up any state that
1050 * attributes might have built up. */
1051 static void o2hb_region_release(struct config_item
*item
)
1055 struct o2hb_region
*reg
= to_o2hb_region(item
);
1057 if (reg
->hr_tmp_block
)
1058 kfree(reg
->hr_tmp_block
);
1060 if (reg
->hr_slot_data
) {
1061 for (i
= 0; i
< reg
->hr_num_pages
; i
++) {
1062 page
= reg
->hr_slot_data
[i
];
1066 kfree(reg
->hr_slot_data
);
1070 blkdev_put(reg
->hr_bdev
, FMODE_READ
|FMODE_WRITE
);
1073 kfree(reg
->hr_slots
);
1075 spin_lock(&o2hb_live_lock
);
1076 list_del(®
->hr_all_item
);
1077 spin_unlock(&o2hb_live_lock
);
1082 static int o2hb_read_block_input(struct o2hb_region
*reg
,
1085 unsigned long *ret_bytes
,
1086 unsigned int *ret_bits
)
1088 unsigned long bytes
;
1089 char *p
= (char *)page
;
1091 bytes
= simple_strtoul(p
, &p
, 0);
1092 if (!p
|| (*p
&& (*p
!= '\n')))
1095 /* Heartbeat and fs min / max block sizes are the same. */
1096 if (bytes
> 4096 || bytes
< 512)
1098 if (hweight16(bytes
) != 1)
1104 *ret_bits
= ffs(bytes
) - 1;
1109 static ssize_t
o2hb_region_block_bytes_read(struct o2hb_region
*reg
,
1112 return sprintf(page
, "%u\n", reg
->hr_block_bytes
);
1115 static ssize_t
o2hb_region_block_bytes_write(struct o2hb_region
*reg
,
1120 unsigned long block_bytes
;
1121 unsigned int block_bits
;
1126 status
= o2hb_read_block_input(reg
, page
, count
,
1127 &block_bytes
, &block_bits
);
1131 reg
->hr_block_bytes
= (unsigned int)block_bytes
;
1132 reg
->hr_block_bits
= block_bits
;
1137 static ssize_t
o2hb_region_start_block_read(struct o2hb_region
*reg
,
1140 return sprintf(page
, "%llu\n", reg
->hr_start_block
);
1143 static ssize_t
o2hb_region_start_block_write(struct o2hb_region
*reg
,
1147 unsigned long long tmp
;
1148 char *p
= (char *)page
;
1153 tmp
= simple_strtoull(p
, &p
, 0);
1154 if (!p
|| (*p
&& (*p
!= '\n')))
1157 reg
->hr_start_block
= tmp
;
1162 static ssize_t
o2hb_region_blocks_read(struct o2hb_region
*reg
,
1165 return sprintf(page
, "%d\n", reg
->hr_blocks
);
1168 static ssize_t
o2hb_region_blocks_write(struct o2hb_region
*reg
,
1173 char *p
= (char *)page
;
1178 tmp
= simple_strtoul(p
, &p
, 0);
1179 if (!p
|| (*p
&& (*p
!= '\n')))
1182 if (tmp
> O2NM_MAX_NODES
|| tmp
== 0)
1185 reg
->hr_blocks
= (unsigned int)tmp
;
1190 static ssize_t
o2hb_region_dev_read(struct o2hb_region
*reg
,
1193 unsigned int ret
= 0;
1196 ret
= sprintf(page
, "%s\n", reg
->hr_dev_name
);
1201 static void o2hb_init_region_params(struct o2hb_region
*reg
)
1203 reg
->hr_slots_per_page
= PAGE_CACHE_SIZE
>> reg
->hr_block_bits
;
1204 reg
->hr_timeout_ms
= O2HB_REGION_TIMEOUT_MS
;
1206 mlog(ML_HEARTBEAT
, "hr_start_block = %llu, hr_blocks = %u\n",
1207 reg
->hr_start_block
, reg
->hr_blocks
);
1208 mlog(ML_HEARTBEAT
, "hr_block_bytes = %u, hr_block_bits = %u\n",
1209 reg
->hr_block_bytes
, reg
->hr_block_bits
);
1210 mlog(ML_HEARTBEAT
, "hr_timeout_ms = %u\n", reg
->hr_timeout_ms
);
1211 mlog(ML_HEARTBEAT
, "dead threshold = %u\n", o2hb_dead_threshold
);
1214 static int o2hb_map_slot_data(struct o2hb_region
*reg
)
1217 unsigned int last_slot
;
1218 unsigned int spp
= reg
->hr_slots_per_page
;
1221 struct o2hb_disk_slot
*slot
;
1223 reg
->hr_tmp_block
= kmalloc(reg
->hr_block_bytes
, GFP_KERNEL
);
1224 if (reg
->hr_tmp_block
== NULL
) {
1225 mlog_errno(-ENOMEM
);
1229 reg
->hr_slots
= kcalloc(reg
->hr_blocks
,
1230 sizeof(struct o2hb_disk_slot
), GFP_KERNEL
);
1231 if (reg
->hr_slots
== NULL
) {
1232 mlog_errno(-ENOMEM
);
1236 for(i
= 0; i
< reg
->hr_blocks
; i
++) {
1237 slot
= ®
->hr_slots
[i
];
1238 slot
->ds_node_num
= i
;
1239 INIT_LIST_HEAD(&slot
->ds_live_item
);
1240 slot
->ds_raw_block
= NULL
;
1243 reg
->hr_num_pages
= (reg
->hr_blocks
+ spp
- 1) / spp
;
1244 mlog(ML_HEARTBEAT
, "Going to require %u pages to cover %u blocks "
1245 "at %u blocks per page\n",
1246 reg
->hr_num_pages
, reg
->hr_blocks
, spp
);
1248 reg
->hr_slot_data
= kcalloc(reg
->hr_num_pages
, sizeof(struct page
*),
1250 if (!reg
->hr_slot_data
) {
1251 mlog_errno(-ENOMEM
);
1255 for(i
= 0; i
< reg
->hr_num_pages
; i
++) {
1256 page
= alloc_page(GFP_KERNEL
);
1258 mlog_errno(-ENOMEM
);
1262 reg
->hr_slot_data
[i
] = page
;
1264 last_slot
= i
* spp
;
1265 raw
= page_address(page
);
1267 (j
< spp
) && ((j
+ last_slot
) < reg
->hr_blocks
);
1269 BUG_ON((j
+ last_slot
) >= reg
->hr_blocks
);
1271 slot
= ®
->hr_slots
[j
+ last_slot
];
1272 slot
->ds_raw_block
=
1273 (struct o2hb_disk_heartbeat_block
*) raw
;
1275 raw
+= reg
->hr_block_bytes
;
1282 /* Read in all the slots available and populate the tracking
1283 * structures so that we can start with a baseline idea of what's
1285 static int o2hb_populate_slot_data(struct o2hb_region
*reg
)
1288 struct o2hb_disk_slot
*slot
;
1289 struct o2hb_disk_heartbeat_block
*hb_block
;
1293 ret
= o2hb_read_slots(reg
, reg
->hr_blocks
);
1299 /* We only want to get an idea of the values initially in each
1300 * slot, so we do no verification - o2hb_check_slot will
1301 * actually determine if each configured slot is valid and
1302 * whether any values have changed. */
1303 for(i
= 0; i
< reg
->hr_blocks
; i
++) {
1304 slot
= ®
->hr_slots
[i
];
1305 hb_block
= (struct o2hb_disk_heartbeat_block
*) slot
->ds_raw_block
;
1307 /* Only fill the values that o2hb_check_slot uses to
1308 * determine changing slots */
1309 slot
->ds_last_time
= le64_to_cpu(hb_block
->hb_seq
);
1310 slot
->ds_last_generation
= le64_to_cpu(hb_block
->hb_generation
);
1318 /* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1319 static ssize_t
o2hb_region_dev_write(struct o2hb_region
*reg
,
1323 struct task_struct
*hb_task
;
1326 char *p
= (char *)page
;
1327 struct file
*filp
= NULL
;
1328 struct inode
*inode
= NULL
;
1329 ssize_t ret
= -EINVAL
;
1334 /* We can't heartbeat without having had our node number
1335 * configured yet. */
1336 if (o2nm_this_node() == O2NM_MAX_NODES
)
1339 fd
= simple_strtol(p
, &p
, 0);
1340 if (!p
|| (*p
&& (*p
!= '\n')))
1343 if (fd
< 0 || fd
>= INT_MAX
)
1350 if (reg
->hr_blocks
== 0 || reg
->hr_start_block
== 0 ||
1351 reg
->hr_block_bytes
== 0)
1354 inode
= igrab(filp
->f_mapping
->host
);
1358 if (!S_ISBLK(inode
->i_mode
))
1361 reg
->hr_bdev
= I_BDEV(filp
->f_mapping
->host
);
1362 ret
= blkdev_get(reg
->hr_bdev
, FMODE_WRITE
| FMODE_READ
);
1364 reg
->hr_bdev
= NULL
;
1369 bdevname(reg
->hr_bdev
, reg
->hr_dev_name
);
1371 sectsize
= bdev_logical_block_size(reg
->hr_bdev
);
1372 if (sectsize
!= reg
->hr_block_bytes
) {
1374 "blocksize %u incorrect for device, expected %d",
1375 reg
->hr_block_bytes
, sectsize
);
1380 o2hb_init_region_params(reg
);
1382 /* Generation of zero is invalid */
1384 get_random_bytes(®
->hr_generation
,
1385 sizeof(reg
->hr_generation
));
1386 } while (reg
->hr_generation
== 0);
1388 ret
= o2hb_map_slot_data(reg
);
1394 ret
= o2hb_populate_slot_data(reg
);
1400 INIT_DELAYED_WORK(®
->hr_write_timeout_work
, o2hb_write_timeout
);
1403 * A node is considered live after it has beat LIVE_THRESHOLD
1404 * times. We're not steady until we've given them a chance
1405 * _after_ our first read.
1407 atomic_set(®
->hr_steady_iterations
, O2HB_LIVE_THRESHOLD
+ 1);
1409 hb_task
= kthread_run(o2hb_thread
, reg
, "o2hb-%s",
1410 reg
->hr_item
.ci_name
);
1411 if (IS_ERR(hb_task
)) {
1412 ret
= PTR_ERR(hb_task
);
1417 spin_lock(&o2hb_live_lock
);
1418 reg
->hr_task
= hb_task
;
1419 spin_unlock(&o2hb_live_lock
);
1421 ret
= wait_event_interruptible(o2hb_steady_queue
,
1422 atomic_read(®
->hr_steady_iterations
) == 0);
1424 /* We got interrupted (hello ptrace!). Clean up */
1425 spin_lock(&o2hb_live_lock
);
1426 hb_task
= reg
->hr_task
;
1427 reg
->hr_task
= NULL
;
1428 spin_unlock(&o2hb_live_lock
);
1431 kthread_stop(hb_task
);
1435 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1436 spin_lock(&o2hb_live_lock
);
1437 hb_task
= reg
->hr_task
;
1438 spin_unlock(&o2hb_live_lock
);
1452 blkdev_put(reg
->hr_bdev
, FMODE_READ
|FMODE_WRITE
);
1453 reg
->hr_bdev
= NULL
;
1459 static ssize_t
o2hb_region_pid_read(struct o2hb_region
*reg
,
1464 spin_lock(&o2hb_live_lock
);
1466 pid
= task_pid_nr(reg
->hr_task
);
1467 spin_unlock(&o2hb_live_lock
);
1472 return sprintf(page
, "%u\n", pid
);
1475 struct o2hb_region_attribute
{
1476 struct configfs_attribute attr
;
1477 ssize_t (*show
)(struct o2hb_region
*, char *);
1478 ssize_t (*store
)(struct o2hb_region
*, const char *, size_t);
1481 static struct o2hb_region_attribute o2hb_region_attr_block_bytes
= {
1482 .attr
= { .ca_owner
= THIS_MODULE
,
1483 .ca_name
= "block_bytes",
1484 .ca_mode
= S_IRUGO
| S_IWUSR
},
1485 .show
= o2hb_region_block_bytes_read
,
1486 .store
= o2hb_region_block_bytes_write
,
1489 static struct o2hb_region_attribute o2hb_region_attr_start_block
= {
1490 .attr
= { .ca_owner
= THIS_MODULE
,
1491 .ca_name
= "start_block",
1492 .ca_mode
= S_IRUGO
| S_IWUSR
},
1493 .show
= o2hb_region_start_block_read
,
1494 .store
= o2hb_region_start_block_write
,
1497 static struct o2hb_region_attribute o2hb_region_attr_blocks
= {
1498 .attr
= { .ca_owner
= THIS_MODULE
,
1499 .ca_name
= "blocks",
1500 .ca_mode
= S_IRUGO
| S_IWUSR
},
1501 .show
= o2hb_region_blocks_read
,
1502 .store
= o2hb_region_blocks_write
,
1505 static struct o2hb_region_attribute o2hb_region_attr_dev
= {
1506 .attr
= { .ca_owner
= THIS_MODULE
,
1508 .ca_mode
= S_IRUGO
| S_IWUSR
},
1509 .show
= o2hb_region_dev_read
,
1510 .store
= o2hb_region_dev_write
,
1513 static struct o2hb_region_attribute o2hb_region_attr_pid
= {
1514 .attr
= { .ca_owner
= THIS_MODULE
,
1516 .ca_mode
= S_IRUGO
| S_IRUSR
},
1517 .show
= o2hb_region_pid_read
,
1520 static struct configfs_attribute
*o2hb_region_attrs
[] = {
1521 &o2hb_region_attr_block_bytes
.attr
,
1522 &o2hb_region_attr_start_block
.attr
,
1523 &o2hb_region_attr_blocks
.attr
,
1524 &o2hb_region_attr_dev
.attr
,
1525 &o2hb_region_attr_pid
.attr
,
1529 static ssize_t
o2hb_region_show(struct config_item
*item
,
1530 struct configfs_attribute
*attr
,
1533 struct o2hb_region
*reg
= to_o2hb_region(item
);
1534 struct o2hb_region_attribute
*o2hb_region_attr
=
1535 container_of(attr
, struct o2hb_region_attribute
, attr
);
1538 if (o2hb_region_attr
->show
)
1539 ret
= o2hb_region_attr
->show(reg
, page
);
1543 static ssize_t
o2hb_region_store(struct config_item
*item
,
1544 struct configfs_attribute
*attr
,
1545 const char *page
, size_t count
)
1547 struct o2hb_region
*reg
= to_o2hb_region(item
);
1548 struct o2hb_region_attribute
*o2hb_region_attr
=
1549 container_of(attr
, struct o2hb_region_attribute
, attr
);
1550 ssize_t ret
= -EINVAL
;
1552 if (o2hb_region_attr
->store
)
1553 ret
= o2hb_region_attr
->store(reg
, page
, count
);
1557 static struct configfs_item_operations o2hb_region_item_ops
= {
1558 .release
= o2hb_region_release
,
1559 .show_attribute
= o2hb_region_show
,
1560 .store_attribute
= o2hb_region_store
,
1563 static struct config_item_type o2hb_region_type
= {
1564 .ct_item_ops
= &o2hb_region_item_ops
,
1565 .ct_attrs
= o2hb_region_attrs
,
1566 .ct_owner
= THIS_MODULE
,
1571 struct o2hb_heartbeat_group
{
1572 struct config_group hs_group
;
1576 static struct o2hb_heartbeat_group
*to_o2hb_heartbeat_group(struct config_group
*group
)
1579 container_of(group
, struct o2hb_heartbeat_group
, hs_group
)
1583 static struct config_item
*o2hb_heartbeat_group_make_item(struct config_group
*group
,
1586 struct o2hb_region
*reg
= NULL
;
1588 reg
= kzalloc(sizeof(struct o2hb_region
), GFP_KERNEL
);
1590 return ERR_PTR(-ENOMEM
);
1592 config_item_init_type_name(®
->hr_item
, name
, &o2hb_region_type
);
1594 spin_lock(&o2hb_live_lock
);
1595 list_add_tail(®
->hr_all_item
, &o2hb_all_regions
);
1596 spin_unlock(&o2hb_live_lock
);
1598 return ®
->hr_item
;
1601 static void o2hb_heartbeat_group_drop_item(struct config_group
*group
,
1602 struct config_item
*item
)
1604 struct task_struct
*hb_task
;
1605 struct o2hb_region
*reg
= to_o2hb_region(item
);
1607 /* stop the thread when the user removes the region dir */
1608 spin_lock(&o2hb_live_lock
);
1609 hb_task
= reg
->hr_task
;
1610 reg
->hr_task
= NULL
;
1611 spin_unlock(&o2hb_live_lock
);
1614 kthread_stop(hb_task
);
1617 * If we're racing a dev_write(), we need to wake them. They will
1618 * check reg->hr_task
1620 if (atomic_read(®
->hr_steady_iterations
) != 0) {
1621 atomic_set(®
->hr_steady_iterations
, 0);
1622 wake_up(&o2hb_steady_queue
);
1625 config_item_put(item
);
1628 struct o2hb_heartbeat_group_attribute
{
1629 struct configfs_attribute attr
;
1630 ssize_t (*show
)(struct o2hb_heartbeat_group
*, char *);
1631 ssize_t (*store
)(struct o2hb_heartbeat_group
*, const char *, size_t);
1634 static ssize_t
o2hb_heartbeat_group_show(struct config_item
*item
,
1635 struct configfs_attribute
*attr
,
1638 struct o2hb_heartbeat_group
*reg
= to_o2hb_heartbeat_group(to_config_group(item
));
1639 struct o2hb_heartbeat_group_attribute
*o2hb_heartbeat_group_attr
=
1640 container_of(attr
, struct o2hb_heartbeat_group_attribute
, attr
);
1643 if (o2hb_heartbeat_group_attr
->show
)
1644 ret
= o2hb_heartbeat_group_attr
->show(reg
, page
);
1648 static ssize_t
o2hb_heartbeat_group_store(struct config_item
*item
,
1649 struct configfs_attribute
*attr
,
1650 const char *page
, size_t count
)
1652 struct o2hb_heartbeat_group
*reg
= to_o2hb_heartbeat_group(to_config_group(item
));
1653 struct o2hb_heartbeat_group_attribute
*o2hb_heartbeat_group_attr
=
1654 container_of(attr
, struct o2hb_heartbeat_group_attribute
, attr
);
1655 ssize_t ret
= -EINVAL
;
1657 if (o2hb_heartbeat_group_attr
->store
)
1658 ret
= o2hb_heartbeat_group_attr
->store(reg
, page
, count
);
1662 static ssize_t
o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group
*group
,
1665 return sprintf(page
, "%u\n", o2hb_dead_threshold
);
1668 static ssize_t
o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group
*group
,
1673 char *p
= (char *)page
;
1675 tmp
= simple_strtoul(p
, &p
, 10);
1676 if (!p
|| (*p
&& (*p
!= '\n')))
1679 /* this will validate ranges for us. */
1680 o2hb_dead_threshold_set((unsigned int) tmp
);
1685 static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold
= {
1686 .attr
= { .ca_owner
= THIS_MODULE
,
1687 .ca_name
= "dead_threshold",
1688 .ca_mode
= S_IRUGO
| S_IWUSR
},
1689 .show
= o2hb_heartbeat_group_threshold_show
,
1690 .store
= o2hb_heartbeat_group_threshold_store
,
1693 static struct configfs_attribute
*o2hb_heartbeat_group_attrs
[] = {
1694 &o2hb_heartbeat_group_attr_threshold
.attr
,
1698 static struct configfs_item_operations o2hb_hearbeat_group_item_ops
= {
1699 .show_attribute
= o2hb_heartbeat_group_show
,
1700 .store_attribute
= o2hb_heartbeat_group_store
,
1703 static struct configfs_group_operations o2hb_heartbeat_group_group_ops
= {
1704 .make_item
= o2hb_heartbeat_group_make_item
,
1705 .drop_item
= o2hb_heartbeat_group_drop_item
,
1708 static struct config_item_type o2hb_heartbeat_group_type
= {
1709 .ct_group_ops
= &o2hb_heartbeat_group_group_ops
,
1710 .ct_item_ops
= &o2hb_hearbeat_group_item_ops
,
1711 .ct_attrs
= o2hb_heartbeat_group_attrs
,
1712 .ct_owner
= THIS_MODULE
,
1715 /* this is just here to avoid touching group in heartbeat.h which the
1716 * entire damn world #includes */
1717 struct config_group
*o2hb_alloc_hb_set(void)
1719 struct o2hb_heartbeat_group
*hs
= NULL
;
1720 struct config_group
*ret
= NULL
;
1722 hs
= kzalloc(sizeof(struct o2hb_heartbeat_group
), GFP_KERNEL
);
1726 config_group_init_type_name(&hs
->hs_group
, "heartbeat",
1727 &o2hb_heartbeat_group_type
);
1729 ret
= &hs
->hs_group
;
1736 void o2hb_free_hb_set(struct config_group
*group
)
1738 struct o2hb_heartbeat_group
*hs
= to_o2hb_heartbeat_group(group
);
1742 /* hb callback registration and issueing */
1744 static struct o2hb_callback
*hbcall_from_type(enum o2hb_callback_type type
)
1746 if (type
== O2HB_NUM_CB
)
1747 return ERR_PTR(-EINVAL
);
1749 return &o2hb_callbacks
[type
];
1752 void o2hb_setup_callback(struct o2hb_callback_func
*hc
,
1753 enum o2hb_callback_type type
,
1758 INIT_LIST_HEAD(&hc
->hc_item
);
1761 hc
->hc_priority
= priority
;
1763 hc
->hc_magic
= O2HB_CB_MAGIC
;
1765 EXPORT_SYMBOL_GPL(o2hb_setup_callback
);
1767 static struct o2hb_region
*o2hb_find_region(const char *region_uuid
)
1769 struct o2hb_region
*p
, *reg
= NULL
;
1771 assert_spin_locked(&o2hb_live_lock
);
1773 list_for_each_entry(p
, &o2hb_all_regions
, hr_all_item
) {
1774 if (!strcmp(region_uuid
, config_item_name(&p
->hr_item
))) {
1783 static int o2hb_region_get(const char *region_uuid
)
1786 struct o2hb_region
*reg
;
1788 spin_lock(&o2hb_live_lock
);
1790 reg
= o2hb_find_region(region_uuid
);
1793 spin_unlock(&o2hb_live_lock
);
1798 ret
= o2nm_depend_this_node();
1802 ret
= o2nm_depend_item(®
->hr_item
);
1804 o2nm_undepend_this_node();
1810 static void o2hb_region_put(const char *region_uuid
)
1812 struct o2hb_region
*reg
;
1814 spin_lock(&o2hb_live_lock
);
1816 reg
= o2hb_find_region(region_uuid
);
1818 spin_unlock(&o2hb_live_lock
);
1821 o2nm_undepend_item(®
->hr_item
);
1822 o2nm_undepend_this_node();
1826 int o2hb_register_callback(const char *region_uuid
,
1827 struct o2hb_callback_func
*hc
)
1829 struct o2hb_callback_func
*tmp
;
1830 struct list_head
*iter
;
1831 struct o2hb_callback
*hbcall
;
1834 BUG_ON(hc
->hc_magic
!= O2HB_CB_MAGIC
);
1835 BUG_ON(!list_empty(&hc
->hc_item
));
1837 hbcall
= hbcall_from_type(hc
->hc_type
);
1838 if (IS_ERR(hbcall
)) {
1839 ret
= PTR_ERR(hbcall
);
1844 ret
= o2hb_region_get(region_uuid
);
1849 down_write(&o2hb_callback_sem
);
1851 list_for_each(iter
, &hbcall
->list
) {
1852 tmp
= list_entry(iter
, struct o2hb_callback_func
, hc_item
);
1853 if (hc
->hc_priority
< tmp
->hc_priority
) {
1854 list_add_tail(&hc
->hc_item
, iter
);
1858 if (list_empty(&hc
->hc_item
))
1859 list_add_tail(&hc
->hc_item
, &hbcall
->list
);
1861 up_write(&o2hb_callback_sem
);
1864 mlog(ML_HEARTBEAT
, "returning %d on behalf of %p for funcs %p\n",
1865 ret
, __builtin_return_address(0), hc
);
1868 EXPORT_SYMBOL_GPL(o2hb_register_callback
);
1870 void o2hb_unregister_callback(const char *region_uuid
,
1871 struct o2hb_callback_func
*hc
)
1873 BUG_ON(hc
->hc_magic
!= O2HB_CB_MAGIC
);
1875 mlog(ML_HEARTBEAT
, "on behalf of %p for funcs %p\n",
1876 __builtin_return_address(0), hc
);
1878 if (list_empty(&hc
->hc_item
))
1882 o2hb_region_put(region_uuid
);
1884 down_write(&o2hb_callback_sem
);
1886 list_del_init(&hc
->hc_item
);
1888 up_write(&o2hb_callback_sem
);
1890 EXPORT_SYMBOL_GPL(o2hb_unregister_callback
);
1892 int o2hb_check_node_heartbeating(u8 node_num
)
1894 unsigned long testing_map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
1896 o2hb_fill_node_map(testing_map
, sizeof(testing_map
));
1897 if (!test_bit(node_num
, testing_map
)) {
1899 "node (%u) does not have heartbeating enabled.\n",
1906 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating
);
1908 int o2hb_check_node_heartbeating_from_callback(u8 node_num
)
1910 unsigned long testing_map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
1912 o2hb_fill_node_map_from_callback(testing_map
, sizeof(testing_map
));
1913 if (!test_bit(node_num
, testing_map
)) {
1915 "node (%u) does not have heartbeating enabled.\n",
1922 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback
);
1924 /* Makes sure our local node is configured with a node number, and is
1926 int o2hb_check_local_node_heartbeating(void)
1930 /* if this node was set then we have networking */
1931 node_num
= o2nm_this_node();
1932 if (node_num
== O2NM_MAX_NODES
) {
1933 mlog(ML_HEARTBEAT
, "this node has not been configured.\n");
1937 return o2hb_check_node_heartbeating(node_num
);
1939 EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating
);
1942 * this is just a hack until we get the plumbing which flips file systems
1943 * read only and drops the hb ref instead of killing the node dead.
1945 void o2hb_stop_all_regions(void)
1947 struct o2hb_region
*reg
;
1949 mlog(ML_ERROR
, "stopping heartbeat on all active regions.\n");
1951 spin_lock(&o2hb_live_lock
);
1953 list_for_each_entry(reg
, &o2hb_all_regions
, hr_all_item
)
1954 reg
->hr_unclean_stop
= 1;
1956 spin_unlock(&o2hb_live_lock
);
1958 EXPORT_SYMBOL_GPL(o2hb_stop_all_regions
);