blob: f28de4b09c6b27acbc8060f16beae6aacf83dd01 [file] [log] [blame]
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001/* -*- 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>
Sunil Mushran87d3d3f2008-12-17 14:17:42 -080036#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Mark Fasheha7f6a5f2005-12-15 14:31:23 -080038
39#include "heartbeat.h"
40#include "tcp.h"
41#include "nodemanager.h"
42#include "quorum.h"
43
44#include "masklog.h"
45
46
47/*
48 * The first heartbeat pass had one global thread that would serialize all hb
49 * callback calls. This global serializing sem should only be removed once
50 * we've made sure that all callees can deal with being called concurrently
51 * from multiple hb region threads.
52 */
53static DECLARE_RWSEM(o2hb_callback_sem);
54
55/*
56 * multiple hb threads are watching multiple regions. A node is live
57 * whenever any of the threads sees activity from the node in its region.
58 */
Ingo Molnar34af9462006-06-27 02:53:55 -070059static DEFINE_SPINLOCK(o2hb_live_lock);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -080060static struct list_head o2hb_live_slots[O2NM_MAX_NODES];
61static unsigned long o2hb_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
62static LIST_HEAD(o2hb_node_events);
63static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue);
64
Sunil Mushran536f0742010-10-07 17:03:07 -070065/*
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.
Sunil Mushrane7d656b2010-10-06 17:55:18 -070068 * - o2hb_live_region_bitmap tracks live regions (seen steady iterations).
Sunil Mushran43182d22010-10-06 17:55:16 -070069 * - o2hb_quorum_region_bitmap tracks live regions that have seen all nodes
70 * heartbeat on it.
Sunil Mushranb1c5ebf2010-10-07 17:05:52 -070071 * - o2hb_failed_region_bitmap tracks the regions that have seen io timeouts.
Sunil Mushran536f0742010-10-07 17:03:07 -070072 */
73static unsigned long o2hb_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
Sunil Mushrane7d656b2010-10-06 17:55:18 -070074static unsigned long o2hb_live_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
Sunil Mushran43182d22010-10-06 17:55:16 -070075static unsigned long o2hb_quorum_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
Sunil Mushranb1c5ebf2010-10-07 17:05:52 -070076static unsigned long o2hb_failed_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
Sunil Mushran536f0742010-10-07 17:03:07 -070077
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -070078#define O2HB_DB_TYPE_LIVENODES 0
Sunil Mushrana6de0132010-10-06 17:55:13 -070079#define O2HB_DB_TYPE_LIVEREGIONS 1
80#define O2HB_DB_TYPE_QUORUMREGIONS 2
81#define O2HB_DB_TYPE_FAILEDREGIONS 3
Sunil Mushran1f285302010-10-06 17:55:12 -070082#define O2HB_DB_TYPE_REGION_LIVENODES 4
83#define O2HB_DB_TYPE_REGION_NUMBER 5
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -070084struct o2hb_debug_buf {
85 int db_type;
86 int db_size;
87 int db_len;
88 void *db_data;
89};
90
91static struct o2hb_debug_buf *o2hb_db_livenodes;
Sunil Mushrana6de0132010-10-06 17:55:13 -070092static struct o2hb_debug_buf *o2hb_db_liveregions;
93static struct o2hb_debug_buf *o2hb_db_quorumregions;
94static struct o2hb_debug_buf *o2hb_db_failedregions;
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -070095
Sunil Mushran87d3d3f2008-12-17 14:17:42 -080096#define O2HB_DEBUG_DIR "o2hb"
97#define O2HB_DEBUG_LIVENODES "livenodes"
Sunil Mushrana6de0132010-10-06 17:55:13 -070098#define O2HB_DEBUG_LIVEREGIONS "live_regions"
99#define O2HB_DEBUG_QUORUMREGIONS "quorum_regions"
100#define O2HB_DEBUG_FAILEDREGIONS "failed_regions"
Sunil Mushran1f285302010-10-06 17:55:12 -0700101#define O2HB_DEBUG_REGION_NUMBER "num"
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -0700102
Sunil Mushran87d3d3f2008-12-17 14:17:42 -0800103static struct dentry *o2hb_debug_dir;
104static struct dentry *o2hb_debug_livenodes;
Sunil Mushrana6de0132010-10-06 17:55:13 -0700105static struct dentry *o2hb_debug_liveregions;
106static struct dentry *o2hb_debug_quorumregions;
107static struct dentry *o2hb_debug_failedregions;
Sunil Mushran87d3d3f2008-12-17 14:17:42 -0800108
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800109static LIST_HEAD(o2hb_all_regions);
110
111static struct o2hb_callback {
112 struct list_head list;
113} o2hb_callbacks[O2HB_NUM_CB];
114
115static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type);
116
117#define O2HB_DEFAULT_BLOCK_BITS 9
118
Sunil Mushran54b51872010-10-07 15:26:08 -0700119enum o2hb_heartbeat_modes {
120 O2HB_HEARTBEAT_LOCAL = 0,
121 O2HB_HEARTBEAT_GLOBAL,
122 O2HB_HEARTBEAT_NUM_MODES,
123};
124
125char *o2hb_heartbeat_mode_desc[O2HB_HEARTBEAT_NUM_MODES] = {
126 "local", /* O2HB_HEARTBEAT_LOCAL */
127 "global", /* O2HB_HEARTBEAT_GLOBAL */
128};
129
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800130unsigned int o2hb_dead_threshold = O2HB_DEFAULT_DEAD_THRESHOLD;
Sunil Mushran54b51872010-10-07 15:26:08 -0700131unsigned int o2hb_heartbeat_mode = O2HB_HEARTBEAT_LOCAL;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800132
Sunil Mushran2bd63212010-01-25 16:57:38 -0800133/* Only sets a new threshold if there are no active regions.
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800134 *
135 * No locking or otherwise interesting code is required for reading
136 * o2hb_dead_threshold as it can't change once regions are active and
137 * it's not interesting to anyone until then anyway. */
138static void o2hb_dead_threshold_set(unsigned int threshold)
139{
140 if (threshold > O2HB_MIN_DEAD_THRESHOLD) {
141 spin_lock(&o2hb_live_lock);
142 if (list_empty(&o2hb_all_regions))
143 o2hb_dead_threshold = threshold;
144 spin_unlock(&o2hb_live_lock);
145 }
146}
147
Sunil Mushran54b51872010-10-07 15:26:08 -0700148static int o2hb_global_hearbeat_mode_set(unsigned int hb_mode)
149{
150 int ret = -1;
151
152 if (hb_mode < O2HB_HEARTBEAT_NUM_MODES) {
153 spin_lock(&o2hb_live_lock);
154 if (list_empty(&o2hb_all_regions)) {
155 o2hb_heartbeat_mode = hb_mode;
156 ret = 0;
157 }
158 spin_unlock(&o2hb_live_lock);
159 }
160
161 return ret;
162}
163
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800164struct o2hb_node_event {
165 struct list_head hn_item;
166 enum o2hb_callback_type hn_event_type;
167 struct o2nm_node *hn_node;
168 int hn_node_num;
169};
170
171struct o2hb_disk_slot {
172 struct o2hb_disk_heartbeat_block *ds_raw_block;
173 u8 ds_node_num;
174 u64 ds_last_time;
175 u64 ds_last_generation;
176 u16 ds_equal_samples;
177 u16 ds_changed_samples;
178 struct list_head ds_live_item;
179};
180
181/* each thread owns a region.. when we're asked to tear down the region
182 * we ask the thread to stop, who cleans up the region */
183struct o2hb_region {
184 struct config_item hr_item;
185
186 struct list_head hr_all_item;
187 unsigned hr_unclean_stop:1;
188
189 /* protected by the hr_callback_sem */
190 struct task_struct *hr_task;
191
192 unsigned int hr_blocks;
193 unsigned long long hr_start_block;
194
195 unsigned int hr_block_bits;
196 unsigned int hr_block_bytes;
197
198 unsigned int hr_slots_per_page;
199 unsigned int hr_num_pages;
200
201 struct page **hr_slot_data;
202 struct block_device *hr_bdev;
203 struct o2hb_disk_slot *hr_slots;
204
Sunil Mushran823a6372010-10-06 17:55:21 -0700205 /* live node map of this region */
206 unsigned long hr_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
Sunil Mushran536f0742010-10-07 17:03:07 -0700207 unsigned int hr_region_num;
Sunil Mushran823a6372010-10-06 17:55:21 -0700208
Sunil Mushran1f285302010-10-06 17:55:12 -0700209 struct dentry *hr_debug_dir;
210 struct dentry *hr_debug_livenodes;
211 struct dentry *hr_debug_regnum;
212 struct o2hb_debug_buf *hr_db_livenodes;
213 struct o2hb_debug_buf *hr_db_regnum;
214
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800215 /* let the person setting up hb wait for it to return until it
216 * has reached a 'steady' state. This will be fixed when we have
217 * a more complete api that doesn't lead to this sort of fragility. */
218 atomic_t hr_steady_iterations;
219
220 char hr_dev_name[BDEVNAME_SIZE];
221
222 unsigned int hr_timeout_ms;
223
224 /* randomized as the region goes up and down so that a node
225 * recognizes a node going up and down in one iteration */
226 u64 hr_generation;
227
David Howellsc4028952006-11-22 14:57:56 +0000228 struct delayed_work hr_write_timeout_work;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800229 unsigned long hr_last_timeout_start;
230
231 /* Used during o2hb_check_slot to hold a copy of the block
232 * being checked because we temporarily have to zero out the
233 * crc field. */
234 struct o2hb_disk_heartbeat_block *hr_tmp_block;
235};
236
237struct o2hb_bio_wait_ctxt {
238 atomic_t wc_num_reqs;
239 struct completion wc_io_complete;
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800240 int wc_error;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800241};
242
Sunil Mushranb1c5ebf2010-10-07 17:05:52 -0700243static int o2hb_pop_count(void *map, int count)
244{
245 int i = -1, pop = 0;
246
247 while ((i = find_next_bit(map, count, i + 1)) < count)
248 pop++;
249 return pop;
250}
251
David Howellsc4028952006-11-22 14:57:56 +0000252static void o2hb_write_timeout(struct work_struct *work)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800253{
Sunil Mushranb1c5ebf2010-10-07 17:05:52 -0700254 int failed, quorum;
255 unsigned long flags;
David Howellsc4028952006-11-22 14:57:56 +0000256 struct o2hb_region *reg =
257 container_of(work, struct o2hb_region,
258 hr_write_timeout_work.work);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800259
260 mlog(ML_ERROR, "Heartbeat write timeout to device %s after %u "
261 "milliseconds\n", reg->hr_dev_name,
Sunil Mushran2bd63212010-01-25 16:57:38 -0800262 jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
Sunil Mushranb1c5ebf2010-10-07 17:05:52 -0700263
264 if (o2hb_global_heartbeat_active()) {
265 spin_lock_irqsave(&o2hb_live_lock, flags);
266 if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
267 set_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
268 failed = o2hb_pop_count(&o2hb_failed_region_bitmap,
269 O2NM_MAX_REGIONS);
270 quorum = o2hb_pop_count(&o2hb_quorum_region_bitmap,
271 O2NM_MAX_REGIONS);
272 spin_unlock_irqrestore(&o2hb_live_lock, flags);
273
274 mlog(ML_HEARTBEAT, "Number of regions %d, failed regions %d\n",
275 quorum, failed);
276
277 /*
278 * Fence if the number of failed regions >= half the number
279 * of quorum regions
280 */
281 if ((failed << 1) < quorum)
282 return;
283 }
284
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800285 o2quo_disk_timeout();
286}
287
288static void o2hb_arm_write_timeout(struct o2hb_region *reg)
289{
Tao Mab31d3082009-12-22 10:32:15 +0800290 mlog(ML_HEARTBEAT, "Queue write timeout for %u ms\n",
291 O2HB_MAX_WRITE_TIMEOUT_MS);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800292
Sunil Mushranb1c5ebf2010-10-07 17:05:52 -0700293 if (o2hb_global_heartbeat_active()) {
294 spin_lock(&o2hb_live_lock);
295 clear_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
296 spin_unlock(&o2hb_live_lock);
297 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800298 cancel_delayed_work(&reg->hr_write_timeout_work);
299 reg->hr_last_timeout_start = jiffies;
300 schedule_delayed_work(&reg->hr_write_timeout_work,
301 msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS));
302}
303
304static void o2hb_disarm_write_timeout(struct o2hb_region *reg)
305{
306 cancel_delayed_work(&reg->hr_write_timeout_work);
307 flush_scheduled_work();
308}
309
Philipp Reisnerb5592922007-01-11 10:58:10 +0100310static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt *wc)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800311{
Philipp Reisnerb5592922007-01-11 10:58:10 +0100312 atomic_set(&wc->wc_num_reqs, 1);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800313 init_completion(&wc->wc_io_complete);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800314 wc->wc_error = 0;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800315}
316
317/* Used in error paths too */
318static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt *wc,
319 unsigned int num)
320{
321 /* sadly atomic_sub_and_test() isn't available on all platforms. The
322 * good news is that the fast path only completes one at a time */
323 while(num--) {
324 if (atomic_dec_and_test(&wc->wc_num_reqs)) {
325 BUG_ON(num > 0);
326 complete(&wc->wc_io_complete);
327 }
328 }
329}
330
331static void o2hb_wait_on_io(struct o2hb_region *reg,
332 struct o2hb_bio_wait_ctxt *wc)
333{
334 struct address_space *mapping = reg->hr_bdev->bd_inode->i_mapping;
335
336 blk_run_address_space(mapping);
Philipp Reisnerb5592922007-01-11 10:58:10 +0100337 o2hb_bio_wait_dec(wc, 1);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800338
339 wait_for_completion(&wc->wc_io_complete);
340}
341
Al Viro782e3b32007-10-12 07:17:47 +0100342static void o2hb_bio_end_io(struct bio *bio,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800343 int error)
344{
345 struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
346
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800347 if (error) {
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800348 mlog(ML_ERROR, "IO Error %d\n", error);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800349 wc->wc_error = error;
350 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800351
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800352 o2hb_bio_wait_dec(wc, 1);
Philipp Reisnerb5592922007-01-11 10:58:10 +0100353 bio_put(bio);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800354}
355
356/* Setup a Bio to cover I/O against num_slots slots starting at
357 * start_slot. */
358static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
359 struct o2hb_bio_wait_ctxt *wc,
Philipp Reisnerb5592922007-01-11 10:58:10 +0100360 unsigned int *current_slot,
361 unsigned int max_slots)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800362{
Philipp Reisnerb5592922007-01-11 10:58:10 +0100363 int len, current_page;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800364 unsigned int vec_len, vec_start;
365 unsigned int bits = reg->hr_block_bits;
366 unsigned int spp = reg->hr_slots_per_page;
Philipp Reisnerb5592922007-01-11 10:58:10 +0100367 unsigned int cs = *current_slot;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800368 struct bio *bio;
369 struct page *page;
370
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800371 /* Testing has shown this allocation to take long enough under
372 * GFP_KERNEL that the local node can get fenced. It would be
373 * nicest if we could pre-allocate these bios and avoid this
374 * all together. */
Philipp Reisnerb5592922007-01-11 10:58:10 +0100375 bio = bio_alloc(GFP_ATOMIC, 16);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800376 if (!bio) {
377 mlog(ML_ERROR, "Could not alloc slots BIO!\n");
378 bio = ERR_PTR(-ENOMEM);
379 goto bail;
380 }
381
382 /* Must put everything in 512 byte sectors for the bio... */
Philipp Reisnerb5592922007-01-11 10:58:10 +0100383 bio->bi_sector = (reg->hr_start_block + cs) << (bits - 9);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800384 bio->bi_bdev = reg->hr_bdev;
385 bio->bi_private = wc;
386 bio->bi_end_io = o2hb_bio_end_io;
387
Philipp Reisnerb5592922007-01-11 10:58:10 +0100388 vec_start = (cs << bits) % PAGE_CACHE_SIZE;
389 while(cs < max_slots) {
390 current_page = cs / spp;
391 page = reg->hr_slot_data[current_page];
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800392
Jan Karabc7e97c2007-10-10 16:25:42 +0200393 vec_len = min(PAGE_CACHE_SIZE - vec_start,
Philipp Reisnerb5592922007-01-11 10:58:10 +0100394 (max_slots-cs) * (PAGE_CACHE_SIZE/spp) );
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800395
396 mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
Philipp Reisnerb5592922007-01-11 10:58:10 +0100397 current_page, vec_len, vec_start);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800398
399 len = bio_add_page(bio, page, vec_len, vec_start);
Philipp Reisnerb5592922007-01-11 10:58:10 +0100400 if (len != vec_len) break;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800401
Philipp Reisnerb5592922007-01-11 10:58:10 +0100402 cs += vec_len / (PAGE_CACHE_SIZE/spp);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800403 vec_start = 0;
404 }
405
406bail:
Philipp Reisnerb5592922007-01-11 10:58:10 +0100407 *current_slot = cs;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800408 return bio;
409}
410
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800411static int o2hb_read_slots(struct o2hb_region *reg,
412 unsigned int max_slots)
413{
Philipp Reisnerb5592922007-01-11 10:58:10 +0100414 unsigned int current_slot=0;
415 int status;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800416 struct o2hb_bio_wait_ctxt wc;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800417 struct bio *bio;
418
Philipp Reisnerb5592922007-01-11 10:58:10 +0100419 o2hb_bio_wait_init(&wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800420
Philipp Reisnerb5592922007-01-11 10:58:10 +0100421 while(current_slot < max_slots) {
422 bio = o2hb_setup_one_bio(reg, &wc, &current_slot, max_slots);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800423 if (IS_ERR(bio)) {
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800424 status = PTR_ERR(bio);
425 mlog_errno(status);
426 goto bail_and_wait;
427 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800428
Philipp Reisnerb5592922007-01-11 10:58:10 +0100429 atomic_inc(&wc.wc_num_reqs);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800430 submit_bio(READ, bio);
431 }
432
433 status = 0;
434
435bail_and_wait:
436 o2hb_wait_on_io(reg, &wc);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800437 if (wc.wc_error && !status)
438 status = wc.wc_error;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800439
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800440 return status;
441}
442
443static int o2hb_issue_node_write(struct o2hb_region *reg,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800444 struct o2hb_bio_wait_ctxt *write_wc)
445{
446 int status;
447 unsigned int slot;
448 struct bio *bio;
449
Philipp Reisnerb5592922007-01-11 10:58:10 +0100450 o2hb_bio_wait_init(write_wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800451
452 slot = o2nm_this_node();
453
Philipp Reisnerb5592922007-01-11 10:58:10 +0100454 bio = o2hb_setup_one_bio(reg, write_wc, &slot, slot+1);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800455 if (IS_ERR(bio)) {
456 status = PTR_ERR(bio);
457 mlog_errno(status);
458 goto bail;
459 }
460
Philipp Reisnerb5592922007-01-11 10:58:10 +0100461 atomic_inc(&write_wc->wc_num_reqs);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800462 submit_bio(WRITE, bio);
463
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800464 status = 0;
465bail:
466 return status;
467}
468
469static u32 o2hb_compute_block_crc_le(struct o2hb_region *reg,
470 struct o2hb_disk_heartbeat_block *hb_block)
471{
472 __le32 old_cksum;
473 u32 ret;
474
475 /* We want to compute the block crc with a 0 value in the
476 * hb_cksum field. Save it off here and replace after the
477 * crc. */
478 old_cksum = hb_block->hb_cksum;
479 hb_block->hb_cksum = 0;
480
481 ret = crc32_le(0, (unsigned char *) hb_block, reg->hr_block_bytes);
482
483 hb_block->hb_cksum = old_cksum;
484
485 return ret;
486}
487
488static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
489{
Mark Fasheh70bacbd2006-03-02 11:10:05 -0800490 mlog(ML_ERROR, "Dump slot information: seq = 0x%llx, node = %u, "
491 "cksum = 0x%x, generation 0x%llx\n",
492 (long long)le64_to_cpu(hb_block->hb_seq),
493 hb_block->hb_node, le32_to_cpu(hb_block->hb_cksum),
494 (long long)le64_to_cpu(hb_block->hb_generation));
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800495}
496
497static int o2hb_verify_crc(struct o2hb_region *reg,
498 struct o2hb_disk_heartbeat_block *hb_block)
499{
500 u32 read, computed;
501
502 read = le32_to_cpu(hb_block->hb_cksum);
503 computed = o2hb_compute_block_crc_le(reg, hb_block);
504
505 return read == computed;
506}
507
508/* We want to make sure that nobody is heartbeating on top of us --
509 * this will help detect an invalid configuration. */
510static int o2hb_check_last_timestamp(struct o2hb_region *reg)
511{
512 int node_num, ret;
513 struct o2hb_disk_slot *slot;
514 struct o2hb_disk_heartbeat_block *hb_block;
515
516 node_num = o2nm_this_node();
517
518 ret = 1;
519 slot = &reg->hr_slots[node_num];
520 /* Don't check on our 1st timestamp */
521 if (slot->ds_last_time) {
522 hb_block = slot->ds_raw_block;
523
524 if (le64_to_cpu(hb_block->hb_seq) != slot->ds_last_time)
525 ret = 0;
526 }
527
528 return ret;
529}
530
531static inline void o2hb_prepare_block(struct o2hb_region *reg,
532 u64 generation)
533{
534 int node_num;
535 u64 cputime;
536 struct o2hb_disk_slot *slot;
537 struct o2hb_disk_heartbeat_block *hb_block;
538
539 node_num = o2nm_this_node();
540 slot = &reg->hr_slots[node_num];
541
542 hb_block = (struct o2hb_disk_heartbeat_block *)slot->ds_raw_block;
543 memset(hb_block, 0, reg->hr_block_bytes);
544 /* TODO: time stuff */
545 cputime = CURRENT_TIME.tv_sec;
546 if (!cputime)
547 cputime = 1;
548
549 hb_block->hb_seq = cpu_to_le64(cputime);
550 hb_block->hb_node = node_num;
551 hb_block->hb_generation = cpu_to_le64(generation);
Mark Fasheh0db638f2006-05-09 15:09:35 -0700552 hb_block->hb_dead_ms = cpu_to_le32(o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800553
554 /* This step must always happen last! */
555 hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
556 hb_block));
557
Mark Fasheh70bacbd2006-03-02 11:10:05 -0800558 mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x\n",
Mark Fasheh5fdf1e62007-04-27 16:50:03 -0700559 (long long)generation,
Mark Fasheh70bacbd2006-03-02 11:10:05 -0800560 le32_to_cpu(hb_block->hb_cksum));
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800561}
562
563static void o2hb_fire_callbacks(struct o2hb_callback *hbcall,
564 struct o2nm_node *node,
565 int idx)
566{
567 struct list_head *iter;
568 struct o2hb_callback_func *f;
569
570 list_for_each(iter, &hbcall->list) {
571 f = list_entry(iter, struct o2hb_callback_func, hc_item);
572 mlog(ML_HEARTBEAT, "calling funcs %p\n", f);
573 (f->hc_func)(node, idx, f->hc_data);
574 }
575}
576
577/* Will run the list in order until we process the passed event */
578static void o2hb_run_event_list(struct o2hb_node_event *queued_event)
579{
580 int empty;
581 struct o2hb_callback *hbcall;
582 struct o2hb_node_event *event;
583
584 spin_lock(&o2hb_live_lock);
585 empty = list_empty(&queued_event->hn_item);
586 spin_unlock(&o2hb_live_lock);
587 if (empty)
588 return;
589
590 /* Holding callback sem assures we don't alter the callback
591 * lists when doing this, and serializes ourselves with other
592 * processes wanting callbacks. */
593 down_write(&o2hb_callback_sem);
594
595 spin_lock(&o2hb_live_lock);
596 while (!list_empty(&o2hb_node_events)
597 && !list_empty(&queued_event->hn_item)) {
598 event = list_entry(o2hb_node_events.next,
599 struct o2hb_node_event,
600 hn_item);
601 list_del_init(&event->hn_item);
602 spin_unlock(&o2hb_live_lock);
603
604 mlog(ML_HEARTBEAT, "Node %s event for %d\n",
605 event->hn_event_type == O2HB_NODE_UP_CB ? "UP" : "DOWN",
606 event->hn_node_num);
607
608 hbcall = hbcall_from_type(event->hn_event_type);
609
610 /* We should *never* have gotten on to the list with a
611 * bad type... This isn't something that we should try
612 * to recover from. */
613 BUG_ON(IS_ERR(hbcall));
614
615 o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num);
616
617 spin_lock(&o2hb_live_lock);
618 }
619 spin_unlock(&o2hb_live_lock);
620
621 up_write(&o2hb_callback_sem);
622}
623
624static void o2hb_queue_node_event(struct o2hb_node_event *event,
625 enum o2hb_callback_type type,
626 struct o2nm_node *node,
627 int node_num)
628{
629 assert_spin_locked(&o2hb_live_lock);
630
Sunil Mushran0e105d32010-10-07 17:00:16 -0700631 BUG_ON((!node) && (type != O2HB_NODE_DOWN_CB));
632
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800633 event->hn_event_type = type;
634 event->hn_node = node;
635 event->hn_node_num = node_num;
636
637 mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
638 type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
639
640 list_add_tail(&event->hn_item, &o2hb_node_events);
641}
642
643static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
644{
645 struct o2hb_node_event event =
646 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
647 struct o2nm_node *node;
648
649 node = o2nm_get_node_by_num(slot->ds_node_num);
650 if (!node)
651 return;
652
653 spin_lock(&o2hb_live_lock);
654 if (!list_empty(&slot->ds_live_item)) {
655 mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
656 slot->ds_node_num);
657
658 list_del_init(&slot->ds_live_item);
659
660 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
661 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
662
663 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
664 slot->ds_node_num);
665 }
666 }
667 spin_unlock(&o2hb_live_lock);
668
669 o2hb_run_event_list(&event);
670
671 o2nm_node_put(node);
672}
673
Sunil Mushran43182d22010-10-06 17:55:16 -0700674static void o2hb_set_quorum_device(struct o2hb_region *reg,
675 struct o2hb_disk_slot *slot)
676{
677 assert_spin_locked(&o2hb_live_lock);
678
679 if (!o2hb_global_heartbeat_active())
680 return;
681
682 if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
683 return;
684
685 /*
686 * A region can be added to the quorum only when it sees all
687 * live nodes heartbeat on it. In other words, the region has been
688 * added to all nodes.
689 */
690 if (memcmp(reg->hr_live_node_bitmap, o2hb_live_node_bitmap,
691 sizeof(o2hb_live_node_bitmap)))
692 return;
693
694 if (slot->ds_changed_samples < O2HB_LIVE_THRESHOLD)
695 return;
696
697 printk(KERN_NOTICE "o2hb: Region %s is now a quorum device\n",
698 config_item_name(&reg->hr_item));
699
700 set_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
701}
702
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800703static int o2hb_check_slot(struct o2hb_region *reg,
704 struct o2hb_disk_slot *slot)
705{
706 int changed = 0, gen_changed = 0;
707 struct o2hb_node_event event =
708 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
709 struct o2nm_node *node;
710 struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
711 u64 cputime;
Mark Fasheh0db638f2006-05-09 15:09:35 -0700712 unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
713 unsigned int slot_dead_ms;
Sunil Mushran0e105d32010-10-07 17:00:16 -0700714 int tmp;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800715
716 memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
717
Sunil Mushran0e105d32010-10-07 17:00:16 -0700718 /*
719 * If a node is no longer configured but is still in the livemap, we
720 * may need to clear that bit from the livemap.
721 */
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800722 node = o2nm_get_node_by_num(slot->ds_node_num);
Sunil Mushran0e105d32010-10-07 17:00:16 -0700723 if (!node) {
724 spin_lock(&o2hb_live_lock);
725 tmp = test_bit(slot->ds_node_num, o2hb_live_node_bitmap);
726 spin_unlock(&o2hb_live_lock);
727 if (!tmp)
728 return 0;
729 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800730
731 if (!o2hb_verify_crc(reg, hb_block)) {
732 /* all paths from here will drop o2hb_live_lock for
733 * us. */
734 spin_lock(&o2hb_live_lock);
735
736 /* Don't print an error on the console in this case -
737 * a freshly formatted heartbeat area will not have a
738 * crc set on it. */
739 if (list_empty(&slot->ds_live_item))
740 goto out;
741
742 /* The node is live but pushed out a bad crc. We
743 * consider it a transient miss but don't populate any
744 * other values as they may be junk. */
745 mlog(ML_ERROR, "Node %d has written a bad crc to %s\n",
746 slot->ds_node_num, reg->hr_dev_name);
747 o2hb_dump_slot(hb_block);
748
749 slot->ds_equal_samples++;
750 goto fire_callbacks;
751 }
752
753 /* we don't care if these wrap.. the state transitions below
754 * clear at the right places */
755 cputime = le64_to_cpu(hb_block->hb_seq);
756 if (slot->ds_last_time != cputime)
757 slot->ds_changed_samples++;
758 else
759 slot->ds_equal_samples++;
760 slot->ds_last_time = cputime;
761
762 /* The node changed heartbeat generations. We assume this to
763 * mean it dropped off but came back before we timed out. We
764 * want to consider it down for the time being but don't want
765 * to lose any changed_samples state we might build up to
766 * considering it live again. */
767 if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
768 gen_changed = 1;
769 slot->ds_equal_samples = 0;
Mark Fasheh70bacbd2006-03-02 11:10:05 -0800770 mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
771 "to 0x%llx)\n", slot->ds_node_num,
772 (long long)slot->ds_last_generation,
773 (long long)le64_to_cpu(hb_block->hb_generation));
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800774 }
775
776 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
777
Mark Fasheh70bacbd2006-03-02 11:10:05 -0800778 mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
779 "seq %llu last %llu changed %u equal %u\n",
780 slot->ds_node_num, (long long)slot->ds_last_generation,
781 le32_to_cpu(hb_block->hb_cksum),
Sunil Mushran2bd63212010-01-25 16:57:38 -0800782 (unsigned long long)le64_to_cpu(hb_block->hb_seq),
Mark Fasheh70bacbd2006-03-02 11:10:05 -0800783 (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800784 slot->ds_equal_samples);
785
786 spin_lock(&o2hb_live_lock);
787
788fire_callbacks:
789 /* dead nodes only come to life after some number of
790 * changes at any time during their dead time */
791 if (list_empty(&slot->ds_live_item) &&
792 slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
Mark Fasheh70bacbd2006-03-02 11:10:05 -0800793 mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
794 slot->ds_node_num, (long long)slot->ds_last_generation);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800795
Sunil Mushran823a6372010-10-06 17:55:21 -0700796 set_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
797
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800798 /* first on the list generates a callback */
799 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
800 set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
801
802 o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
803 slot->ds_node_num);
804
805 changed = 1;
806 }
807
808 list_add_tail(&slot->ds_live_item,
809 &o2hb_live_slots[slot->ds_node_num]);
810
811 slot->ds_equal_samples = 0;
Mark Fasheh0db638f2006-05-09 15:09:35 -0700812
813 /* We want to be sure that all nodes agree on the
814 * number of milliseconds before a node will be
815 * considered dead. The self-fencing timeout is
816 * computed from this value, and a discrepancy might
817 * result in heartbeat calling a node dead when it
818 * hasn't self-fenced yet. */
819 slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
820 if (slot_dead_ms && slot_dead_ms != dead_ms) {
821 /* TODO: Perhaps we can fail the region here. */
822 mlog(ML_ERROR, "Node %d on device %s has a dead count "
823 "of %u ms, but our count is %u ms.\n"
824 "Please double check your configuration values "
825 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
826 slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
827 dead_ms);
828 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800829 goto out;
830 }
831
832 /* if the list is dead, we're done.. */
833 if (list_empty(&slot->ds_live_item))
834 goto out;
835
836 /* live nodes only go dead after enough consequtive missed
837 * samples.. reset the missed counter whenever we see
838 * activity */
839 if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
840 mlog(ML_HEARTBEAT, "Node %d left my region\n",
841 slot->ds_node_num);
842
Sunil Mushran823a6372010-10-06 17:55:21 -0700843 clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
844
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800845 /* last off the live_slot generates a callback */
846 list_del_init(&slot->ds_live_item);
847 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
848 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
849
Sunil Mushran0e105d32010-10-07 17:00:16 -0700850 /* node can be null */
851 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
852 node, slot->ds_node_num);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800853
854 changed = 1;
855 }
856
857 /* We don't clear this because the node is still
858 * actually writing new blocks. */
859 if (!gen_changed)
860 slot->ds_changed_samples = 0;
861 goto out;
862 }
863 if (slot->ds_changed_samples) {
864 slot->ds_changed_samples = 0;
865 slot->ds_equal_samples = 0;
866 }
867out:
Sunil Mushran43182d22010-10-06 17:55:16 -0700868 o2hb_set_quorum_device(reg, slot);
869
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800870 spin_unlock(&o2hb_live_lock);
871
872 o2hb_run_event_list(&event);
873
Sunil Mushran0e105d32010-10-07 17:00:16 -0700874 if (node)
875 o2nm_node_put(node);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800876 return changed;
877}
878
879/* This could be faster if we just implmented a find_last_bit, but I
880 * don't think the circumstances warrant it. */
881static int o2hb_highest_node(unsigned long *nodes,
882 int numbits)
883{
884 int highest, node;
885
886 highest = numbits;
887 node = -1;
888 while ((node = find_next_bit(nodes, numbits, node + 1)) != -1) {
889 if (node >= numbits)
890 break;
891
892 highest = node;
893 }
894
895 return highest;
896}
897
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800898static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800899{
900 int i, ret, highest_node, change = 0;
901 unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
Sunil Mushran0e105d32010-10-07 17:00:16 -0700902 unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800903 struct o2hb_bio_wait_ctxt write_wc;
904
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800905 ret = o2nm_configured_node_map(configured_nodes,
906 sizeof(configured_nodes));
907 if (ret) {
908 mlog_errno(ret);
909 return ret;
910 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800911
Sunil Mushran0e105d32010-10-07 17:00:16 -0700912 /*
913 * If a node is not configured but is in the livemap, we still need
914 * to read the slot so as to be able to remove it from the livemap.
915 */
916 o2hb_fill_node_map(live_node_bitmap, sizeof(live_node_bitmap));
917 i = -1;
918 while ((i = find_next_bit(live_node_bitmap,
919 O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
920 set_bit(i, configured_nodes);
921 }
922
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800923 highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
924 if (highest_node >= O2NM_MAX_NODES) {
925 mlog(ML_NOTICE, "ocfs2_heartbeat: no configured nodes found!\n");
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800926 return -EINVAL;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800927 }
928
929 /* No sense in reading the slots of nodes that don't exist
930 * yet. Of course, if the node definitions have holes in them
931 * then we're reading an empty slot anyway... Consider this
932 * best-effort. */
933 ret = o2hb_read_slots(reg, highest_node + 1);
934 if (ret < 0) {
935 mlog_errno(ret);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800936 return ret;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800937 }
938
939 /* With an up to date view of the slots, we can check that no
940 * other node has been improperly configured to heartbeat in
941 * our slot. */
942 if (!o2hb_check_last_timestamp(reg))
943 mlog(ML_ERROR, "Device \"%s\": another node is heartbeating "
944 "in our slot!\n", reg->hr_dev_name);
945
946 /* fill in the proper info for our next heartbeat */
947 o2hb_prepare_block(reg, reg->hr_generation);
948
949 /* And fire off the write. Note that we don't wait on this I/O
950 * until later. */
Philipp Reisnerb5592922007-01-11 10:58:10 +0100951 ret = o2hb_issue_node_write(reg, &write_wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800952 if (ret < 0) {
953 mlog_errno(ret);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800954 return ret;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800955 }
956
957 i = -1;
958 while((i = find_next_bit(configured_nodes, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
959
960 change |= o2hb_check_slot(reg, &reg->hr_slots[i]);
961 }
962
963 /*
964 * We have to be sure we've advertised ourselves on disk
965 * before we can go to steady state. This ensures that
966 * people we find in our steady state have seen us.
967 */
968 o2hb_wait_on_io(reg, &write_wc);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800969 if (write_wc.wc_error) {
970 /* Do not re-arm the write timeout on I/O error - we
971 * can't be sure that the new block ever made it to
972 * disk */
973 mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
974 write_wc.wc_error, reg->hr_dev_name);
975 return write_wc.wc_error;
976 }
977
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800978 o2hb_arm_write_timeout(reg);
979
980 /* let the person who launched us know when things are steady */
981 if (!change && (atomic_read(&reg->hr_steady_iterations) != 0)) {
982 if (atomic_dec_and_test(&reg->hr_steady_iterations))
983 wake_up(&o2hb_steady_queue);
984 }
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800985
986 return 0;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800987}
988
989/* Subtract b from a, storing the result in a. a *must* have a larger
990 * value than b. */
991static void o2hb_tv_subtract(struct timeval *a,
992 struct timeval *b)
993{
994 /* just return 0 when a is after b */
995 if (a->tv_sec < b->tv_sec ||
996 (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) {
997 a->tv_sec = 0;
998 a->tv_usec = 0;
999 return;
1000 }
1001
1002 a->tv_sec -= b->tv_sec;
1003 a->tv_usec -= b->tv_usec;
1004 while ( a->tv_usec < 0 ) {
1005 a->tv_sec--;
1006 a->tv_usec += 1000000;
1007 }
1008}
1009
1010static unsigned int o2hb_elapsed_msecs(struct timeval *start,
1011 struct timeval *end)
1012{
1013 struct timeval res = *end;
1014
1015 o2hb_tv_subtract(&res, start);
1016
1017 return res.tv_sec * 1000 + res.tv_usec / 1000;
1018}
1019
1020/*
1021 * we ride the region ref that the region dir holds. before the region
1022 * dir is removed and drops it ref it will wait to tear down this
1023 * thread.
1024 */
1025static int o2hb_thread(void *data)
1026{
1027 int i, ret;
1028 struct o2hb_region *reg = data;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001029 struct o2hb_bio_wait_ctxt write_wc;
1030 struct timeval before_hb, after_hb;
1031 unsigned int elapsed_msec;
1032
1033 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
1034
1035 set_user_nice(current, -20);
1036
1037 while (!kthread_should_stop() && !reg->hr_unclean_stop) {
1038 /* We track the time spent inside
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001039 * o2hb_do_disk_heartbeat so that we avoid more than
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001040 * hr_timeout_ms between disk writes. On busy systems
1041 * this should result in a heartbeat which is less
1042 * likely to time itself out. */
1043 do_gettimeofday(&before_hb);
1044
Mark Fasheha9e2ae32006-03-24 14:20:17 -08001045 i = 0;
1046 do {
1047 ret = o2hb_do_disk_heartbeat(reg);
1048 } while (ret && ++i < 2);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001049
1050 do_gettimeofday(&after_hb);
1051 elapsed_msec = o2hb_elapsed_msecs(&before_hb, &after_hb);
1052
Tao Mab31d3082009-12-22 10:32:15 +08001053 mlog(ML_HEARTBEAT,
1054 "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
Mark Fasheh215c7f92006-02-01 16:42:10 -08001055 before_hb.tv_sec, (unsigned long) before_hb.tv_usec,
1056 after_hb.tv_sec, (unsigned long) after_hb.tv_usec,
1057 elapsed_msec);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001058
1059 if (elapsed_msec < reg->hr_timeout_ms) {
1060 /* the kthread api has blocked signals for us so no
1061 * need to record the return value. */
1062 msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
1063 }
1064 }
1065
1066 o2hb_disarm_write_timeout(reg);
1067
1068 /* unclean stop is only used in very bad situation */
1069 for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
1070 o2hb_shutdown_slot(&reg->hr_slots[i]);
1071
1072 /* Explicit down notification - avoid forcing the other nodes
1073 * to timeout on this region when we could just as easily
1074 * write a clear generation - thus indicating to them that
1075 * this node has left this region.
1076 *
1077 * XXX: Should we skip this on unclean_stop? */
1078 o2hb_prepare_block(reg, 0);
Philipp Reisnerb5592922007-01-11 10:58:10 +01001079 ret = o2hb_issue_node_write(reg, &write_wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001080 if (ret == 0) {
1081 o2hb_wait_on_io(reg, &write_wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001082 } else {
1083 mlog_errno(ret);
1084 }
1085
1086 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread exiting\n");
1087
1088 return 0;
1089}
1090
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001091#ifdef CONFIG_DEBUG_FS
1092static int o2hb_debug_open(struct inode *inode, struct file *file)
1093{
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -07001094 struct o2hb_debug_buf *db = inode->i_private;
Sunil Mushran1f285302010-10-06 17:55:12 -07001095 struct o2hb_region *reg;
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001096 unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1097 char *buf = NULL;
1098 int i = -1;
1099 int out = 0;
1100
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -07001101 /* max_nodes should be the largest bitmap we pass here */
1102 BUG_ON(sizeof(map) < db->db_size);
1103
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001104 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1105 if (!buf)
1106 goto bail;
1107
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -07001108 switch (db->db_type) {
1109 case O2HB_DB_TYPE_LIVENODES:
Sunil Mushrana6de0132010-10-06 17:55:13 -07001110 case O2HB_DB_TYPE_LIVEREGIONS:
1111 case O2HB_DB_TYPE_QUORUMREGIONS:
1112 case O2HB_DB_TYPE_FAILEDREGIONS:
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -07001113 spin_lock(&o2hb_live_lock);
1114 memcpy(map, db->db_data, db->db_size);
1115 spin_unlock(&o2hb_live_lock);
1116 break;
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001117
Sunil Mushran1f285302010-10-06 17:55:12 -07001118 case O2HB_DB_TYPE_REGION_LIVENODES:
1119 spin_lock(&o2hb_live_lock);
1120 reg = (struct o2hb_region *)db->db_data;
1121 memcpy(map, reg->hr_live_node_bitmap, db->db_size);
1122 spin_unlock(&o2hb_live_lock);
1123 break;
1124
1125 case O2HB_DB_TYPE_REGION_NUMBER:
1126 reg = (struct o2hb_region *)db->db_data;
1127 out += snprintf(buf + out, PAGE_SIZE - out, "%d\n",
1128 reg->hr_region_num);
1129 goto done;
1130
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -07001131 default:
1132 goto done;
1133 }
1134
1135 while ((i = find_next_bit(map, db->db_len, i + 1)) < db->db_len)
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001136 out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
1137 out += snprintf(buf + out, PAGE_SIZE - out, "\n");
1138
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -07001139done:
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001140 i_size_write(inode, out);
1141
1142 file->private_data = buf;
1143
1144 return 0;
1145bail:
1146 return -ENOMEM;
1147}
1148
1149static int o2hb_debug_release(struct inode *inode, struct file *file)
1150{
1151 kfree(file->private_data);
1152 return 0;
1153}
1154
1155static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1156 size_t nbytes, loff_t *ppos)
1157{
1158 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
1159 i_size_read(file->f_mapping->host));
1160}
1161#else
1162static int o2hb_debug_open(struct inode *inode, struct file *file)
1163{
1164 return 0;
1165}
1166static int o2hb_debug_release(struct inode *inode, struct file *file)
1167{
1168 return 0;
1169}
1170static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1171 size_t nbytes, loff_t *ppos)
1172{
1173 return 0;
1174}
1175#endif /* CONFIG_DEBUG_FS */
1176
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001177static const struct file_operations o2hb_debug_fops = {
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001178 .open = o2hb_debug_open,
1179 .release = o2hb_debug_release,
1180 .read = o2hb_debug_read,
1181 .llseek = generic_file_llseek,
1182};
1183
1184void o2hb_exit(void)
1185{
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -07001186 kfree(o2hb_db_livenodes);
Sunil Mushrana6de0132010-10-06 17:55:13 -07001187 kfree(o2hb_db_liveregions);
1188 kfree(o2hb_db_quorumregions);
1189 kfree(o2hb_db_failedregions);
1190 debugfs_remove(o2hb_debug_failedregions);
1191 debugfs_remove(o2hb_debug_quorumregions);
1192 debugfs_remove(o2hb_debug_liveregions);
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -07001193 debugfs_remove(o2hb_debug_livenodes);
1194 debugfs_remove(o2hb_debug_dir);
1195}
1196
1197static struct dentry *o2hb_debug_create(const char *name, struct dentry *dir,
1198 struct o2hb_debug_buf **db, int db_len,
1199 int type, int size, int len, void *data)
1200{
1201 *db = kmalloc(db_len, GFP_KERNEL);
1202 if (!*db)
1203 return NULL;
1204
1205 (*db)->db_type = type;
1206 (*db)->db_size = size;
1207 (*db)->db_len = len;
1208 (*db)->db_data = data;
1209
1210 return debugfs_create_file(name, S_IFREG|S_IRUSR, dir, *db,
1211 &o2hb_debug_fops);
1212}
1213
1214static int o2hb_debug_init(void)
1215{
1216 int ret = -ENOMEM;
1217
1218 o2hb_debug_dir = debugfs_create_dir(O2HB_DEBUG_DIR, NULL);
1219 if (!o2hb_debug_dir) {
1220 mlog_errno(ret);
1221 goto bail;
1222 }
1223
1224 o2hb_debug_livenodes = o2hb_debug_create(O2HB_DEBUG_LIVENODES,
1225 o2hb_debug_dir,
1226 &o2hb_db_livenodes,
1227 sizeof(*o2hb_db_livenodes),
1228 O2HB_DB_TYPE_LIVENODES,
1229 sizeof(o2hb_live_node_bitmap),
1230 O2NM_MAX_NODES,
1231 o2hb_live_node_bitmap);
1232 if (!o2hb_debug_livenodes) {
1233 mlog_errno(ret);
1234 goto bail;
1235 }
Sunil Mushrana6de0132010-10-06 17:55:13 -07001236
1237 o2hb_debug_liveregions = o2hb_debug_create(O2HB_DEBUG_LIVEREGIONS,
1238 o2hb_debug_dir,
1239 &o2hb_db_liveregions,
1240 sizeof(*o2hb_db_liveregions),
1241 O2HB_DB_TYPE_LIVEREGIONS,
1242 sizeof(o2hb_live_region_bitmap),
1243 O2NM_MAX_REGIONS,
1244 o2hb_live_region_bitmap);
1245 if (!o2hb_debug_liveregions) {
1246 mlog_errno(ret);
1247 goto bail;
1248 }
1249
1250 o2hb_debug_quorumregions =
1251 o2hb_debug_create(O2HB_DEBUG_QUORUMREGIONS,
1252 o2hb_debug_dir,
1253 &o2hb_db_quorumregions,
1254 sizeof(*o2hb_db_quorumregions),
1255 O2HB_DB_TYPE_QUORUMREGIONS,
1256 sizeof(o2hb_quorum_region_bitmap),
1257 O2NM_MAX_REGIONS,
1258 o2hb_quorum_region_bitmap);
1259 if (!o2hb_debug_quorumregions) {
1260 mlog_errno(ret);
1261 goto bail;
1262 }
1263
1264 o2hb_debug_failedregions =
1265 o2hb_debug_create(O2HB_DEBUG_FAILEDREGIONS,
1266 o2hb_debug_dir,
1267 &o2hb_db_failedregions,
1268 sizeof(*o2hb_db_failedregions),
1269 O2HB_DB_TYPE_FAILEDREGIONS,
1270 sizeof(o2hb_failed_region_bitmap),
1271 O2NM_MAX_REGIONS,
1272 o2hb_failed_region_bitmap);
1273 if (!o2hb_debug_failedregions) {
1274 mlog_errno(ret);
1275 goto bail;
1276 }
1277
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -07001278 ret = 0;
1279bail:
1280 if (ret)
1281 o2hb_exit();
1282
1283 return ret;
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001284}
1285
1286int o2hb_init(void)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001287{
1288 int i;
1289
1290 for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
1291 INIT_LIST_HEAD(&o2hb_callbacks[i].list);
1292
1293 for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
1294 INIT_LIST_HEAD(&o2hb_live_slots[i]);
1295
1296 INIT_LIST_HEAD(&o2hb_node_events);
1297
1298 memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
Sunil Mushran536f0742010-10-07 17:03:07 -07001299 memset(o2hb_region_bitmap, 0, sizeof(o2hb_region_bitmap));
Sunil Mushrane7d656b2010-10-06 17:55:18 -07001300 memset(o2hb_live_region_bitmap, 0, sizeof(o2hb_live_region_bitmap));
Sunil Mushran43182d22010-10-06 17:55:16 -07001301 memset(o2hb_quorum_region_bitmap, 0, sizeof(o2hb_quorum_region_bitmap));
Sunil Mushranb1c5ebf2010-10-07 17:05:52 -07001302 memset(o2hb_failed_region_bitmap, 0, sizeof(o2hb_failed_region_bitmap));
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001303
Sunil Mushran8ca8b0bb2010-10-07 17:01:27 -07001304 return o2hb_debug_init();
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001305}
1306
1307/* if we're already in a callback then we're already serialized by the sem */
1308static void o2hb_fill_node_map_from_callback(unsigned long *map,
1309 unsigned bytes)
1310{
1311 BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
1312
1313 memcpy(map, &o2hb_live_node_bitmap, bytes);
1314}
1315
1316/*
1317 * get a map of all nodes that are heartbeating in any regions
1318 */
1319void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
1320{
1321 /* callers want to serialize this map and callbacks so that they
1322 * can trust that they don't miss nodes coming to the party */
1323 down_read(&o2hb_callback_sem);
1324 spin_lock(&o2hb_live_lock);
1325 o2hb_fill_node_map_from_callback(map, bytes);
1326 spin_unlock(&o2hb_live_lock);
1327 up_read(&o2hb_callback_sem);
1328}
1329EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
1330
1331/*
1332 * heartbeat configfs bits. The heartbeat set is a default set under
1333 * the cluster set in nodemanager.c.
1334 */
1335
1336static struct o2hb_region *to_o2hb_region(struct config_item *item)
1337{
1338 return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
1339}
1340
1341/* drop_item only drops its ref after killing the thread, nothing should
1342 * be using the region anymore. this has to clean up any state that
1343 * attributes might have built up. */
1344static void o2hb_region_release(struct config_item *item)
1345{
1346 int i;
1347 struct page *page;
1348 struct o2hb_region *reg = to_o2hb_region(item);
1349
1350 if (reg->hr_tmp_block)
1351 kfree(reg->hr_tmp_block);
1352
1353 if (reg->hr_slot_data) {
1354 for (i = 0; i < reg->hr_num_pages; i++) {
1355 page = reg->hr_slot_data[i];
1356 if (page)
1357 __free_page(page);
1358 }
1359 kfree(reg->hr_slot_data);
1360 }
1361
1362 if (reg->hr_bdev)
Al Viro9a1c3542008-02-22 20:40:24 -05001363 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001364
1365 if (reg->hr_slots)
1366 kfree(reg->hr_slots);
1367
Sunil Mushran1f285302010-10-06 17:55:12 -07001368 kfree(reg->hr_db_regnum);
1369 kfree(reg->hr_db_livenodes);
1370 debugfs_remove(reg->hr_debug_livenodes);
1371 debugfs_remove(reg->hr_debug_regnum);
1372 debugfs_remove(reg->hr_debug_dir);
1373
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001374 spin_lock(&o2hb_live_lock);
1375 list_del(&reg->hr_all_item);
1376 spin_unlock(&o2hb_live_lock);
1377
1378 kfree(reg);
1379}
1380
1381static int o2hb_read_block_input(struct o2hb_region *reg,
1382 const char *page,
1383 size_t count,
1384 unsigned long *ret_bytes,
1385 unsigned int *ret_bits)
1386{
1387 unsigned long bytes;
1388 char *p = (char *)page;
1389
1390 bytes = simple_strtoul(p, &p, 0);
1391 if (!p || (*p && (*p != '\n')))
1392 return -EINVAL;
1393
1394 /* Heartbeat and fs min / max block sizes are the same. */
1395 if (bytes > 4096 || bytes < 512)
1396 return -ERANGE;
1397 if (hweight16(bytes) != 1)
1398 return -EINVAL;
1399
1400 if (ret_bytes)
1401 *ret_bytes = bytes;
1402 if (ret_bits)
1403 *ret_bits = ffs(bytes) - 1;
1404
1405 return 0;
1406}
1407
1408static ssize_t o2hb_region_block_bytes_read(struct o2hb_region *reg,
1409 char *page)
1410{
1411 return sprintf(page, "%u\n", reg->hr_block_bytes);
1412}
1413
1414static ssize_t o2hb_region_block_bytes_write(struct o2hb_region *reg,
1415 const char *page,
1416 size_t count)
1417{
1418 int status;
1419 unsigned long block_bytes;
1420 unsigned int block_bits;
1421
1422 if (reg->hr_bdev)
1423 return -EINVAL;
1424
1425 status = o2hb_read_block_input(reg, page, count,
1426 &block_bytes, &block_bits);
1427 if (status)
1428 return status;
1429
1430 reg->hr_block_bytes = (unsigned int)block_bytes;
1431 reg->hr_block_bits = block_bits;
1432
1433 return count;
1434}
1435
1436static ssize_t o2hb_region_start_block_read(struct o2hb_region *reg,
1437 char *page)
1438{
1439 return sprintf(page, "%llu\n", reg->hr_start_block);
1440}
1441
1442static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
1443 const char *page,
1444 size_t count)
1445{
1446 unsigned long long tmp;
1447 char *p = (char *)page;
1448
1449 if (reg->hr_bdev)
1450 return -EINVAL;
1451
1452 tmp = simple_strtoull(p, &p, 0);
1453 if (!p || (*p && (*p != '\n')))
1454 return -EINVAL;
1455
1456 reg->hr_start_block = tmp;
1457
1458 return count;
1459}
1460
1461static ssize_t o2hb_region_blocks_read(struct o2hb_region *reg,
1462 char *page)
1463{
1464 return sprintf(page, "%d\n", reg->hr_blocks);
1465}
1466
1467static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
1468 const char *page,
1469 size_t count)
1470{
1471 unsigned long tmp;
1472 char *p = (char *)page;
1473
1474 if (reg->hr_bdev)
1475 return -EINVAL;
1476
1477 tmp = simple_strtoul(p, &p, 0);
1478 if (!p || (*p && (*p != '\n')))
1479 return -EINVAL;
1480
1481 if (tmp > O2NM_MAX_NODES || tmp == 0)
1482 return -ERANGE;
1483
1484 reg->hr_blocks = (unsigned int)tmp;
1485
1486 return count;
1487}
1488
1489static ssize_t o2hb_region_dev_read(struct o2hb_region *reg,
1490 char *page)
1491{
1492 unsigned int ret = 0;
1493
1494 if (reg->hr_bdev)
1495 ret = sprintf(page, "%s\n", reg->hr_dev_name);
1496
1497 return ret;
1498}
1499
1500static void o2hb_init_region_params(struct o2hb_region *reg)
1501{
1502 reg->hr_slots_per_page = PAGE_CACHE_SIZE >> reg->hr_block_bits;
1503 reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
1504
1505 mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
1506 reg->hr_start_block, reg->hr_blocks);
1507 mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
1508 reg->hr_block_bytes, reg->hr_block_bits);
1509 mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
1510 mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
1511}
1512
1513static int o2hb_map_slot_data(struct o2hb_region *reg)
1514{
1515 int i, j;
1516 unsigned int last_slot;
1517 unsigned int spp = reg->hr_slots_per_page;
1518 struct page *page;
1519 char *raw;
1520 struct o2hb_disk_slot *slot;
1521
1522 reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
1523 if (reg->hr_tmp_block == NULL) {
1524 mlog_errno(-ENOMEM);
1525 return -ENOMEM;
1526 }
1527
1528 reg->hr_slots = kcalloc(reg->hr_blocks,
1529 sizeof(struct o2hb_disk_slot), GFP_KERNEL);
1530 if (reg->hr_slots == NULL) {
1531 mlog_errno(-ENOMEM);
1532 return -ENOMEM;
1533 }
1534
1535 for(i = 0; i < reg->hr_blocks; i++) {
1536 slot = &reg->hr_slots[i];
1537 slot->ds_node_num = i;
1538 INIT_LIST_HEAD(&slot->ds_live_item);
1539 slot->ds_raw_block = NULL;
1540 }
1541
1542 reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
1543 mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
1544 "at %u blocks per page\n",
1545 reg->hr_num_pages, reg->hr_blocks, spp);
1546
1547 reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
1548 GFP_KERNEL);
1549 if (!reg->hr_slot_data) {
1550 mlog_errno(-ENOMEM);
1551 return -ENOMEM;
1552 }
1553
1554 for(i = 0; i < reg->hr_num_pages; i++) {
1555 page = alloc_page(GFP_KERNEL);
1556 if (!page) {
1557 mlog_errno(-ENOMEM);
1558 return -ENOMEM;
1559 }
1560
1561 reg->hr_slot_data[i] = page;
1562
1563 last_slot = i * spp;
1564 raw = page_address(page);
1565 for (j = 0;
1566 (j < spp) && ((j + last_slot) < reg->hr_blocks);
1567 j++) {
1568 BUG_ON((j + last_slot) >= reg->hr_blocks);
1569
1570 slot = &reg->hr_slots[j + last_slot];
1571 slot->ds_raw_block =
1572 (struct o2hb_disk_heartbeat_block *) raw;
1573
1574 raw += reg->hr_block_bytes;
1575 }
1576 }
1577
1578 return 0;
1579}
1580
1581/* Read in all the slots available and populate the tracking
1582 * structures so that we can start with a baseline idea of what's
1583 * there. */
1584static int o2hb_populate_slot_data(struct o2hb_region *reg)
1585{
1586 int ret, i;
1587 struct o2hb_disk_slot *slot;
1588 struct o2hb_disk_heartbeat_block *hb_block;
1589
1590 mlog_entry_void();
1591
1592 ret = o2hb_read_slots(reg, reg->hr_blocks);
1593 if (ret) {
1594 mlog_errno(ret);
1595 goto out;
1596 }
1597
1598 /* We only want to get an idea of the values initially in each
1599 * slot, so we do no verification - o2hb_check_slot will
1600 * actually determine if each configured slot is valid and
1601 * whether any values have changed. */
1602 for(i = 0; i < reg->hr_blocks; i++) {
1603 slot = &reg->hr_slots[i];
1604 hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
1605
1606 /* Only fill the values that o2hb_check_slot uses to
1607 * determine changing slots */
1608 slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
1609 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
1610 }
1611
1612out:
1613 mlog_exit(ret);
1614 return ret;
1615}
1616
1617/* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1618static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
1619 const char *page,
1620 size_t count)
1621{
Joel Beckere6c352d2007-02-03 03:04:20 -08001622 struct task_struct *hb_task;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001623 long fd;
1624 int sectsize;
1625 char *p = (char *)page;
1626 struct file *filp = NULL;
1627 struct inode *inode = NULL;
1628 ssize_t ret = -EINVAL;
1629
1630 if (reg->hr_bdev)
1631 goto out;
1632
1633 /* We can't heartbeat without having had our node number
1634 * configured yet. */
1635 if (o2nm_this_node() == O2NM_MAX_NODES)
1636 goto out;
1637
1638 fd = simple_strtol(p, &p, 0);
1639 if (!p || (*p && (*p != '\n')))
1640 goto out;
1641
1642 if (fd < 0 || fd >= INT_MAX)
1643 goto out;
1644
1645 filp = fget(fd);
1646 if (filp == NULL)
1647 goto out;
1648
1649 if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
1650 reg->hr_block_bytes == 0)
1651 goto out;
1652
1653 inode = igrab(filp->f_mapping->host);
1654 if (inode == NULL)
1655 goto out;
1656
1657 if (!S_ISBLK(inode->i_mode))
1658 goto out;
1659
1660 reg->hr_bdev = I_BDEV(filp->f_mapping->host);
Al Viro572c4892007-10-08 13:24:05 -04001661 ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001662 if (ret) {
1663 reg->hr_bdev = NULL;
1664 goto out;
1665 }
1666 inode = NULL;
1667
1668 bdevname(reg->hr_bdev, reg->hr_dev_name);
1669
Martin K. Petersene1defc42009-05-22 17:17:49 -04001670 sectsize = bdev_logical_block_size(reg->hr_bdev);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001671 if (sectsize != reg->hr_block_bytes) {
1672 mlog(ML_ERROR,
1673 "blocksize %u incorrect for device, expected %d",
1674 reg->hr_block_bytes, sectsize);
1675 ret = -EINVAL;
1676 goto out;
1677 }
1678
1679 o2hb_init_region_params(reg);
1680
1681 /* Generation of zero is invalid */
1682 do {
1683 get_random_bytes(&reg->hr_generation,
1684 sizeof(reg->hr_generation));
1685 } while (reg->hr_generation == 0);
1686
1687 ret = o2hb_map_slot_data(reg);
1688 if (ret) {
1689 mlog_errno(ret);
1690 goto out;
1691 }
1692
1693 ret = o2hb_populate_slot_data(reg);
1694 if (ret) {
1695 mlog_errno(ret);
1696 goto out;
1697 }
1698
David Howellsc4028952006-11-22 14:57:56 +00001699 INIT_DELAYED_WORK(&reg->hr_write_timeout_work, o2hb_write_timeout);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001700
1701 /*
1702 * A node is considered live after it has beat LIVE_THRESHOLD
1703 * times. We're not steady until we've given them a chance
1704 * _after_ our first read.
1705 */
1706 atomic_set(&reg->hr_steady_iterations, O2HB_LIVE_THRESHOLD + 1);
1707
Joel Beckere6c352d2007-02-03 03:04:20 -08001708 hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
1709 reg->hr_item.ci_name);
1710 if (IS_ERR(hb_task)) {
1711 ret = PTR_ERR(hb_task);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001712 mlog_errno(ret);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001713 goto out;
1714 }
1715
Joel Beckere6c352d2007-02-03 03:04:20 -08001716 spin_lock(&o2hb_live_lock);
1717 reg->hr_task = hb_task;
1718 spin_unlock(&o2hb_live_lock);
1719
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001720 ret = wait_event_interruptible(o2hb_steady_queue,
1721 atomic_read(&reg->hr_steady_iterations) == 0);
1722 if (ret) {
Joel Beckere6df3a62007-02-06 15:45:39 -08001723 /* We got interrupted (hello ptrace!). Clean up */
Joel Beckere6c352d2007-02-03 03:04:20 -08001724 spin_lock(&o2hb_live_lock);
1725 hb_task = reg->hr_task;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001726 reg->hr_task = NULL;
Joel Beckere6c352d2007-02-03 03:04:20 -08001727 spin_unlock(&o2hb_live_lock);
1728
1729 if (hb_task)
1730 kthread_stop(hb_task);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001731 goto out;
1732 }
1733
Joel Beckere6df3a62007-02-06 15:45:39 -08001734 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1735 spin_lock(&o2hb_live_lock);
1736 hb_task = reg->hr_task;
Sunil Mushrane7d656b2010-10-06 17:55:18 -07001737 if (o2hb_global_heartbeat_active())
1738 set_bit(reg->hr_region_num, o2hb_live_region_bitmap);
Joel Beckere6df3a62007-02-06 15:45:39 -08001739 spin_unlock(&o2hb_live_lock);
1740
1741 if (hb_task)
1742 ret = count;
1743 else
1744 ret = -EIO;
1745
Sunil Mushran18c50cb2010-10-06 18:26:59 -07001746 if (hb_task && o2hb_global_heartbeat_active())
1747 printk(KERN_NOTICE "o2hb: Heartbeat started on region %s\n",
1748 config_item_name(&reg->hr_item));
1749
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001750out:
1751 if (filp)
1752 fput(filp);
1753 if (inode)
1754 iput(inode);
1755 if (ret < 0) {
1756 if (reg->hr_bdev) {
Al Viro9a1c3542008-02-22 20:40:24 -05001757 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001758 reg->hr_bdev = NULL;
1759 }
1760 }
1761 return ret;
1762}
1763
Zhen Wei92efc152006-12-08 00:48:17 -07001764static ssize_t o2hb_region_pid_read(struct o2hb_region *reg,
1765 char *page)
1766{
Joel Beckere6c352d2007-02-03 03:04:20 -08001767 pid_t pid = 0;
1768
1769 spin_lock(&o2hb_live_lock);
1770 if (reg->hr_task)
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001771 pid = task_pid_nr(reg->hr_task);
Joel Beckere6c352d2007-02-03 03:04:20 -08001772 spin_unlock(&o2hb_live_lock);
1773
1774 if (!pid)
Zhen Wei92efc152006-12-08 00:48:17 -07001775 return 0;
1776
Joel Beckere6c352d2007-02-03 03:04:20 -08001777 return sprintf(page, "%u\n", pid);
Zhen Wei92efc152006-12-08 00:48:17 -07001778}
1779
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001780struct o2hb_region_attribute {
1781 struct configfs_attribute attr;
1782 ssize_t (*show)(struct o2hb_region *, char *);
1783 ssize_t (*store)(struct o2hb_region *, const char *, size_t);
1784};
1785
1786static struct o2hb_region_attribute o2hb_region_attr_block_bytes = {
1787 .attr = { .ca_owner = THIS_MODULE,
1788 .ca_name = "block_bytes",
1789 .ca_mode = S_IRUGO | S_IWUSR },
1790 .show = o2hb_region_block_bytes_read,
1791 .store = o2hb_region_block_bytes_write,
1792};
1793
1794static struct o2hb_region_attribute o2hb_region_attr_start_block = {
1795 .attr = { .ca_owner = THIS_MODULE,
1796 .ca_name = "start_block",
1797 .ca_mode = S_IRUGO | S_IWUSR },
1798 .show = o2hb_region_start_block_read,
1799 .store = o2hb_region_start_block_write,
1800};
1801
1802static struct o2hb_region_attribute o2hb_region_attr_blocks = {
1803 .attr = { .ca_owner = THIS_MODULE,
1804 .ca_name = "blocks",
1805 .ca_mode = S_IRUGO | S_IWUSR },
1806 .show = o2hb_region_blocks_read,
1807 .store = o2hb_region_blocks_write,
1808};
1809
1810static struct o2hb_region_attribute o2hb_region_attr_dev = {
1811 .attr = { .ca_owner = THIS_MODULE,
1812 .ca_name = "dev",
1813 .ca_mode = S_IRUGO | S_IWUSR },
1814 .show = o2hb_region_dev_read,
1815 .store = o2hb_region_dev_write,
1816};
1817
Zhen Wei92efc152006-12-08 00:48:17 -07001818static struct o2hb_region_attribute o2hb_region_attr_pid = {
1819 .attr = { .ca_owner = THIS_MODULE,
1820 .ca_name = "pid",
1821 .ca_mode = S_IRUGO | S_IRUSR },
1822 .show = o2hb_region_pid_read,
1823};
1824
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001825static struct configfs_attribute *o2hb_region_attrs[] = {
1826 &o2hb_region_attr_block_bytes.attr,
1827 &o2hb_region_attr_start_block.attr,
1828 &o2hb_region_attr_blocks.attr,
1829 &o2hb_region_attr_dev.attr,
Zhen Wei92efc152006-12-08 00:48:17 -07001830 &o2hb_region_attr_pid.attr,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001831 NULL,
1832};
1833
1834static ssize_t o2hb_region_show(struct config_item *item,
1835 struct configfs_attribute *attr,
1836 char *page)
1837{
1838 struct o2hb_region *reg = to_o2hb_region(item);
1839 struct o2hb_region_attribute *o2hb_region_attr =
1840 container_of(attr, struct o2hb_region_attribute, attr);
1841 ssize_t ret = 0;
1842
1843 if (o2hb_region_attr->show)
1844 ret = o2hb_region_attr->show(reg, page);
1845 return ret;
1846}
1847
1848static ssize_t o2hb_region_store(struct config_item *item,
1849 struct configfs_attribute *attr,
1850 const char *page, size_t count)
1851{
1852 struct o2hb_region *reg = to_o2hb_region(item);
1853 struct o2hb_region_attribute *o2hb_region_attr =
1854 container_of(attr, struct o2hb_region_attribute, attr);
1855 ssize_t ret = -EINVAL;
1856
1857 if (o2hb_region_attr->store)
1858 ret = o2hb_region_attr->store(reg, page, count);
1859 return ret;
1860}
1861
1862static struct configfs_item_operations o2hb_region_item_ops = {
1863 .release = o2hb_region_release,
1864 .show_attribute = o2hb_region_show,
1865 .store_attribute = o2hb_region_store,
1866};
1867
1868static struct config_item_type o2hb_region_type = {
1869 .ct_item_ops = &o2hb_region_item_ops,
1870 .ct_attrs = o2hb_region_attrs,
1871 .ct_owner = THIS_MODULE,
1872};
1873
1874/* heartbeat set */
1875
1876struct o2hb_heartbeat_group {
1877 struct config_group hs_group;
1878 /* some stuff? */
1879};
1880
1881static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
1882{
1883 return group ?
1884 container_of(group, struct o2hb_heartbeat_group, hs_group)
1885 : NULL;
1886}
1887
Sunil Mushran1f285302010-10-06 17:55:12 -07001888static int o2hb_debug_region_init(struct o2hb_region *reg, struct dentry *dir)
1889{
1890 int ret = -ENOMEM;
1891
1892 reg->hr_debug_dir =
1893 debugfs_create_dir(config_item_name(&reg->hr_item), dir);
1894 if (!reg->hr_debug_dir) {
1895 mlog_errno(ret);
1896 goto bail;
1897 }
1898
1899 reg->hr_debug_livenodes =
1900 o2hb_debug_create(O2HB_DEBUG_LIVENODES,
1901 reg->hr_debug_dir,
1902 &(reg->hr_db_livenodes),
1903 sizeof(*(reg->hr_db_livenodes)),
1904 O2HB_DB_TYPE_REGION_LIVENODES,
1905 sizeof(reg->hr_live_node_bitmap),
1906 O2NM_MAX_NODES, reg);
1907 if (!reg->hr_debug_livenodes) {
1908 mlog_errno(ret);
1909 goto bail;
1910 }
1911
1912 reg->hr_debug_regnum =
1913 o2hb_debug_create(O2HB_DEBUG_REGION_NUMBER,
1914 reg->hr_debug_dir,
1915 &(reg->hr_db_regnum),
1916 sizeof(*(reg->hr_db_regnum)),
1917 O2HB_DB_TYPE_REGION_NUMBER,
1918 0, O2NM_MAX_NODES, reg);
1919 if (!reg->hr_debug_regnum) {
1920 mlog_errno(ret);
1921 goto bail;
1922 }
1923
1924 ret = 0;
1925bail:
1926 return ret;
1927}
1928
Joel Beckerf89ab862008-07-17 14:53:48 -07001929static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
1930 const char *name)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001931{
1932 struct o2hb_region *reg = NULL;
Sunil Mushran1f285302010-10-06 17:55:12 -07001933 int ret;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001934
Robert P. J. Daycd861282006-12-13 00:34:52 -08001935 reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
Joel Beckerf89ab862008-07-17 14:53:48 -07001936 if (reg == NULL)
Joel Beckera6795e92008-07-17 15:21:29 -07001937 return ERR_PTR(-ENOMEM);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001938
Sunil Mushranb3c85c42010-10-07 14:31:06 -07001939 if (strlen(name) > O2HB_MAX_REGION_NAME_LEN)
1940 return ERR_PTR(-ENAMETOOLONG);
1941
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001942 spin_lock(&o2hb_live_lock);
Sunil Mushran536f0742010-10-07 17:03:07 -07001943 reg->hr_region_num = 0;
1944 if (o2hb_global_heartbeat_active()) {
1945 reg->hr_region_num = find_first_zero_bit(o2hb_region_bitmap,
1946 O2NM_MAX_REGIONS);
1947 if (reg->hr_region_num >= O2NM_MAX_REGIONS) {
1948 spin_unlock(&o2hb_live_lock);
1949 return ERR_PTR(-EFBIG);
1950 }
1951 set_bit(reg->hr_region_num, o2hb_region_bitmap);
1952 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001953 list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
1954 spin_unlock(&o2hb_live_lock);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001955
Sunil Mushran536f0742010-10-07 17:03:07 -07001956 config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
1957
Sunil Mushran1f285302010-10-06 17:55:12 -07001958 ret = o2hb_debug_region_init(reg, o2hb_debug_dir);
1959 if (ret) {
1960 config_item_put(&reg->hr_item);
1961 return ERR_PTR(ret);
1962 }
1963
Joel Beckera6795e92008-07-17 15:21:29 -07001964 return &reg->hr_item;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001965}
1966
1967static void o2hb_heartbeat_group_drop_item(struct config_group *group,
1968 struct config_item *item)
1969{
Joel Beckere6c352d2007-02-03 03:04:20 -08001970 struct task_struct *hb_task;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001971 struct o2hb_region *reg = to_o2hb_region(item);
1972
1973 /* stop the thread when the user removes the region dir */
Joel Beckere6c352d2007-02-03 03:04:20 -08001974 spin_lock(&o2hb_live_lock);
Sunil Mushrane7d656b2010-10-06 17:55:18 -07001975 if (o2hb_global_heartbeat_active()) {
Sunil Mushran536f0742010-10-07 17:03:07 -07001976 clear_bit(reg->hr_region_num, o2hb_region_bitmap);
Sunil Mushrane7d656b2010-10-06 17:55:18 -07001977 clear_bit(reg->hr_region_num, o2hb_live_region_bitmap);
1978 }
Joel Beckere6c352d2007-02-03 03:04:20 -08001979 hb_task = reg->hr_task;
1980 reg->hr_task = NULL;
1981 spin_unlock(&o2hb_live_lock);
1982
1983 if (hb_task)
1984 kthread_stop(hb_task);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001985
Joel Beckere6df3a62007-02-06 15:45:39 -08001986 /*
1987 * If we're racing a dev_write(), we need to wake them. They will
1988 * check reg->hr_task
1989 */
1990 if (atomic_read(&reg->hr_steady_iterations) != 0) {
1991 atomic_set(&reg->hr_steady_iterations, 0);
1992 wake_up(&o2hb_steady_queue);
1993 }
1994
Sunil Mushran18c50cb2010-10-06 18:26:59 -07001995 if (o2hb_global_heartbeat_active())
1996 printk(KERN_NOTICE "o2hb: Heartbeat stopped on region %s\n",
1997 config_item_name(&reg->hr_item));
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001998 config_item_put(item);
1999}
2000
2001struct o2hb_heartbeat_group_attribute {
2002 struct configfs_attribute attr;
2003 ssize_t (*show)(struct o2hb_heartbeat_group *, char *);
2004 ssize_t (*store)(struct o2hb_heartbeat_group *, const char *, size_t);
2005};
2006
2007static ssize_t o2hb_heartbeat_group_show(struct config_item *item,
2008 struct configfs_attribute *attr,
2009 char *page)
2010{
2011 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
2012 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
2013 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
2014 ssize_t ret = 0;
2015
2016 if (o2hb_heartbeat_group_attr->show)
2017 ret = o2hb_heartbeat_group_attr->show(reg, page);
2018 return ret;
2019}
2020
2021static ssize_t o2hb_heartbeat_group_store(struct config_item *item,
2022 struct configfs_attribute *attr,
2023 const char *page, size_t count)
2024{
2025 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
2026 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
2027 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
2028 ssize_t ret = -EINVAL;
2029
2030 if (o2hb_heartbeat_group_attr->store)
2031 ret = o2hb_heartbeat_group_attr->store(reg, page, count);
2032 return ret;
2033}
2034
2035static ssize_t o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group *group,
2036 char *page)
2037{
2038 return sprintf(page, "%u\n", o2hb_dead_threshold);
2039}
2040
2041static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group *group,
2042 const char *page,
2043 size_t count)
2044{
2045 unsigned long tmp;
2046 char *p = (char *)page;
2047
2048 tmp = simple_strtoul(p, &p, 10);
2049 if (!p || (*p && (*p != '\n')))
2050 return -EINVAL;
2051
2052 /* this will validate ranges for us. */
2053 o2hb_dead_threshold_set((unsigned int) tmp);
2054
2055 return count;
2056}
2057
Sunil Mushran54b51872010-10-07 15:26:08 -07002058static
2059ssize_t o2hb_heartbeat_group_mode_show(struct o2hb_heartbeat_group *group,
2060 char *page)
2061{
2062 return sprintf(page, "%s\n",
2063 o2hb_heartbeat_mode_desc[o2hb_heartbeat_mode]);
2064}
2065
2066static
2067ssize_t o2hb_heartbeat_group_mode_store(struct o2hb_heartbeat_group *group,
2068 const char *page, size_t count)
2069{
2070 unsigned int i;
2071 int ret;
2072 size_t len;
2073
2074 len = (page[count - 1] == '\n') ? count - 1 : count;
2075 if (!len)
2076 return -EINVAL;
2077
2078 for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) {
2079 if (strnicmp(page, o2hb_heartbeat_mode_desc[i], len))
2080 continue;
2081
2082 ret = o2hb_global_hearbeat_mode_set(i);
2083 if (!ret)
Sunil Mushran18c50cb2010-10-06 18:26:59 -07002084 printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
Sunil Mushran54b51872010-10-07 15:26:08 -07002085 o2hb_heartbeat_mode_desc[i]);
2086 return count;
2087 }
2088
2089 return -EINVAL;
2090
2091}
2092
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002093static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold = {
2094 .attr = { .ca_owner = THIS_MODULE,
2095 .ca_name = "dead_threshold",
2096 .ca_mode = S_IRUGO | S_IWUSR },
2097 .show = o2hb_heartbeat_group_threshold_show,
2098 .store = o2hb_heartbeat_group_threshold_store,
2099};
2100
Sunil Mushran54b51872010-10-07 15:26:08 -07002101static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_mode = {
2102 .attr = { .ca_owner = THIS_MODULE,
2103 .ca_name = "mode",
2104 .ca_mode = S_IRUGO | S_IWUSR },
2105 .show = o2hb_heartbeat_group_mode_show,
2106 .store = o2hb_heartbeat_group_mode_store,
2107};
2108
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002109static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
2110 &o2hb_heartbeat_group_attr_threshold.attr,
Sunil Mushran54b51872010-10-07 15:26:08 -07002111 &o2hb_heartbeat_group_attr_mode.attr,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002112 NULL,
2113};
2114
2115static struct configfs_item_operations o2hb_hearbeat_group_item_ops = {
2116 .show_attribute = o2hb_heartbeat_group_show,
2117 .store_attribute = o2hb_heartbeat_group_store,
2118};
2119
2120static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
2121 .make_item = o2hb_heartbeat_group_make_item,
2122 .drop_item = o2hb_heartbeat_group_drop_item,
2123};
2124
2125static struct config_item_type o2hb_heartbeat_group_type = {
2126 .ct_group_ops = &o2hb_heartbeat_group_group_ops,
2127 .ct_item_ops = &o2hb_hearbeat_group_item_ops,
2128 .ct_attrs = o2hb_heartbeat_group_attrs,
2129 .ct_owner = THIS_MODULE,
2130};
2131
2132/* this is just here to avoid touching group in heartbeat.h which the
2133 * entire damn world #includes */
2134struct config_group *o2hb_alloc_hb_set(void)
2135{
2136 struct o2hb_heartbeat_group *hs = NULL;
2137 struct config_group *ret = NULL;
2138
Robert P. J. Daycd861282006-12-13 00:34:52 -08002139 hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002140 if (hs == NULL)
2141 goto out;
2142
2143 config_group_init_type_name(&hs->hs_group, "heartbeat",
2144 &o2hb_heartbeat_group_type);
2145
2146 ret = &hs->hs_group;
2147out:
2148 if (ret == NULL)
2149 kfree(hs);
2150 return ret;
2151}
2152
2153void o2hb_free_hb_set(struct config_group *group)
2154{
2155 struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
2156 kfree(hs);
2157}
2158
2159/* hb callback registration and issueing */
2160
2161static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
2162{
2163 if (type == O2HB_NUM_CB)
2164 return ERR_PTR(-EINVAL);
2165
2166 return &o2hb_callbacks[type];
2167}
2168
2169void o2hb_setup_callback(struct o2hb_callback_func *hc,
2170 enum o2hb_callback_type type,
2171 o2hb_cb_func *func,
2172 void *data,
2173 int priority)
2174{
2175 INIT_LIST_HEAD(&hc->hc_item);
2176 hc->hc_func = func;
2177 hc->hc_data = data;
2178 hc->hc_priority = priority;
2179 hc->hc_type = type;
2180 hc->hc_magic = O2HB_CB_MAGIC;
2181}
2182EXPORT_SYMBOL_GPL(o2hb_setup_callback);
2183
Joel Becker14829422007-06-14 21:40:49 -07002184static struct o2hb_region *o2hb_find_region(const char *region_uuid)
2185{
2186 struct o2hb_region *p, *reg = NULL;
2187
2188 assert_spin_locked(&o2hb_live_lock);
2189
2190 list_for_each_entry(p, &o2hb_all_regions, hr_all_item) {
2191 if (!strcmp(region_uuid, config_item_name(&p->hr_item))) {
2192 reg = p;
2193 break;
2194 }
2195 }
2196
2197 return reg;
2198}
2199
2200static int o2hb_region_get(const char *region_uuid)
2201{
2202 int ret = 0;
2203 struct o2hb_region *reg;
2204
2205 spin_lock(&o2hb_live_lock);
2206
2207 reg = o2hb_find_region(region_uuid);
2208 if (!reg)
2209 ret = -ENOENT;
2210 spin_unlock(&o2hb_live_lock);
2211
Joel Becker16c6a4f2007-06-19 11:34:03 -07002212 if (ret)
2213 goto out;
Joel Becker14829422007-06-14 21:40:49 -07002214
Joel Becker16c6a4f2007-06-19 11:34:03 -07002215 ret = o2nm_depend_this_node();
2216 if (ret)
2217 goto out;
2218
2219 ret = o2nm_depend_item(&reg->hr_item);
2220 if (ret)
2221 o2nm_undepend_this_node();
2222
2223out:
Joel Becker14829422007-06-14 21:40:49 -07002224 return ret;
2225}
2226
2227static void o2hb_region_put(const char *region_uuid)
2228{
2229 struct o2hb_region *reg;
2230
2231 spin_lock(&o2hb_live_lock);
2232
2233 reg = o2hb_find_region(region_uuid);
2234
2235 spin_unlock(&o2hb_live_lock);
2236
Joel Becker16c6a4f2007-06-19 11:34:03 -07002237 if (reg) {
Joel Becker14829422007-06-14 21:40:49 -07002238 o2nm_undepend_item(&reg->hr_item);
Joel Becker16c6a4f2007-06-19 11:34:03 -07002239 o2nm_undepend_this_node();
2240 }
Joel Becker14829422007-06-14 21:40:49 -07002241}
2242
2243int o2hb_register_callback(const char *region_uuid,
2244 struct o2hb_callback_func *hc)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002245{
2246 struct o2hb_callback_func *tmp;
2247 struct list_head *iter;
2248 struct o2hb_callback *hbcall;
2249 int ret;
2250
2251 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2252 BUG_ON(!list_empty(&hc->hc_item));
2253
2254 hbcall = hbcall_from_type(hc->hc_type);
2255 if (IS_ERR(hbcall)) {
2256 ret = PTR_ERR(hbcall);
2257 goto out;
2258 }
2259
Joel Becker14829422007-06-14 21:40:49 -07002260 if (region_uuid) {
2261 ret = o2hb_region_get(region_uuid);
2262 if (ret)
2263 goto out;
2264 }
2265
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002266 down_write(&o2hb_callback_sem);
2267
2268 list_for_each(iter, &hbcall->list) {
2269 tmp = list_entry(iter, struct o2hb_callback_func, hc_item);
2270 if (hc->hc_priority < tmp->hc_priority) {
2271 list_add_tail(&hc->hc_item, iter);
2272 break;
2273 }
2274 }
2275 if (list_empty(&hc->hc_item))
2276 list_add_tail(&hc->hc_item, &hbcall->list);
2277
2278 up_write(&o2hb_callback_sem);
2279 ret = 0;
2280out:
2281 mlog(ML_HEARTBEAT, "returning %d on behalf of %p for funcs %p\n",
2282 ret, __builtin_return_address(0), hc);
2283 return ret;
2284}
2285EXPORT_SYMBOL_GPL(o2hb_register_callback);
2286
Joel Becker14829422007-06-14 21:40:49 -07002287void o2hb_unregister_callback(const char *region_uuid,
2288 struct o2hb_callback_func *hc)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002289{
2290 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2291
2292 mlog(ML_HEARTBEAT, "on behalf of %p for funcs %p\n",
2293 __builtin_return_address(0), hc);
2294
Joel Becker14829422007-06-14 21:40:49 -07002295 /* XXX Can this happen _with_ a region reference? */
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002296 if (list_empty(&hc->hc_item))
Joel Beckerc24f72c2007-02-03 03:14:30 -08002297 return;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002298
Joel Becker14829422007-06-14 21:40:49 -07002299 if (region_uuid)
2300 o2hb_region_put(region_uuid);
2301
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002302 down_write(&o2hb_callback_sem);
2303
2304 list_del_init(&hc->hc_item);
2305
2306 up_write(&o2hb_callback_sem);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002307}
2308EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
2309
2310int o2hb_check_node_heartbeating(u8 node_num)
2311{
2312 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2313
2314 o2hb_fill_node_map(testing_map, sizeof(testing_map));
2315 if (!test_bit(node_num, testing_map)) {
2316 mlog(ML_HEARTBEAT,
2317 "node (%u) does not have heartbeating enabled.\n",
2318 node_num);
2319 return 0;
2320 }
2321
2322 return 1;
2323}
2324EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating);
2325
2326int o2hb_check_node_heartbeating_from_callback(u8 node_num)
2327{
2328 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2329
2330 o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
2331 if (!test_bit(node_num, testing_map)) {
2332 mlog(ML_HEARTBEAT,
2333 "node (%u) does not have heartbeating enabled.\n",
2334 node_num);
2335 return 0;
2336 }
2337
2338 return 1;
2339}
2340EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
2341
2342/* Makes sure our local node is configured with a node number, and is
2343 * heartbeating. */
2344int o2hb_check_local_node_heartbeating(void)
2345{
2346 u8 node_num;
2347
2348 /* if this node was set then we have networking */
2349 node_num = o2nm_this_node();
2350 if (node_num == O2NM_MAX_NODES) {
2351 mlog(ML_HEARTBEAT, "this node has not been configured.\n");
2352 return 0;
2353 }
2354
2355 return o2hb_check_node_heartbeating(node_num);
2356}
2357EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating);
2358
2359/*
2360 * this is just a hack until we get the plumbing which flips file systems
2361 * read only and drops the hb ref instead of killing the node dead.
2362 */
2363void o2hb_stop_all_regions(void)
2364{
2365 struct o2hb_region *reg;
2366
2367 mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
2368
2369 spin_lock(&o2hb_live_lock);
2370
2371 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
2372 reg->hr_unclean_stop = 1;
2373
2374 spin_unlock(&o2hb_live_lock);
2375}
2376EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);
Sunil Mushranb3c85c42010-10-07 14:31:06 -07002377
2378int o2hb_get_all_regions(char *region_uuids, u8 max_regions)
2379{
2380 struct o2hb_region *reg;
2381 int numregs = 0;
2382 char *p;
2383
2384 spin_lock(&o2hb_live_lock);
2385
2386 p = region_uuids;
2387 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2388 mlog(0, "Region: %s\n", config_item_name(&reg->hr_item));
2389 if (numregs < max_regions) {
2390 memcpy(p, config_item_name(&reg->hr_item),
2391 O2HB_MAX_REGION_NAME_LEN);
2392 p += O2HB_MAX_REGION_NAME_LEN;
2393 }
2394 numregs++;
2395 }
2396
2397 spin_unlock(&o2hb_live_lock);
2398
2399 return numregs;
2400}
2401EXPORT_SYMBOL_GPL(o2hb_get_all_regions);
2402
2403int o2hb_global_heartbeat_active(void)
2404{
2405 return 0;
2406}
2407EXPORT_SYMBOL(o2hb_global_heartbeat_active);