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