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