blob: e8676accf9022dbd85f75c6f77ea1e500979e872 [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 Mushran8ca8b0b2010-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 Mushran8ca8b0b2010-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 Mushran8ca8b0b2010-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 Mushran8ca8b0b2010-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])) {
Sunil Mushrand6aa1c72010-10-06 18:50:50 -0700800 mlog(ML_HEARTBEAT, "o2hb: Add node %d to live nodes "
801 "bitmap\n", slot->ds_node_num);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800802 set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
803
804 o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
805 slot->ds_node_num);
806
807 changed = 1;
808 }
809
810 list_add_tail(&slot->ds_live_item,
811 &o2hb_live_slots[slot->ds_node_num]);
812
813 slot->ds_equal_samples = 0;
Mark Fasheh0db638f2006-05-09 15:09:35 -0700814
815 /* We want to be sure that all nodes agree on the
816 * number of milliseconds before a node will be
817 * considered dead. The self-fencing timeout is
818 * computed from this value, and a discrepancy might
819 * result in heartbeat calling a node dead when it
820 * hasn't self-fenced yet. */
821 slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
822 if (slot_dead_ms && slot_dead_ms != dead_ms) {
823 /* TODO: Perhaps we can fail the region here. */
824 mlog(ML_ERROR, "Node %d on device %s has a dead count "
825 "of %u ms, but our count is %u ms.\n"
826 "Please double check your configuration values "
827 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
828 slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
829 dead_ms);
830 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800831 goto out;
832 }
833
834 /* if the list is dead, we're done.. */
835 if (list_empty(&slot->ds_live_item))
836 goto out;
837
838 /* live nodes only go dead after enough consequtive missed
839 * samples.. reset the missed counter whenever we see
840 * activity */
841 if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
842 mlog(ML_HEARTBEAT, "Node %d left my region\n",
843 slot->ds_node_num);
844
Sunil Mushran823a6372010-10-06 17:55:21 -0700845 clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
846
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800847 /* last off the live_slot generates a callback */
848 list_del_init(&slot->ds_live_item);
849 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
Sunil Mushrand6aa1c72010-10-06 18:50:50 -0700850 mlog(ML_HEARTBEAT, "o2hb: Remove node %d from live "
851 "nodes bitmap\n", slot->ds_node_num);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800852 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
853
Sunil Mushran0e105d32010-10-07 17:00:16 -0700854 /* node can be null */
855 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
856 node, slot->ds_node_num);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800857
858 changed = 1;
859 }
860
861 /* We don't clear this because the node is still
862 * actually writing new blocks. */
863 if (!gen_changed)
864 slot->ds_changed_samples = 0;
865 goto out;
866 }
867 if (slot->ds_changed_samples) {
868 slot->ds_changed_samples = 0;
869 slot->ds_equal_samples = 0;
870 }
871out:
Sunil Mushran43182d22010-10-06 17:55:16 -0700872 o2hb_set_quorum_device(reg, slot);
873
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800874 spin_unlock(&o2hb_live_lock);
875
876 o2hb_run_event_list(&event);
877
Sunil Mushran0e105d32010-10-07 17:00:16 -0700878 if (node)
879 o2nm_node_put(node);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800880 return changed;
881}
882
883/* This could be faster if we just implmented a find_last_bit, but I
884 * don't think the circumstances warrant it. */
885static int o2hb_highest_node(unsigned long *nodes,
886 int numbits)
887{
888 int highest, node;
889
890 highest = numbits;
891 node = -1;
892 while ((node = find_next_bit(nodes, numbits, node + 1)) != -1) {
893 if (node >= numbits)
894 break;
895
896 highest = node;
897 }
898
899 return highest;
900}
901
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800902static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800903{
904 int i, ret, highest_node, change = 0;
905 unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
Sunil Mushran0e105d32010-10-07 17:00:16 -0700906 unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800907 struct o2hb_bio_wait_ctxt write_wc;
908
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800909 ret = o2nm_configured_node_map(configured_nodes,
910 sizeof(configured_nodes));
911 if (ret) {
912 mlog_errno(ret);
913 return ret;
914 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800915
Sunil Mushran0e105d32010-10-07 17:00:16 -0700916 /*
917 * If a node is not configured but is in the livemap, we still need
918 * to read the slot so as to be able to remove it from the livemap.
919 */
920 o2hb_fill_node_map(live_node_bitmap, sizeof(live_node_bitmap));
921 i = -1;
922 while ((i = find_next_bit(live_node_bitmap,
923 O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
924 set_bit(i, configured_nodes);
925 }
926
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800927 highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
928 if (highest_node >= O2NM_MAX_NODES) {
929 mlog(ML_NOTICE, "ocfs2_heartbeat: no configured nodes found!\n");
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800930 return -EINVAL;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800931 }
932
933 /* No sense in reading the slots of nodes that don't exist
934 * yet. Of course, if the node definitions have holes in them
935 * then we're reading an empty slot anyway... Consider this
936 * best-effort. */
937 ret = o2hb_read_slots(reg, highest_node + 1);
938 if (ret < 0) {
939 mlog_errno(ret);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800940 return ret;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800941 }
942
943 /* With an up to date view of the slots, we can check that no
944 * other node has been improperly configured to heartbeat in
945 * our slot. */
946 if (!o2hb_check_last_timestamp(reg))
947 mlog(ML_ERROR, "Device \"%s\": another node is heartbeating "
948 "in our slot!\n", reg->hr_dev_name);
949
950 /* fill in the proper info for our next heartbeat */
951 o2hb_prepare_block(reg, reg->hr_generation);
952
953 /* And fire off the write. Note that we don't wait on this I/O
954 * until later. */
Philipp Reisnerb5592922007-01-11 10:58:10 +0100955 ret = o2hb_issue_node_write(reg, &write_wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800956 if (ret < 0) {
957 mlog_errno(ret);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800958 return ret;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800959 }
960
961 i = -1;
962 while((i = find_next_bit(configured_nodes, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
963
964 change |= o2hb_check_slot(reg, &reg->hr_slots[i]);
965 }
966
967 /*
968 * We have to be sure we've advertised ourselves on disk
969 * before we can go to steady state. This ensures that
970 * people we find in our steady state have seen us.
971 */
972 o2hb_wait_on_io(reg, &write_wc);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800973 if (write_wc.wc_error) {
974 /* Do not re-arm the write timeout on I/O error - we
975 * can't be sure that the new block ever made it to
976 * disk */
977 mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
978 write_wc.wc_error, reg->hr_dev_name);
979 return write_wc.wc_error;
980 }
981
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800982 o2hb_arm_write_timeout(reg);
983
984 /* let the person who launched us know when things are steady */
985 if (!change && (atomic_read(&reg->hr_steady_iterations) != 0)) {
986 if (atomic_dec_and_test(&reg->hr_steady_iterations))
987 wake_up(&o2hb_steady_queue);
988 }
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800989
990 return 0;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800991}
992
993/* Subtract b from a, storing the result in a. a *must* have a larger
994 * value than b. */
995static void o2hb_tv_subtract(struct timeval *a,
996 struct timeval *b)
997{
998 /* just return 0 when a is after b */
999 if (a->tv_sec < b->tv_sec ||
1000 (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) {
1001 a->tv_sec = 0;
1002 a->tv_usec = 0;
1003 return;
1004 }
1005
1006 a->tv_sec -= b->tv_sec;
1007 a->tv_usec -= b->tv_usec;
1008 while ( a->tv_usec < 0 ) {
1009 a->tv_sec--;
1010 a->tv_usec += 1000000;
1011 }
1012}
1013
1014static unsigned int o2hb_elapsed_msecs(struct timeval *start,
1015 struct timeval *end)
1016{
1017 struct timeval res = *end;
1018
1019 o2hb_tv_subtract(&res, start);
1020
1021 return res.tv_sec * 1000 + res.tv_usec / 1000;
1022}
1023
1024/*
1025 * we ride the region ref that the region dir holds. before the region
1026 * dir is removed and drops it ref it will wait to tear down this
1027 * thread.
1028 */
1029static int o2hb_thread(void *data)
1030{
1031 int i, ret;
1032 struct o2hb_region *reg = data;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001033 struct o2hb_bio_wait_ctxt write_wc;
1034 struct timeval before_hb, after_hb;
1035 unsigned int elapsed_msec;
1036
1037 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
1038
1039 set_user_nice(current, -20);
1040
1041 while (!kthread_should_stop() && !reg->hr_unclean_stop) {
1042 /* We track the time spent inside
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001043 * o2hb_do_disk_heartbeat so that we avoid more than
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001044 * hr_timeout_ms between disk writes. On busy systems
1045 * this should result in a heartbeat which is less
1046 * likely to time itself out. */
1047 do_gettimeofday(&before_hb);
1048
Mark Fasheha9e2ae32006-03-24 14:20:17 -08001049 i = 0;
1050 do {
1051 ret = o2hb_do_disk_heartbeat(reg);
1052 } while (ret && ++i < 2);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001053
1054 do_gettimeofday(&after_hb);
1055 elapsed_msec = o2hb_elapsed_msecs(&before_hb, &after_hb);
1056
Tao Mab31d3082009-12-22 10:32:15 +08001057 mlog(ML_HEARTBEAT,
1058 "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
Mark Fasheh215c7f92006-02-01 16:42:10 -08001059 before_hb.tv_sec, (unsigned long) before_hb.tv_usec,
1060 after_hb.tv_sec, (unsigned long) after_hb.tv_usec,
1061 elapsed_msec);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001062
1063 if (elapsed_msec < reg->hr_timeout_ms) {
1064 /* the kthread api has blocked signals for us so no
1065 * need to record the return value. */
1066 msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
1067 }
1068 }
1069
1070 o2hb_disarm_write_timeout(reg);
1071
1072 /* unclean stop is only used in very bad situation */
1073 for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
1074 o2hb_shutdown_slot(&reg->hr_slots[i]);
1075
1076 /* Explicit down notification - avoid forcing the other nodes
1077 * to timeout on this region when we could just as easily
1078 * write a clear generation - thus indicating to them that
1079 * this node has left this region.
1080 *
1081 * XXX: Should we skip this on unclean_stop? */
1082 o2hb_prepare_block(reg, 0);
Philipp Reisnerb5592922007-01-11 10:58:10 +01001083 ret = o2hb_issue_node_write(reg, &write_wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001084 if (ret == 0) {
1085 o2hb_wait_on_io(reg, &write_wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001086 } else {
1087 mlog_errno(ret);
1088 }
1089
1090 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread exiting\n");
1091
1092 return 0;
1093}
1094
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001095#ifdef CONFIG_DEBUG_FS
1096static int o2hb_debug_open(struct inode *inode, struct file *file)
1097{
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001098 struct o2hb_debug_buf *db = inode->i_private;
Sunil Mushran1f285302010-10-06 17:55:12 -07001099 struct o2hb_region *reg;
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001100 unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1101 char *buf = NULL;
1102 int i = -1;
1103 int out = 0;
1104
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001105 /* max_nodes should be the largest bitmap we pass here */
1106 BUG_ON(sizeof(map) < db->db_size);
1107
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1109 if (!buf)
1110 goto bail;
1111
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001112 switch (db->db_type) {
1113 case O2HB_DB_TYPE_LIVENODES:
Sunil Mushrana6de0132010-10-06 17:55:13 -07001114 case O2HB_DB_TYPE_LIVEREGIONS:
1115 case O2HB_DB_TYPE_QUORUMREGIONS:
1116 case O2HB_DB_TYPE_FAILEDREGIONS:
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001117 spin_lock(&o2hb_live_lock);
1118 memcpy(map, db->db_data, db->db_size);
1119 spin_unlock(&o2hb_live_lock);
1120 break;
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001121
Sunil Mushran1f285302010-10-06 17:55:12 -07001122 case O2HB_DB_TYPE_REGION_LIVENODES:
1123 spin_lock(&o2hb_live_lock);
1124 reg = (struct o2hb_region *)db->db_data;
1125 memcpy(map, reg->hr_live_node_bitmap, db->db_size);
1126 spin_unlock(&o2hb_live_lock);
1127 break;
1128
1129 case O2HB_DB_TYPE_REGION_NUMBER:
1130 reg = (struct o2hb_region *)db->db_data;
1131 out += snprintf(buf + out, PAGE_SIZE - out, "%d\n",
1132 reg->hr_region_num);
1133 goto done;
1134
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001135 default:
1136 goto done;
1137 }
1138
1139 while ((i = find_next_bit(map, db->db_len, i + 1)) < db->db_len)
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001140 out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
1141 out += snprintf(buf + out, PAGE_SIZE - out, "\n");
1142
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001143done:
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001144 i_size_write(inode, out);
1145
1146 file->private_data = buf;
1147
1148 return 0;
1149bail:
1150 return -ENOMEM;
1151}
1152
1153static int o2hb_debug_release(struct inode *inode, struct file *file)
1154{
1155 kfree(file->private_data);
1156 return 0;
1157}
1158
1159static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1160 size_t nbytes, loff_t *ppos)
1161{
1162 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
1163 i_size_read(file->f_mapping->host));
1164}
1165#else
1166static int o2hb_debug_open(struct inode *inode, struct file *file)
1167{
1168 return 0;
1169}
1170static int o2hb_debug_release(struct inode *inode, struct file *file)
1171{
1172 return 0;
1173}
1174static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1175 size_t nbytes, loff_t *ppos)
1176{
1177 return 0;
1178}
1179#endif /* CONFIG_DEBUG_FS */
1180
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001181static const struct file_operations o2hb_debug_fops = {
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001182 .open = o2hb_debug_open,
1183 .release = o2hb_debug_release,
1184 .read = o2hb_debug_read,
1185 .llseek = generic_file_llseek,
1186};
1187
1188void o2hb_exit(void)
1189{
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001190 kfree(o2hb_db_livenodes);
Sunil Mushrana6de0132010-10-06 17:55:13 -07001191 kfree(o2hb_db_liveregions);
1192 kfree(o2hb_db_quorumregions);
1193 kfree(o2hb_db_failedregions);
1194 debugfs_remove(o2hb_debug_failedregions);
1195 debugfs_remove(o2hb_debug_quorumregions);
1196 debugfs_remove(o2hb_debug_liveregions);
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001197 debugfs_remove(o2hb_debug_livenodes);
1198 debugfs_remove(o2hb_debug_dir);
1199}
1200
1201static struct dentry *o2hb_debug_create(const char *name, struct dentry *dir,
1202 struct o2hb_debug_buf **db, int db_len,
1203 int type, int size, int len, void *data)
1204{
1205 *db = kmalloc(db_len, GFP_KERNEL);
1206 if (!*db)
1207 return NULL;
1208
1209 (*db)->db_type = type;
1210 (*db)->db_size = size;
1211 (*db)->db_len = len;
1212 (*db)->db_data = data;
1213
1214 return debugfs_create_file(name, S_IFREG|S_IRUSR, dir, *db,
1215 &o2hb_debug_fops);
1216}
1217
1218static int o2hb_debug_init(void)
1219{
1220 int ret = -ENOMEM;
1221
1222 o2hb_debug_dir = debugfs_create_dir(O2HB_DEBUG_DIR, NULL);
1223 if (!o2hb_debug_dir) {
1224 mlog_errno(ret);
1225 goto bail;
1226 }
1227
1228 o2hb_debug_livenodes = o2hb_debug_create(O2HB_DEBUG_LIVENODES,
1229 o2hb_debug_dir,
1230 &o2hb_db_livenodes,
1231 sizeof(*o2hb_db_livenodes),
1232 O2HB_DB_TYPE_LIVENODES,
1233 sizeof(o2hb_live_node_bitmap),
1234 O2NM_MAX_NODES,
1235 o2hb_live_node_bitmap);
1236 if (!o2hb_debug_livenodes) {
1237 mlog_errno(ret);
1238 goto bail;
1239 }
Sunil Mushrana6de0132010-10-06 17:55:13 -07001240
1241 o2hb_debug_liveregions = o2hb_debug_create(O2HB_DEBUG_LIVEREGIONS,
1242 o2hb_debug_dir,
1243 &o2hb_db_liveregions,
1244 sizeof(*o2hb_db_liveregions),
1245 O2HB_DB_TYPE_LIVEREGIONS,
1246 sizeof(o2hb_live_region_bitmap),
1247 O2NM_MAX_REGIONS,
1248 o2hb_live_region_bitmap);
1249 if (!o2hb_debug_liveregions) {
1250 mlog_errno(ret);
1251 goto bail;
1252 }
1253
1254 o2hb_debug_quorumregions =
1255 o2hb_debug_create(O2HB_DEBUG_QUORUMREGIONS,
1256 o2hb_debug_dir,
1257 &o2hb_db_quorumregions,
1258 sizeof(*o2hb_db_quorumregions),
1259 O2HB_DB_TYPE_QUORUMREGIONS,
1260 sizeof(o2hb_quorum_region_bitmap),
1261 O2NM_MAX_REGIONS,
1262 o2hb_quorum_region_bitmap);
1263 if (!o2hb_debug_quorumregions) {
1264 mlog_errno(ret);
1265 goto bail;
1266 }
1267
1268 o2hb_debug_failedregions =
1269 o2hb_debug_create(O2HB_DEBUG_FAILEDREGIONS,
1270 o2hb_debug_dir,
1271 &o2hb_db_failedregions,
1272 sizeof(*o2hb_db_failedregions),
1273 O2HB_DB_TYPE_FAILEDREGIONS,
1274 sizeof(o2hb_failed_region_bitmap),
1275 O2NM_MAX_REGIONS,
1276 o2hb_failed_region_bitmap);
1277 if (!o2hb_debug_failedregions) {
1278 mlog_errno(ret);
1279 goto bail;
1280 }
1281
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001282 ret = 0;
1283bail:
1284 if (ret)
1285 o2hb_exit();
1286
1287 return ret;
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001288}
1289
1290int o2hb_init(void)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001291{
1292 int i;
1293
1294 for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
1295 INIT_LIST_HEAD(&o2hb_callbacks[i].list);
1296
1297 for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
1298 INIT_LIST_HEAD(&o2hb_live_slots[i]);
1299
1300 INIT_LIST_HEAD(&o2hb_node_events);
1301
1302 memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
Sunil Mushran536f0742010-10-07 17:03:07 -07001303 memset(o2hb_region_bitmap, 0, sizeof(o2hb_region_bitmap));
Sunil Mushrane7d656b2010-10-06 17:55:18 -07001304 memset(o2hb_live_region_bitmap, 0, sizeof(o2hb_live_region_bitmap));
Sunil Mushran43182d22010-10-06 17:55:16 -07001305 memset(o2hb_quorum_region_bitmap, 0, sizeof(o2hb_quorum_region_bitmap));
Sunil Mushranb1c5ebf2010-10-07 17:05:52 -07001306 memset(o2hb_failed_region_bitmap, 0, sizeof(o2hb_failed_region_bitmap));
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001307
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001308 return o2hb_debug_init();
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001309}
1310
1311/* if we're already in a callback then we're already serialized by the sem */
1312static void o2hb_fill_node_map_from_callback(unsigned long *map,
1313 unsigned bytes)
1314{
1315 BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
1316
1317 memcpy(map, &o2hb_live_node_bitmap, bytes);
1318}
1319
1320/*
1321 * get a map of all nodes that are heartbeating in any regions
1322 */
1323void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
1324{
1325 /* callers want to serialize this map and callbacks so that they
1326 * can trust that they don't miss nodes coming to the party */
1327 down_read(&o2hb_callback_sem);
1328 spin_lock(&o2hb_live_lock);
1329 o2hb_fill_node_map_from_callback(map, bytes);
1330 spin_unlock(&o2hb_live_lock);
1331 up_read(&o2hb_callback_sem);
1332}
1333EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
1334
1335/*
1336 * heartbeat configfs bits. The heartbeat set is a default set under
1337 * the cluster set in nodemanager.c.
1338 */
1339
1340static struct o2hb_region *to_o2hb_region(struct config_item *item)
1341{
1342 return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
1343}
1344
1345/* drop_item only drops its ref after killing the thread, nothing should
1346 * be using the region anymore. this has to clean up any state that
1347 * attributes might have built up. */
1348static void o2hb_region_release(struct config_item *item)
1349{
1350 int i;
1351 struct page *page;
1352 struct o2hb_region *reg = to_o2hb_region(item);
1353
1354 if (reg->hr_tmp_block)
1355 kfree(reg->hr_tmp_block);
1356
1357 if (reg->hr_slot_data) {
1358 for (i = 0; i < reg->hr_num_pages; i++) {
1359 page = reg->hr_slot_data[i];
1360 if (page)
1361 __free_page(page);
1362 }
1363 kfree(reg->hr_slot_data);
1364 }
1365
1366 if (reg->hr_bdev)
Al Viro9a1c3542008-02-22 20:40:24 -05001367 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001368
1369 if (reg->hr_slots)
1370 kfree(reg->hr_slots);
1371
Sunil Mushran1f285302010-10-06 17:55:12 -07001372 kfree(reg->hr_db_regnum);
1373 kfree(reg->hr_db_livenodes);
1374 debugfs_remove(reg->hr_debug_livenodes);
1375 debugfs_remove(reg->hr_debug_regnum);
1376 debugfs_remove(reg->hr_debug_dir);
1377
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001378 spin_lock(&o2hb_live_lock);
1379 list_del(&reg->hr_all_item);
1380 spin_unlock(&o2hb_live_lock);
1381
1382 kfree(reg);
1383}
1384
1385static int o2hb_read_block_input(struct o2hb_region *reg,
1386 const char *page,
1387 size_t count,
1388 unsigned long *ret_bytes,
1389 unsigned int *ret_bits)
1390{
1391 unsigned long bytes;
1392 char *p = (char *)page;
1393
1394 bytes = simple_strtoul(p, &p, 0);
1395 if (!p || (*p && (*p != '\n')))
1396 return -EINVAL;
1397
1398 /* Heartbeat and fs min / max block sizes are the same. */
1399 if (bytes > 4096 || bytes < 512)
1400 return -ERANGE;
1401 if (hweight16(bytes) != 1)
1402 return -EINVAL;
1403
1404 if (ret_bytes)
1405 *ret_bytes = bytes;
1406 if (ret_bits)
1407 *ret_bits = ffs(bytes) - 1;
1408
1409 return 0;
1410}
1411
1412static ssize_t o2hb_region_block_bytes_read(struct o2hb_region *reg,
1413 char *page)
1414{
1415 return sprintf(page, "%u\n", reg->hr_block_bytes);
1416}
1417
1418static ssize_t o2hb_region_block_bytes_write(struct o2hb_region *reg,
1419 const char *page,
1420 size_t count)
1421{
1422 int status;
1423 unsigned long block_bytes;
1424 unsigned int block_bits;
1425
1426 if (reg->hr_bdev)
1427 return -EINVAL;
1428
1429 status = o2hb_read_block_input(reg, page, count,
1430 &block_bytes, &block_bits);
1431 if (status)
1432 return status;
1433
1434 reg->hr_block_bytes = (unsigned int)block_bytes;
1435 reg->hr_block_bits = block_bits;
1436
1437 return count;
1438}
1439
1440static ssize_t o2hb_region_start_block_read(struct o2hb_region *reg,
1441 char *page)
1442{
1443 return sprintf(page, "%llu\n", reg->hr_start_block);
1444}
1445
1446static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
1447 const char *page,
1448 size_t count)
1449{
1450 unsigned long long tmp;
1451 char *p = (char *)page;
1452
1453 if (reg->hr_bdev)
1454 return -EINVAL;
1455
1456 tmp = simple_strtoull(p, &p, 0);
1457 if (!p || (*p && (*p != '\n')))
1458 return -EINVAL;
1459
1460 reg->hr_start_block = tmp;
1461
1462 return count;
1463}
1464
1465static ssize_t o2hb_region_blocks_read(struct o2hb_region *reg,
1466 char *page)
1467{
1468 return sprintf(page, "%d\n", reg->hr_blocks);
1469}
1470
1471static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
1472 const char *page,
1473 size_t count)
1474{
1475 unsigned long tmp;
1476 char *p = (char *)page;
1477
1478 if (reg->hr_bdev)
1479 return -EINVAL;
1480
1481 tmp = simple_strtoul(p, &p, 0);
1482 if (!p || (*p && (*p != '\n')))
1483 return -EINVAL;
1484
1485 if (tmp > O2NM_MAX_NODES || tmp == 0)
1486 return -ERANGE;
1487
1488 reg->hr_blocks = (unsigned int)tmp;
1489
1490 return count;
1491}
1492
1493static ssize_t o2hb_region_dev_read(struct o2hb_region *reg,
1494 char *page)
1495{
1496 unsigned int ret = 0;
1497
1498 if (reg->hr_bdev)
1499 ret = sprintf(page, "%s\n", reg->hr_dev_name);
1500
1501 return ret;
1502}
1503
1504static void o2hb_init_region_params(struct o2hb_region *reg)
1505{
1506 reg->hr_slots_per_page = PAGE_CACHE_SIZE >> reg->hr_block_bits;
1507 reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
1508
1509 mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
1510 reg->hr_start_block, reg->hr_blocks);
1511 mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
1512 reg->hr_block_bytes, reg->hr_block_bits);
1513 mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
1514 mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
1515}
1516
1517static int o2hb_map_slot_data(struct o2hb_region *reg)
1518{
1519 int i, j;
1520 unsigned int last_slot;
1521 unsigned int spp = reg->hr_slots_per_page;
1522 struct page *page;
1523 char *raw;
1524 struct o2hb_disk_slot *slot;
1525
1526 reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
1527 if (reg->hr_tmp_block == NULL) {
1528 mlog_errno(-ENOMEM);
1529 return -ENOMEM;
1530 }
1531
1532 reg->hr_slots = kcalloc(reg->hr_blocks,
1533 sizeof(struct o2hb_disk_slot), GFP_KERNEL);
1534 if (reg->hr_slots == NULL) {
1535 mlog_errno(-ENOMEM);
1536 return -ENOMEM;
1537 }
1538
1539 for(i = 0; i < reg->hr_blocks; i++) {
1540 slot = &reg->hr_slots[i];
1541 slot->ds_node_num = i;
1542 INIT_LIST_HEAD(&slot->ds_live_item);
1543 slot->ds_raw_block = NULL;
1544 }
1545
1546 reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
1547 mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
1548 "at %u blocks per page\n",
1549 reg->hr_num_pages, reg->hr_blocks, spp);
1550
1551 reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
1552 GFP_KERNEL);
1553 if (!reg->hr_slot_data) {
1554 mlog_errno(-ENOMEM);
1555 return -ENOMEM;
1556 }
1557
1558 for(i = 0; i < reg->hr_num_pages; i++) {
1559 page = alloc_page(GFP_KERNEL);
1560 if (!page) {
1561 mlog_errno(-ENOMEM);
1562 return -ENOMEM;
1563 }
1564
1565 reg->hr_slot_data[i] = page;
1566
1567 last_slot = i * spp;
1568 raw = page_address(page);
1569 for (j = 0;
1570 (j < spp) && ((j + last_slot) < reg->hr_blocks);
1571 j++) {
1572 BUG_ON((j + last_slot) >= reg->hr_blocks);
1573
1574 slot = &reg->hr_slots[j + last_slot];
1575 slot->ds_raw_block =
1576 (struct o2hb_disk_heartbeat_block *) raw;
1577
1578 raw += reg->hr_block_bytes;
1579 }
1580 }
1581
1582 return 0;
1583}
1584
1585/* Read in all the slots available and populate the tracking
1586 * structures so that we can start with a baseline idea of what's
1587 * there. */
1588static int o2hb_populate_slot_data(struct o2hb_region *reg)
1589{
1590 int ret, i;
1591 struct o2hb_disk_slot *slot;
1592 struct o2hb_disk_heartbeat_block *hb_block;
1593
1594 mlog_entry_void();
1595
1596 ret = o2hb_read_slots(reg, reg->hr_blocks);
1597 if (ret) {
1598 mlog_errno(ret);
1599 goto out;
1600 }
1601
1602 /* We only want to get an idea of the values initially in each
1603 * slot, so we do no verification - o2hb_check_slot will
1604 * actually determine if each configured slot is valid and
1605 * whether any values have changed. */
1606 for(i = 0; i < reg->hr_blocks; i++) {
1607 slot = &reg->hr_slots[i];
1608 hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
1609
1610 /* Only fill the values that o2hb_check_slot uses to
1611 * determine changing slots */
1612 slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
1613 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
1614 }
1615
1616out:
1617 mlog_exit(ret);
1618 return ret;
1619}
1620
1621/* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1622static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
1623 const char *page,
1624 size_t count)
1625{
Joel Beckere6c352d2007-02-03 03:04:20 -08001626 struct task_struct *hb_task;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001627 long fd;
1628 int sectsize;
1629 char *p = (char *)page;
1630 struct file *filp = NULL;
1631 struct inode *inode = NULL;
1632 ssize_t ret = -EINVAL;
1633
1634 if (reg->hr_bdev)
1635 goto out;
1636
1637 /* We can't heartbeat without having had our node number
1638 * configured yet. */
1639 if (o2nm_this_node() == O2NM_MAX_NODES)
1640 goto out;
1641
1642 fd = simple_strtol(p, &p, 0);
1643 if (!p || (*p && (*p != '\n')))
1644 goto out;
1645
1646 if (fd < 0 || fd >= INT_MAX)
1647 goto out;
1648
1649 filp = fget(fd);
1650 if (filp == NULL)
1651 goto out;
1652
1653 if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
1654 reg->hr_block_bytes == 0)
1655 goto out;
1656
1657 inode = igrab(filp->f_mapping->host);
1658 if (inode == NULL)
1659 goto out;
1660
1661 if (!S_ISBLK(inode->i_mode))
1662 goto out;
1663
1664 reg->hr_bdev = I_BDEV(filp->f_mapping->host);
Al Viro572c4892007-10-08 13:24:05 -04001665 ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001666 if (ret) {
1667 reg->hr_bdev = NULL;
1668 goto out;
1669 }
1670 inode = NULL;
1671
1672 bdevname(reg->hr_bdev, reg->hr_dev_name);
1673
Martin K. Petersene1defc42009-05-22 17:17:49 -04001674 sectsize = bdev_logical_block_size(reg->hr_bdev);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001675 if (sectsize != reg->hr_block_bytes) {
1676 mlog(ML_ERROR,
1677 "blocksize %u incorrect for device, expected %d",
1678 reg->hr_block_bytes, sectsize);
1679 ret = -EINVAL;
1680 goto out;
1681 }
1682
1683 o2hb_init_region_params(reg);
1684
1685 /* Generation of zero is invalid */
1686 do {
1687 get_random_bytes(&reg->hr_generation,
1688 sizeof(reg->hr_generation));
1689 } while (reg->hr_generation == 0);
1690
1691 ret = o2hb_map_slot_data(reg);
1692 if (ret) {
1693 mlog_errno(ret);
1694 goto out;
1695 }
1696
1697 ret = o2hb_populate_slot_data(reg);
1698 if (ret) {
1699 mlog_errno(ret);
1700 goto out;
1701 }
1702
David Howellsc4028952006-11-22 14:57:56 +00001703 INIT_DELAYED_WORK(&reg->hr_write_timeout_work, o2hb_write_timeout);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001704
1705 /*
1706 * A node is considered live after it has beat LIVE_THRESHOLD
1707 * times. We're not steady until we've given them a chance
1708 * _after_ our first read.
1709 */
1710 atomic_set(&reg->hr_steady_iterations, O2HB_LIVE_THRESHOLD + 1);
1711
Joel Beckere6c352d2007-02-03 03:04:20 -08001712 hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
1713 reg->hr_item.ci_name);
1714 if (IS_ERR(hb_task)) {
1715 ret = PTR_ERR(hb_task);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001716 mlog_errno(ret);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001717 goto out;
1718 }
1719
Joel Beckere6c352d2007-02-03 03:04:20 -08001720 spin_lock(&o2hb_live_lock);
1721 reg->hr_task = hb_task;
1722 spin_unlock(&o2hb_live_lock);
1723
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001724 ret = wait_event_interruptible(o2hb_steady_queue,
1725 atomic_read(&reg->hr_steady_iterations) == 0);
1726 if (ret) {
Joel Beckere6df3a62007-02-06 15:45:39 -08001727 /* We got interrupted (hello ptrace!). Clean up */
Joel Beckere6c352d2007-02-03 03:04:20 -08001728 spin_lock(&o2hb_live_lock);
1729 hb_task = reg->hr_task;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001730 reg->hr_task = NULL;
Joel Beckere6c352d2007-02-03 03:04:20 -08001731 spin_unlock(&o2hb_live_lock);
1732
1733 if (hb_task)
1734 kthread_stop(hb_task);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001735 goto out;
1736 }
1737
Joel Beckere6df3a62007-02-06 15:45:39 -08001738 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1739 spin_lock(&o2hb_live_lock);
1740 hb_task = reg->hr_task;
Sunil Mushrane7d656b2010-10-06 17:55:18 -07001741 if (o2hb_global_heartbeat_active())
1742 set_bit(reg->hr_region_num, o2hb_live_region_bitmap);
Joel Beckere6df3a62007-02-06 15:45:39 -08001743 spin_unlock(&o2hb_live_lock);
1744
1745 if (hb_task)
1746 ret = count;
1747 else
1748 ret = -EIO;
1749
Sunil Mushran18c50cb2010-10-06 18:26:59 -07001750 if (hb_task && o2hb_global_heartbeat_active())
1751 printk(KERN_NOTICE "o2hb: Heartbeat started on region %s\n",
1752 config_item_name(&reg->hr_item));
1753
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001754out:
1755 if (filp)
1756 fput(filp);
1757 if (inode)
1758 iput(inode);
1759 if (ret < 0) {
1760 if (reg->hr_bdev) {
Al Viro9a1c3542008-02-22 20:40:24 -05001761 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001762 reg->hr_bdev = NULL;
1763 }
1764 }
1765 return ret;
1766}
1767
Zhen Wei92efc152006-12-08 00:48:17 -07001768static ssize_t o2hb_region_pid_read(struct o2hb_region *reg,
1769 char *page)
1770{
Joel Beckere6c352d2007-02-03 03:04:20 -08001771 pid_t pid = 0;
1772
1773 spin_lock(&o2hb_live_lock);
1774 if (reg->hr_task)
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001775 pid = task_pid_nr(reg->hr_task);
Joel Beckere6c352d2007-02-03 03:04:20 -08001776 spin_unlock(&o2hb_live_lock);
1777
1778 if (!pid)
Zhen Wei92efc152006-12-08 00:48:17 -07001779 return 0;
1780
Joel Beckere6c352d2007-02-03 03:04:20 -08001781 return sprintf(page, "%u\n", pid);
Zhen Wei92efc152006-12-08 00:48:17 -07001782}
1783
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001784struct o2hb_region_attribute {
1785 struct configfs_attribute attr;
1786 ssize_t (*show)(struct o2hb_region *, char *);
1787 ssize_t (*store)(struct o2hb_region *, const char *, size_t);
1788};
1789
1790static struct o2hb_region_attribute o2hb_region_attr_block_bytes = {
1791 .attr = { .ca_owner = THIS_MODULE,
1792 .ca_name = "block_bytes",
1793 .ca_mode = S_IRUGO | S_IWUSR },
1794 .show = o2hb_region_block_bytes_read,
1795 .store = o2hb_region_block_bytes_write,
1796};
1797
1798static struct o2hb_region_attribute o2hb_region_attr_start_block = {
1799 .attr = { .ca_owner = THIS_MODULE,
1800 .ca_name = "start_block",
1801 .ca_mode = S_IRUGO | S_IWUSR },
1802 .show = o2hb_region_start_block_read,
1803 .store = o2hb_region_start_block_write,
1804};
1805
1806static struct o2hb_region_attribute o2hb_region_attr_blocks = {
1807 .attr = { .ca_owner = THIS_MODULE,
1808 .ca_name = "blocks",
1809 .ca_mode = S_IRUGO | S_IWUSR },
1810 .show = o2hb_region_blocks_read,
1811 .store = o2hb_region_blocks_write,
1812};
1813
1814static struct o2hb_region_attribute o2hb_region_attr_dev = {
1815 .attr = { .ca_owner = THIS_MODULE,
1816 .ca_name = "dev",
1817 .ca_mode = S_IRUGO | S_IWUSR },
1818 .show = o2hb_region_dev_read,
1819 .store = o2hb_region_dev_write,
1820};
1821
Zhen Wei92efc152006-12-08 00:48:17 -07001822static struct o2hb_region_attribute o2hb_region_attr_pid = {
1823 .attr = { .ca_owner = THIS_MODULE,
1824 .ca_name = "pid",
1825 .ca_mode = S_IRUGO | S_IRUSR },
1826 .show = o2hb_region_pid_read,
1827};
1828
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001829static struct configfs_attribute *o2hb_region_attrs[] = {
1830 &o2hb_region_attr_block_bytes.attr,
1831 &o2hb_region_attr_start_block.attr,
1832 &o2hb_region_attr_blocks.attr,
1833 &o2hb_region_attr_dev.attr,
Zhen Wei92efc152006-12-08 00:48:17 -07001834 &o2hb_region_attr_pid.attr,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001835 NULL,
1836};
1837
1838static ssize_t o2hb_region_show(struct config_item *item,
1839 struct configfs_attribute *attr,
1840 char *page)
1841{
1842 struct o2hb_region *reg = to_o2hb_region(item);
1843 struct o2hb_region_attribute *o2hb_region_attr =
1844 container_of(attr, struct o2hb_region_attribute, attr);
1845 ssize_t ret = 0;
1846
1847 if (o2hb_region_attr->show)
1848 ret = o2hb_region_attr->show(reg, page);
1849 return ret;
1850}
1851
1852static ssize_t o2hb_region_store(struct config_item *item,
1853 struct configfs_attribute *attr,
1854 const char *page, size_t count)
1855{
1856 struct o2hb_region *reg = to_o2hb_region(item);
1857 struct o2hb_region_attribute *o2hb_region_attr =
1858 container_of(attr, struct o2hb_region_attribute, attr);
1859 ssize_t ret = -EINVAL;
1860
1861 if (o2hb_region_attr->store)
1862 ret = o2hb_region_attr->store(reg, page, count);
1863 return ret;
1864}
1865
1866static struct configfs_item_operations o2hb_region_item_ops = {
1867 .release = o2hb_region_release,
1868 .show_attribute = o2hb_region_show,
1869 .store_attribute = o2hb_region_store,
1870};
1871
1872static struct config_item_type o2hb_region_type = {
1873 .ct_item_ops = &o2hb_region_item_ops,
1874 .ct_attrs = o2hb_region_attrs,
1875 .ct_owner = THIS_MODULE,
1876};
1877
1878/* heartbeat set */
1879
1880struct o2hb_heartbeat_group {
1881 struct config_group hs_group;
1882 /* some stuff? */
1883};
1884
1885static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
1886{
1887 return group ?
1888 container_of(group, struct o2hb_heartbeat_group, hs_group)
1889 : NULL;
1890}
1891
Sunil Mushran1f285302010-10-06 17:55:12 -07001892static int o2hb_debug_region_init(struct o2hb_region *reg, struct dentry *dir)
1893{
1894 int ret = -ENOMEM;
1895
1896 reg->hr_debug_dir =
1897 debugfs_create_dir(config_item_name(&reg->hr_item), dir);
1898 if (!reg->hr_debug_dir) {
1899 mlog_errno(ret);
1900 goto bail;
1901 }
1902
1903 reg->hr_debug_livenodes =
1904 o2hb_debug_create(O2HB_DEBUG_LIVENODES,
1905 reg->hr_debug_dir,
1906 &(reg->hr_db_livenodes),
1907 sizeof(*(reg->hr_db_livenodes)),
1908 O2HB_DB_TYPE_REGION_LIVENODES,
1909 sizeof(reg->hr_live_node_bitmap),
1910 O2NM_MAX_NODES, reg);
1911 if (!reg->hr_debug_livenodes) {
1912 mlog_errno(ret);
1913 goto bail;
1914 }
1915
1916 reg->hr_debug_regnum =
1917 o2hb_debug_create(O2HB_DEBUG_REGION_NUMBER,
1918 reg->hr_debug_dir,
1919 &(reg->hr_db_regnum),
1920 sizeof(*(reg->hr_db_regnum)),
1921 O2HB_DB_TYPE_REGION_NUMBER,
1922 0, O2NM_MAX_NODES, reg);
1923 if (!reg->hr_debug_regnum) {
1924 mlog_errno(ret);
1925 goto bail;
1926 }
1927
1928 ret = 0;
1929bail:
1930 return ret;
1931}
1932
Joel Beckerf89ab862008-07-17 14:53:48 -07001933static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
1934 const char *name)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001935{
1936 struct o2hb_region *reg = NULL;
Sunil Mushran1f285302010-10-06 17:55:12 -07001937 int ret;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001938
Robert P. J. Daycd861282006-12-13 00:34:52 -08001939 reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
Joel Beckerf89ab862008-07-17 14:53:48 -07001940 if (reg == NULL)
Joel Beckera6795e92008-07-17 15:21:29 -07001941 return ERR_PTR(-ENOMEM);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001942
Sunil Mushranb3c85c42010-10-07 14:31:06 -07001943 if (strlen(name) > O2HB_MAX_REGION_NAME_LEN)
1944 return ERR_PTR(-ENAMETOOLONG);
1945
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001946 spin_lock(&o2hb_live_lock);
Sunil Mushran536f0742010-10-07 17:03:07 -07001947 reg->hr_region_num = 0;
1948 if (o2hb_global_heartbeat_active()) {
1949 reg->hr_region_num = find_first_zero_bit(o2hb_region_bitmap,
1950 O2NM_MAX_REGIONS);
1951 if (reg->hr_region_num >= O2NM_MAX_REGIONS) {
1952 spin_unlock(&o2hb_live_lock);
1953 return ERR_PTR(-EFBIG);
1954 }
1955 set_bit(reg->hr_region_num, o2hb_region_bitmap);
1956 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001957 list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
1958 spin_unlock(&o2hb_live_lock);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001959
Sunil Mushran536f0742010-10-07 17:03:07 -07001960 config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
1961
Sunil Mushran1f285302010-10-06 17:55:12 -07001962 ret = o2hb_debug_region_init(reg, o2hb_debug_dir);
1963 if (ret) {
1964 config_item_put(&reg->hr_item);
1965 return ERR_PTR(ret);
1966 }
1967
Joel Beckera6795e92008-07-17 15:21:29 -07001968 return &reg->hr_item;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001969}
1970
1971static void o2hb_heartbeat_group_drop_item(struct config_group *group,
1972 struct config_item *item)
1973{
Joel Beckere6c352d2007-02-03 03:04:20 -08001974 struct task_struct *hb_task;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001975 struct o2hb_region *reg = to_o2hb_region(item);
1976
1977 /* stop the thread when the user removes the region dir */
Joel Beckere6c352d2007-02-03 03:04:20 -08001978 spin_lock(&o2hb_live_lock);
Sunil Mushrane7d656b2010-10-06 17:55:18 -07001979 if (o2hb_global_heartbeat_active()) {
Sunil Mushran536f0742010-10-07 17:03:07 -07001980 clear_bit(reg->hr_region_num, o2hb_region_bitmap);
Sunil Mushrane7d656b2010-10-06 17:55:18 -07001981 clear_bit(reg->hr_region_num, o2hb_live_region_bitmap);
1982 }
Joel Beckere6c352d2007-02-03 03:04:20 -08001983 hb_task = reg->hr_task;
1984 reg->hr_task = NULL;
1985 spin_unlock(&o2hb_live_lock);
1986
1987 if (hb_task)
1988 kthread_stop(hb_task);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001989
Joel Beckere6df3a62007-02-06 15:45:39 -08001990 /*
1991 * If we're racing a dev_write(), we need to wake them. They will
1992 * check reg->hr_task
1993 */
1994 if (atomic_read(&reg->hr_steady_iterations) != 0) {
1995 atomic_set(&reg->hr_steady_iterations, 0);
1996 wake_up(&o2hb_steady_queue);
1997 }
1998
Sunil Mushran18c50cb2010-10-06 18:26:59 -07001999 if (o2hb_global_heartbeat_active())
2000 printk(KERN_NOTICE "o2hb: Heartbeat stopped on region %s\n",
2001 config_item_name(&reg->hr_item));
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002002 config_item_put(item);
2003}
2004
2005struct o2hb_heartbeat_group_attribute {
2006 struct configfs_attribute attr;
2007 ssize_t (*show)(struct o2hb_heartbeat_group *, char *);
2008 ssize_t (*store)(struct o2hb_heartbeat_group *, const char *, size_t);
2009};
2010
2011static ssize_t o2hb_heartbeat_group_show(struct config_item *item,
2012 struct configfs_attribute *attr,
2013 char *page)
2014{
2015 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
2016 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
2017 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
2018 ssize_t ret = 0;
2019
2020 if (o2hb_heartbeat_group_attr->show)
2021 ret = o2hb_heartbeat_group_attr->show(reg, page);
2022 return ret;
2023}
2024
2025static ssize_t o2hb_heartbeat_group_store(struct config_item *item,
2026 struct configfs_attribute *attr,
2027 const char *page, size_t count)
2028{
2029 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
2030 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
2031 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
2032 ssize_t ret = -EINVAL;
2033
2034 if (o2hb_heartbeat_group_attr->store)
2035 ret = o2hb_heartbeat_group_attr->store(reg, page, count);
2036 return ret;
2037}
2038
2039static ssize_t o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group *group,
2040 char *page)
2041{
2042 return sprintf(page, "%u\n", o2hb_dead_threshold);
2043}
2044
2045static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group *group,
2046 const char *page,
2047 size_t count)
2048{
2049 unsigned long tmp;
2050 char *p = (char *)page;
2051
2052 tmp = simple_strtoul(p, &p, 10);
2053 if (!p || (*p && (*p != '\n')))
2054 return -EINVAL;
2055
2056 /* this will validate ranges for us. */
2057 o2hb_dead_threshold_set((unsigned int) tmp);
2058
2059 return count;
2060}
2061
Sunil Mushran54b51872010-10-07 15:26:08 -07002062static
2063ssize_t o2hb_heartbeat_group_mode_show(struct o2hb_heartbeat_group *group,
2064 char *page)
2065{
2066 return sprintf(page, "%s\n",
2067 o2hb_heartbeat_mode_desc[o2hb_heartbeat_mode]);
2068}
2069
2070static
2071ssize_t o2hb_heartbeat_group_mode_store(struct o2hb_heartbeat_group *group,
2072 const char *page, size_t count)
2073{
2074 unsigned int i;
2075 int ret;
2076 size_t len;
2077
2078 len = (page[count - 1] == '\n') ? count - 1 : count;
2079 if (!len)
2080 return -EINVAL;
2081
2082 for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) {
2083 if (strnicmp(page, o2hb_heartbeat_mode_desc[i], len))
2084 continue;
2085
2086 ret = o2hb_global_hearbeat_mode_set(i);
2087 if (!ret)
Sunil Mushran18c50cb2010-10-06 18:26:59 -07002088 printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
Sunil Mushran54b51872010-10-07 15:26:08 -07002089 o2hb_heartbeat_mode_desc[i]);
2090 return count;
2091 }
2092
2093 return -EINVAL;
2094
2095}
2096
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002097static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold = {
2098 .attr = { .ca_owner = THIS_MODULE,
2099 .ca_name = "dead_threshold",
2100 .ca_mode = S_IRUGO | S_IWUSR },
2101 .show = o2hb_heartbeat_group_threshold_show,
2102 .store = o2hb_heartbeat_group_threshold_store,
2103};
2104
Sunil Mushran54b51872010-10-07 15:26:08 -07002105static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_mode = {
2106 .attr = { .ca_owner = THIS_MODULE,
2107 .ca_name = "mode",
2108 .ca_mode = S_IRUGO | S_IWUSR },
2109 .show = o2hb_heartbeat_group_mode_show,
2110 .store = o2hb_heartbeat_group_mode_store,
2111};
2112
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002113static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
2114 &o2hb_heartbeat_group_attr_threshold.attr,
Sunil Mushran54b51872010-10-07 15:26:08 -07002115 &o2hb_heartbeat_group_attr_mode.attr,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002116 NULL,
2117};
2118
2119static struct configfs_item_operations o2hb_hearbeat_group_item_ops = {
2120 .show_attribute = o2hb_heartbeat_group_show,
2121 .store_attribute = o2hb_heartbeat_group_store,
2122};
2123
2124static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
2125 .make_item = o2hb_heartbeat_group_make_item,
2126 .drop_item = o2hb_heartbeat_group_drop_item,
2127};
2128
2129static struct config_item_type o2hb_heartbeat_group_type = {
2130 .ct_group_ops = &o2hb_heartbeat_group_group_ops,
2131 .ct_item_ops = &o2hb_hearbeat_group_item_ops,
2132 .ct_attrs = o2hb_heartbeat_group_attrs,
2133 .ct_owner = THIS_MODULE,
2134};
2135
2136/* this is just here to avoid touching group in heartbeat.h which the
2137 * entire damn world #includes */
2138struct config_group *o2hb_alloc_hb_set(void)
2139{
2140 struct o2hb_heartbeat_group *hs = NULL;
2141 struct config_group *ret = NULL;
2142
Robert P. J. Daycd861282006-12-13 00:34:52 -08002143 hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002144 if (hs == NULL)
2145 goto out;
2146
2147 config_group_init_type_name(&hs->hs_group, "heartbeat",
2148 &o2hb_heartbeat_group_type);
2149
2150 ret = &hs->hs_group;
2151out:
2152 if (ret == NULL)
2153 kfree(hs);
2154 return ret;
2155}
2156
2157void o2hb_free_hb_set(struct config_group *group)
2158{
2159 struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
2160 kfree(hs);
2161}
2162
2163/* hb callback registration and issueing */
2164
2165static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
2166{
2167 if (type == O2HB_NUM_CB)
2168 return ERR_PTR(-EINVAL);
2169
2170 return &o2hb_callbacks[type];
2171}
2172
2173void o2hb_setup_callback(struct o2hb_callback_func *hc,
2174 enum o2hb_callback_type type,
2175 o2hb_cb_func *func,
2176 void *data,
2177 int priority)
2178{
2179 INIT_LIST_HEAD(&hc->hc_item);
2180 hc->hc_func = func;
2181 hc->hc_data = data;
2182 hc->hc_priority = priority;
2183 hc->hc_type = type;
2184 hc->hc_magic = O2HB_CB_MAGIC;
2185}
2186EXPORT_SYMBOL_GPL(o2hb_setup_callback);
2187
Joel Becker14829422007-06-14 21:40:49 -07002188static struct o2hb_region *o2hb_find_region(const char *region_uuid)
2189{
2190 struct o2hb_region *p, *reg = NULL;
2191
2192 assert_spin_locked(&o2hb_live_lock);
2193
2194 list_for_each_entry(p, &o2hb_all_regions, hr_all_item) {
2195 if (!strcmp(region_uuid, config_item_name(&p->hr_item))) {
2196 reg = p;
2197 break;
2198 }
2199 }
2200
2201 return reg;
2202}
2203
2204static int o2hb_region_get(const char *region_uuid)
2205{
2206 int ret = 0;
2207 struct o2hb_region *reg;
2208
2209 spin_lock(&o2hb_live_lock);
2210
2211 reg = o2hb_find_region(region_uuid);
2212 if (!reg)
2213 ret = -ENOENT;
2214 spin_unlock(&o2hb_live_lock);
2215
Joel Becker16c6a4f2007-06-19 11:34:03 -07002216 if (ret)
2217 goto out;
Joel Becker14829422007-06-14 21:40:49 -07002218
Joel Becker16c6a4f2007-06-19 11:34:03 -07002219 ret = o2nm_depend_this_node();
2220 if (ret)
2221 goto out;
2222
2223 ret = o2nm_depend_item(&reg->hr_item);
2224 if (ret)
2225 o2nm_undepend_this_node();
2226
2227out:
Joel Becker14829422007-06-14 21:40:49 -07002228 return ret;
2229}
2230
2231static void o2hb_region_put(const char *region_uuid)
2232{
2233 struct o2hb_region *reg;
2234
2235 spin_lock(&o2hb_live_lock);
2236
2237 reg = o2hb_find_region(region_uuid);
2238
2239 spin_unlock(&o2hb_live_lock);
2240
Joel Becker16c6a4f2007-06-19 11:34:03 -07002241 if (reg) {
Joel Becker14829422007-06-14 21:40:49 -07002242 o2nm_undepend_item(&reg->hr_item);
Joel Becker16c6a4f2007-06-19 11:34:03 -07002243 o2nm_undepend_this_node();
2244 }
Joel Becker14829422007-06-14 21:40:49 -07002245}
2246
2247int o2hb_register_callback(const char *region_uuid,
2248 struct o2hb_callback_func *hc)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002249{
2250 struct o2hb_callback_func *tmp;
2251 struct list_head *iter;
2252 struct o2hb_callback *hbcall;
2253 int ret;
2254
2255 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2256 BUG_ON(!list_empty(&hc->hc_item));
2257
2258 hbcall = hbcall_from_type(hc->hc_type);
2259 if (IS_ERR(hbcall)) {
2260 ret = PTR_ERR(hbcall);
2261 goto out;
2262 }
2263
Joel Becker14829422007-06-14 21:40:49 -07002264 if (region_uuid) {
2265 ret = o2hb_region_get(region_uuid);
2266 if (ret)
2267 goto out;
2268 }
2269
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002270 down_write(&o2hb_callback_sem);
2271
2272 list_for_each(iter, &hbcall->list) {
2273 tmp = list_entry(iter, struct o2hb_callback_func, hc_item);
2274 if (hc->hc_priority < tmp->hc_priority) {
2275 list_add_tail(&hc->hc_item, iter);
2276 break;
2277 }
2278 }
2279 if (list_empty(&hc->hc_item))
2280 list_add_tail(&hc->hc_item, &hbcall->list);
2281
2282 up_write(&o2hb_callback_sem);
2283 ret = 0;
2284out:
2285 mlog(ML_HEARTBEAT, "returning %d on behalf of %p for funcs %p\n",
2286 ret, __builtin_return_address(0), hc);
2287 return ret;
2288}
2289EXPORT_SYMBOL_GPL(o2hb_register_callback);
2290
Joel Becker14829422007-06-14 21:40:49 -07002291void o2hb_unregister_callback(const char *region_uuid,
2292 struct o2hb_callback_func *hc)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002293{
2294 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2295
2296 mlog(ML_HEARTBEAT, "on behalf of %p for funcs %p\n",
2297 __builtin_return_address(0), hc);
2298
Joel Becker14829422007-06-14 21:40:49 -07002299 /* XXX Can this happen _with_ a region reference? */
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002300 if (list_empty(&hc->hc_item))
Joel Beckerc24f72c2007-02-03 03:14:30 -08002301 return;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002302
Joel Becker14829422007-06-14 21:40:49 -07002303 if (region_uuid)
2304 o2hb_region_put(region_uuid);
2305
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002306 down_write(&o2hb_callback_sem);
2307
2308 list_del_init(&hc->hc_item);
2309
2310 up_write(&o2hb_callback_sem);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002311}
2312EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
2313
2314int o2hb_check_node_heartbeating(u8 node_num)
2315{
2316 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2317
2318 o2hb_fill_node_map(testing_map, sizeof(testing_map));
2319 if (!test_bit(node_num, testing_map)) {
2320 mlog(ML_HEARTBEAT,
2321 "node (%u) does not have heartbeating enabled.\n",
2322 node_num);
2323 return 0;
2324 }
2325
2326 return 1;
2327}
2328EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating);
2329
2330int o2hb_check_node_heartbeating_from_callback(u8 node_num)
2331{
2332 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2333
2334 o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
2335 if (!test_bit(node_num, testing_map)) {
2336 mlog(ML_HEARTBEAT,
2337 "node (%u) does not have heartbeating enabled.\n",
2338 node_num);
2339 return 0;
2340 }
2341
2342 return 1;
2343}
2344EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
2345
2346/* Makes sure our local node is configured with a node number, and is
2347 * heartbeating. */
2348int o2hb_check_local_node_heartbeating(void)
2349{
2350 u8 node_num;
2351
2352 /* if this node was set then we have networking */
2353 node_num = o2nm_this_node();
2354 if (node_num == O2NM_MAX_NODES) {
2355 mlog(ML_HEARTBEAT, "this node has not been configured.\n");
2356 return 0;
2357 }
2358
2359 return o2hb_check_node_heartbeating(node_num);
2360}
2361EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating);
2362
2363/*
2364 * this is just a hack until we get the plumbing which flips file systems
2365 * read only and drops the hb ref instead of killing the node dead.
2366 */
2367void o2hb_stop_all_regions(void)
2368{
2369 struct o2hb_region *reg;
2370
2371 mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
2372
2373 spin_lock(&o2hb_live_lock);
2374
2375 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
2376 reg->hr_unclean_stop = 1;
2377
2378 spin_unlock(&o2hb_live_lock);
2379}
2380EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);
Sunil Mushranb3c85c42010-10-07 14:31:06 -07002381
2382int o2hb_get_all_regions(char *region_uuids, u8 max_regions)
2383{
2384 struct o2hb_region *reg;
2385 int numregs = 0;
2386 char *p;
2387
2388 spin_lock(&o2hb_live_lock);
2389
2390 p = region_uuids;
2391 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2392 mlog(0, "Region: %s\n", config_item_name(&reg->hr_item));
2393 if (numregs < max_regions) {
2394 memcpy(p, config_item_name(&reg->hr_item),
2395 O2HB_MAX_REGION_NAME_LEN);
2396 p += O2HB_MAX_REGION_NAME_LEN;
2397 }
2398 numregs++;
2399 }
2400
2401 spin_unlock(&o2hb_live_lock);
2402
2403 return numregs;
2404}
2405EXPORT_SYMBOL_GPL(o2hb_get_all_regions);
2406
2407int o2hb_global_heartbeat_active(void)
2408{
2409 return 0;
2410}
2411EXPORT_SYMBOL(o2hb_global_heartbeat_active);