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