]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/ocfs2/cluster/heartbeat.c
ocfs2/dlm: Expose dlm_protocol in dlm_state
[mirror_ubuntu-artful-kernel.git] / fs / ocfs2 / cluster / heartbeat.c
CommitLineData
a7f6a5fb
MF
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * Copyright (C) 2004, 2005 Oracle. All rights reserved.
5 *
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.
10 *
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.
15 *
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.
20 */
21
22#include <linux/kernel.h>
23#include <linux/sched.h>
24#include <linux/jiffies.h>
25#include <linux/module.h>
26#include <linux/fs.h>
27#include <linux/bio.h>
28#include <linux/blkdev.h>
29#include <linux/delay.h>
30#include <linux/file.h>
31#include <linux/kthread.h>
32#include <linux/configfs.h>
33#include <linux/random.h>
34#include <linux/crc32.h>
35#include <linux/time.h>
87d3d3f3 36#include <linux/debugfs.h>
5a0e3ad6 37#include <linux/slab.h>
a7f6a5fb
MF
38
39#include "heartbeat.h"
40#include "tcp.h"
41#include "nodemanager.h"
42#include "quorum.h"
43
44#include "masklog.h"
45
46
47/*
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.
52 */
53static DECLARE_RWSEM(o2hb_callback_sem);
54
55/*
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.
58 */
34af946a 59static DEFINE_SPINLOCK(o2hb_live_lock);
a7f6a5fb
MF
60static struct list_head o2hb_live_slots[O2NM_MAX_NODES];
61static unsigned long o2hb_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
62static LIST_HEAD(o2hb_node_events);
63static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue);
64
87d3d3f3
SM
65#define O2HB_DEBUG_DIR "o2hb"
66#define O2HB_DEBUG_LIVENODES "livenodes"
67static struct dentry *o2hb_debug_dir;
68static struct dentry *o2hb_debug_livenodes;
69
a7f6a5fb
MF
70static LIST_HEAD(o2hb_all_regions);
71
72static struct o2hb_callback {
73 struct list_head list;
74} o2hb_callbacks[O2HB_NUM_CB];
75
76static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type);
77
78#define O2HB_DEFAULT_BLOCK_BITS 9
79
54b5187b
SM
80enum o2hb_heartbeat_modes {
81 O2HB_HEARTBEAT_LOCAL = 0,
82 O2HB_HEARTBEAT_GLOBAL,
83 O2HB_HEARTBEAT_NUM_MODES,
84};
85
86char *o2hb_heartbeat_mode_desc[O2HB_HEARTBEAT_NUM_MODES] = {
87 "local", /* O2HB_HEARTBEAT_LOCAL */
88 "global", /* O2HB_HEARTBEAT_GLOBAL */
89};
90
a7f6a5fb 91unsigned int o2hb_dead_threshold = O2HB_DEFAULT_DEAD_THRESHOLD;
54b5187b 92unsigned int o2hb_heartbeat_mode = O2HB_HEARTBEAT_LOCAL;
a7f6a5fb 93
2bd63216 94/* Only sets a new threshold if there are no active regions.
a7f6a5fb
MF
95 *
96 * No locking or otherwise interesting code is required for reading
97 * o2hb_dead_threshold as it can't change once regions are active and
98 * it's not interesting to anyone until then anyway. */
99static void o2hb_dead_threshold_set(unsigned int threshold)
100{
101 if (threshold > O2HB_MIN_DEAD_THRESHOLD) {
102 spin_lock(&o2hb_live_lock);
103 if (list_empty(&o2hb_all_regions))
104 o2hb_dead_threshold = threshold;
105 spin_unlock(&o2hb_live_lock);
106 }
107}
108
54b5187b
SM
109static int o2hb_global_hearbeat_mode_set(unsigned int hb_mode)
110{
111 int ret = -1;
112
113 if (hb_mode < O2HB_HEARTBEAT_NUM_MODES) {
114 spin_lock(&o2hb_live_lock);
115 if (list_empty(&o2hb_all_regions)) {
116 o2hb_heartbeat_mode = hb_mode;
117 ret = 0;
118 }
119 spin_unlock(&o2hb_live_lock);
120 }
121
122 return ret;
123}
124
a7f6a5fb
MF
125struct o2hb_node_event {
126 struct list_head hn_item;
127 enum o2hb_callback_type hn_event_type;
128 struct o2nm_node *hn_node;
129 int hn_node_num;
130};
131
132struct o2hb_disk_slot {
133 struct o2hb_disk_heartbeat_block *ds_raw_block;
134 u8 ds_node_num;
135 u64 ds_last_time;
136 u64 ds_last_generation;
137 u16 ds_equal_samples;
138 u16 ds_changed_samples;
139 struct list_head ds_live_item;
140};
141
142/* each thread owns a region.. when we're asked to tear down the region
143 * we ask the thread to stop, who cleans up the region */
144struct o2hb_region {
145 struct config_item hr_item;
146
147 struct list_head hr_all_item;
148 unsigned hr_unclean_stop:1;
149
150 /* protected by the hr_callback_sem */
151 struct task_struct *hr_task;
152
153 unsigned int hr_blocks;
154 unsigned long long hr_start_block;
155
156 unsigned int hr_block_bits;
157 unsigned int hr_block_bytes;
158
159 unsigned int hr_slots_per_page;
160 unsigned int hr_num_pages;
161
162 struct page **hr_slot_data;
163 struct block_device *hr_bdev;
164 struct o2hb_disk_slot *hr_slots;
165
166 /* let the person setting up hb wait for it to return until it
167 * has reached a 'steady' state. This will be fixed when we have
168 * a more complete api that doesn't lead to this sort of fragility. */
169 atomic_t hr_steady_iterations;
170
171 char hr_dev_name[BDEVNAME_SIZE];
172
173 unsigned int hr_timeout_ms;
174
175 /* randomized as the region goes up and down so that a node
176 * recognizes a node going up and down in one iteration */
177 u64 hr_generation;
178
c4028958 179 struct delayed_work hr_write_timeout_work;
a7f6a5fb
MF
180 unsigned long hr_last_timeout_start;
181
182 /* Used during o2hb_check_slot to hold a copy of the block
183 * being checked because we temporarily have to zero out the
184 * crc field. */
185 struct o2hb_disk_heartbeat_block *hr_tmp_block;
186};
187
188struct o2hb_bio_wait_ctxt {
189 atomic_t wc_num_reqs;
190 struct completion wc_io_complete;
a9e2ae39 191 int wc_error;
a7f6a5fb
MF
192};
193
c4028958 194static void o2hb_write_timeout(struct work_struct *work)
a7f6a5fb 195{
c4028958
DH
196 struct o2hb_region *reg =
197 container_of(work, struct o2hb_region,
198 hr_write_timeout_work.work);
a7f6a5fb
MF
199
200 mlog(ML_ERROR, "Heartbeat write timeout to device %s after %u "
201 "milliseconds\n", reg->hr_dev_name,
2bd63216 202 jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
a7f6a5fb
MF
203 o2quo_disk_timeout();
204}
205
206static void o2hb_arm_write_timeout(struct o2hb_region *reg)
207{
b31d308d
TM
208 mlog(ML_HEARTBEAT, "Queue write timeout for %u ms\n",
209 O2HB_MAX_WRITE_TIMEOUT_MS);
a7f6a5fb
MF
210
211 cancel_delayed_work(&reg->hr_write_timeout_work);
212 reg->hr_last_timeout_start = jiffies;
213 schedule_delayed_work(&reg->hr_write_timeout_work,
214 msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS));
215}
216
217static void o2hb_disarm_write_timeout(struct o2hb_region *reg)
218{
219 cancel_delayed_work(&reg->hr_write_timeout_work);
220 flush_scheduled_work();
221}
222
b559292e 223static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt *wc)
a7f6a5fb 224{
b559292e 225 atomic_set(&wc->wc_num_reqs, 1);
a7f6a5fb 226 init_completion(&wc->wc_io_complete);
a9e2ae39 227 wc->wc_error = 0;
a7f6a5fb
MF
228}
229
230/* Used in error paths too */
231static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt *wc,
232 unsigned int num)
233{
234 /* sadly atomic_sub_and_test() isn't available on all platforms. The
235 * good news is that the fast path only completes one at a time */
236 while(num--) {
237 if (atomic_dec_and_test(&wc->wc_num_reqs)) {
238 BUG_ON(num > 0);
239 complete(&wc->wc_io_complete);
240 }
241 }
242}
243
244static void o2hb_wait_on_io(struct o2hb_region *reg,
245 struct o2hb_bio_wait_ctxt *wc)
246{
247 struct address_space *mapping = reg->hr_bdev->bd_inode->i_mapping;
248
249 blk_run_address_space(mapping);
b559292e 250 o2hb_bio_wait_dec(wc, 1);
a7f6a5fb
MF
251
252 wait_for_completion(&wc->wc_io_complete);
253}
254
782e3b3b 255static void o2hb_bio_end_io(struct bio *bio,
a7f6a5fb
MF
256 int error)
257{
258 struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
259
a9e2ae39 260 if (error) {
a7f6a5fb 261 mlog(ML_ERROR, "IO Error %d\n", error);
a9e2ae39
MF
262 wc->wc_error = error;
263 }
a7f6a5fb 264
a7f6a5fb 265 o2hb_bio_wait_dec(wc, 1);
b559292e 266 bio_put(bio);
a7f6a5fb
MF
267}
268
269/* Setup a Bio to cover I/O against num_slots slots starting at
270 * start_slot. */
271static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
272 struct o2hb_bio_wait_ctxt *wc,
b559292e
PR
273 unsigned int *current_slot,
274 unsigned int max_slots)
a7f6a5fb 275{
b559292e 276 int len, current_page;
a7f6a5fb
MF
277 unsigned int vec_len, vec_start;
278 unsigned int bits = reg->hr_block_bits;
279 unsigned int spp = reg->hr_slots_per_page;
b559292e 280 unsigned int cs = *current_slot;
a7f6a5fb
MF
281 struct bio *bio;
282 struct page *page;
283
a7f6a5fb
MF
284 /* Testing has shown this allocation to take long enough under
285 * GFP_KERNEL that the local node can get fenced. It would be
286 * nicest if we could pre-allocate these bios and avoid this
287 * all together. */
b559292e 288 bio = bio_alloc(GFP_ATOMIC, 16);
a7f6a5fb
MF
289 if (!bio) {
290 mlog(ML_ERROR, "Could not alloc slots BIO!\n");
291 bio = ERR_PTR(-ENOMEM);
292 goto bail;
293 }
294
295 /* Must put everything in 512 byte sectors for the bio... */
b559292e 296 bio->bi_sector = (reg->hr_start_block + cs) << (bits - 9);
a7f6a5fb
MF
297 bio->bi_bdev = reg->hr_bdev;
298 bio->bi_private = wc;
299 bio->bi_end_io = o2hb_bio_end_io;
300
b559292e
PR
301 vec_start = (cs << bits) % PAGE_CACHE_SIZE;
302 while(cs < max_slots) {
303 current_page = cs / spp;
304 page = reg->hr_slot_data[current_page];
a7f6a5fb 305
bc7e97cb 306 vec_len = min(PAGE_CACHE_SIZE - vec_start,
b559292e 307 (max_slots-cs) * (PAGE_CACHE_SIZE/spp) );
a7f6a5fb
MF
308
309 mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
b559292e 310 current_page, vec_len, vec_start);
a7f6a5fb
MF
311
312 len = bio_add_page(bio, page, vec_len, vec_start);
b559292e 313 if (len != vec_len) break;
a7f6a5fb 314
b559292e 315 cs += vec_len / (PAGE_CACHE_SIZE/spp);
a7f6a5fb
MF
316 vec_start = 0;
317 }
318
319bail:
b559292e 320 *current_slot = cs;
a7f6a5fb
MF
321 return bio;
322}
323
a7f6a5fb
MF
324static int o2hb_read_slots(struct o2hb_region *reg,
325 unsigned int max_slots)
326{
b559292e
PR
327 unsigned int current_slot=0;
328 int status;
a7f6a5fb 329 struct o2hb_bio_wait_ctxt wc;
a7f6a5fb
MF
330 struct bio *bio;
331
b559292e 332 o2hb_bio_wait_init(&wc);
a7f6a5fb 333
b559292e
PR
334 while(current_slot < max_slots) {
335 bio = o2hb_setup_one_bio(reg, &wc, &current_slot, max_slots);
a7f6a5fb 336 if (IS_ERR(bio)) {
a7f6a5fb
MF
337 status = PTR_ERR(bio);
338 mlog_errno(status);
339 goto bail_and_wait;
340 }
a7f6a5fb 341
b559292e 342 atomic_inc(&wc.wc_num_reqs);
a7f6a5fb
MF
343 submit_bio(READ, bio);
344 }
345
346 status = 0;
347
348bail_and_wait:
349 o2hb_wait_on_io(reg, &wc);
a9e2ae39
MF
350 if (wc.wc_error && !status)
351 status = wc.wc_error;
a7f6a5fb 352
a7f6a5fb
MF
353 return status;
354}
355
356static int o2hb_issue_node_write(struct o2hb_region *reg,
a7f6a5fb
MF
357 struct o2hb_bio_wait_ctxt *write_wc)
358{
359 int status;
360 unsigned int slot;
361 struct bio *bio;
362
b559292e 363 o2hb_bio_wait_init(write_wc);
a7f6a5fb
MF
364
365 slot = o2nm_this_node();
366
b559292e 367 bio = o2hb_setup_one_bio(reg, write_wc, &slot, slot+1);
a7f6a5fb
MF
368 if (IS_ERR(bio)) {
369 status = PTR_ERR(bio);
370 mlog_errno(status);
371 goto bail;
372 }
373
b559292e 374 atomic_inc(&write_wc->wc_num_reqs);
a7f6a5fb
MF
375 submit_bio(WRITE, bio);
376
a7f6a5fb
MF
377 status = 0;
378bail:
379 return status;
380}
381
382static u32 o2hb_compute_block_crc_le(struct o2hb_region *reg,
383 struct o2hb_disk_heartbeat_block *hb_block)
384{
385 __le32 old_cksum;
386 u32 ret;
387
388 /* We want to compute the block crc with a 0 value in the
389 * hb_cksum field. Save it off here and replace after the
390 * crc. */
391 old_cksum = hb_block->hb_cksum;
392 hb_block->hb_cksum = 0;
393
394 ret = crc32_le(0, (unsigned char *) hb_block, reg->hr_block_bytes);
395
396 hb_block->hb_cksum = old_cksum;
397
398 return ret;
399}
400
401static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
402{
70bacbdb
MF
403 mlog(ML_ERROR, "Dump slot information: seq = 0x%llx, node = %u, "
404 "cksum = 0x%x, generation 0x%llx\n",
405 (long long)le64_to_cpu(hb_block->hb_seq),
406 hb_block->hb_node, le32_to_cpu(hb_block->hb_cksum),
407 (long long)le64_to_cpu(hb_block->hb_generation));
a7f6a5fb
MF
408}
409
410static int o2hb_verify_crc(struct o2hb_region *reg,
411 struct o2hb_disk_heartbeat_block *hb_block)
412{
413 u32 read, computed;
414
415 read = le32_to_cpu(hb_block->hb_cksum);
416 computed = o2hb_compute_block_crc_le(reg, hb_block);
417
418 return read == computed;
419}
420
421/* We want to make sure that nobody is heartbeating on top of us --
422 * this will help detect an invalid configuration. */
423static int o2hb_check_last_timestamp(struct o2hb_region *reg)
424{
425 int node_num, ret;
426 struct o2hb_disk_slot *slot;
427 struct o2hb_disk_heartbeat_block *hb_block;
428
429 node_num = o2nm_this_node();
430
431 ret = 1;
432 slot = &reg->hr_slots[node_num];
433 /* Don't check on our 1st timestamp */
434 if (slot->ds_last_time) {
435 hb_block = slot->ds_raw_block;
436
437 if (le64_to_cpu(hb_block->hb_seq) != slot->ds_last_time)
438 ret = 0;
439 }
440
441 return ret;
442}
443
444static inline void o2hb_prepare_block(struct o2hb_region *reg,
445 u64 generation)
446{
447 int node_num;
448 u64 cputime;
449 struct o2hb_disk_slot *slot;
450 struct o2hb_disk_heartbeat_block *hb_block;
451
452 node_num = o2nm_this_node();
453 slot = &reg->hr_slots[node_num];
454
455 hb_block = (struct o2hb_disk_heartbeat_block *)slot->ds_raw_block;
456 memset(hb_block, 0, reg->hr_block_bytes);
457 /* TODO: time stuff */
458 cputime = CURRENT_TIME.tv_sec;
459 if (!cputime)
460 cputime = 1;
461
462 hb_block->hb_seq = cpu_to_le64(cputime);
463 hb_block->hb_node = node_num;
464 hb_block->hb_generation = cpu_to_le64(generation);
0db638f4 465 hb_block->hb_dead_ms = cpu_to_le32(o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS);
a7f6a5fb
MF
466
467 /* This step must always happen last! */
468 hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
469 hb_block));
470
70bacbdb 471 mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x\n",
5fdf1e67 472 (long long)generation,
70bacbdb 473 le32_to_cpu(hb_block->hb_cksum));
a7f6a5fb
MF
474}
475
476static void o2hb_fire_callbacks(struct o2hb_callback *hbcall,
477 struct o2nm_node *node,
478 int idx)
479{
480 struct list_head *iter;
481 struct o2hb_callback_func *f;
482
483 list_for_each(iter, &hbcall->list) {
484 f = list_entry(iter, struct o2hb_callback_func, hc_item);
485 mlog(ML_HEARTBEAT, "calling funcs %p\n", f);
486 (f->hc_func)(node, idx, f->hc_data);
487 }
488}
489
490/* Will run the list in order until we process the passed event */
491static void o2hb_run_event_list(struct o2hb_node_event *queued_event)
492{
493 int empty;
494 struct o2hb_callback *hbcall;
495 struct o2hb_node_event *event;
496
497 spin_lock(&o2hb_live_lock);
498 empty = list_empty(&queued_event->hn_item);
499 spin_unlock(&o2hb_live_lock);
500 if (empty)
501 return;
502
503 /* Holding callback sem assures we don't alter the callback
504 * lists when doing this, and serializes ourselves with other
505 * processes wanting callbacks. */
506 down_write(&o2hb_callback_sem);
507
508 spin_lock(&o2hb_live_lock);
509 while (!list_empty(&o2hb_node_events)
510 && !list_empty(&queued_event->hn_item)) {
511 event = list_entry(o2hb_node_events.next,
512 struct o2hb_node_event,
513 hn_item);
514 list_del_init(&event->hn_item);
515 spin_unlock(&o2hb_live_lock);
516
517 mlog(ML_HEARTBEAT, "Node %s event for %d\n",
518 event->hn_event_type == O2HB_NODE_UP_CB ? "UP" : "DOWN",
519 event->hn_node_num);
520
521 hbcall = hbcall_from_type(event->hn_event_type);
522
523 /* We should *never* have gotten on to the list with a
524 * bad type... This isn't something that we should try
525 * to recover from. */
526 BUG_ON(IS_ERR(hbcall));
527
528 o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num);
529
530 spin_lock(&o2hb_live_lock);
531 }
532 spin_unlock(&o2hb_live_lock);
533
534 up_write(&o2hb_callback_sem);
535}
536
537static void o2hb_queue_node_event(struct o2hb_node_event *event,
538 enum o2hb_callback_type type,
539 struct o2nm_node *node,
540 int node_num)
541{
542 assert_spin_locked(&o2hb_live_lock);
543
544 event->hn_event_type = type;
545 event->hn_node = node;
546 event->hn_node_num = node_num;
547
548 mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
549 type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
550
551 list_add_tail(&event->hn_item, &o2hb_node_events);
552}
553
554static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
555{
556 struct o2hb_node_event event =
557 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
558 struct o2nm_node *node;
559
560 node = o2nm_get_node_by_num(slot->ds_node_num);
561 if (!node)
562 return;
563
564 spin_lock(&o2hb_live_lock);
565 if (!list_empty(&slot->ds_live_item)) {
566 mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
567 slot->ds_node_num);
568
569 list_del_init(&slot->ds_live_item);
570
571 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
572 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
573
574 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
575 slot->ds_node_num);
576 }
577 }
578 spin_unlock(&o2hb_live_lock);
579
580 o2hb_run_event_list(&event);
581
582 o2nm_node_put(node);
583}
584
585static int o2hb_check_slot(struct o2hb_region *reg,
586 struct o2hb_disk_slot *slot)
587{
588 int changed = 0, gen_changed = 0;
589 struct o2hb_node_event event =
590 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
591 struct o2nm_node *node;
592 struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
593 u64 cputime;
0db638f4
MF
594 unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
595 unsigned int slot_dead_ms;
a7f6a5fb
MF
596
597 memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
598
599 /* Is this correct? Do we assume that the node doesn't exist
600 * if we're not configured for him? */
601 node = o2nm_get_node_by_num(slot->ds_node_num);
602 if (!node)
603 return 0;
604
605 if (!o2hb_verify_crc(reg, hb_block)) {
606 /* all paths from here will drop o2hb_live_lock for
607 * us. */
608 spin_lock(&o2hb_live_lock);
609
610 /* Don't print an error on the console in this case -
611 * a freshly formatted heartbeat area will not have a
612 * crc set on it. */
613 if (list_empty(&slot->ds_live_item))
614 goto out;
615
616 /* The node is live but pushed out a bad crc. We
617 * consider it a transient miss but don't populate any
618 * other values as they may be junk. */
619 mlog(ML_ERROR, "Node %d has written a bad crc to %s\n",
620 slot->ds_node_num, reg->hr_dev_name);
621 o2hb_dump_slot(hb_block);
622
623 slot->ds_equal_samples++;
624 goto fire_callbacks;
625 }
626
627 /* we don't care if these wrap.. the state transitions below
628 * clear at the right places */
629 cputime = le64_to_cpu(hb_block->hb_seq);
630 if (slot->ds_last_time != cputime)
631 slot->ds_changed_samples++;
632 else
633 slot->ds_equal_samples++;
634 slot->ds_last_time = cputime;
635
636 /* The node changed heartbeat generations. We assume this to
637 * mean it dropped off but came back before we timed out. We
638 * want to consider it down for the time being but don't want
639 * to lose any changed_samples state we might build up to
640 * considering it live again. */
641 if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
642 gen_changed = 1;
643 slot->ds_equal_samples = 0;
70bacbdb
MF
644 mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
645 "to 0x%llx)\n", slot->ds_node_num,
646 (long long)slot->ds_last_generation,
647 (long long)le64_to_cpu(hb_block->hb_generation));
a7f6a5fb
MF
648 }
649
650 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
651
70bacbdb
MF
652 mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
653 "seq %llu last %llu changed %u equal %u\n",
654 slot->ds_node_num, (long long)slot->ds_last_generation,
655 le32_to_cpu(hb_block->hb_cksum),
2bd63216 656 (unsigned long long)le64_to_cpu(hb_block->hb_seq),
70bacbdb 657 (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
a7f6a5fb
MF
658 slot->ds_equal_samples);
659
660 spin_lock(&o2hb_live_lock);
661
662fire_callbacks:
663 /* dead nodes only come to life after some number of
664 * changes at any time during their dead time */
665 if (list_empty(&slot->ds_live_item) &&
666 slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
70bacbdb
MF
667 mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
668 slot->ds_node_num, (long long)slot->ds_last_generation);
a7f6a5fb
MF
669
670 /* first on the list generates a callback */
671 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
672 set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
673
674 o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
675 slot->ds_node_num);
676
677 changed = 1;
678 }
679
680 list_add_tail(&slot->ds_live_item,
681 &o2hb_live_slots[slot->ds_node_num]);
682
683 slot->ds_equal_samples = 0;
0db638f4
MF
684
685 /* We want to be sure that all nodes agree on the
686 * number of milliseconds before a node will be
687 * considered dead. The self-fencing timeout is
688 * computed from this value, and a discrepancy might
689 * result in heartbeat calling a node dead when it
690 * hasn't self-fenced yet. */
691 slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
692 if (slot_dead_ms && slot_dead_ms != dead_ms) {
693 /* TODO: Perhaps we can fail the region here. */
694 mlog(ML_ERROR, "Node %d on device %s has a dead count "
695 "of %u ms, but our count is %u ms.\n"
696 "Please double check your configuration values "
697 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
698 slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
699 dead_ms);
700 }
a7f6a5fb
MF
701 goto out;
702 }
703
704 /* if the list is dead, we're done.. */
705 if (list_empty(&slot->ds_live_item))
706 goto out;
707
708 /* live nodes only go dead after enough consequtive missed
709 * samples.. reset the missed counter whenever we see
710 * activity */
711 if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
712 mlog(ML_HEARTBEAT, "Node %d left my region\n",
713 slot->ds_node_num);
714
715 /* last off the live_slot generates a callback */
716 list_del_init(&slot->ds_live_item);
717 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
718 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
719
720 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
721 slot->ds_node_num);
722
723 changed = 1;
724 }
725
726 /* We don't clear this because the node is still
727 * actually writing new blocks. */
728 if (!gen_changed)
729 slot->ds_changed_samples = 0;
730 goto out;
731 }
732 if (slot->ds_changed_samples) {
733 slot->ds_changed_samples = 0;
734 slot->ds_equal_samples = 0;
735 }
736out:
737 spin_unlock(&o2hb_live_lock);
738
739 o2hb_run_event_list(&event);
740
741 o2nm_node_put(node);
742 return changed;
743}
744
745/* This could be faster if we just implmented a find_last_bit, but I
746 * don't think the circumstances warrant it. */
747static int o2hb_highest_node(unsigned long *nodes,
748 int numbits)
749{
750 int highest, node;
751
752 highest = numbits;
753 node = -1;
754 while ((node = find_next_bit(nodes, numbits, node + 1)) != -1) {
755 if (node >= numbits)
756 break;
757
758 highest = node;
759 }
760
761 return highest;
762}
763
a9e2ae39 764static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
a7f6a5fb
MF
765{
766 int i, ret, highest_node, change = 0;
767 unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
a7f6a5fb
MF
768 struct o2hb_bio_wait_ctxt write_wc;
769
a9e2ae39
MF
770 ret = o2nm_configured_node_map(configured_nodes,
771 sizeof(configured_nodes));
772 if (ret) {
773 mlog_errno(ret);
774 return ret;
775 }
a7f6a5fb
MF
776
777 highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
778 if (highest_node >= O2NM_MAX_NODES) {
779 mlog(ML_NOTICE, "ocfs2_heartbeat: no configured nodes found!\n");
a9e2ae39 780 return -EINVAL;
a7f6a5fb
MF
781 }
782
783 /* No sense in reading the slots of nodes that don't exist
784 * yet. Of course, if the node definitions have holes in them
785 * then we're reading an empty slot anyway... Consider this
786 * best-effort. */
787 ret = o2hb_read_slots(reg, highest_node + 1);
788 if (ret < 0) {
789 mlog_errno(ret);
a9e2ae39 790 return ret;
a7f6a5fb
MF
791 }
792
793 /* With an up to date view of the slots, we can check that no
794 * other node has been improperly configured to heartbeat in
795 * our slot. */
796 if (!o2hb_check_last_timestamp(reg))
797 mlog(ML_ERROR, "Device \"%s\": another node is heartbeating "
798 "in our slot!\n", reg->hr_dev_name);
799
800 /* fill in the proper info for our next heartbeat */
801 o2hb_prepare_block(reg, reg->hr_generation);
802
803 /* And fire off the write. Note that we don't wait on this I/O
804 * until later. */
b559292e 805 ret = o2hb_issue_node_write(reg, &write_wc);
a7f6a5fb
MF
806 if (ret < 0) {
807 mlog_errno(ret);
a9e2ae39 808 return ret;
a7f6a5fb
MF
809 }
810
811 i = -1;
812 while((i = find_next_bit(configured_nodes, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
813
814 change |= o2hb_check_slot(reg, &reg->hr_slots[i]);
815 }
816
817 /*
818 * We have to be sure we've advertised ourselves on disk
819 * before we can go to steady state. This ensures that
820 * people we find in our steady state have seen us.
821 */
822 o2hb_wait_on_io(reg, &write_wc);
a9e2ae39
MF
823 if (write_wc.wc_error) {
824 /* Do not re-arm the write timeout on I/O error - we
825 * can't be sure that the new block ever made it to
826 * disk */
827 mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
828 write_wc.wc_error, reg->hr_dev_name);
829 return write_wc.wc_error;
830 }
831
a7f6a5fb
MF
832 o2hb_arm_write_timeout(reg);
833
834 /* let the person who launched us know when things are steady */
835 if (!change && (atomic_read(&reg->hr_steady_iterations) != 0)) {
836 if (atomic_dec_and_test(&reg->hr_steady_iterations))
837 wake_up(&o2hb_steady_queue);
838 }
a9e2ae39
MF
839
840 return 0;
a7f6a5fb
MF
841}
842
843/* Subtract b from a, storing the result in a. a *must* have a larger
844 * value than b. */
845static void o2hb_tv_subtract(struct timeval *a,
846 struct timeval *b)
847{
848 /* just return 0 when a is after b */
849 if (a->tv_sec < b->tv_sec ||
850 (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) {
851 a->tv_sec = 0;
852 a->tv_usec = 0;
853 return;
854 }
855
856 a->tv_sec -= b->tv_sec;
857 a->tv_usec -= b->tv_usec;
858 while ( a->tv_usec < 0 ) {
859 a->tv_sec--;
860 a->tv_usec += 1000000;
861 }
862}
863
864static unsigned int o2hb_elapsed_msecs(struct timeval *start,
865 struct timeval *end)
866{
867 struct timeval res = *end;
868
869 o2hb_tv_subtract(&res, start);
870
871 return res.tv_sec * 1000 + res.tv_usec / 1000;
872}
873
874/*
875 * we ride the region ref that the region dir holds. before the region
876 * dir is removed and drops it ref it will wait to tear down this
877 * thread.
878 */
879static int o2hb_thread(void *data)
880{
881 int i, ret;
882 struct o2hb_region *reg = data;
a7f6a5fb
MF
883 struct o2hb_bio_wait_ctxt write_wc;
884 struct timeval before_hb, after_hb;
885 unsigned int elapsed_msec;
886
887 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
888
889 set_user_nice(current, -20);
890
891 while (!kthread_should_stop() && !reg->hr_unclean_stop) {
892 /* We track the time spent inside
025dfdaf 893 * o2hb_do_disk_heartbeat so that we avoid more than
a7f6a5fb
MF
894 * hr_timeout_ms between disk writes. On busy systems
895 * this should result in a heartbeat which is less
896 * likely to time itself out. */
897 do_gettimeofday(&before_hb);
898
a9e2ae39
MF
899 i = 0;
900 do {
901 ret = o2hb_do_disk_heartbeat(reg);
902 } while (ret && ++i < 2);
a7f6a5fb
MF
903
904 do_gettimeofday(&after_hb);
905 elapsed_msec = o2hb_elapsed_msecs(&before_hb, &after_hb);
906
b31d308d
TM
907 mlog(ML_HEARTBEAT,
908 "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
215c7f9f
MF
909 before_hb.tv_sec, (unsigned long) before_hb.tv_usec,
910 after_hb.tv_sec, (unsigned long) after_hb.tv_usec,
911 elapsed_msec);
a7f6a5fb
MF
912
913 if (elapsed_msec < reg->hr_timeout_ms) {
914 /* the kthread api has blocked signals for us so no
915 * need to record the return value. */
916 msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
917 }
918 }
919
920 o2hb_disarm_write_timeout(reg);
921
922 /* unclean stop is only used in very bad situation */
923 for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
924 o2hb_shutdown_slot(&reg->hr_slots[i]);
925
926 /* Explicit down notification - avoid forcing the other nodes
927 * to timeout on this region when we could just as easily
928 * write a clear generation - thus indicating to them that
929 * this node has left this region.
930 *
931 * XXX: Should we skip this on unclean_stop? */
932 o2hb_prepare_block(reg, 0);
b559292e 933 ret = o2hb_issue_node_write(reg, &write_wc);
a7f6a5fb
MF
934 if (ret == 0) {
935 o2hb_wait_on_io(reg, &write_wc);
a7f6a5fb
MF
936 } else {
937 mlog_errno(ret);
938 }
939
940 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread exiting\n");
941
942 return 0;
943}
944
87d3d3f3
SM
945#ifdef CONFIG_DEBUG_FS
946static int o2hb_debug_open(struct inode *inode, struct file *file)
947{
948 unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
949 char *buf = NULL;
950 int i = -1;
951 int out = 0;
952
953 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
954 if (!buf)
955 goto bail;
956
957 o2hb_fill_node_map(map, sizeof(map));
958
959 while ((i = find_next_bit(map, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES)
960 out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
961 out += snprintf(buf + out, PAGE_SIZE - out, "\n");
962
963 i_size_write(inode, out);
964
965 file->private_data = buf;
966
967 return 0;
968bail:
969 return -ENOMEM;
970}
971
972static int o2hb_debug_release(struct inode *inode, struct file *file)
973{
974 kfree(file->private_data);
975 return 0;
976}
977
978static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
979 size_t nbytes, loff_t *ppos)
980{
981 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
982 i_size_read(file->f_mapping->host));
983}
984#else
985static int o2hb_debug_open(struct inode *inode, struct file *file)
986{
987 return 0;
988}
989static int o2hb_debug_release(struct inode *inode, struct file *file)
990{
991 return 0;
992}
993static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
994 size_t nbytes, loff_t *ppos)
995{
996 return 0;
997}
998#endif /* CONFIG_DEBUG_FS */
999
828c0950 1000static const struct file_operations o2hb_debug_fops = {
87d3d3f3
SM
1001 .open = o2hb_debug_open,
1002 .release = o2hb_debug_release,
1003 .read = o2hb_debug_read,
1004 .llseek = generic_file_llseek,
1005};
1006
1007void o2hb_exit(void)
1008{
1009 if (o2hb_debug_livenodes)
1010 debugfs_remove(o2hb_debug_livenodes);
1011 if (o2hb_debug_dir)
1012 debugfs_remove(o2hb_debug_dir);
1013}
1014
1015int o2hb_init(void)
a7f6a5fb
MF
1016{
1017 int i;
1018
1019 for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
1020 INIT_LIST_HEAD(&o2hb_callbacks[i].list);
1021
1022 for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
1023 INIT_LIST_HEAD(&o2hb_live_slots[i]);
1024
1025 INIT_LIST_HEAD(&o2hb_node_events);
1026
1027 memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
87d3d3f3
SM
1028
1029 o2hb_debug_dir = debugfs_create_dir(O2HB_DEBUG_DIR, NULL);
1030 if (!o2hb_debug_dir) {
1031 mlog_errno(-ENOMEM);
1032 return -ENOMEM;
1033 }
1034
1035 o2hb_debug_livenodes = debugfs_create_file(O2HB_DEBUG_LIVENODES,
1036 S_IFREG|S_IRUSR,
1037 o2hb_debug_dir, NULL,
1038 &o2hb_debug_fops);
1039 if (!o2hb_debug_livenodes) {
1040 mlog_errno(-ENOMEM);
1041 debugfs_remove(o2hb_debug_dir);
1042 return -ENOMEM;
1043 }
1044
1045 return 0;
a7f6a5fb
MF
1046}
1047
1048/* if we're already in a callback then we're already serialized by the sem */
1049static void o2hb_fill_node_map_from_callback(unsigned long *map,
1050 unsigned bytes)
1051{
1052 BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
1053
1054 memcpy(map, &o2hb_live_node_bitmap, bytes);
1055}
1056
1057/*
1058 * get a map of all nodes that are heartbeating in any regions
1059 */
1060void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
1061{
1062 /* callers want to serialize this map and callbacks so that they
1063 * can trust that they don't miss nodes coming to the party */
1064 down_read(&o2hb_callback_sem);
1065 spin_lock(&o2hb_live_lock);
1066 o2hb_fill_node_map_from_callback(map, bytes);
1067 spin_unlock(&o2hb_live_lock);
1068 up_read(&o2hb_callback_sem);
1069}
1070EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
1071
1072/*
1073 * heartbeat configfs bits. The heartbeat set is a default set under
1074 * the cluster set in nodemanager.c.
1075 */
1076
1077static struct o2hb_region *to_o2hb_region(struct config_item *item)
1078{
1079 return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
1080}
1081
1082/* drop_item only drops its ref after killing the thread, nothing should
1083 * be using the region anymore. this has to clean up any state that
1084 * attributes might have built up. */
1085static void o2hb_region_release(struct config_item *item)
1086{
1087 int i;
1088 struct page *page;
1089 struct o2hb_region *reg = to_o2hb_region(item);
1090
1091 if (reg->hr_tmp_block)
1092 kfree(reg->hr_tmp_block);
1093
1094 if (reg->hr_slot_data) {
1095 for (i = 0; i < reg->hr_num_pages; i++) {
1096 page = reg->hr_slot_data[i];
1097 if (page)
1098 __free_page(page);
1099 }
1100 kfree(reg->hr_slot_data);
1101 }
1102
1103 if (reg->hr_bdev)
9a1c3542 1104 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
a7f6a5fb
MF
1105
1106 if (reg->hr_slots)
1107 kfree(reg->hr_slots);
1108
1109 spin_lock(&o2hb_live_lock);
1110 list_del(&reg->hr_all_item);
1111 spin_unlock(&o2hb_live_lock);
1112
1113 kfree(reg);
1114}
1115
1116static int o2hb_read_block_input(struct o2hb_region *reg,
1117 const char *page,
1118 size_t count,
1119 unsigned long *ret_bytes,
1120 unsigned int *ret_bits)
1121{
1122 unsigned long bytes;
1123 char *p = (char *)page;
1124
1125 bytes = simple_strtoul(p, &p, 0);
1126 if (!p || (*p && (*p != '\n')))
1127 return -EINVAL;
1128
1129 /* Heartbeat and fs min / max block sizes are the same. */
1130 if (bytes > 4096 || bytes < 512)
1131 return -ERANGE;
1132 if (hweight16(bytes) != 1)
1133 return -EINVAL;
1134
1135 if (ret_bytes)
1136 *ret_bytes = bytes;
1137 if (ret_bits)
1138 *ret_bits = ffs(bytes) - 1;
1139
1140 return 0;
1141}
1142
1143static ssize_t o2hb_region_block_bytes_read(struct o2hb_region *reg,
1144 char *page)
1145{
1146 return sprintf(page, "%u\n", reg->hr_block_bytes);
1147}
1148
1149static ssize_t o2hb_region_block_bytes_write(struct o2hb_region *reg,
1150 const char *page,
1151 size_t count)
1152{
1153 int status;
1154 unsigned long block_bytes;
1155 unsigned int block_bits;
1156
1157 if (reg->hr_bdev)
1158 return -EINVAL;
1159
1160 status = o2hb_read_block_input(reg, page, count,
1161 &block_bytes, &block_bits);
1162 if (status)
1163 return status;
1164
1165 reg->hr_block_bytes = (unsigned int)block_bytes;
1166 reg->hr_block_bits = block_bits;
1167
1168 return count;
1169}
1170
1171static ssize_t o2hb_region_start_block_read(struct o2hb_region *reg,
1172 char *page)
1173{
1174 return sprintf(page, "%llu\n", reg->hr_start_block);
1175}
1176
1177static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
1178 const char *page,
1179 size_t count)
1180{
1181 unsigned long long tmp;
1182 char *p = (char *)page;
1183
1184 if (reg->hr_bdev)
1185 return -EINVAL;
1186
1187 tmp = simple_strtoull(p, &p, 0);
1188 if (!p || (*p && (*p != '\n')))
1189 return -EINVAL;
1190
1191 reg->hr_start_block = tmp;
1192
1193 return count;
1194}
1195
1196static ssize_t o2hb_region_blocks_read(struct o2hb_region *reg,
1197 char *page)
1198{
1199 return sprintf(page, "%d\n", reg->hr_blocks);
1200}
1201
1202static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
1203 const char *page,
1204 size_t count)
1205{
1206 unsigned long tmp;
1207 char *p = (char *)page;
1208
1209 if (reg->hr_bdev)
1210 return -EINVAL;
1211
1212 tmp = simple_strtoul(p, &p, 0);
1213 if (!p || (*p && (*p != '\n')))
1214 return -EINVAL;
1215
1216 if (tmp > O2NM_MAX_NODES || tmp == 0)
1217 return -ERANGE;
1218
1219 reg->hr_blocks = (unsigned int)tmp;
1220
1221 return count;
1222}
1223
1224static ssize_t o2hb_region_dev_read(struct o2hb_region *reg,
1225 char *page)
1226{
1227 unsigned int ret = 0;
1228
1229 if (reg->hr_bdev)
1230 ret = sprintf(page, "%s\n", reg->hr_dev_name);
1231
1232 return ret;
1233}
1234
1235static void o2hb_init_region_params(struct o2hb_region *reg)
1236{
1237 reg->hr_slots_per_page = PAGE_CACHE_SIZE >> reg->hr_block_bits;
1238 reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
1239
1240 mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
1241 reg->hr_start_block, reg->hr_blocks);
1242 mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
1243 reg->hr_block_bytes, reg->hr_block_bits);
1244 mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
1245 mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
1246}
1247
1248static int o2hb_map_slot_data(struct o2hb_region *reg)
1249{
1250 int i, j;
1251 unsigned int last_slot;
1252 unsigned int spp = reg->hr_slots_per_page;
1253 struct page *page;
1254 char *raw;
1255 struct o2hb_disk_slot *slot;
1256
1257 reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
1258 if (reg->hr_tmp_block == NULL) {
1259 mlog_errno(-ENOMEM);
1260 return -ENOMEM;
1261 }
1262
1263 reg->hr_slots = kcalloc(reg->hr_blocks,
1264 sizeof(struct o2hb_disk_slot), GFP_KERNEL);
1265 if (reg->hr_slots == NULL) {
1266 mlog_errno(-ENOMEM);
1267 return -ENOMEM;
1268 }
1269
1270 for(i = 0; i < reg->hr_blocks; i++) {
1271 slot = &reg->hr_slots[i];
1272 slot->ds_node_num = i;
1273 INIT_LIST_HEAD(&slot->ds_live_item);
1274 slot->ds_raw_block = NULL;
1275 }
1276
1277 reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
1278 mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
1279 "at %u blocks per page\n",
1280 reg->hr_num_pages, reg->hr_blocks, spp);
1281
1282 reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
1283 GFP_KERNEL);
1284 if (!reg->hr_slot_data) {
1285 mlog_errno(-ENOMEM);
1286 return -ENOMEM;
1287 }
1288
1289 for(i = 0; i < reg->hr_num_pages; i++) {
1290 page = alloc_page(GFP_KERNEL);
1291 if (!page) {
1292 mlog_errno(-ENOMEM);
1293 return -ENOMEM;
1294 }
1295
1296 reg->hr_slot_data[i] = page;
1297
1298 last_slot = i * spp;
1299 raw = page_address(page);
1300 for (j = 0;
1301 (j < spp) && ((j + last_slot) < reg->hr_blocks);
1302 j++) {
1303 BUG_ON((j + last_slot) >= reg->hr_blocks);
1304
1305 slot = &reg->hr_slots[j + last_slot];
1306 slot->ds_raw_block =
1307 (struct o2hb_disk_heartbeat_block *) raw;
1308
1309 raw += reg->hr_block_bytes;
1310 }
1311 }
1312
1313 return 0;
1314}
1315
1316/* Read in all the slots available and populate the tracking
1317 * structures so that we can start with a baseline idea of what's
1318 * there. */
1319static int o2hb_populate_slot_data(struct o2hb_region *reg)
1320{
1321 int ret, i;
1322 struct o2hb_disk_slot *slot;
1323 struct o2hb_disk_heartbeat_block *hb_block;
1324
1325 mlog_entry_void();
1326
1327 ret = o2hb_read_slots(reg, reg->hr_blocks);
1328 if (ret) {
1329 mlog_errno(ret);
1330 goto out;
1331 }
1332
1333 /* We only want to get an idea of the values initially in each
1334 * slot, so we do no verification - o2hb_check_slot will
1335 * actually determine if each configured slot is valid and
1336 * whether any values have changed. */
1337 for(i = 0; i < reg->hr_blocks; i++) {
1338 slot = &reg->hr_slots[i];
1339 hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
1340
1341 /* Only fill the values that o2hb_check_slot uses to
1342 * determine changing slots */
1343 slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
1344 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
1345 }
1346
1347out:
1348 mlog_exit(ret);
1349 return ret;
1350}
1351
1352/* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1353static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
1354 const char *page,
1355 size_t count)
1356{
e6c352db 1357 struct task_struct *hb_task;
a7f6a5fb
MF
1358 long fd;
1359 int sectsize;
1360 char *p = (char *)page;
1361 struct file *filp = NULL;
1362 struct inode *inode = NULL;
1363 ssize_t ret = -EINVAL;
1364
1365 if (reg->hr_bdev)
1366 goto out;
1367
1368 /* We can't heartbeat without having had our node number
1369 * configured yet. */
1370 if (o2nm_this_node() == O2NM_MAX_NODES)
1371 goto out;
1372
1373 fd = simple_strtol(p, &p, 0);
1374 if (!p || (*p && (*p != '\n')))
1375 goto out;
1376
1377 if (fd < 0 || fd >= INT_MAX)
1378 goto out;
1379
1380 filp = fget(fd);
1381 if (filp == NULL)
1382 goto out;
1383
1384 if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
1385 reg->hr_block_bytes == 0)
1386 goto out;
1387
1388 inode = igrab(filp->f_mapping->host);
1389 if (inode == NULL)
1390 goto out;
1391
1392 if (!S_ISBLK(inode->i_mode))
1393 goto out;
1394
1395 reg->hr_bdev = I_BDEV(filp->f_mapping->host);
572c4892 1396 ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ);
a7f6a5fb
MF
1397 if (ret) {
1398 reg->hr_bdev = NULL;
1399 goto out;
1400 }
1401 inode = NULL;
1402
1403 bdevname(reg->hr_bdev, reg->hr_dev_name);
1404
e1defc4f 1405 sectsize = bdev_logical_block_size(reg->hr_bdev);
a7f6a5fb
MF
1406 if (sectsize != reg->hr_block_bytes) {
1407 mlog(ML_ERROR,
1408 "blocksize %u incorrect for device, expected %d",
1409 reg->hr_block_bytes, sectsize);
1410 ret = -EINVAL;
1411 goto out;
1412 }
1413
1414 o2hb_init_region_params(reg);
1415
1416 /* Generation of zero is invalid */
1417 do {
1418 get_random_bytes(&reg->hr_generation,
1419 sizeof(reg->hr_generation));
1420 } while (reg->hr_generation == 0);
1421
1422 ret = o2hb_map_slot_data(reg);
1423 if (ret) {
1424 mlog_errno(ret);
1425 goto out;
1426 }
1427
1428 ret = o2hb_populate_slot_data(reg);
1429 if (ret) {
1430 mlog_errno(ret);
1431 goto out;
1432 }
1433
c4028958 1434 INIT_DELAYED_WORK(&reg->hr_write_timeout_work, o2hb_write_timeout);
a7f6a5fb
MF
1435
1436 /*
1437 * A node is considered live after it has beat LIVE_THRESHOLD
1438 * times. We're not steady until we've given them a chance
1439 * _after_ our first read.
1440 */
1441 atomic_set(&reg->hr_steady_iterations, O2HB_LIVE_THRESHOLD + 1);
1442
e6c352db
JB
1443 hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
1444 reg->hr_item.ci_name);
1445 if (IS_ERR(hb_task)) {
1446 ret = PTR_ERR(hb_task);
a7f6a5fb 1447 mlog_errno(ret);
a7f6a5fb
MF
1448 goto out;
1449 }
1450
e6c352db
JB
1451 spin_lock(&o2hb_live_lock);
1452 reg->hr_task = hb_task;
1453 spin_unlock(&o2hb_live_lock);
1454
a7f6a5fb
MF
1455 ret = wait_event_interruptible(o2hb_steady_queue,
1456 atomic_read(&reg->hr_steady_iterations) == 0);
1457 if (ret) {
e6df3a66 1458 /* We got interrupted (hello ptrace!). Clean up */
e6c352db
JB
1459 spin_lock(&o2hb_live_lock);
1460 hb_task = reg->hr_task;
a7f6a5fb 1461 reg->hr_task = NULL;
e6c352db
JB
1462 spin_unlock(&o2hb_live_lock);
1463
1464 if (hb_task)
1465 kthread_stop(hb_task);
a7f6a5fb
MF
1466 goto out;
1467 }
1468
e6df3a66
JB
1469 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1470 spin_lock(&o2hb_live_lock);
1471 hb_task = reg->hr_task;
1472 spin_unlock(&o2hb_live_lock);
1473
1474 if (hb_task)
1475 ret = count;
1476 else
1477 ret = -EIO;
1478
a7f6a5fb
MF
1479out:
1480 if (filp)
1481 fput(filp);
1482 if (inode)
1483 iput(inode);
1484 if (ret < 0) {
1485 if (reg->hr_bdev) {
9a1c3542 1486 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
a7f6a5fb
MF
1487 reg->hr_bdev = NULL;
1488 }
1489 }
1490 return ret;
1491}
1492
92efc152
ZW
1493static ssize_t o2hb_region_pid_read(struct o2hb_region *reg,
1494 char *page)
1495{
e6c352db
JB
1496 pid_t pid = 0;
1497
1498 spin_lock(&o2hb_live_lock);
1499 if (reg->hr_task)
ba25f9dc 1500 pid = task_pid_nr(reg->hr_task);
e6c352db
JB
1501 spin_unlock(&o2hb_live_lock);
1502
1503 if (!pid)
92efc152
ZW
1504 return 0;
1505
e6c352db 1506 return sprintf(page, "%u\n", pid);
92efc152
ZW
1507}
1508
a7f6a5fb
MF
1509struct o2hb_region_attribute {
1510 struct configfs_attribute attr;
1511 ssize_t (*show)(struct o2hb_region *, char *);
1512 ssize_t (*store)(struct o2hb_region *, const char *, size_t);
1513};
1514
1515static struct o2hb_region_attribute o2hb_region_attr_block_bytes = {
1516 .attr = { .ca_owner = THIS_MODULE,
1517 .ca_name = "block_bytes",
1518 .ca_mode = S_IRUGO | S_IWUSR },
1519 .show = o2hb_region_block_bytes_read,
1520 .store = o2hb_region_block_bytes_write,
1521};
1522
1523static struct o2hb_region_attribute o2hb_region_attr_start_block = {
1524 .attr = { .ca_owner = THIS_MODULE,
1525 .ca_name = "start_block",
1526 .ca_mode = S_IRUGO | S_IWUSR },
1527 .show = o2hb_region_start_block_read,
1528 .store = o2hb_region_start_block_write,
1529};
1530
1531static struct o2hb_region_attribute o2hb_region_attr_blocks = {
1532 .attr = { .ca_owner = THIS_MODULE,
1533 .ca_name = "blocks",
1534 .ca_mode = S_IRUGO | S_IWUSR },
1535 .show = o2hb_region_blocks_read,
1536 .store = o2hb_region_blocks_write,
1537};
1538
1539static struct o2hb_region_attribute o2hb_region_attr_dev = {
1540 .attr = { .ca_owner = THIS_MODULE,
1541 .ca_name = "dev",
1542 .ca_mode = S_IRUGO | S_IWUSR },
1543 .show = o2hb_region_dev_read,
1544 .store = o2hb_region_dev_write,
1545};
1546
92efc152
ZW
1547static struct o2hb_region_attribute o2hb_region_attr_pid = {
1548 .attr = { .ca_owner = THIS_MODULE,
1549 .ca_name = "pid",
1550 .ca_mode = S_IRUGO | S_IRUSR },
1551 .show = o2hb_region_pid_read,
1552};
1553
a7f6a5fb
MF
1554static struct configfs_attribute *o2hb_region_attrs[] = {
1555 &o2hb_region_attr_block_bytes.attr,
1556 &o2hb_region_attr_start_block.attr,
1557 &o2hb_region_attr_blocks.attr,
1558 &o2hb_region_attr_dev.attr,
92efc152 1559 &o2hb_region_attr_pid.attr,
a7f6a5fb
MF
1560 NULL,
1561};
1562
1563static ssize_t o2hb_region_show(struct config_item *item,
1564 struct configfs_attribute *attr,
1565 char *page)
1566{
1567 struct o2hb_region *reg = to_o2hb_region(item);
1568 struct o2hb_region_attribute *o2hb_region_attr =
1569 container_of(attr, struct o2hb_region_attribute, attr);
1570 ssize_t ret = 0;
1571
1572 if (o2hb_region_attr->show)
1573 ret = o2hb_region_attr->show(reg, page);
1574 return ret;
1575}
1576
1577static ssize_t o2hb_region_store(struct config_item *item,
1578 struct configfs_attribute *attr,
1579 const char *page, size_t count)
1580{
1581 struct o2hb_region *reg = to_o2hb_region(item);
1582 struct o2hb_region_attribute *o2hb_region_attr =
1583 container_of(attr, struct o2hb_region_attribute, attr);
1584 ssize_t ret = -EINVAL;
1585
1586 if (o2hb_region_attr->store)
1587 ret = o2hb_region_attr->store(reg, page, count);
1588 return ret;
1589}
1590
1591static struct configfs_item_operations o2hb_region_item_ops = {
1592 .release = o2hb_region_release,
1593 .show_attribute = o2hb_region_show,
1594 .store_attribute = o2hb_region_store,
1595};
1596
1597static struct config_item_type o2hb_region_type = {
1598 .ct_item_ops = &o2hb_region_item_ops,
1599 .ct_attrs = o2hb_region_attrs,
1600 .ct_owner = THIS_MODULE,
1601};
1602
1603/* heartbeat set */
1604
1605struct o2hb_heartbeat_group {
1606 struct config_group hs_group;
1607 /* some stuff? */
1608};
1609
1610static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
1611{
1612 return group ?
1613 container_of(group, struct o2hb_heartbeat_group, hs_group)
1614 : NULL;
1615}
1616
f89ab861
JB
1617static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
1618 const char *name)
a7f6a5fb
MF
1619{
1620 struct o2hb_region *reg = NULL;
a7f6a5fb 1621
cd861280 1622 reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
f89ab861 1623 if (reg == NULL)
a6795e9e 1624 return ERR_PTR(-ENOMEM);
a7f6a5fb
MF
1625
1626 config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
1627
a7f6a5fb
MF
1628 spin_lock(&o2hb_live_lock);
1629 list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
1630 spin_unlock(&o2hb_live_lock);
a7f6a5fb 1631
a6795e9e 1632 return &reg->hr_item;
a7f6a5fb
MF
1633}
1634
1635static void o2hb_heartbeat_group_drop_item(struct config_group *group,
1636 struct config_item *item)
1637{
e6c352db 1638 struct task_struct *hb_task;
a7f6a5fb
MF
1639 struct o2hb_region *reg = to_o2hb_region(item);
1640
1641 /* stop the thread when the user removes the region dir */
e6c352db
JB
1642 spin_lock(&o2hb_live_lock);
1643 hb_task = reg->hr_task;
1644 reg->hr_task = NULL;
1645 spin_unlock(&o2hb_live_lock);
1646
1647 if (hb_task)
1648 kthread_stop(hb_task);
a7f6a5fb 1649
e6df3a66
JB
1650 /*
1651 * If we're racing a dev_write(), we need to wake them. They will
1652 * check reg->hr_task
1653 */
1654 if (atomic_read(&reg->hr_steady_iterations) != 0) {
1655 atomic_set(&reg->hr_steady_iterations, 0);
1656 wake_up(&o2hb_steady_queue);
1657 }
1658
a7f6a5fb
MF
1659 config_item_put(item);
1660}
1661
1662struct o2hb_heartbeat_group_attribute {
1663 struct configfs_attribute attr;
1664 ssize_t (*show)(struct o2hb_heartbeat_group *, char *);
1665 ssize_t (*store)(struct o2hb_heartbeat_group *, const char *, size_t);
1666};
1667
1668static ssize_t o2hb_heartbeat_group_show(struct config_item *item,
1669 struct configfs_attribute *attr,
1670 char *page)
1671{
1672 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1673 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1674 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1675 ssize_t ret = 0;
1676
1677 if (o2hb_heartbeat_group_attr->show)
1678 ret = o2hb_heartbeat_group_attr->show(reg, page);
1679 return ret;
1680}
1681
1682static ssize_t o2hb_heartbeat_group_store(struct config_item *item,
1683 struct configfs_attribute *attr,
1684 const char *page, size_t count)
1685{
1686 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1687 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1688 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1689 ssize_t ret = -EINVAL;
1690
1691 if (o2hb_heartbeat_group_attr->store)
1692 ret = o2hb_heartbeat_group_attr->store(reg, page, count);
1693 return ret;
1694}
1695
1696static ssize_t o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group *group,
1697 char *page)
1698{
1699 return sprintf(page, "%u\n", o2hb_dead_threshold);
1700}
1701
1702static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group *group,
1703 const char *page,
1704 size_t count)
1705{
1706 unsigned long tmp;
1707 char *p = (char *)page;
1708
1709 tmp = simple_strtoul(p, &p, 10);
1710 if (!p || (*p && (*p != '\n')))
1711 return -EINVAL;
1712
1713 /* this will validate ranges for us. */
1714 o2hb_dead_threshold_set((unsigned int) tmp);
1715
1716 return count;
1717}
1718
54b5187b
SM
1719static
1720ssize_t o2hb_heartbeat_group_mode_show(struct o2hb_heartbeat_group *group,
1721 char *page)
1722{
1723 return sprintf(page, "%s\n",
1724 o2hb_heartbeat_mode_desc[o2hb_heartbeat_mode]);
1725}
1726
1727static
1728ssize_t o2hb_heartbeat_group_mode_store(struct o2hb_heartbeat_group *group,
1729 const char *page, size_t count)
1730{
1731 unsigned int i;
1732 int ret;
1733 size_t len;
1734
1735 len = (page[count - 1] == '\n') ? count - 1 : count;
1736 if (!len)
1737 return -EINVAL;
1738
1739 for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) {
1740 if (strnicmp(page, o2hb_heartbeat_mode_desc[i], len))
1741 continue;
1742
1743 ret = o2hb_global_hearbeat_mode_set(i);
1744 if (!ret)
1745 printk(KERN_NOTICE "ocfs2: Heartbeat mode set to %s\n",
1746 o2hb_heartbeat_mode_desc[i]);
1747 return count;
1748 }
1749
1750 return -EINVAL;
1751
1752}
1753
a7f6a5fb
MF
1754static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold = {
1755 .attr = { .ca_owner = THIS_MODULE,
1756 .ca_name = "dead_threshold",
1757 .ca_mode = S_IRUGO | S_IWUSR },
1758 .show = o2hb_heartbeat_group_threshold_show,
1759 .store = o2hb_heartbeat_group_threshold_store,
1760};
1761
54b5187b
SM
1762static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_mode = {
1763 .attr = { .ca_owner = THIS_MODULE,
1764 .ca_name = "mode",
1765 .ca_mode = S_IRUGO | S_IWUSR },
1766 .show = o2hb_heartbeat_group_mode_show,
1767 .store = o2hb_heartbeat_group_mode_store,
1768};
1769
a7f6a5fb
MF
1770static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
1771 &o2hb_heartbeat_group_attr_threshold.attr,
54b5187b 1772 &o2hb_heartbeat_group_attr_mode.attr,
a7f6a5fb
MF
1773 NULL,
1774};
1775
1776static struct configfs_item_operations o2hb_hearbeat_group_item_ops = {
1777 .show_attribute = o2hb_heartbeat_group_show,
1778 .store_attribute = o2hb_heartbeat_group_store,
1779};
1780
1781static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
1782 .make_item = o2hb_heartbeat_group_make_item,
1783 .drop_item = o2hb_heartbeat_group_drop_item,
1784};
1785
1786static struct config_item_type o2hb_heartbeat_group_type = {
1787 .ct_group_ops = &o2hb_heartbeat_group_group_ops,
1788 .ct_item_ops = &o2hb_hearbeat_group_item_ops,
1789 .ct_attrs = o2hb_heartbeat_group_attrs,
1790 .ct_owner = THIS_MODULE,
1791};
1792
1793/* this is just here to avoid touching group in heartbeat.h which the
1794 * entire damn world #includes */
1795struct config_group *o2hb_alloc_hb_set(void)
1796{
1797 struct o2hb_heartbeat_group *hs = NULL;
1798 struct config_group *ret = NULL;
1799
cd861280 1800 hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
a7f6a5fb
MF
1801 if (hs == NULL)
1802 goto out;
1803
1804 config_group_init_type_name(&hs->hs_group, "heartbeat",
1805 &o2hb_heartbeat_group_type);
1806
1807 ret = &hs->hs_group;
1808out:
1809 if (ret == NULL)
1810 kfree(hs);
1811 return ret;
1812}
1813
1814void o2hb_free_hb_set(struct config_group *group)
1815{
1816 struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
1817 kfree(hs);
1818}
1819
1820/* hb callback registration and issueing */
1821
1822static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
1823{
1824 if (type == O2HB_NUM_CB)
1825 return ERR_PTR(-EINVAL);
1826
1827 return &o2hb_callbacks[type];
1828}
1829
1830void o2hb_setup_callback(struct o2hb_callback_func *hc,
1831 enum o2hb_callback_type type,
1832 o2hb_cb_func *func,
1833 void *data,
1834 int priority)
1835{
1836 INIT_LIST_HEAD(&hc->hc_item);
1837 hc->hc_func = func;
1838 hc->hc_data = data;
1839 hc->hc_priority = priority;
1840 hc->hc_type = type;
1841 hc->hc_magic = O2HB_CB_MAGIC;
1842}
1843EXPORT_SYMBOL_GPL(o2hb_setup_callback);
1844
14829422
JB
1845static struct o2hb_region *o2hb_find_region(const char *region_uuid)
1846{
1847 struct o2hb_region *p, *reg = NULL;
1848
1849 assert_spin_locked(&o2hb_live_lock);
1850
1851 list_for_each_entry(p, &o2hb_all_regions, hr_all_item) {
1852 if (!strcmp(region_uuid, config_item_name(&p->hr_item))) {
1853 reg = p;
1854 break;
1855 }
1856 }
1857
1858 return reg;
1859}
1860
1861static int o2hb_region_get(const char *region_uuid)
1862{
1863 int ret = 0;
1864 struct o2hb_region *reg;
1865
1866 spin_lock(&o2hb_live_lock);
1867
1868 reg = o2hb_find_region(region_uuid);
1869 if (!reg)
1870 ret = -ENOENT;
1871 spin_unlock(&o2hb_live_lock);
1872
16c6a4f2
JB
1873 if (ret)
1874 goto out;
1875
1876 ret = o2nm_depend_this_node();
1877 if (ret)
1878 goto out;
14829422 1879
16c6a4f2
JB
1880 ret = o2nm_depend_item(&reg->hr_item);
1881 if (ret)
1882 o2nm_undepend_this_node();
1883
1884out:
14829422
JB
1885 return ret;
1886}
1887
1888static void o2hb_region_put(const char *region_uuid)
1889{
1890 struct o2hb_region *reg;
1891
1892 spin_lock(&o2hb_live_lock);
1893
1894 reg = o2hb_find_region(region_uuid);
1895
1896 spin_unlock(&o2hb_live_lock);
1897
16c6a4f2 1898 if (reg) {
14829422 1899 o2nm_undepend_item(&reg->hr_item);
16c6a4f2
JB
1900 o2nm_undepend_this_node();
1901 }
14829422
JB
1902}
1903
1904int o2hb_register_callback(const char *region_uuid,
1905 struct o2hb_callback_func *hc)
a7f6a5fb
MF
1906{
1907 struct o2hb_callback_func *tmp;
1908 struct list_head *iter;
1909 struct o2hb_callback *hbcall;
1910 int ret;
1911
1912 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
1913 BUG_ON(!list_empty(&hc->hc_item));
1914
1915 hbcall = hbcall_from_type(hc->hc_type);
1916 if (IS_ERR(hbcall)) {
1917 ret = PTR_ERR(hbcall);
1918 goto out;
1919 }
1920
14829422
JB
1921 if (region_uuid) {
1922 ret = o2hb_region_get(region_uuid);
1923 if (ret)
1924 goto out;
1925 }
1926
a7f6a5fb
MF
1927 down_write(&o2hb_callback_sem);
1928
1929 list_for_each(iter, &hbcall->list) {
1930 tmp = list_entry(iter, struct o2hb_callback_func, hc_item);
1931 if (hc->hc_priority < tmp->hc_priority) {
1932 list_add_tail(&hc->hc_item, iter);
1933 break;
1934 }
1935 }
1936 if (list_empty(&hc->hc_item))
1937 list_add_tail(&hc->hc_item, &hbcall->list);
1938
1939 up_write(&o2hb_callback_sem);
1940 ret = 0;
1941out:
1942 mlog(ML_HEARTBEAT, "returning %d on behalf of %p for funcs %p\n",
1943 ret, __builtin_return_address(0), hc);
1944 return ret;
1945}
1946EXPORT_SYMBOL_GPL(o2hb_register_callback);
1947
14829422
JB
1948void o2hb_unregister_callback(const char *region_uuid,
1949 struct o2hb_callback_func *hc)
a7f6a5fb
MF
1950{
1951 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
1952
1953 mlog(ML_HEARTBEAT, "on behalf of %p for funcs %p\n",
1954 __builtin_return_address(0), hc);
1955
14829422 1956 /* XXX Can this happen _with_ a region reference? */
a7f6a5fb 1957 if (list_empty(&hc->hc_item))
c24f72cc 1958 return;
a7f6a5fb 1959
14829422
JB
1960 if (region_uuid)
1961 o2hb_region_put(region_uuid);
1962
a7f6a5fb
MF
1963 down_write(&o2hb_callback_sem);
1964
1965 list_del_init(&hc->hc_item);
1966
1967 up_write(&o2hb_callback_sem);
a7f6a5fb
MF
1968}
1969EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
1970
1971int o2hb_check_node_heartbeating(u8 node_num)
1972{
1973 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1974
1975 o2hb_fill_node_map(testing_map, sizeof(testing_map));
1976 if (!test_bit(node_num, testing_map)) {
1977 mlog(ML_HEARTBEAT,
1978 "node (%u) does not have heartbeating enabled.\n",
1979 node_num);
1980 return 0;
1981 }
1982
1983 return 1;
1984}
1985EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating);
1986
1987int o2hb_check_node_heartbeating_from_callback(u8 node_num)
1988{
1989 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1990
1991 o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
1992 if (!test_bit(node_num, testing_map)) {
1993 mlog(ML_HEARTBEAT,
1994 "node (%u) does not have heartbeating enabled.\n",
1995 node_num);
1996 return 0;
1997 }
1998
1999 return 1;
2000}
2001EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
2002
2003/* Makes sure our local node is configured with a node number, and is
2004 * heartbeating. */
2005int o2hb_check_local_node_heartbeating(void)
2006{
2007 u8 node_num;
2008
2009 /* if this node was set then we have networking */
2010 node_num = o2nm_this_node();
2011 if (node_num == O2NM_MAX_NODES) {
2012 mlog(ML_HEARTBEAT, "this node has not been configured.\n");
2013 return 0;
2014 }
2015
2016 return o2hb_check_node_heartbeating(node_num);
2017}
2018EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating);
2019
2020/*
2021 * this is just a hack until we get the plumbing which flips file systems
2022 * read only and drops the hb ref instead of killing the node dead.
2023 */
2024void o2hb_stop_all_regions(void)
2025{
2026 struct o2hb_region *reg;
2027
2028 mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
2029
2030 spin_lock(&o2hb_live_lock);
2031
2032 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
2033 reg->hr_unclean_stop = 1;
2034
2035 spin_unlock(&o2hb_live_lock);
2036}
2037EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);