]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/ocfs2/cluster/heartbeat.c
ocfs2/cluster: Check slots for unconfigured live nodes
[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
0e105d37
SM
544 BUG_ON((!node) && (type != O2HB_NODE_DOWN_CB));
545
a7f6a5fb
MF
546 event->hn_event_type = type;
547 event->hn_node = node;
548 event->hn_node_num = node_num;
549
550 mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
551 type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
552
553 list_add_tail(&event->hn_item, &o2hb_node_events);
554}
555
556static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
557{
558 struct o2hb_node_event event =
559 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
560 struct o2nm_node *node;
561
562 node = o2nm_get_node_by_num(slot->ds_node_num);
563 if (!node)
564 return;
565
566 spin_lock(&o2hb_live_lock);
567 if (!list_empty(&slot->ds_live_item)) {
568 mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
569 slot->ds_node_num);
570
571 list_del_init(&slot->ds_live_item);
572
573 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
574 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
575
576 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
577 slot->ds_node_num);
578 }
579 }
580 spin_unlock(&o2hb_live_lock);
581
582 o2hb_run_event_list(&event);
583
584 o2nm_node_put(node);
585}
586
587static int o2hb_check_slot(struct o2hb_region *reg,
588 struct o2hb_disk_slot *slot)
589{
590 int changed = 0, gen_changed = 0;
591 struct o2hb_node_event event =
592 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
593 struct o2nm_node *node;
594 struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
595 u64 cputime;
0db638f4
MF
596 unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
597 unsigned int slot_dead_ms;
0e105d37 598 int tmp;
a7f6a5fb
MF
599
600 memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
601
0e105d37
SM
602 /*
603 * If a node is no longer configured but is still in the livemap, we
604 * may need to clear that bit from the livemap.
605 */
a7f6a5fb 606 node = o2nm_get_node_by_num(slot->ds_node_num);
0e105d37
SM
607 if (!node) {
608 spin_lock(&o2hb_live_lock);
609 tmp = test_bit(slot->ds_node_num, o2hb_live_node_bitmap);
610 spin_unlock(&o2hb_live_lock);
611 if (!tmp)
612 return 0;
613 }
a7f6a5fb
MF
614
615 if (!o2hb_verify_crc(reg, hb_block)) {
616 /* all paths from here will drop o2hb_live_lock for
617 * us. */
618 spin_lock(&o2hb_live_lock);
619
620 /* Don't print an error on the console in this case -
621 * a freshly formatted heartbeat area will not have a
622 * crc set on it. */
623 if (list_empty(&slot->ds_live_item))
624 goto out;
625
626 /* The node is live but pushed out a bad crc. We
627 * consider it a transient miss but don't populate any
628 * other values as they may be junk. */
629 mlog(ML_ERROR, "Node %d has written a bad crc to %s\n",
630 slot->ds_node_num, reg->hr_dev_name);
631 o2hb_dump_slot(hb_block);
632
633 slot->ds_equal_samples++;
634 goto fire_callbacks;
635 }
636
637 /* we don't care if these wrap.. the state transitions below
638 * clear at the right places */
639 cputime = le64_to_cpu(hb_block->hb_seq);
640 if (slot->ds_last_time != cputime)
641 slot->ds_changed_samples++;
642 else
643 slot->ds_equal_samples++;
644 slot->ds_last_time = cputime;
645
646 /* The node changed heartbeat generations. We assume this to
647 * mean it dropped off but came back before we timed out. We
648 * want to consider it down for the time being but don't want
649 * to lose any changed_samples state we might build up to
650 * considering it live again. */
651 if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
652 gen_changed = 1;
653 slot->ds_equal_samples = 0;
70bacbdb
MF
654 mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
655 "to 0x%llx)\n", slot->ds_node_num,
656 (long long)slot->ds_last_generation,
657 (long long)le64_to_cpu(hb_block->hb_generation));
a7f6a5fb
MF
658 }
659
660 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
661
70bacbdb
MF
662 mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
663 "seq %llu last %llu changed %u equal %u\n",
664 slot->ds_node_num, (long long)slot->ds_last_generation,
665 le32_to_cpu(hb_block->hb_cksum),
2bd63216 666 (unsigned long long)le64_to_cpu(hb_block->hb_seq),
70bacbdb 667 (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
a7f6a5fb
MF
668 slot->ds_equal_samples);
669
670 spin_lock(&o2hb_live_lock);
671
672fire_callbacks:
673 /* dead nodes only come to life after some number of
674 * changes at any time during their dead time */
675 if (list_empty(&slot->ds_live_item) &&
676 slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
70bacbdb
MF
677 mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
678 slot->ds_node_num, (long long)slot->ds_last_generation);
a7f6a5fb
MF
679
680 /* first on the list generates a callback */
681 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
682 set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
683
684 o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
685 slot->ds_node_num);
686
687 changed = 1;
688 }
689
690 list_add_tail(&slot->ds_live_item,
691 &o2hb_live_slots[slot->ds_node_num]);
692
693 slot->ds_equal_samples = 0;
0db638f4
MF
694
695 /* We want to be sure that all nodes agree on the
696 * number of milliseconds before a node will be
697 * considered dead. The self-fencing timeout is
698 * computed from this value, and a discrepancy might
699 * result in heartbeat calling a node dead when it
700 * hasn't self-fenced yet. */
701 slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
702 if (slot_dead_ms && slot_dead_ms != dead_ms) {
703 /* TODO: Perhaps we can fail the region here. */
704 mlog(ML_ERROR, "Node %d on device %s has a dead count "
705 "of %u ms, but our count is %u ms.\n"
706 "Please double check your configuration values "
707 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
708 slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
709 dead_ms);
710 }
a7f6a5fb
MF
711 goto out;
712 }
713
714 /* if the list is dead, we're done.. */
715 if (list_empty(&slot->ds_live_item))
716 goto out;
717
718 /* live nodes only go dead after enough consequtive missed
719 * samples.. reset the missed counter whenever we see
720 * activity */
721 if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
722 mlog(ML_HEARTBEAT, "Node %d left my region\n",
723 slot->ds_node_num);
724
725 /* last off the live_slot generates a callback */
726 list_del_init(&slot->ds_live_item);
727 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
728 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
729
0e105d37
SM
730 /* node can be null */
731 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
732 node, slot->ds_node_num);
a7f6a5fb
MF
733
734 changed = 1;
735 }
736
737 /* We don't clear this because the node is still
738 * actually writing new blocks. */
739 if (!gen_changed)
740 slot->ds_changed_samples = 0;
741 goto out;
742 }
743 if (slot->ds_changed_samples) {
744 slot->ds_changed_samples = 0;
745 slot->ds_equal_samples = 0;
746 }
747out:
748 spin_unlock(&o2hb_live_lock);
749
750 o2hb_run_event_list(&event);
751
0e105d37
SM
752 if (node)
753 o2nm_node_put(node);
a7f6a5fb
MF
754 return changed;
755}
756
757/* This could be faster if we just implmented a find_last_bit, but I
758 * don't think the circumstances warrant it. */
759static int o2hb_highest_node(unsigned long *nodes,
760 int numbits)
761{
762 int highest, node;
763
764 highest = numbits;
765 node = -1;
766 while ((node = find_next_bit(nodes, numbits, node + 1)) != -1) {
767 if (node >= numbits)
768 break;
769
770 highest = node;
771 }
772
773 return highest;
774}
775
a9e2ae39 776static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
a7f6a5fb
MF
777{
778 int i, ret, highest_node, change = 0;
779 unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
0e105d37 780 unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
a7f6a5fb
MF
781 struct o2hb_bio_wait_ctxt write_wc;
782
a9e2ae39
MF
783 ret = o2nm_configured_node_map(configured_nodes,
784 sizeof(configured_nodes));
785 if (ret) {
786 mlog_errno(ret);
787 return ret;
788 }
a7f6a5fb 789
0e105d37
SM
790 /*
791 * If a node is not configured but is in the livemap, we still need
792 * to read the slot so as to be able to remove it from the livemap.
793 */
794 o2hb_fill_node_map(live_node_bitmap, sizeof(live_node_bitmap));
795 i = -1;
796 while ((i = find_next_bit(live_node_bitmap,
797 O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
798 set_bit(i, configured_nodes);
799 }
800
a7f6a5fb
MF
801 highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
802 if (highest_node >= O2NM_MAX_NODES) {
803 mlog(ML_NOTICE, "ocfs2_heartbeat: no configured nodes found!\n");
a9e2ae39 804 return -EINVAL;
a7f6a5fb
MF
805 }
806
807 /* No sense in reading the slots of nodes that don't exist
808 * yet. Of course, if the node definitions have holes in them
809 * then we're reading an empty slot anyway... Consider this
810 * best-effort. */
811 ret = o2hb_read_slots(reg, highest_node + 1);
812 if (ret < 0) {
813 mlog_errno(ret);
a9e2ae39 814 return ret;
a7f6a5fb
MF
815 }
816
817 /* With an up to date view of the slots, we can check that no
818 * other node has been improperly configured to heartbeat in
819 * our slot. */
820 if (!o2hb_check_last_timestamp(reg))
821 mlog(ML_ERROR, "Device \"%s\": another node is heartbeating "
822 "in our slot!\n", reg->hr_dev_name);
823
824 /* fill in the proper info for our next heartbeat */
825 o2hb_prepare_block(reg, reg->hr_generation);
826
827 /* And fire off the write. Note that we don't wait on this I/O
828 * until later. */
b559292e 829 ret = o2hb_issue_node_write(reg, &write_wc);
a7f6a5fb
MF
830 if (ret < 0) {
831 mlog_errno(ret);
a9e2ae39 832 return ret;
a7f6a5fb
MF
833 }
834
835 i = -1;
836 while((i = find_next_bit(configured_nodes, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
837
838 change |= o2hb_check_slot(reg, &reg->hr_slots[i]);
839 }
840
841 /*
842 * We have to be sure we've advertised ourselves on disk
843 * before we can go to steady state. This ensures that
844 * people we find in our steady state have seen us.
845 */
846 o2hb_wait_on_io(reg, &write_wc);
a9e2ae39
MF
847 if (write_wc.wc_error) {
848 /* Do not re-arm the write timeout on I/O error - we
849 * can't be sure that the new block ever made it to
850 * disk */
851 mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
852 write_wc.wc_error, reg->hr_dev_name);
853 return write_wc.wc_error;
854 }
855
a7f6a5fb
MF
856 o2hb_arm_write_timeout(reg);
857
858 /* let the person who launched us know when things are steady */
859 if (!change && (atomic_read(&reg->hr_steady_iterations) != 0)) {
860 if (atomic_dec_and_test(&reg->hr_steady_iterations))
861 wake_up(&o2hb_steady_queue);
862 }
a9e2ae39
MF
863
864 return 0;
a7f6a5fb
MF
865}
866
867/* Subtract b from a, storing the result in a. a *must* have a larger
868 * value than b. */
869static void o2hb_tv_subtract(struct timeval *a,
870 struct timeval *b)
871{
872 /* just return 0 when a is after b */
873 if (a->tv_sec < b->tv_sec ||
874 (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) {
875 a->tv_sec = 0;
876 a->tv_usec = 0;
877 return;
878 }
879
880 a->tv_sec -= b->tv_sec;
881 a->tv_usec -= b->tv_usec;
882 while ( a->tv_usec < 0 ) {
883 a->tv_sec--;
884 a->tv_usec += 1000000;
885 }
886}
887
888static unsigned int o2hb_elapsed_msecs(struct timeval *start,
889 struct timeval *end)
890{
891 struct timeval res = *end;
892
893 o2hb_tv_subtract(&res, start);
894
895 return res.tv_sec * 1000 + res.tv_usec / 1000;
896}
897
898/*
899 * we ride the region ref that the region dir holds. before the region
900 * dir is removed and drops it ref it will wait to tear down this
901 * thread.
902 */
903static int o2hb_thread(void *data)
904{
905 int i, ret;
906 struct o2hb_region *reg = data;
a7f6a5fb
MF
907 struct o2hb_bio_wait_ctxt write_wc;
908 struct timeval before_hb, after_hb;
909 unsigned int elapsed_msec;
910
911 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
912
913 set_user_nice(current, -20);
914
915 while (!kthread_should_stop() && !reg->hr_unclean_stop) {
916 /* We track the time spent inside
025dfdaf 917 * o2hb_do_disk_heartbeat so that we avoid more than
a7f6a5fb
MF
918 * hr_timeout_ms between disk writes. On busy systems
919 * this should result in a heartbeat which is less
920 * likely to time itself out. */
921 do_gettimeofday(&before_hb);
922
a9e2ae39
MF
923 i = 0;
924 do {
925 ret = o2hb_do_disk_heartbeat(reg);
926 } while (ret && ++i < 2);
a7f6a5fb
MF
927
928 do_gettimeofday(&after_hb);
929 elapsed_msec = o2hb_elapsed_msecs(&before_hb, &after_hb);
930
b31d308d
TM
931 mlog(ML_HEARTBEAT,
932 "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
215c7f9f
MF
933 before_hb.tv_sec, (unsigned long) before_hb.tv_usec,
934 after_hb.tv_sec, (unsigned long) after_hb.tv_usec,
935 elapsed_msec);
a7f6a5fb
MF
936
937 if (elapsed_msec < reg->hr_timeout_ms) {
938 /* the kthread api has blocked signals for us so no
939 * need to record the return value. */
940 msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
941 }
942 }
943
944 o2hb_disarm_write_timeout(reg);
945
946 /* unclean stop is only used in very bad situation */
947 for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
948 o2hb_shutdown_slot(&reg->hr_slots[i]);
949
950 /* Explicit down notification - avoid forcing the other nodes
951 * to timeout on this region when we could just as easily
952 * write a clear generation - thus indicating to them that
953 * this node has left this region.
954 *
955 * XXX: Should we skip this on unclean_stop? */
956 o2hb_prepare_block(reg, 0);
b559292e 957 ret = o2hb_issue_node_write(reg, &write_wc);
a7f6a5fb
MF
958 if (ret == 0) {
959 o2hb_wait_on_io(reg, &write_wc);
a7f6a5fb
MF
960 } else {
961 mlog_errno(ret);
962 }
963
964 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread exiting\n");
965
966 return 0;
967}
968
87d3d3f3
SM
969#ifdef CONFIG_DEBUG_FS
970static int o2hb_debug_open(struct inode *inode, struct file *file)
971{
972 unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
973 char *buf = NULL;
974 int i = -1;
975 int out = 0;
976
977 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
978 if (!buf)
979 goto bail;
980
981 o2hb_fill_node_map(map, sizeof(map));
982
983 while ((i = find_next_bit(map, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES)
984 out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
985 out += snprintf(buf + out, PAGE_SIZE - out, "\n");
986
987 i_size_write(inode, out);
988
989 file->private_data = buf;
990
991 return 0;
992bail:
993 return -ENOMEM;
994}
995
996static int o2hb_debug_release(struct inode *inode, struct file *file)
997{
998 kfree(file->private_data);
999 return 0;
1000}
1001
1002static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1003 size_t nbytes, loff_t *ppos)
1004{
1005 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
1006 i_size_read(file->f_mapping->host));
1007}
1008#else
1009static int o2hb_debug_open(struct inode *inode, struct file *file)
1010{
1011 return 0;
1012}
1013static int o2hb_debug_release(struct inode *inode, struct file *file)
1014{
1015 return 0;
1016}
1017static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1018 size_t nbytes, loff_t *ppos)
1019{
1020 return 0;
1021}
1022#endif /* CONFIG_DEBUG_FS */
1023
828c0950 1024static const struct file_operations o2hb_debug_fops = {
87d3d3f3
SM
1025 .open = o2hb_debug_open,
1026 .release = o2hb_debug_release,
1027 .read = o2hb_debug_read,
1028 .llseek = generic_file_llseek,
1029};
1030
1031void o2hb_exit(void)
1032{
1033 if (o2hb_debug_livenodes)
1034 debugfs_remove(o2hb_debug_livenodes);
1035 if (o2hb_debug_dir)
1036 debugfs_remove(o2hb_debug_dir);
1037}
1038
1039int o2hb_init(void)
a7f6a5fb
MF
1040{
1041 int i;
1042
1043 for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
1044 INIT_LIST_HEAD(&o2hb_callbacks[i].list);
1045
1046 for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
1047 INIT_LIST_HEAD(&o2hb_live_slots[i]);
1048
1049 INIT_LIST_HEAD(&o2hb_node_events);
1050
1051 memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
87d3d3f3
SM
1052
1053 o2hb_debug_dir = debugfs_create_dir(O2HB_DEBUG_DIR, NULL);
1054 if (!o2hb_debug_dir) {
1055 mlog_errno(-ENOMEM);
1056 return -ENOMEM;
1057 }
1058
1059 o2hb_debug_livenodes = debugfs_create_file(O2HB_DEBUG_LIVENODES,
1060 S_IFREG|S_IRUSR,
1061 o2hb_debug_dir, NULL,
1062 &o2hb_debug_fops);
1063 if (!o2hb_debug_livenodes) {
1064 mlog_errno(-ENOMEM);
1065 debugfs_remove(o2hb_debug_dir);
1066 return -ENOMEM;
1067 }
1068
1069 return 0;
a7f6a5fb
MF
1070}
1071
1072/* if we're already in a callback then we're already serialized by the sem */
1073static void o2hb_fill_node_map_from_callback(unsigned long *map,
1074 unsigned bytes)
1075{
1076 BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
1077
1078 memcpy(map, &o2hb_live_node_bitmap, bytes);
1079}
1080
1081/*
1082 * get a map of all nodes that are heartbeating in any regions
1083 */
1084void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
1085{
1086 /* callers want to serialize this map and callbacks so that they
1087 * can trust that they don't miss nodes coming to the party */
1088 down_read(&o2hb_callback_sem);
1089 spin_lock(&o2hb_live_lock);
1090 o2hb_fill_node_map_from_callback(map, bytes);
1091 spin_unlock(&o2hb_live_lock);
1092 up_read(&o2hb_callback_sem);
1093}
1094EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
1095
1096/*
1097 * heartbeat configfs bits. The heartbeat set is a default set under
1098 * the cluster set in nodemanager.c.
1099 */
1100
1101static struct o2hb_region *to_o2hb_region(struct config_item *item)
1102{
1103 return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
1104}
1105
1106/* drop_item only drops its ref after killing the thread, nothing should
1107 * be using the region anymore. this has to clean up any state that
1108 * attributes might have built up. */
1109static void o2hb_region_release(struct config_item *item)
1110{
1111 int i;
1112 struct page *page;
1113 struct o2hb_region *reg = to_o2hb_region(item);
1114
1115 if (reg->hr_tmp_block)
1116 kfree(reg->hr_tmp_block);
1117
1118 if (reg->hr_slot_data) {
1119 for (i = 0; i < reg->hr_num_pages; i++) {
1120 page = reg->hr_slot_data[i];
1121 if (page)
1122 __free_page(page);
1123 }
1124 kfree(reg->hr_slot_data);
1125 }
1126
1127 if (reg->hr_bdev)
9a1c3542 1128 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
a7f6a5fb
MF
1129
1130 if (reg->hr_slots)
1131 kfree(reg->hr_slots);
1132
1133 spin_lock(&o2hb_live_lock);
1134 list_del(&reg->hr_all_item);
1135 spin_unlock(&o2hb_live_lock);
1136
1137 kfree(reg);
1138}
1139
1140static int o2hb_read_block_input(struct o2hb_region *reg,
1141 const char *page,
1142 size_t count,
1143 unsigned long *ret_bytes,
1144 unsigned int *ret_bits)
1145{
1146 unsigned long bytes;
1147 char *p = (char *)page;
1148
1149 bytes = simple_strtoul(p, &p, 0);
1150 if (!p || (*p && (*p != '\n')))
1151 return -EINVAL;
1152
1153 /* Heartbeat and fs min / max block sizes are the same. */
1154 if (bytes > 4096 || bytes < 512)
1155 return -ERANGE;
1156 if (hweight16(bytes) != 1)
1157 return -EINVAL;
1158
1159 if (ret_bytes)
1160 *ret_bytes = bytes;
1161 if (ret_bits)
1162 *ret_bits = ffs(bytes) - 1;
1163
1164 return 0;
1165}
1166
1167static ssize_t o2hb_region_block_bytes_read(struct o2hb_region *reg,
1168 char *page)
1169{
1170 return sprintf(page, "%u\n", reg->hr_block_bytes);
1171}
1172
1173static ssize_t o2hb_region_block_bytes_write(struct o2hb_region *reg,
1174 const char *page,
1175 size_t count)
1176{
1177 int status;
1178 unsigned long block_bytes;
1179 unsigned int block_bits;
1180
1181 if (reg->hr_bdev)
1182 return -EINVAL;
1183
1184 status = o2hb_read_block_input(reg, page, count,
1185 &block_bytes, &block_bits);
1186 if (status)
1187 return status;
1188
1189 reg->hr_block_bytes = (unsigned int)block_bytes;
1190 reg->hr_block_bits = block_bits;
1191
1192 return count;
1193}
1194
1195static ssize_t o2hb_region_start_block_read(struct o2hb_region *reg,
1196 char *page)
1197{
1198 return sprintf(page, "%llu\n", reg->hr_start_block);
1199}
1200
1201static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
1202 const char *page,
1203 size_t count)
1204{
1205 unsigned long long tmp;
1206 char *p = (char *)page;
1207
1208 if (reg->hr_bdev)
1209 return -EINVAL;
1210
1211 tmp = simple_strtoull(p, &p, 0);
1212 if (!p || (*p && (*p != '\n')))
1213 return -EINVAL;
1214
1215 reg->hr_start_block = tmp;
1216
1217 return count;
1218}
1219
1220static ssize_t o2hb_region_blocks_read(struct o2hb_region *reg,
1221 char *page)
1222{
1223 return sprintf(page, "%d\n", reg->hr_blocks);
1224}
1225
1226static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
1227 const char *page,
1228 size_t count)
1229{
1230 unsigned long tmp;
1231 char *p = (char *)page;
1232
1233 if (reg->hr_bdev)
1234 return -EINVAL;
1235
1236 tmp = simple_strtoul(p, &p, 0);
1237 if (!p || (*p && (*p != '\n')))
1238 return -EINVAL;
1239
1240 if (tmp > O2NM_MAX_NODES || tmp == 0)
1241 return -ERANGE;
1242
1243 reg->hr_blocks = (unsigned int)tmp;
1244
1245 return count;
1246}
1247
1248static ssize_t o2hb_region_dev_read(struct o2hb_region *reg,
1249 char *page)
1250{
1251 unsigned int ret = 0;
1252
1253 if (reg->hr_bdev)
1254 ret = sprintf(page, "%s\n", reg->hr_dev_name);
1255
1256 return ret;
1257}
1258
1259static void o2hb_init_region_params(struct o2hb_region *reg)
1260{
1261 reg->hr_slots_per_page = PAGE_CACHE_SIZE >> reg->hr_block_bits;
1262 reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
1263
1264 mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
1265 reg->hr_start_block, reg->hr_blocks);
1266 mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
1267 reg->hr_block_bytes, reg->hr_block_bits);
1268 mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
1269 mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
1270}
1271
1272static int o2hb_map_slot_data(struct o2hb_region *reg)
1273{
1274 int i, j;
1275 unsigned int last_slot;
1276 unsigned int spp = reg->hr_slots_per_page;
1277 struct page *page;
1278 char *raw;
1279 struct o2hb_disk_slot *slot;
1280
1281 reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
1282 if (reg->hr_tmp_block == NULL) {
1283 mlog_errno(-ENOMEM);
1284 return -ENOMEM;
1285 }
1286
1287 reg->hr_slots = kcalloc(reg->hr_blocks,
1288 sizeof(struct o2hb_disk_slot), GFP_KERNEL);
1289 if (reg->hr_slots == NULL) {
1290 mlog_errno(-ENOMEM);
1291 return -ENOMEM;
1292 }
1293
1294 for(i = 0; i < reg->hr_blocks; i++) {
1295 slot = &reg->hr_slots[i];
1296 slot->ds_node_num = i;
1297 INIT_LIST_HEAD(&slot->ds_live_item);
1298 slot->ds_raw_block = NULL;
1299 }
1300
1301 reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
1302 mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
1303 "at %u blocks per page\n",
1304 reg->hr_num_pages, reg->hr_blocks, spp);
1305
1306 reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
1307 GFP_KERNEL);
1308 if (!reg->hr_slot_data) {
1309 mlog_errno(-ENOMEM);
1310 return -ENOMEM;
1311 }
1312
1313 for(i = 0; i < reg->hr_num_pages; i++) {
1314 page = alloc_page(GFP_KERNEL);
1315 if (!page) {
1316 mlog_errno(-ENOMEM);
1317 return -ENOMEM;
1318 }
1319
1320 reg->hr_slot_data[i] = page;
1321
1322 last_slot = i * spp;
1323 raw = page_address(page);
1324 for (j = 0;
1325 (j < spp) && ((j + last_slot) < reg->hr_blocks);
1326 j++) {
1327 BUG_ON((j + last_slot) >= reg->hr_blocks);
1328
1329 slot = &reg->hr_slots[j + last_slot];
1330 slot->ds_raw_block =
1331 (struct o2hb_disk_heartbeat_block *) raw;
1332
1333 raw += reg->hr_block_bytes;
1334 }
1335 }
1336
1337 return 0;
1338}
1339
1340/* Read in all the slots available and populate the tracking
1341 * structures so that we can start with a baseline idea of what's
1342 * there. */
1343static int o2hb_populate_slot_data(struct o2hb_region *reg)
1344{
1345 int ret, i;
1346 struct o2hb_disk_slot *slot;
1347 struct o2hb_disk_heartbeat_block *hb_block;
1348
1349 mlog_entry_void();
1350
1351 ret = o2hb_read_slots(reg, reg->hr_blocks);
1352 if (ret) {
1353 mlog_errno(ret);
1354 goto out;
1355 }
1356
1357 /* We only want to get an idea of the values initially in each
1358 * slot, so we do no verification - o2hb_check_slot will
1359 * actually determine if each configured slot is valid and
1360 * whether any values have changed. */
1361 for(i = 0; i < reg->hr_blocks; i++) {
1362 slot = &reg->hr_slots[i];
1363 hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
1364
1365 /* Only fill the values that o2hb_check_slot uses to
1366 * determine changing slots */
1367 slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
1368 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
1369 }
1370
1371out:
1372 mlog_exit(ret);
1373 return ret;
1374}
1375
1376/* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1377static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
1378 const char *page,
1379 size_t count)
1380{
e6c352db 1381 struct task_struct *hb_task;
a7f6a5fb
MF
1382 long fd;
1383 int sectsize;
1384 char *p = (char *)page;
1385 struct file *filp = NULL;
1386 struct inode *inode = NULL;
1387 ssize_t ret = -EINVAL;
1388
1389 if (reg->hr_bdev)
1390 goto out;
1391
1392 /* We can't heartbeat without having had our node number
1393 * configured yet. */
1394 if (o2nm_this_node() == O2NM_MAX_NODES)
1395 goto out;
1396
1397 fd = simple_strtol(p, &p, 0);
1398 if (!p || (*p && (*p != '\n')))
1399 goto out;
1400
1401 if (fd < 0 || fd >= INT_MAX)
1402 goto out;
1403
1404 filp = fget(fd);
1405 if (filp == NULL)
1406 goto out;
1407
1408 if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
1409 reg->hr_block_bytes == 0)
1410 goto out;
1411
1412 inode = igrab(filp->f_mapping->host);
1413 if (inode == NULL)
1414 goto out;
1415
1416 if (!S_ISBLK(inode->i_mode))
1417 goto out;
1418
1419 reg->hr_bdev = I_BDEV(filp->f_mapping->host);
572c4892 1420 ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ);
a7f6a5fb
MF
1421 if (ret) {
1422 reg->hr_bdev = NULL;
1423 goto out;
1424 }
1425 inode = NULL;
1426
1427 bdevname(reg->hr_bdev, reg->hr_dev_name);
1428
e1defc4f 1429 sectsize = bdev_logical_block_size(reg->hr_bdev);
a7f6a5fb
MF
1430 if (sectsize != reg->hr_block_bytes) {
1431 mlog(ML_ERROR,
1432 "blocksize %u incorrect for device, expected %d",
1433 reg->hr_block_bytes, sectsize);
1434 ret = -EINVAL;
1435 goto out;
1436 }
1437
1438 o2hb_init_region_params(reg);
1439
1440 /* Generation of zero is invalid */
1441 do {
1442 get_random_bytes(&reg->hr_generation,
1443 sizeof(reg->hr_generation));
1444 } while (reg->hr_generation == 0);
1445
1446 ret = o2hb_map_slot_data(reg);
1447 if (ret) {
1448 mlog_errno(ret);
1449 goto out;
1450 }
1451
1452 ret = o2hb_populate_slot_data(reg);
1453 if (ret) {
1454 mlog_errno(ret);
1455 goto out;
1456 }
1457
c4028958 1458 INIT_DELAYED_WORK(&reg->hr_write_timeout_work, o2hb_write_timeout);
a7f6a5fb
MF
1459
1460 /*
1461 * A node is considered live after it has beat LIVE_THRESHOLD
1462 * times. We're not steady until we've given them a chance
1463 * _after_ our first read.
1464 */
1465 atomic_set(&reg->hr_steady_iterations, O2HB_LIVE_THRESHOLD + 1);
1466
e6c352db
JB
1467 hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
1468 reg->hr_item.ci_name);
1469 if (IS_ERR(hb_task)) {
1470 ret = PTR_ERR(hb_task);
a7f6a5fb 1471 mlog_errno(ret);
a7f6a5fb
MF
1472 goto out;
1473 }
1474
e6c352db
JB
1475 spin_lock(&o2hb_live_lock);
1476 reg->hr_task = hb_task;
1477 spin_unlock(&o2hb_live_lock);
1478
a7f6a5fb
MF
1479 ret = wait_event_interruptible(o2hb_steady_queue,
1480 atomic_read(&reg->hr_steady_iterations) == 0);
1481 if (ret) {
e6df3a66 1482 /* We got interrupted (hello ptrace!). Clean up */
e6c352db
JB
1483 spin_lock(&o2hb_live_lock);
1484 hb_task = reg->hr_task;
a7f6a5fb 1485 reg->hr_task = NULL;
e6c352db
JB
1486 spin_unlock(&o2hb_live_lock);
1487
1488 if (hb_task)
1489 kthread_stop(hb_task);
a7f6a5fb
MF
1490 goto out;
1491 }
1492
e6df3a66
JB
1493 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1494 spin_lock(&o2hb_live_lock);
1495 hb_task = reg->hr_task;
1496 spin_unlock(&o2hb_live_lock);
1497
1498 if (hb_task)
1499 ret = count;
1500 else
1501 ret = -EIO;
1502
18c50cb0
SM
1503 if (hb_task && o2hb_global_heartbeat_active())
1504 printk(KERN_NOTICE "o2hb: Heartbeat started on region %s\n",
1505 config_item_name(&reg->hr_item));
1506
a7f6a5fb
MF
1507out:
1508 if (filp)
1509 fput(filp);
1510 if (inode)
1511 iput(inode);
1512 if (ret < 0) {
1513 if (reg->hr_bdev) {
9a1c3542 1514 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
a7f6a5fb
MF
1515 reg->hr_bdev = NULL;
1516 }
1517 }
1518 return ret;
1519}
1520
92efc152
ZW
1521static ssize_t o2hb_region_pid_read(struct o2hb_region *reg,
1522 char *page)
1523{
e6c352db
JB
1524 pid_t pid = 0;
1525
1526 spin_lock(&o2hb_live_lock);
1527 if (reg->hr_task)
ba25f9dc 1528 pid = task_pid_nr(reg->hr_task);
e6c352db
JB
1529 spin_unlock(&o2hb_live_lock);
1530
1531 if (!pid)
92efc152
ZW
1532 return 0;
1533
e6c352db 1534 return sprintf(page, "%u\n", pid);
92efc152
ZW
1535}
1536
a7f6a5fb
MF
1537struct o2hb_region_attribute {
1538 struct configfs_attribute attr;
1539 ssize_t (*show)(struct o2hb_region *, char *);
1540 ssize_t (*store)(struct o2hb_region *, const char *, size_t);
1541};
1542
1543static struct o2hb_region_attribute o2hb_region_attr_block_bytes = {
1544 .attr = { .ca_owner = THIS_MODULE,
1545 .ca_name = "block_bytes",
1546 .ca_mode = S_IRUGO | S_IWUSR },
1547 .show = o2hb_region_block_bytes_read,
1548 .store = o2hb_region_block_bytes_write,
1549};
1550
1551static struct o2hb_region_attribute o2hb_region_attr_start_block = {
1552 .attr = { .ca_owner = THIS_MODULE,
1553 .ca_name = "start_block",
1554 .ca_mode = S_IRUGO | S_IWUSR },
1555 .show = o2hb_region_start_block_read,
1556 .store = o2hb_region_start_block_write,
1557};
1558
1559static struct o2hb_region_attribute o2hb_region_attr_blocks = {
1560 .attr = { .ca_owner = THIS_MODULE,
1561 .ca_name = "blocks",
1562 .ca_mode = S_IRUGO | S_IWUSR },
1563 .show = o2hb_region_blocks_read,
1564 .store = o2hb_region_blocks_write,
1565};
1566
1567static struct o2hb_region_attribute o2hb_region_attr_dev = {
1568 .attr = { .ca_owner = THIS_MODULE,
1569 .ca_name = "dev",
1570 .ca_mode = S_IRUGO | S_IWUSR },
1571 .show = o2hb_region_dev_read,
1572 .store = o2hb_region_dev_write,
1573};
1574
92efc152
ZW
1575static struct o2hb_region_attribute o2hb_region_attr_pid = {
1576 .attr = { .ca_owner = THIS_MODULE,
1577 .ca_name = "pid",
1578 .ca_mode = S_IRUGO | S_IRUSR },
1579 .show = o2hb_region_pid_read,
1580};
1581
a7f6a5fb
MF
1582static struct configfs_attribute *o2hb_region_attrs[] = {
1583 &o2hb_region_attr_block_bytes.attr,
1584 &o2hb_region_attr_start_block.attr,
1585 &o2hb_region_attr_blocks.attr,
1586 &o2hb_region_attr_dev.attr,
92efc152 1587 &o2hb_region_attr_pid.attr,
a7f6a5fb
MF
1588 NULL,
1589};
1590
1591static ssize_t o2hb_region_show(struct config_item *item,
1592 struct configfs_attribute *attr,
1593 char *page)
1594{
1595 struct o2hb_region *reg = to_o2hb_region(item);
1596 struct o2hb_region_attribute *o2hb_region_attr =
1597 container_of(attr, struct o2hb_region_attribute, attr);
1598 ssize_t ret = 0;
1599
1600 if (o2hb_region_attr->show)
1601 ret = o2hb_region_attr->show(reg, page);
1602 return ret;
1603}
1604
1605static ssize_t o2hb_region_store(struct config_item *item,
1606 struct configfs_attribute *attr,
1607 const char *page, size_t count)
1608{
1609 struct o2hb_region *reg = to_o2hb_region(item);
1610 struct o2hb_region_attribute *o2hb_region_attr =
1611 container_of(attr, struct o2hb_region_attribute, attr);
1612 ssize_t ret = -EINVAL;
1613
1614 if (o2hb_region_attr->store)
1615 ret = o2hb_region_attr->store(reg, page, count);
1616 return ret;
1617}
1618
1619static struct configfs_item_operations o2hb_region_item_ops = {
1620 .release = o2hb_region_release,
1621 .show_attribute = o2hb_region_show,
1622 .store_attribute = o2hb_region_store,
1623};
1624
1625static struct config_item_type o2hb_region_type = {
1626 .ct_item_ops = &o2hb_region_item_ops,
1627 .ct_attrs = o2hb_region_attrs,
1628 .ct_owner = THIS_MODULE,
1629};
1630
1631/* heartbeat set */
1632
1633struct o2hb_heartbeat_group {
1634 struct config_group hs_group;
1635 /* some stuff? */
1636};
1637
1638static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
1639{
1640 return group ?
1641 container_of(group, struct o2hb_heartbeat_group, hs_group)
1642 : NULL;
1643}
1644
f89ab861
JB
1645static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
1646 const char *name)
a7f6a5fb
MF
1647{
1648 struct o2hb_region *reg = NULL;
a7f6a5fb 1649
cd861280 1650 reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
f89ab861 1651 if (reg == NULL)
a6795e9e 1652 return ERR_PTR(-ENOMEM);
a7f6a5fb 1653
b3c85c4c
SM
1654 if (strlen(name) > O2HB_MAX_REGION_NAME_LEN)
1655 return ERR_PTR(-ENAMETOOLONG);
1656
a7f6a5fb
MF
1657 config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
1658
a7f6a5fb
MF
1659 spin_lock(&o2hb_live_lock);
1660 list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
1661 spin_unlock(&o2hb_live_lock);
a7f6a5fb 1662
a6795e9e 1663 return &reg->hr_item;
a7f6a5fb
MF
1664}
1665
1666static void o2hb_heartbeat_group_drop_item(struct config_group *group,
1667 struct config_item *item)
1668{
e6c352db 1669 struct task_struct *hb_task;
a7f6a5fb
MF
1670 struct o2hb_region *reg = to_o2hb_region(item);
1671
1672 /* stop the thread when the user removes the region dir */
e6c352db
JB
1673 spin_lock(&o2hb_live_lock);
1674 hb_task = reg->hr_task;
1675 reg->hr_task = NULL;
1676 spin_unlock(&o2hb_live_lock);
1677
1678 if (hb_task)
1679 kthread_stop(hb_task);
a7f6a5fb 1680
e6df3a66
JB
1681 /*
1682 * If we're racing a dev_write(), we need to wake them. They will
1683 * check reg->hr_task
1684 */
1685 if (atomic_read(&reg->hr_steady_iterations) != 0) {
1686 atomic_set(&reg->hr_steady_iterations, 0);
1687 wake_up(&o2hb_steady_queue);
1688 }
1689
18c50cb0
SM
1690 if (o2hb_global_heartbeat_active())
1691 printk(KERN_NOTICE "o2hb: Heartbeat stopped on region %s\n",
1692 config_item_name(&reg->hr_item));
a7f6a5fb
MF
1693 config_item_put(item);
1694}
1695
1696struct o2hb_heartbeat_group_attribute {
1697 struct configfs_attribute attr;
1698 ssize_t (*show)(struct o2hb_heartbeat_group *, char *);
1699 ssize_t (*store)(struct o2hb_heartbeat_group *, const char *, size_t);
1700};
1701
1702static ssize_t o2hb_heartbeat_group_show(struct config_item *item,
1703 struct configfs_attribute *attr,
1704 char *page)
1705{
1706 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1707 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1708 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1709 ssize_t ret = 0;
1710
1711 if (o2hb_heartbeat_group_attr->show)
1712 ret = o2hb_heartbeat_group_attr->show(reg, page);
1713 return ret;
1714}
1715
1716static ssize_t o2hb_heartbeat_group_store(struct config_item *item,
1717 struct configfs_attribute *attr,
1718 const char *page, size_t count)
1719{
1720 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1721 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1722 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1723 ssize_t ret = -EINVAL;
1724
1725 if (o2hb_heartbeat_group_attr->store)
1726 ret = o2hb_heartbeat_group_attr->store(reg, page, count);
1727 return ret;
1728}
1729
1730static ssize_t o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group *group,
1731 char *page)
1732{
1733 return sprintf(page, "%u\n", o2hb_dead_threshold);
1734}
1735
1736static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group *group,
1737 const char *page,
1738 size_t count)
1739{
1740 unsigned long tmp;
1741 char *p = (char *)page;
1742
1743 tmp = simple_strtoul(p, &p, 10);
1744 if (!p || (*p && (*p != '\n')))
1745 return -EINVAL;
1746
1747 /* this will validate ranges for us. */
1748 o2hb_dead_threshold_set((unsigned int) tmp);
1749
1750 return count;
1751}
1752
54b5187b
SM
1753static
1754ssize_t o2hb_heartbeat_group_mode_show(struct o2hb_heartbeat_group *group,
1755 char *page)
1756{
1757 return sprintf(page, "%s\n",
1758 o2hb_heartbeat_mode_desc[o2hb_heartbeat_mode]);
1759}
1760
1761static
1762ssize_t o2hb_heartbeat_group_mode_store(struct o2hb_heartbeat_group *group,
1763 const char *page, size_t count)
1764{
1765 unsigned int i;
1766 int ret;
1767 size_t len;
1768
1769 len = (page[count - 1] == '\n') ? count - 1 : count;
1770 if (!len)
1771 return -EINVAL;
1772
1773 for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) {
1774 if (strnicmp(page, o2hb_heartbeat_mode_desc[i], len))
1775 continue;
1776
1777 ret = o2hb_global_hearbeat_mode_set(i);
1778 if (!ret)
18c50cb0 1779 printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
54b5187b
SM
1780 o2hb_heartbeat_mode_desc[i]);
1781 return count;
1782 }
1783
1784 return -EINVAL;
1785
1786}
1787
a7f6a5fb
MF
1788static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold = {
1789 .attr = { .ca_owner = THIS_MODULE,
1790 .ca_name = "dead_threshold",
1791 .ca_mode = S_IRUGO | S_IWUSR },
1792 .show = o2hb_heartbeat_group_threshold_show,
1793 .store = o2hb_heartbeat_group_threshold_store,
1794};
1795
54b5187b
SM
1796static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_mode = {
1797 .attr = { .ca_owner = THIS_MODULE,
1798 .ca_name = "mode",
1799 .ca_mode = S_IRUGO | S_IWUSR },
1800 .show = o2hb_heartbeat_group_mode_show,
1801 .store = o2hb_heartbeat_group_mode_store,
1802};
1803
a7f6a5fb
MF
1804static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
1805 &o2hb_heartbeat_group_attr_threshold.attr,
54b5187b 1806 &o2hb_heartbeat_group_attr_mode.attr,
a7f6a5fb
MF
1807 NULL,
1808};
1809
1810static struct configfs_item_operations o2hb_hearbeat_group_item_ops = {
1811 .show_attribute = o2hb_heartbeat_group_show,
1812 .store_attribute = o2hb_heartbeat_group_store,
1813};
1814
1815static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
1816 .make_item = o2hb_heartbeat_group_make_item,
1817 .drop_item = o2hb_heartbeat_group_drop_item,
1818};
1819
1820static struct config_item_type o2hb_heartbeat_group_type = {
1821 .ct_group_ops = &o2hb_heartbeat_group_group_ops,
1822 .ct_item_ops = &o2hb_hearbeat_group_item_ops,
1823 .ct_attrs = o2hb_heartbeat_group_attrs,
1824 .ct_owner = THIS_MODULE,
1825};
1826
1827/* this is just here to avoid touching group in heartbeat.h which the
1828 * entire damn world #includes */
1829struct config_group *o2hb_alloc_hb_set(void)
1830{
1831 struct o2hb_heartbeat_group *hs = NULL;
1832 struct config_group *ret = NULL;
1833
cd861280 1834 hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
a7f6a5fb
MF
1835 if (hs == NULL)
1836 goto out;
1837
1838 config_group_init_type_name(&hs->hs_group, "heartbeat",
1839 &o2hb_heartbeat_group_type);
1840
1841 ret = &hs->hs_group;
1842out:
1843 if (ret == NULL)
1844 kfree(hs);
1845 return ret;
1846}
1847
1848void o2hb_free_hb_set(struct config_group *group)
1849{
1850 struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
1851 kfree(hs);
1852}
1853
1854/* hb callback registration and issueing */
1855
1856static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
1857{
1858 if (type == O2HB_NUM_CB)
1859 return ERR_PTR(-EINVAL);
1860
1861 return &o2hb_callbacks[type];
1862}
1863
1864void o2hb_setup_callback(struct o2hb_callback_func *hc,
1865 enum o2hb_callback_type type,
1866 o2hb_cb_func *func,
1867 void *data,
1868 int priority)
1869{
1870 INIT_LIST_HEAD(&hc->hc_item);
1871 hc->hc_func = func;
1872 hc->hc_data = data;
1873 hc->hc_priority = priority;
1874 hc->hc_type = type;
1875 hc->hc_magic = O2HB_CB_MAGIC;
1876}
1877EXPORT_SYMBOL_GPL(o2hb_setup_callback);
1878
14829422
JB
1879static struct o2hb_region *o2hb_find_region(const char *region_uuid)
1880{
1881 struct o2hb_region *p, *reg = NULL;
1882
1883 assert_spin_locked(&o2hb_live_lock);
1884
1885 list_for_each_entry(p, &o2hb_all_regions, hr_all_item) {
1886 if (!strcmp(region_uuid, config_item_name(&p->hr_item))) {
1887 reg = p;
1888 break;
1889 }
1890 }
1891
1892 return reg;
1893}
1894
1895static int o2hb_region_get(const char *region_uuid)
1896{
1897 int ret = 0;
1898 struct o2hb_region *reg;
1899
1900 spin_lock(&o2hb_live_lock);
1901
1902 reg = o2hb_find_region(region_uuid);
1903 if (!reg)
1904 ret = -ENOENT;
1905 spin_unlock(&o2hb_live_lock);
1906
16c6a4f2
JB
1907 if (ret)
1908 goto out;
1909
1910 ret = o2nm_depend_this_node();
1911 if (ret)
1912 goto out;
14829422 1913
16c6a4f2
JB
1914 ret = o2nm_depend_item(&reg->hr_item);
1915 if (ret)
1916 o2nm_undepend_this_node();
1917
1918out:
14829422
JB
1919 return ret;
1920}
1921
1922static void o2hb_region_put(const char *region_uuid)
1923{
1924 struct o2hb_region *reg;
1925
1926 spin_lock(&o2hb_live_lock);
1927
1928 reg = o2hb_find_region(region_uuid);
1929
1930 spin_unlock(&o2hb_live_lock);
1931
16c6a4f2 1932 if (reg) {
14829422 1933 o2nm_undepend_item(&reg->hr_item);
16c6a4f2
JB
1934 o2nm_undepend_this_node();
1935 }
14829422
JB
1936}
1937
1938int o2hb_register_callback(const char *region_uuid,
1939 struct o2hb_callback_func *hc)
a7f6a5fb
MF
1940{
1941 struct o2hb_callback_func *tmp;
1942 struct list_head *iter;
1943 struct o2hb_callback *hbcall;
1944 int ret;
1945
1946 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
1947 BUG_ON(!list_empty(&hc->hc_item));
1948
1949 hbcall = hbcall_from_type(hc->hc_type);
1950 if (IS_ERR(hbcall)) {
1951 ret = PTR_ERR(hbcall);
1952 goto out;
1953 }
1954
14829422
JB
1955 if (region_uuid) {
1956 ret = o2hb_region_get(region_uuid);
1957 if (ret)
1958 goto out;
1959 }
1960
a7f6a5fb
MF
1961 down_write(&o2hb_callback_sem);
1962
1963 list_for_each(iter, &hbcall->list) {
1964 tmp = list_entry(iter, struct o2hb_callback_func, hc_item);
1965 if (hc->hc_priority < tmp->hc_priority) {
1966 list_add_tail(&hc->hc_item, iter);
1967 break;
1968 }
1969 }
1970 if (list_empty(&hc->hc_item))
1971 list_add_tail(&hc->hc_item, &hbcall->list);
1972
1973 up_write(&o2hb_callback_sem);
1974 ret = 0;
1975out:
1976 mlog(ML_HEARTBEAT, "returning %d on behalf of %p for funcs %p\n",
1977 ret, __builtin_return_address(0), hc);
1978 return ret;
1979}
1980EXPORT_SYMBOL_GPL(o2hb_register_callback);
1981
14829422
JB
1982void o2hb_unregister_callback(const char *region_uuid,
1983 struct o2hb_callback_func *hc)
a7f6a5fb
MF
1984{
1985 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
1986
1987 mlog(ML_HEARTBEAT, "on behalf of %p for funcs %p\n",
1988 __builtin_return_address(0), hc);
1989
14829422 1990 /* XXX Can this happen _with_ a region reference? */
a7f6a5fb 1991 if (list_empty(&hc->hc_item))
c24f72cc 1992 return;
a7f6a5fb 1993
14829422
JB
1994 if (region_uuid)
1995 o2hb_region_put(region_uuid);
1996
a7f6a5fb
MF
1997 down_write(&o2hb_callback_sem);
1998
1999 list_del_init(&hc->hc_item);
2000
2001 up_write(&o2hb_callback_sem);
a7f6a5fb
MF
2002}
2003EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
2004
2005int o2hb_check_node_heartbeating(u8 node_num)
2006{
2007 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2008
2009 o2hb_fill_node_map(testing_map, sizeof(testing_map));
2010 if (!test_bit(node_num, testing_map)) {
2011 mlog(ML_HEARTBEAT,
2012 "node (%u) does not have heartbeating enabled.\n",
2013 node_num);
2014 return 0;
2015 }
2016
2017 return 1;
2018}
2019EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating);
2020
2021int o2hb_check_node_heartbeating_from_callback(u8 node_num)
2022{
2023 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2024
2025 o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
2026 if (!test_bit(node_num, testing_map)) {
2027 mlog(ML_HEARTBEAT,
2028 "node (%u) does not have heartbeating enabled.\n",
2029 node_num);
2030 return 0;
2031 }
2032
2033 return 1;
2034}
2035EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
2036
2037/* Makes sure our local node is configured with a node number, and is
2038 * heartbeating. */
2039int o2hb_check_local_node_heartbeating(void)
2040{
2041 u8 node_num;
2042
2043 /* if this node was set then we have networking */
2044 node_num = o2nm_this_node();
2045 if (node_num == O2NM_MAX_NODES) {
2046 mlog(ML_HEARTBEAT, "this node has not been configured.\n");
2047 return 0;
2048 }
2049
2050 return o2hb_check_node_heartbeating(node_num);
2051}
2052EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating);
2053
2054/*
2055 * this is just a hack until we get the plumbing which flips file systems
2056 * read only and drops the hb ref instead of killing the node dead.
2057 */
2058void o2hb_stop_all_regions(void)
2059{
2060 struct o2hb_region *reg;
2061
2062 mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
2063
2064 spin_lock(&o2hb_live_lock);
2065
2066 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
2067 reg->hr_unclean_stop = 1;
2068
2069 spin_unlock(&o2hb_live_lock);
2070}
2071EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);
b3c85c4c
SM
2072
2073int o2hb_get_all_regions(char *region_uuids, u8 max_regions)
2074{
2075 struct o2hb_region *reg;
2076 int numregs = 0;
2077 char *p;
2078
2079 spin_lock(&o2hb_live_lock);
2080
2081 p = region_uuids;
2082 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2083 mlog(0, "Region: %s\n", config_item_name(&reg->hr_item));
2084 if (numregs < max_regions) {
2085 memcpy(p, config_item_name(&reg->hr_item),
2086 O2HB_MAX_REGION_NAME_LEN);
2087 p += O2HB_MAX_REGION_NAME_LEN;
2088 }
2089 numregs++;
2090 }
2091
2092 spin_unlock(&o2hb_live_lock);
2093
2094 return numregs;
2095}
2096EXPORT_SYMBOL_GPL(o2hb_get_all_regions);
2097
2098int o2hb_global_heartbeat_active(void)
2099{
2100 return 0;
2101}
2102EXPORT_SYMBOL(o2hb_global_heartbeat_active);