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