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