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