1d28505caff835a47bbb9112c371c3cc7ba0186d
[linux-2.6.git] / fs / ocfs2 / cluster / heartbeat.c
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>
36 #include <linux/debugfs.h>
37 #include <linux/slab.h>
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  */
53 static 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  */
59 static DEFINE_SPINLOCK(o2hb_live_lock);
60 static struct list_head o2hb_live_slots[O2NM_MAX_NODES];
61 static unsigned long o2hb_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
62 static LIST_HEAD(o2hb_node_events);
63 static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue);
64
65 /*
66  * In global heartbeat, we maintain a series of region bitmaps.
67  *      - o2hb_region_bitmap allows us to limit the region number to max region.
68  *      - o2hb_live_region_bitmap tracks live regions (seen steady iterations).
69  *      - o2hb_quorum_region_bitmap tracks live regions that have seen all nodes
70  *              heartbeat on it.
71  *      - o2hb_failed_region_bitmap tracks the regions that have seen io timeouts.
72  */
73 static unsigned long o2hb_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
74 static unsigned long o2hb_live_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
75 static unsigned long o2hb_quorum_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
76 static unsigned long o2hb_failed_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
77
78 #define O2HB_DB_TYPE_LIVENODES          0
79 #define O2HB_DB_TYPE_LIVEREGIONS        1
80 #define O2HB_DB_TYPE_QUORUMREGIONS      2
81 #define O2HB_DB_TYPE_FAILEDREGIONS      3
82 #define O2HB_DB_TYPE_REGION_LIVENODES   4
83 #define O2HB_DB_TYPE_REGION_NUMBER      5
84 #define O2HB_DB_TYPE_REGION_ELAPSED_TIME        6
85 #define O2HB_DB_TYPE_REGION_PINNED      7
86 struct o2hb_debug_buf {
87         int db_type;
88         int db_size;
89         int db_len;
90         void *db_data;
91 };
92
93 static struct o2hb_debug_buf *o2hb_db_livenodes;
94 static struct o2hb_debug_buf *o2hb_db_liveregions;
95 static struct o2hb_debug_buf *o2hb_db_quorumregions;
96 static struct o2hb_debug_buf *o2hb_db_failedregions;
97
98 #define O2HB_DEBUG_DIR                  "o2hb"
99 #define O2HB_DEBUG_LIVENODES            "livenodes"
100 #define O2HB_DEBUG_LIVEREGIONS          "live_regions"
101 #define O2HB_DEBUG_QUORUMREGIONS        "quorum_regions"
102 #define O2HB_DEBUG_FAILEDREGIONS        "failed_regions"
103 #define O2HB_DEBUG_REGION_NUMBER        "num"
104 #define O2HB_DEBUG_REGION_ELAPSED_TIME  "elapsed_time_in_ms"
105 #define O2HB_DEBUG_REGION_PINNED        "pinned"
106
107 static struct dentry *o2hb_debug_dir;
108 static struct dentry *o2hb_debug_livenodes;
109 static struct dentry *o2hb_debug_liveregions;
110 static struct dentry *o2hb_debug_quorumregions;
111 static struct dentry *o2hb_debug_failedregions;
112
113 static LIST_HEAD(o2hb_all_regions);
114
115 static struct o2hb_callback {
116         struct list_head list;
117 } o2hb_callbacks[O2HB_NUM_CB];
118
119 static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type);
120
121 #define O2HB_DEFAULT_BLOCK_BITS       9
122
123 enum o2hb_heartbeat_modes {
124         O2HB_HEARTBEAT_LOCAL            = 0,
125         O2HB_HEARTBEAT_GLOBAL,
126         O2HB_HEARTBEAT_NUM_MODES,
127 };
128
129 char *o2hb_heartbeat_mode_desc[O2HB_HEARTBEAT_NUM_MODES] = {
130                 "local",        /* O2HB_HEARTBEAT_LOCAL */
131                 "global",       /* O2HB_HEARTBEAT_GLOBAL */
132 };
133
134 unsigned int o2hb_dead_threshold = O2HB_DEFAULT_DEAD_THRESHOLD;
135 unsigned int o2hb_heartbeat_mode = O2HB_HEARTBEAT_LOCAL;
136
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  */
143 unsigned 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  */
161 static int o2hb_region_pin(const char *region_uuid);
162 static void o2hb_region_unpin(const char *region_uuid);
163
164 /* Only sets a new threshold if there are no active regions.
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. */
169 static 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
179 static 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
195 struct 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
202 struct 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 */
214 struct o2hb_region {
215         struct config_item      hr_item;
216
217         struct list_head        hr_all_item;
218         unsigned                hr_unclean_stop:1,
219                                 hr_item_pinned:1,
220                                 hr_item_dropped:1;
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
238         /* live node map of this region */
239         unsigned long           hr_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
240         unsigned int            hr_region_num;
241
242         struct dentry           *hr_debug_dir;
243         struct dentry           *hr_debug_livenodes;
244         struct dentry           *hr_debug_regnum;
245         struct dentry           *hr_debug_elapsed_time;
246         struct dentry           *hr_debug_pinned;
247         struct o2hb_debug_buf   *hr_db_livenodes;
248         struct o2hb_debug_buf   *hr_db_regnum;
249         struct o2hb_debug_buf   *hr_db_elapsed_time;
250         struct o2hb_debug_buf   *hr_db_pinned;
251
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
265         struct delayed_work     hr_write_timeout_work;
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
274 struct o2hb_bio_wait_ctxt {
275         atomic_t          wc_num_reqs;
276         struct completion wc_io_complete;
277         int               wc_error;
278 };
279
280 static 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
289 static void o2hb_write_timeout(struct work_struct *work)
290 {
291         int failed, quorum;
292         unsigned long flags;
293         struct o2hb_region *reg =
294                 container_of(work, struct o2hb_region,
295                              hr_write_timeout_work.work);
296
297         mlog(ML_ERROR, "Heartbeat write timeout to device %s after %u "
298              "milliseconds\n", reg->hr_dev_name,
299              jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
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
322         o2quo_disk_timeout();
323 }
324
325 static void o2hb_arm_write_timeout(struct o2hb_region *reg)
326 {
327         mlog(ML_HEARTBEAT, "Queue write timeout for %u ms\n",
328              O2HB_MAX_WRITE_TIMEOUT_MS);
329
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         }
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
341 static void o2hb_disarm_write_timeout(struct o2hb_region *reg)
342 {
343         cancel_delayed_work_sync(&reg->hr_write_timeout_work);
344 }
345
346 static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt *wc)
347 {
348         atomic_set(&wc->wc_num_reqs, 1);
349         init_completion(&wc->wc_io_complete);
350         wc->wc_error = 0;
351 }
352
353 /* Used in error paths too */
354 static 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
367 static void o2hb_wait_on_io(struct o2hb_region *reg,
368                             struct o2hb_bio_wait_ctxt *wc)
369 {
370         o2hb_bio_wait_dec(wc, 1);
371         wait_for_completion(&wc->wc_io_complete);
372 }
373
374 static void o2hb_bio_end_io(struct bio *bio,
375                            int error)
376 {
377         struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
378
379         if (error) {
380                 mlog(ML_ERROR, "IO Error %d\n", error);
381                 wc->wc_error = error;
382         }
383
384         o2hb_bio_wait_dec(wc, 1);
385         bio_put(bio);
386 }
387
388 /* Setup a Bio to cover I/O against num_slots slots starting at
389  * start_slot. */
390 static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
391                                       struct o2hb_bio_wait_ctxt *wc,
392                                       unsigned int *current_slot,
393                                       unsigned int max_slots)
394 {
395         int len, current_page;
396         unsigned int vec_len, vec_start;
397         unsigned int bits = reg->hr_block_bits;
398         unsigned int spp = reg->hr_slots_per_page;
399         unsigned int cs = *current_slot;
400         struct bio *bio;
401         struct page *page;
402
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. */
407         bio = bio_alloc(GFP_ATOMIC, 16);
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... */
415         bio->bi_sector = (reg->hr_start_block + cs) << (bits - 9);
416         bio->bi_bdev = reg->hr_bdev;
417         bio->bi_private = wc;
418         bio->bi_end_io = o2hb_bio_end_io;
419
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];
424
425                 vec_len = min(PAGE_CACHE_SIZE - vec_start,
426                               (max_slots-cs) * (PAGE_CACHE_SIZE/spp) );
427
428                 mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
429                      current_page, vec_len, vec_start);
430
431                 len = bio_add_page(bio, page, vec_len, vec_start);
432                 if (len != vec_len) break;
433
434                 cs += vec_len / (PAGE_CACHE_SIZE/spp);
435                 vec_start = 0;
436         }
437
438 bail:
439         *current_slot = cs;
440         return bio;
441 }
442
443 static int o2hb_read_slots(struct o2hb_region *reg,
444                            unsigned int max_slots)
445 {
446         unsigned int current_slot=0;
447         int status;
448         struct o2hb_bio_wait_ctxt wc;
449         struct bio *bio;
450
451         o2hb_bio_wait_init(&wc);
452
453         while(current_slot < max_slots) {
454                 bio = o2hb_setup_one_bio(reg, &wc, &current_slot, max_slots);
455                 if (IS_ERR(bio)) {
456                         status = PTR_ERR(bio);
457                         mlog_errno(status);
458                         goto bail_and_wait;
459                 }
460
461                 atomic_inc(&wc.wc_num_reqs);
462                 submit_bio(READ, bio);
463         }
464
465         status = 0;
466
467 bail_and_wait:
468         o2hb_wait_on_io(reg, &wc);
469         if (wc.wc_error && !status)
470                 status = wc.wc_error;
471
472         return status;
473 }
474
475 static int o2hb_issue_node_write(struct o2hb_region *reg,
476                                  struct o2hb_bio_wait_ctxt *write_wc)
477 {
478         int status;
479         unsigned int slot;
480         struct bio *bio;
481
482         o2hb_bio_wait_init(write_wc);
483
484         slot = o2nm_this_node();
485
486         bio = o2hb_setup_one_bio(reg, write_wc, &slot, slot+1);
487         if (IS_ERR(bio)) {
488                 status = PTR_ERR(bio);
489                 mlog_errno(status);
490                 goto bail;
491         }
492
493         atomic_inc(&write_wc->wc_num_reqs);
494         submit_bio(WRITE, bio);
495
496         status = 0;
497 bail:
498         return status;
499 }
500
501 static 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
520 static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
521 {
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));
527 }
528
529 static 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. */
542 static 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
563 static 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);
584         hb_block->hb_dead_ms = cpu_to_le32(o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS);
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
590         mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x\n",
591              (long long)generation,
592              le32_to_cpu(hb_block->hb_cksum));
593 }
594
595 static 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 */
610 static 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
656 static 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
663         BUG_ON((!node) && (type != O2HB_NODE_DOWN_CB));
664
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
675 static 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
706 static 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);
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);
741 }
742
743 static 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;
752         unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
753         unsigned int slot_dead_ms;
754         int tmp;
755
756         memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
757
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          */
762         node = o2nm_get_node_by_num(slot->ds_node_num);
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         }
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;
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));
814         }
815
816         slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
817
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),
822              (unsigned long long)le64_to_cpu(hb_block->hb_seq),
823              (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
824              slot->ds_equal_samples);
825
826         spin_lock(&o2hb_live_lock);
827
828 fire_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) {
833                 mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
834                      slot->ds_node_num, (long long)slot->ds_last_generation);
835
836                 set_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
837
838                 /* first on the list generates a callback */
839                 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
840                         mlog(ML_HEARTBEAT, "o2hb: Add node %d to live nodes "
841                              "bitmap\n", slot->ds_node_num);
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;
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                 }
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
885                 clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
886
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])) {
890                         mlog(ML_HEARTBEAT, "o2hb: Remove node %d from live "
891                              "nodes bitmap\n", slot->ds_node_num);
892                         clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
893
894                         /* node can be null */
895                         o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
896                                               node, slot->ds_node_num);
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         }
911 out:
912         o2hb_set_quorum_device(reg, slot);
913
914         spin_unlock(&o2hb_live_lock);
915
916         o2hb_run_event_list(&event);
917
918         if (node)
919                 o2nm_node_put(node);
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. */
925 static 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
942 static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
943 {
944         int i, ret, highest_node, change = 0;
945         unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
946         unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
947         struct o2hb_bio_wait_ctxt write_wc;
948
949         ret = o2nm_configured_node_map(configured_nodes,
950                                        sizeof(configured_nodes));
951         if (ret) {
952                 mlog_errno(ret);
953                 return ret;
954         }
955
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
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");
970                 return -EINVAL;
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);
980                 return ret;
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. */
995         ret = o2hb_issue_node_write(reg, &write_wc);
996         if (ret < 0) {
997                 mlog_errno(ret);
998                 return ret;
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);
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
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         }
1029
1030         return 0;
1031 }
1032
1033 /* Subtract b from a, storing the result in a. a *must* have a larger
1034  * value than b. */
1035 static 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
1054 static 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  */
1069 static int o2hb_thread(void *data)
1070 {
1071         int i, ret;
1072         struct o2hb_region *reg = data;
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
1081         /* Pin node */
1082         o2nm_depend_this_node();
1083
1084         while (!kthread_should_stop() && !reg->hr_unclean_stop) {
1085                 /* We track the time spent inside
1086                  * o2hb_do_disk_heartbeat so that we avoid more than
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
1092                 i = 0;
1093                 do {
1094                         ret = o2hb_do_disk_heartbeat(reg);
1095                 } while (ret && ++i < 2);
1096
1097                 do_gettimeofday(&after_hb);
1098                 elapsed_msec = o2hb_elapsed_msecs(&before_hb, &after_hb);
1099
1100                 mlog(ML_HEARTBEAT,
1101                      "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
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);
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);
1126         ret = o2hb_issue_node_write(reg, &write_wc);
1127         if (ret == 0) {
1128                 o2hb_wait_on_io(reg, &write_wc);
1129         } else {
1130                 mlog_errno(ret);
1131         }
1132
1133         /* Unpin node */
1134         o2nm_undepend_this_node();
1135
1136         mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread exiting\n");
1137
1138         return 0;
1139 }
1140
1141 #ifdef CONFIG_DEBUG_FS
1142 static int o2hb_debug_open(struct inode *inode, struct file *file)
1143 {
1144         struct o2hb_debug_buf *db = inode->i_private;
1145         struct o2hb_region *reg;
1146         unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1147         char *buf = NULL;
1148         int i = -1;
1149         int out = 0;
1150
1151         /* max_nodes should be the largest bitmap we pass here */
1152         BUG_ON(sizeof(map) < db->db_size);
1153
1154         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1155         if (!buf)
1156                 goto bail;
1157
1158         switch (db->db_type) {
1159         case O2HB_DB_TYPE_LIVENODES:
1160         case O2HB_DB_TYPE_LIVEREGIONS:
1161         case O2HB_DB_TYPE_QUORUMREGIONS:
1162         case O2HB_DB_TYPE_FAILEDREGIONS:
1163                 spin_lock(&o2hb_live_lock);
1164                 memcpy(map, db->db_data, db->db_size);
1165                 spin_unlock(&o2hb_live_lock);
1166                 break;
1167
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
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
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
1194         default:
1195                 goto done;
1196         }
1197
1198         while ((i = find_next_bit(map, db->db_len, i + 1)) < db->db_len)
1199                 out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
1200         out += snprintf(buf + out, PAGE_SIZE - out, "\n");
1201
1202 done:
1203         i_size_write(inode, out);
1204
1205         file->private_data = buf;
1206
1207         return 0;
1208 bail:
1209         return -ENOMEM;
1210 }
1211
1212 static int o2hb_debug_release(struct inode *inode, struct file *file)
1213 {
1214         kfree(file->private_data);
1215         return 0;
1216 }
1217
1218 static 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
1225 static int o2hb_debug_open(struct inode *inode, struct file *file)
1226 {
1227         return 0;
1228 }
1229 static int o2hb_debug_release(struct inode *inode, struct file *file)
1230 {
1231         return 0;
1232 }
1233 static 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
1240 static const struct file_operations o2hb_debug_fops = {
1241         .open =         o2hb_debug_open,
1242         .release =      o2hb_debug_release,
1243         .read =         o2hb_debug_read,
1244         .llseek =       generic_file_llseek,
1245 };
1246
1247 void o2hb_exit(void)
1248 {
1249         kfree(o2hb_db_livenodes);
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);
1256         debugfs_remove(o2hb_debug_livenodes);
1257         debugfs_remove(o2hb_debug_dir);
1258 }
1259
1260 static 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
1277 static 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         }
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
1341         ret = 0;
1342 bail:
1343         if (ret)
1344                 o2hb_exit();
1345
1346         return ret;
1347 }
1348
1349 int o2hb_init(void)
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));
1362         memset(o2hb_region_bitmap, 0, sizeof(o2hb_region_bitmap));
1363         memset(o2hb_live_region_bitmap, 0, sizeof(o2hb_live_region_bitmap));
1364         memset(o2hb_quorum_region_bitmap, 0, sizeof(o2hb_quorum_region_bitmap));
1365         memset(o2hb_failed_region_bitmap, 0, sizeof(o2hb_failed_region_bitmap));
1366
1367         o2hb_dependent_users = 0;
1368
1369         return o2hb_debug_init();
1370 }
1371
1372 /* if we're already in a callback then we're already serialized by the sem */
1373 static 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  */
1384 void 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 }
1394 EXPORT_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
1401 static 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. */
1409 static 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)
1428                 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
1429
1430         if (reg->hr_slots)
1431                 kfree(reg->hr_slots);
1432
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);
1437         debugfs_remove(reg->hr_debug_elapsed_time);
1438         debugfs_remove(reg->hr_debug_pinned);
1439         debugfs_remove(reg->hr_debug_dir);
1440
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
1448 static 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
1475 static 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
1481 static 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
1503 static 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
1509 static 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
1528 static 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
1534 static 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
1556 static 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
1567 static 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
1580 static 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. */
1651 static 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
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
1677 out:
1678         return ret;
1679 }
1680
1681 /* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1682 static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
1683                                      const char *page,
1684                                      size_t count)
1685 {
1686         struct task_struct *hb_task;
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;
1693         int live_threshold;
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);
1726         ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ, NULL);
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
1735         sectsize = bdev_logical_block_size(reg->hr_bdev);
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
1764         INIT_DELAYED_WORK(&reg->hr_write_timeout_work, o2hb_write_timeout);
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.
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.
1773          */
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);
1782
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);
1787                 mlog_errno(ret);
1788                 goto out;
1789         }
1790
1791         spin_lock(&o2hb_live_lock);
1792         reg->hr_task = hb_task;
1793         spin_unlock(&o2hb_live_lock);
1794
1795         ret = wait_event_interruptible(o2hb_steady_queue,
1796                                 atomic_read(&reg->hr_steady_iterations) == 0);
1797         if (ret) {
1798                 /* We got interrupted (hello ptrace!).  Clean up */
1799                 spin_lock(&o2hb_live_lock);
1800                 hb_task = reg->hr_task;
1801                 reg->hr_task = NULL;
1802                 spin_unlock(&o2hb_live_lock);
1803
1804                 if (hb_task)
1805                         kthread_stop(hb_task);
1806                 goto out;
1807         }
1808
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;
1812         if (o2hb_global_heartbeat_active())
1813                 set_bit(reg->hr_region_num, o2hb_live_region_bitmap);
1814         spin_unlock(&o2hb_live_lock);
1815
1816         if (hb_task)
1817                 ret = count;
1818         else
1819                 ret = -EIO;
1820
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
1825 out:
1826         if (filp)
1827                 fput(filp);
1828         if (inode)
1829                 iput(inode);
1830         if (ret < 0) {
1831                 if (reg->hr_bdev) {
1832                         blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
1833                         reg->hr_bdev = NULL;
1834                 }
1835         }
1836         return ret;
1837 }
1838
1839 static ssize_t o2hb_region_pid_read(struct o2hb_region *reg,
1840                                       char *page)
1841 {
1842         pid_t pid = 0;
1843
1844         spin_lock(&o2hb_live_lock);
1845         if (reg->hr_task)
1846                 pid = task_pid_nr(reg->hr_task);
1847         spin_unlock(&o2hb_live_lock);
1848
1849         if (!pid)
1850                 return 0;
1851
1852         return sprintf(page, "%u\n", pid);
1853 }
1854
1855 struct 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
1861 static 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
1869 static 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
1877 static 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
1885 static 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
1893 static 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
1900 static 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,
1905         &o2hb_region_attr_pid.attr,
1906         NULL,
1907 };
1908
1909 static 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
1923 static 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
1937 static 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
1943 static 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
1951 struct o2hb_heartbeat_group {
1952         struct config_group hs_group;
1953         /* some stuff? */
1954 };
1955
1956 static 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
1963 static 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
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
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
2023         ret = 0;
2024 bail:
2025         return ret;
2026 }
2027
2028 static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
2029                                                           const char *name)
2030 {
2031         struct o2hb_region *reg = NULL;
2032         int ret;
2033
2034         reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
2035         if (reg == NULL)
2036                 return ERR_PTR(-ENOMEM);
2037
2038         if (strlen(name) > O2HB_MAX_REGION_NAME_LEN) {
2039                 ret = -ENAMETOOLONG;
2040                 goto free;
2041         }
2042
2043         spin_lock(&o2hb_live_lock);
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);
2050                         ret = -EFBIG;
2051                         goto free;
2052                 }
2053                 set_bit(reg->hr_region_num, o2hb_region_bitmap);
2054         }
2055         list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
2056         spin_unlock(&o2hb_live_lock);
2057
2058         config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
2059
2060         ret = o2hb_debug_region_init(reg, o2hb_debug_dir);
2061         if (ret) {
2062                 config_item_put(&reg->hr_item);
2063                 goto free;
2064         }
2065
2066         return &reg->hr_item;
2067 free:
2068         kfree(reg);
2069         return ERR_PTR(ret);
2070 }
2071
2072 static void o2hb_heartbeat_group_drop_item(struct config_group *group,
2073                                            struct config_item *item)
2074 {
2075         struct task_struct *hb_task;
2076         struct o2hb_region *reg = to_o2hb_region(item);
2077         int quorum_region = 0;
2078
2079         /* stop the thread when the user removes the region dir */
2080         spin_lock(&o2hb_live_lock);
2081         if (o2hb_global_heartbeat_active()) {
2082                 clear_bit(reg->hr_region_num, o2hb_region_bitmap);
2083                 clear_bit(reg->hr_region_num, o2hb_live_region_bitmap);
2084                 if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
2085                         quorum_region = 1;
2086                 clear_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
2087         }
2088         hb_task = reg->hr_task;
2089         reg->hr_task = NULL;
2090         reg->hr_item_dropped = 1;
2091         spin_unlock(&o2hb_live_lock);
2092
2093         if (hb_task)
2094                 kthread_stop(hb_task);
2095
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
2105         if (o2hb_global_heartbeat_active())
2106                 printk(KERN_NOTICE "o2hb: Heartbeat stopped on region %s\n",
2107                        config_item_name(&reg->hr_item));
2108
2109         config_item_put(item);
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
2127 unlock:
2128         spin_unlock(&o2hb_live_lock);
2129 }
2130
2131 struct 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
2137 static 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
2151 static 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
2165 static 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
2171 static 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
2188 static
2189 ssize_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
2196 static
2197 ssize_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)
2214                         printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
2215                                o2hb_heartbeat_mode_desc[i]);
2216                 return count;
2217         }
2218
2219         return -EINVAL;
2220
2221 }
2222
2223 static 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
2231 static 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
2239 static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
2240         &o2hb_heartbeat_group_attr_threshold.attr,
2241         &o2hb_heartbeat_group_attr_mode.attr,
2242         NULL,
2243 };
2244
2245 static 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
2250 static 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
2255 static 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 */
2264 struct config_group *o2hb_alloc_hb_set(void)
2265 {
2266         struct o2hb_heartbeat_group *hs = NULL;
2267         struct config_group *ret = NULL;
2268
2269         hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
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;
2277 out:
2278         if (ret == NULL)
2279                 kfree(hs);
2280         return ret;
2281 }
2282
2283 void 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
2289 /* hb callback registration and issuing */
2290
2291 static 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
2299 void 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 }
2312 EXPORT_SYMBOL_GPL(o2hb_setup_callback);
2313
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  */
2321 static int o2hb_region_pin(const char *region_uuid)
2322 {
2323         int ret = 0, found = 0;
2324         struct o2hb_region *reg;
2325         char *uuid;
2326
2327         assert_spin_locked(&o2hb_live_lock);
2328
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;
2337                 }
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                         }
2355                 }
2356 skip_pin:
2357                 if (found)
2358                         break;
2359         }
2360
2361         return ret;
2362 }
2363
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  */
2371 static void o2hb_region_unpin(const char *region_uuid)
2372 {
2373         struct o2hb_region *reg;
2374         char *uuid;
2375         int found = 0;
2376
2377         assert_spin_locked(&o2hb_live_lock);
2378
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                 }
2386
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 }
2396
2397 static int o2hb_region_inc_user(const char *region_uuid)
2398 {
2399         int ret = 0;
2400
2401         spin_lock(&o2hb_live_lock);
2402
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
2421 unlock:
2422         spin_unlock(&o2hb_live_lock);
2423         return ret;
2424 }
2425
2426 void o2hb_region_dec_user(const char *region_uuid)
2427 {
2428         spin_lock(&o2hb_live_lock);
2429
2430         /* local heartbeat */
2431         if (!o2hb_global_heartbeat_active()) {
2432             o2hb_region_unpin(region_uuid);
2433             goto unlock;
2434         }
2435
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);
2443
2444 unlock:
2445         spin_unlock(&o2hb_live_lock);
2446 }
2447
2448 int o2hb_register_callback(const char *region_uuid,
2449                            struct o2hb_callback_func *hc)
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
2465         if (region_uuid) {
2466                 ret = o2hb_region_inc_user(region_uuid);
2467                 if (ret) {
2468                         mlog_errno(ret);
2469                         goto out;
2470                 }
2471         }
2472
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;
2487 out:
2488         mlog(ML_CLUSTER, "returning %d on behalf of %p for funcs %p\n",
2489              ret, __builtin_return_address(0), hc);
2490         return ret;
2491 }
2492 EXPORT_SYMBOL_GPL(o2hb_register_callback);
2493
2494 void o2hb_unregister_callback(const char *region_uuid,
2495                               struct o2hb_callback_func *hc)
2496 {
2497         BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2498
2499         mlog(ML_CLUSTER, "on behalf of %p for funcs %p\n",
2500              __builtin_return_address(0), hc);
2501
2502         /* XXX Can this happen _with_ a region reference? */
2503         if (list_empty(&hc->hc_item))
2504                 return;
2505
2506         if (region_uuid)
2507                 o2hb_region_dec_user(region_uuid);
2508
2509         down_write(&o2hb_callback_sem);
2510
2511         list_del_init(&hc->hc_item);
2512
2513         up_write(&o2hb_callback_sem);
2514 }
2515 EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
2516
2517 int 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 }
2531 EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating);
2532
2533 int 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 }
2547 EXPORT_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. */
2551 int 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 }
2564 EXPORT_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  */
2570 void 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 }
2583 EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);
2584
2585 int 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 }
2608 EXPORT_SYMBOL_GPL(o2hb_get_all_regions);
2609
2610 int o2hb_global_heartbeat_active(void)
2611 {
2612         return (o2hb_heartbeat_mode == O2HB_HEARTBEAT_GLOBAL);
2613 }
2614 EXPORT_SYMBOL(o2hb_global_heartbeat_active);