blob: 188f50269b892e84c3687087fe3e5ea775199ce2 [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 Mushran8ca8b0b2010-10-07 17:01:27 -070065#define O2HB_DB_TYPE_LIVENODES 0
66struct o2hb_debug_buf {
67 int db_type;
68 int db_size;
69 int db_len;
70 void *db_data;
71};
72
73static struct o2hb_debug_buf *o2hb_db_livenodes;
74
Sunil Mushran87d3d3f2008-12-17 14:17:42 -080075#define O2HB_DEBUG_DIR "o2hb"
76#define O2HB_DEBUG_LIVENODES "livenodes"
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -070077
Sunil Mushran87d3d3f2008-12-17 14:17:42 -080078static struct dentry *o2hb_debug_dir;
79static struct dentry *o2hb_debug_livenodes;
80
Mark Fasheha7f6a5f2005-12-15 14:31:23 -080081static LIST_HEAD(o2hb_all_regions);
82
83static struct o2hb_callback {
84 struct list_head list;
85} o2hb_callbacks[O2HB_NUM_CB];
86
87static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type);
88
89#define O2HB_DEFAULT_BLOCK_BITS 9
90
Sunil Mushran54b51872010-10-07 15:26:08 -070091enum o2hb_heartbeat_modes {
92 O2HB_HEARTBEAT_LOCAL = 0,
93 O2HB_HEARTBEAT_GLOBAL,
94 O2HB_HEARTBEAT_NUM_MODES,
95};
96
97char *o2hb_heartbeat_mode_desc[O2HB_HEARTBEAT_NUM_MODES] = {
98 "local", /* O2HB_HEARTBEAT_LOCAL */
99 "global", /* O2HB_HEARTBEAT_GLOBAL */
100};
101
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800102unsigned int o2hb_dead_threshold = O2HB_DEFAULT_DEAD_THRESHOLD;
Sunil Mushran54b51872010-10-07 15:26:08 -0700103unsigned int o2hb_heartbeat_mode = O2HB_HEARTBEAT_LOCAL;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800104
Sunil Mushran2bd63212010-01-25 16:57:38 -0800105/* Only sets a new threshold if there are no active regions.
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800106 *
107 * No locking or otherwise interesting code is required for reading
108 * o2hb_dead_threshold as it can't change once regions are active and
109 * it's not interesting to anyone until then anyway. */
110static void o2hb_dead_threshold_set(unsigned int threshold)
111{
112 if (threshold > O2HB_MIN_DEAD_THRESHOLD) {
113 spin_lock(&o2hb_live_lock);
114 if (list_empty(&o2hb_all_regions))
115 o2hb_dead_threshold = threshold;
116 spin_unlock(&o2hb_live_lock);
117 }
118}
119
Sunil Mushran54b51872010-10-07 15:26:08 -0700120static int o2hb_global_hearbeat_mode_set(unsigned int hb_mode)
121{
122 int ret = -1;
123
124 if (hb_mode < O2HB_HEARTBEAT_NUM_MODES) {
125 spin_lock(&o2hb_live_lock);
126 if (list_empty(&o2hb_all_regions)) {
127 o2hb_heartbeat_mode = hb_mode;
128 ret = 0;
129 }
130 spin_unlock(&o2hb_live_lock);
131 }
132
133 return ret;
134}
135
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800136struct o2hb_node_event {
137 struct list_head hn_item;
138 enum o2hb_callback_type hn_event_type;
139 struct o2nm_node *hn_node;
140 int hn_node_num;
141};
142
143struct o2hb_disk_slot {
144 struct o2hb_disk_heartbeat_block *ds_raw_block;
145 u8 ds_node_num;
146 u64 ds_last_time;
147 u64 ds_last_generation;
148 u16 ds_equal_samples;
149 u16 ds_changed_samples;
150 struct list_head ds_live_item;
151};
152
153/* each thread owns a region.. when we're asked to tear down the region
154 * we ask the thread to stop, who cleans up the region */
155struct o2hb_region {
156 struct config_item hr_item;
157
158 struct list_head hr_all_item;
159 unsigned hr_unclean_stop:1;
160
161 /* protected by the hr_callback_sem */
162 struct task_struct *hr_task;
163
164 unsigned int hr_blocks;
165 unsigned long long hr_start_block;
166
167 unsigned int hr_block_bits;
168 unsigned int hr_block_bytes;
169
170 unsigned int hr_slots_per_page;
171 unsigned int hr_num_pages;
172
173 struct page **hr_slot_data;
174 struct block_device *hr_bdev;
175 struct o2hb_disk_slot *hr_slots;
176
Sunil Mushran823a6372010-10-06 17:55:21 -0700177 /* live node map of this region */
178 unsigned long hr_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
179
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800180 /* let the person setting up hb wait for it to return until it
181 * has reached a 'steady' state. This will be fixed when we have
182 * a more complete api that doesn't lead to this sort of fragility. */
183 atomic_t hr_steady_iterations;
184
185 char hr_dev_name[BDEVNAME_SIZE];
186
187 unsigned int hr_timeout_ms;
188
189 /* randomized as the region goes up and down so that a node
190 * recognizes a node going up and down in one iteration */
191 u64 hr_generation;
192
David Howellsc4028952006-11-22 14:57:56 +0000193 struct delayed_work hr_write_timeout_work;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800194 unsigned long hr_last_timeout_start;
195
196 /* Used during o2hb_check_slot to hold a copy of the block
197 * being checked because we temporarily have to zero out the
198 * crc field. */
199 struct o2hb_disk_heartbeat_block *hr_tmp_block;
200};
201
202struct o2hb_bio_wait_ctxt {
203 atomic_t wc_num_reqs;
204 struct completion wc_io_complete;
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800205 int wc_error;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800206};
207
David Howellsc4028952006-11-22 14:57:56 +0000208static void o2hb_write_timeout(struct work_struct *work)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800209{
David Howellsc4028952006-11-22 14:57:56 +0000210 struct o2hb_region *reg =
211 container_of(work, struct o2hb_region,
212 hr_write_timeout_work.work);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800213
214 mlog(ML_ERROR, "Heartbeat write timeout to device %s after %u "
215 "milliseconds\n", reg->hr_dev_name,
Sunil Mushran2bd63212010-01-25 16:57:38 -0800216 jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800217 o2quo_disk_timeout();
218}
219
220static void o2hb_arm_write_timeout(struct o2hb_region *reg)
221{
Tao Mab31d3082009-12-22 10:32:15 +0800222 mlog(ML_HEARTBEAT, "Queue write timeout for %u ms\n",
223 O2HB_MAX_WRITE_TIMEOUT_MS);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800224
225 cancel_delayed_work(&reg->hr_write_timeout_work);
226 reg->hr_last_timeout_start = jiffies;
227 schedule_delayed_work(&reg->hr_write_timeout_work,
228 msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS));
229}
230
231static void o2hb_disarm_write_timeout(struct o2hb_region *reg)
232{
233 cancel_delayed_work(&reg->hr_write_timeout_work);
234 flush_scheduled_work();
235}
236
Philipp Reisnerb5592922007-01-11 10:58:10 +0100237static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt *wc)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800238{
Philipp Reisnerb5592922007-01-11 10:58:10 +0100239 atomic_set(&wc->wc_num_reqs, 1);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800240 init_completion(&wc->wc_io_complete);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800241 wc->wc_error = 0;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800242}
243
244/* Used in error paths too */
245static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt *wc,
246 unsigned int num)
247{
248 /* sadly atomic_sub_and_test() isn't available on all platforms. The
249 * good news is that the fast path only completes one at a time */
250 while(num--) {
251 if (atomic_dec_and_test(&wc->wc_num_reqs)) {
252 BUG_ON(num > 0);
253 complete(&wc->wc_io_complete);
254 }
255 }
256}
257
258static void o2hb_wait_on_io(struct o2hb_region *reg,
259 struct o2hb_bio_wait_ctxt *wc)
260{
261 struct address_space *mapping = reg->hr_bdev->bd_inode->i_mapping;
262
263 blk_run_address_space(mapping);
Philipp Reisnerb5592922007-01-11 10:58:10 +0100264 o2hb_bio_wait_dec(wc, 1);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800265
266 wait_for_completion(&wc->wc_io_complete);
267}
268
Al Viro782e3b32007-10-12 07:17:47 +0100269static void o2hb_bio_end_io(struct bio *bio,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800270 int error)
271{
272 struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
273
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800274 if (error) {
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800275 mlog(ML_ERROR, "IO Error %d\n", error);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800276 wc->wc_error = error;
277 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800278
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800279 o2hb_bio_wait_dec(wc, 1);
Philipp Reisnerb5592922007-01-11 10:58:10 +0100280 bio_put(bio);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800281}
282
283/* Setup a Bio to cover I/O against num_slots slots starting at
284 * start_slot. */
285static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
286 struct o2hb_bio_wait_ctxt *wc,
Philipp Reisnerb5592922007-01-11 10:58:10 +0100287 unsigned int *current_slot,
288 unsigned int max_slots)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800289{
Philipp Reisnerb5592922007-01-11 10:58:10 +0100290 int len, current_page;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800291 unsigned int vec_len, vec_start;
292 unsigned int bits = reg->hr_block_bits;
293 unsigned int spp = reg->hr_slots_per_page;
Philipp Reisnerb5592922007-01-11 10:58:10 +0100294 unsigned int cs = *current_slot;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800295 struct bio *bio;
296 struct page *page;
297
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800298 /* Testing has shown this allocation to take long enough under
299 * GFP_KERNEL that the local node can get fenced. It would be
300 * nicest if we could pre-allocate these bios and avoid this
301 * all together. */
Philipp Reisnerb5592922007-01-11 10:58:10 +0100302 bio = bio_alloc(GFP_ATOMIC, 16);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800303 if (!bio) {
304 mlog(ML_ERROR, "Could not alloc slots BIO!\n");
305 bio = ERR_PTR(-ENOMEM);
306 goto bail;
307 }
308
309 /* Must put everything in 512 byte sectors for the bio... */
Philipp Reisnerb5592922007-01-11 10:58:10 +0100310 bio->bi_sector = (reg->hr_start_block + cs) << (bits - 9);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800311 bio->bi_bdev = reg->hr_bdev;
312 bio->bi_private = wc;
313 bio->bi_end_io = o2hb_bio_end_io;
314
Philipp Reisnerb5592922007-01-11 10:58:10 +0100315 vec_start = (cs << bits) % PAGE_CACHE_SIZE;
316 while(cs < max_slots) {
317 current_page = cs / spp;
318 page = reg->hr_slot_data[current_page];
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800319
Jan Karabc7e97c2007-10-10 16:25:42 +0200320 vec_len = min(PAGE_CACHE_SIZE - vec_start,
Philipp Reisnerb5592922007-01-11 10:58:10 +0100321 (max_slots-cs) * (PAGE_CACHE_SIZE/spp) );
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800322
323 mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
Philipp Reisnerb5592922007-01-11 10:58:10 +0100324 current_page, vec_len, vec_start);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800325
326 len = bio_add_page(bio, page, vec_len, vec_start);
Philipp Reisnerb5592922007-01-11 10:58:10 +0100327 if (len != vec_len) break;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800328
Philipp Reisnerb5592922007-01-11 10:58:10 +0100329 cs += vec_len / (PAGE_CACHE_SIZE/spp);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800330 vec_start = 0;
331 }
332
333bail:
Philipp Reisnerb5592922007-01-11 10:58:10 +0100334 *current_slot = cs;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800335 return bio;
336}
337
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800338static int o2hb_read_slots(struct o2hb_region *reg,
339 unsigned int max_slots)
340{
Philipp Reisnerb5592922007-01-11 10:58:10 +0100341 unsigned int current_slot=0;
342 int status;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800343 struct o2hb_bio_wait_ctxt wc;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800344 struct bio *bio;
345
Philipp Reisnerb5592922007-01-11 10:58:10 +0100346 o2hb_bio_wait_init(&wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800347
Philipp Reisnerb5592922007-01-11 10:58:10 +0100348 while(current_slot < max_slots) {
349 bio = o2hb_setup_one_bio(reg, &wc, &current_slot, max_slots);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800350 if (IS_ERR(bio)) {
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800351 status = PTR_ERR(bio);
352 mlog_errno(status);
353 goto bail_and_wait;
354 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800355
Philipp Reisnerb5592922007-01-11 10:58:10 +0100356 atomic_inc(&wc.wc_num_reqs);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800357 submit_bio(READ, bio);
358 }
359
360 status = 0;
361
362bail_and_wait:
363 o2hb_wait_on_io(reg, &wc);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800364 if (wc.wc_error && !status)
365 status = wc.wc_error;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800366
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800367 return status;
368}
369
370static int o2hb_issue_node_write(struct o2hb_region *reg,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800371 struct o2hb_bio_wait_ctxt *write_wc)
372{
373 int status;
374 unsigned int slot;
375 struct bio *bio;
376
Philipp Reisnerb5592922007-01-11 10:58:10 +0100377 o2hb_bio_wait_init(write_wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800378
379 slot = o2nm_this_node();
380
Philipp Reisnerb5592922007-01-11 10:58:10 +0100381 bio = o2hb_setup_one_bio(reg, write_wc, &slot, slot+1);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800382 if (IS_ERR(bio)) {
383 status = PTR_ERR(bio);
384 mlog_errno(status);
385 goto bail;
386 }
387
Philipp Reisnerb5592922007-01-11 10:58:10 +0100388 atomic_inc(&write_wc->wc_num_reqs);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800389 submit_bio(WRITE, bio);
390
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800391 status = 0;
392bail:
393 return status;
394}
395
396static u32 o2hb_compute_block_crc_le(struct o2hb_region *reg,
397 struct o2hb_disk_heartbeat_block *hb_block)
398{
399 __le32 old_cksum;
400 u32 ret;
401
402 /* We want to compute the block crc with a 0 value in the
403 * hb_cksum field. Save it off here and replace after the
404 * crc. */
405 old_cksum = hb_block->hb_cksum;
406 hb_block->hb_cksum = 0;
407
408 ret = crc32_le(0, (unsigned char *) hb_block, reg->hr_block_bytes);
409
410 hb_block->hb_cksum = old_cksum;
411
412 return ret;
413}
414
415static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
416{
Mark Fasheh70bacbdb2006-03-02 11:10:05 -0800417 mlog(ML_ERROR, "Dump slot information: seq = 0x%llx, node = %u, "
418 "cksum = 0x%x, generation 0x%llx\n",
419 (long long)le64_to_cpu(hb_block->hb_seq),
420 hb_block->hb_node, le32_to_cpu(hb_block->hb_cksum),
421 (long long)le64_to_cpu(hb_block->hb_generation));
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800422}
423
424static int o2hb_verify_crc(struct o2hb_region *reg,
425 struct o2hb_disk_heartbeat_block *hb_block)
426{
427 u32 read, computed;
428
429 read = le32_to_cpu(hb_block->hb_cksum);
430 computed = o2hb_compute_block_crc_le(reg, hb_block);
431
432 return read == computed;
433}
434
435/* We want to make sure that nobody is heartbeating on top of us --
436 * this will help detect an invalid configuration. */
437static int o2hb_check_last_timestamp(struct o2hb_region *reg)
438{
439 int node_num, ret;
440 struct o2hb_disk_slot *slot;
441 struct o2hb_disk_heartbeat_block *hb_block;
442
443 node_num = o2nm_this_node();
444
445 ret = 1;
446 slot = &reg->hr_slots[node_num];
447 /* Don't check on our 1st timestamp */
448 if (slot->ds_last_time) {
449 hb_block = slot->ds_raw_block;
450
451 if (le64_to_cpu(hb_block->hb_seq) != slot->ds_last_time)
452 ret = 0;
453 }
454
455 return ret;
456}
457
458static inline void o2hb_prepare_block(struct o2hb_region *reg,
459 u64 generation)
460{
461 int node_num;
462 u64 cputime;
463 struct o2hb_disk_slot *slot;
464 struct o2hb_disk_heartbeat_block *hb_block;
465
466 node_num = o2nm_this_node();
467 slot = &reg->hr_slots[node_num];
468
469 hb_block = (struct o2hb_disk_heartbeat_block *)slot->ds_raw_block;
470 memset(hb_block, 0, reg->hr_block_bytes);
471 /* TODO: time stuff */
472 cputime = CURRENT_TIME.tv_sec;
473 if (!cputime)
474 cputime = 1;
475
476 hb_block->hb_seq = cpu_to_le64(cputime);
477 hb_block->hb_node = node_num;
478 hb_block->hb_generation = cpu_to_le64(generation);
Mark Fasheh0db638f2006-05-09 15:09:35 -0700479 hb_block->hb_dead_ms = cpu_to_le32(o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800480
481 /* This step must always happen last! */
482 hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
483 hb_block));
484
Mark Fasheh70bacbdb2006-03-02 11:10:05 -0800485 mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x\n",
Mark Fasheh5fdf1e62007-04-27 16:50:03 -0700486 (long long)generation,
Mark Fasheh70bacbdb2006-03-02 11:10:05 -0800487 le32_to_cpu(hb_block->hb_cksum));
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800488}
489
490static void o2hb_fire_callbacks(struct o2hb_callback *hbcall,
491 struct o2nm_node *node,
492 int idx)
493{
494 struct list_head *iter;
495 struct o2hb_callback_func *f;
496
497 list_for_each(iter, &hbcall->list) {
498 f = list_entry(iter, struct o2hb_callback_func, hc_item);
499 mlog(ML_HEARTBEAT, "calling funcs %p\n", f);
500 (f->hc_func)(node, idx, f->hc_data);
501 }
502}
503
504/* Will run the list in order until we process the passed event */
505static void o2hb_run_event_list(struct o2hb_node_event *queued_event)
506{
507 int empty;
508 struct o2hb_callback *hbcall;
509 struct o2hb_node_event *event;
510
511 spin_lock(&o2hb_live_lock);
512 empty = list_empty(&queued_event->hn_item);
513 spin_unlock(&o2hb_live_lock);
514 if (empty)
515 return;
516
517 /* Holding callback sem assures we don't alter the callback
518 * lists when doing this, and serializes ourselves with other
519 * processes wanting callbacks. */
520 down_write(&o2hb_callback_sem);
521
522 spin_lock(&o2hb_live_lock);
523 while (!list_empty(&o2hb_node_events)
524 && !list_empty(&queued_event->hn_item)) {
525 event = list_entry(o2hb_node_events.next,
526 struct o2hb_node_event,
527 hn_item);
528 list_del_init(&event->hn_item);
529 spin_unlock(&o2hb_live_lock);
530
531 mlog(ML_HEARTBEAT, "Node %s event for %d\n",
532 event->hn_event_type == O2HB_NODE_UP_CB ? "UP" : "DOWN",
533 event->hn_node_num);
534
535 hbcall = hbcall_from_type(event->hn_event_type);
536
537 /* We should *never* have gotten on to the list with a
538 * bad type... This isn't something that we should try
539 * to recover from. */
540 BUG_ON(IS_ERR(hbcall));
541
542 o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num);
543
544 spin_lock(&o2hb_live_lock);
545 }
546 spin_unlock(&o2hb_live_lock);
547
548 up_write(&o2hb_callback_sem);
549}
550
551static void o2hb_queue_node_event(struct o2hb_node_event *event,
552 enum o2hb_callback_type type,
553 struct o2nm_node *node,
554 int node_num)
555{
556 assert_spin_locked(&o2hb_live_lock);
557
Sunil Mushran0e105d32010-10-07 17:00:16 -0700558 BUG_ON((!node) && (type != O2HB_NODE_DOWN_CB));
559
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800560 event->hn_event_type = type;
561 event->hn_node = node;
562 event->hn_node_num = node_num;
563
564 mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
565 type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
566
567 list_add_tail(&event->hn_item, &o2hb_node_events);
568}
569
570static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
571{
572 struct o2hb_node_event event =
573 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
574 struct o2nm_node *node;
575
576 node = o2nm_get_node_by_num(slot->ds_node_num);
577 if (!node)
578 return;
579
580 spin_lock(&o2hb_live_lock);
581 if (!list_empty(&slot->ds_live_item)) {
582 mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
583 slot->ds_node_num);
584
585 list_del_init(&slot->ds_live_item);
586
587 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
588 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
589
590 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
591 slot->ds_node_num);
592 }
593 }
594 spin_unlock(&o2hb_live_lock);
595
596 o2hb_run_event_list(&event);
597
598 o2nm_node_put(node);
599}
600
601static int o2hb_check_slot(struct o2hb_region *reg,
602 struct o2hb_disk_slot *slot)
603{
604 int changed = 0, gen_changed = 0;
605 struct o2hb_node_event event =
606 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
607 struct o2nm_node *node;
608 struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
609 u64 cputime;
Mark Fasheh0db638f2006-05-09 15:09:35 -0700610 unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
611 unsigned int slot_dead_ms;
Sunil Mushran0e105d32010-10-07 17:00:16 -0700612 int tmp;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800613
614 memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
615
Sunil Mushran0e105d32010-10-07 17:00:16 -0700616 /*
617 * If a node is no longer configured but is still in the livemap, we
618 * may need to clear that bit from the livemap.
619 */
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800620 node = o2nm_get_node_by_num(slot->ds_node_num);
Sunil Mushran0e105d32010-10-07 17:00:16 -0700621 if (!node) {
622 spin_lock(&o2hb_live_lock);
623 tmp = test_bit(slot->ds_node_num, o2hb_live_node_bitmap);
624 spin_unlock(&o2hb_live_lock);
625 if (!tmp)
626 return 0;
627 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800628
629 if (!o2hb_verify_crc(reg, hb_block)) {
630 /* all paths from here will drop o2hb_live_lock for
631 * us. */
632 spin_lock(&o2hb_live_lock);
633
634 /* Don't print an error on the console in this case -
635 * a freshly formatted heartbeat area will not have a
636 * crc set on it. */
637 if (list_empty(&slot->ds_live_item))
638 goto out;
639
640 /* The node is live but pushed out a bad crc. We
641 * consider it a transient miss but don't populate any
642 * other values as they may be junk. */
643 mlog(ML_ERROR, "Node %d has written a bad crc to %s\n",
644 slot->ds_node_num, reg->hr_dev_name);
645 o2hb_dump_slot(hb_block);
646
647 slot->ds_equal_samples++;
648 goto fire_callbacks;
649 }
650
651 /* we don't care if these wrap.. the state transitions below
652 * clear at the right places */
653 cputime = le64_to_cpu(hb_block->hb_seq);
654 if (slot->ds_last_time != cputime)
655 slot->ds_changed_samples++;
656 else
657 slot->ds_equal_samples++;
658 slot->ds_last_time = cputime;
659
660 /* The node changed heartbeat generations. We assume this to
661 * mean it dropped off but came back before we timed out. We
662 * want to consider it down for the time being but don't want
663 * to lose any changed_samples state we might build up to
664 * considering it live again. */
665 if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
666 gen_changed = 1;
667 slot->ds_equal_samples = 0;
Mark Fasheh70bacbdb2006-03-02 11:10:05 -0800668 mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
669 "to 0x%llx)\n", slot->ds_node_num,
670 (long long)slot->ds_last_generation,
671 (long long)le64_to_cpu(hb_block->hb_generation));
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800672 }
673
674 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
675
Mark Fasheh70bacbdb2006-03-02 11:10:05 -0800676 mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
677 "seq %llu last %llu changed %u equal %u\n",
678 slot->ds_node_num, (long long)slot->ds_last_generation,
679 le32_to_cpu(hb_block->hb_cksum),
Sunil Mushran2bd63212010-01-25 16:57:38 -0800680 (unsigned long long)le64_to_cpu(hb_block->hb_seq),
Mark Fasheh70bacbdb2006-03-02 11:10:05 -0800681 (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800682 slot->ds_equal_samples);
683
684 spin_lock(&o2hb_live_lock);
685
686fire_callbacks:
687 /* dead nodes only come to life after some number of
688 * changes at any time during their dead time */
689 if (list_empty(&slot->ds_live_item) &&
690 slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
Mark Fasheh70bacbdb2006-03-02 11:10:05 -0800691 mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
692 slot->ds_node_num, (long long)slot->ds_last_generation);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800693
Sunil Mushran823a6372010-10-06 17:55:21 -0700694 set_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
695
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800696 /* first on the list generates a callback */
697 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
698 set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
699
700 o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
701 slot->ds_node_num);
702
703 changed = 1;
704 }
705
706 list_add_tail(&slot->ds_live_item,
707 &o2hb_live_slots[slot->ds_node_num]);
708
709 slot->ds_equal_samples = 0;
Mark Fasheh0db638f2006-05-09 15:09:35 -0700710
711 /* We want to be sure that all nodes agree on the
712 * number of milliseconds before a node will be
713 * considered dead. The self-fencing timeout is
714 * computed from this value, and a discrepancy might
715 * result in heartbeat calling a node dead when it
716 * hasn't self-fenced yet. */
717 slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
718 if (slot_dead_ms && slot_dead_ms != dead_ms) {
719 /* TODO: Perhaps we can fail the region here. */
720 mlog(ML_ERROR, "Node %d on device %s has a dead count "
721 "of %u ms, but our count is %u ms.\n"
722 "Please double check your configuration values "
723 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
724 slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
725 dead_ms);
726 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800727 goto out;
728 }
729
730 /* if the list is dead, we're done.. */
731 if (list_empty(&slot->ds_live_item))
732 goto out;
733
734 /* live nodes only go dead after enough consequtive missed
735 * samples.. reset the missed counter whenever we see
736 * activity */
737 if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
738 mlog(ML_HEARTBEAT, "Node %d left my region\n",
739 slot->ds_node_num);
740
Sunil Mushran823a6372010-10-06 17:55:21 -0700741 clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
742
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800743 /* last off the live_slot generates a callback */
744 list_del_init(&slot->ds_live_item);
745 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
746 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
747
Sunil Mushran0e105d32010-10-07 17:00:16 -0700748 /* node can be null */
749 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
750 node, slot->ds_node_num);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800751
752 changed = 1;
753 }
754
755 /* We don't clear this because the node is still
756 * actually writing new blocks. */
757 if (!gen_changed)
758 slot->ds_changed_samples = 0;
759 goto out;
760 }
761 if (slot->ds_changed_samples) {
762 slot->ds_changed_samples = 0;
763 slot->ds_equal_samples = 0;
764 }
765out:
766 spin_unlock(&o2hb_live_lock);
767
768 o2hb_run_event_list(&event);
769
Sunil Mushran0e105d32010-10-07 17:00:16 -0700770 if (node)
771 o2nm_node_put(node);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800772 return changed;
773}
774
775/* This could be faster if we just implmented a find_last_bit, but I
776 * don't think the circumstances warrant it. */
777static int o2hb_highest_node(unsigned long *nodes,
778 int numbits)
779{
780 int highest, node;
781
782 highest = numbits;
783 node = -1;
784 while ((node = find_next_bit(nodes, numbits, node + 1)) != -1) {
785 if (node >= numbits)
786 break;
787
788 highest = node;
789 }
790
791 return highest;
792}
793
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800794static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800795{
796 int i, ret, highest_node, change = 0;
797 unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
Sunil Mushran0e105d32010-10-07 17:00:16 -0700798 unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800799 struct o2hb_bio_wait_ctxt write_wc;
800
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800801 ret = o2nm_configured_node_map(configured_nodes,
802 sizeof(configured_nodes));
803 if (ret) {
804 mlog_errno(ret);
805 return ret;
806 }
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800807
Sunil Mushran0e105d32010-10-07 17:00:16 -0700808 /*
809 * If a node is not configured but is in the livemap, we still need
810 * to read the slot so as to be able to remove it from the livemap.
811 */
812 o2hb_fill_node_map(live_node_bitmap, sizeof(live_node_bitmap));
813 i = -1;
814 while ((i = find_next_bit(live_node_bitmap,
815 O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
816 set_bit(i, configured_nodes);
817 }
818
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800819 highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
820 if (highest_node >= O2NM_MAX_NODES) {
821 mlog(ML_NOTICE, "ocfs2_heartbeat: no configured nodes found!\n");
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800822 return -EINVAL;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800823 }
824
825 /* No sense in reading the slots of nodes that don't exist
826 * yet. Of course, if the node definitions have holes in them
827 * then we're reading an empty slot anyway... Consider this
828 * best-effort. */
829 ret = o2hb_read_slots(reg, highest_node + 1);
830 if (ret < 0) {
831 mlog_errno(ret);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800832 return ret;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800833 }
834
835 /* With an up to date view of the slots, we can check that no
836 * other node has been improperly configured to heartbeat in
837 * our slot. */
838 if (!o2hb_check_last_timestamp(reg))
839 mlog(ML_ERROR, "Device \"%s\": another node is heartbeating "
840 "in our slot!\n", reg->hr_dev_name);
841
842 /* fill in the proper info for our next heartbeat */
843 o2hb_prepare_block(reg, reg->hr_generation);
844
845 /* And fire off the write. Note that we don't wait on this I/O
846 * until later. */
Philipp Reisnerb5592922007-01-11 10:58:10 +0100847 ret = o2hb_issue_node_write(reg, &write_wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800848 if (ret < 0) {
849 mlog_errno(ret);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800850 return ret;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800851 }
852
853 i = -1;
854 while((i = find_next_bit(configured_nodes, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
855
856 change |= o2hb_check_slot(reg, &reg->hr_slots[i]);
857 }
858
859 /*
860 * We have to be sure we've advertised ourselves on disk
861 * before we can go to steady state. This ensures that
862 * people we find in our steady state have seen us.
863 */
864 o2hb_wait_on_io(reg, &write_wc);
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800865 if (write_wc.wc_error) {
866 /* Do not re-arm the write timeout on I/O error - we
867 * can't be sure that the new block ever made it to
868 * disk */
869 mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
870 write_wc.wc_error, reg->hr_dev_name);
871 return write_wc.wc_error;
872 }
873
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800874 o2hb_arm_write_timeout(reg);
875
876 /* let the person who launched us know when things are steady */
877 if (!change && (atomic_read(&reg->hr_steady_iterations) != 0)) {
878 if (atomic_dec_and_test(&reg->hr_steady_iterations))
879 wake_up(&o2hb_steady_queue);
880 }
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800881
882 return 0;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800883}
884
885/* Subtract b from a, storing the result in a. a *must* have a larger
886 * value than b. */
887static void o2hb_tv_subtract(struct timeval *a,
888 struct timeval *b)
889{
890 /* just return 0 when a is after b */
891 if (a->tv_sec < b->tv_sec ||
892 (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) {
893 a->tv_sec = 0;
894 a->tv_usec = 0;
895 return;
896 }
897
898 a->tv_sec -= b->tv_sec;
899 a->tv_usec -= b->tv_usec;
900 while ( a->tv_usec < 0 ) {
901 a->tv_sec--;
902 a->tv_usec += 1000000;
903 }
904}
905
906static unsigned int o2hb_elapsed_msecs(struct timeval *start,
907 struct timeval *end)
908{
909 struct timeval res = *end;
910
911 o2hb_tv_subtract(&res, start);
912
913 return res.tv_sec * 1000 + res.tv_usec / 1000;
914}
915
916/*
917 * we ride the region ref that the region dir holds. before the region
918 * dir is removed and drops it ref it will wait to tear down this
919 * thread.
920 */
921static int o2hb_thread(void *data)
922{
923 int i, ret;
924 struct o2hb_region *reg = data;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800925 struct o2hb_bio_wait_ctxt write_wc;
926 struct timeval before_hb, after_hb;
927 unsigned int elapsed_msec;
928
929 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
930
931 set_user_nice(current, -20);
932
933 while (!kthread_should_stop() && !reg->hr_unclean_stop) {
934 /* We track the time spent inside
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200935 * o2hb_do_disk_heartbeat so that we avoid more than
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800936 * hr_timeout_ms between disk writes. On busy systems
937 * this should result in a heartbeat which is less
938 * likely to time itself out. */
939 do_gettimeofday(&before_hb);
940
Mark Fasheha9e2ae32006-03-24 14:20:17 -0800941 i = 0;
942 do {
943 ret = o2hb_do_disk_heartbeat(reg);
944 } while (ret && ++i < 2);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800945
946 do_gettimeofday(&after_hb);
947 elapsed_msec = o2hb_elapsed_msecs(&before_hb, &after_hb);
948
Tao Mab31d3082009-12-22 10:32:15 +0800949 mlog(ML_HEARTBEAT,
950 "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
Mark Fasheh215c7f92006-02-01 16:42:10 -0800951 before_hb.tv_sec, (unsigned long) before_hb.tv_usec,
952 after_hb.tv_sec, (unsigned long) after_hb.tv_usec,
953 elapsed_msec);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800954
955 if (elapsed_msec < reg->hr_timeout_ms) {
956 /* the kthread api has blocked signals for us so no
957 * need to record the return value. */
958 msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
959 }
960 }
961
962 o2hb_disarm_write_timeout(reg);
963
964 /* unclean stop is only used in very bad situation */
965 for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
966 o2hb_shutdown_slot(&reg->hr_slots[i]);
967
968 /* Explicit down notification - avoid forcing the other nodes
969 * to timeout on this region when we could just as easily
970 * write a clear generation - thus indicating to them that
971 * this node has left this region.
972 *
973 * XXX: Should we skip this on unclean_stop? */
974 o2hb_prepare_block(reg, 0);
Philipp Reisnerb5592922007-01-11 10:58:10 +0100975 ret = o2hb_issue_node_write(reg, &write_wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800976 if (ret == 0) {
977 o2hb_wait_on_io(reg, &write_wc);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -0800978 } else {
979 mlog_errno(ret);
980 }
981
982 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread exiting\n");
983
984 return 0;
985}
986
Sunil Mushran87d3d3f2008-12-17 14:17:42 -0800987#ifdef CONFIG_DEBUG_FS
988static int o2hb_debug_open(struct inode *inode, struct file *file)
989{
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -0700990 struct o2hb_debug_buf *db = inode->i_private;
Sunil Mushran87d3d3f2008-12-17 14:17:42 -0800991 unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
992 char *buf = NULL;
993 int i = -1;
994 int out = 0;
995
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -0700996 /* max_nodes should be the largest bitmap we pass here */
997 BUG_ON(sizeof(map) < db->db_size);
998
Sunil Mushran87d3d3f2008-12-17 14:17:42 -0800999 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1000 if (!buf)
1001 goto bail;
1002
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001003 switch (db->db_type) {
1004 case O2HB_DB_TYPE_LIVENODES:
1005 spin_lock(&o2hb_live_lock);
1006 memcpy(map, db->db_data, db->db_size);
1007 spin_unlock(&o2hb_live_lock);
1008 break;
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001009
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001010 default:
1011 goto done;
1012 }
1013
1014 while ((i = find_next_bit(map, db->db_len, i + 1)) < db->db_len)
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001015 out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
1016 out += snprintf(buf + out, PAGE_SIZE - out, "\n");
1017
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001018done:
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001019 i_size_write(inode, out);
1020
1021 file->private_data = buf;
1022
1023 return 0;
1024bail:
1025 return -ENOMEM;
1026}
1027
1028static int o2hb_debug_release(struct inode *inode, struct file *file)
1029{
1030 kfree(file->private_data);
1031 return 0;
1032}
1033
1034static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1035 size_t nbytes, loff_t *ppos)
1036{
1037 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
1038 i_size_read(file->f_mapping->host));
1039}
1040#else
1041static int o2hb_debug_open(struct inode *inode, struct file *file)
1042{
1043 return 0;
1044}
1045static int o2hb_debug_release(struct inode *inode, struct file *file)
1046{
1047 return 0;
1048}
1049static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1050 size_t nbytes, loff_t *ppos)
1051{
1052 return 0;
1053}
1054#endif /* CONFIG_DEBUG_FS */
1055
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001056static const struct file_operations o2hb_debug_fops = {
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001057 .open = o2hb_debug_open,
1058 .release = o2hb_debug_release,
1059 .read = o2hb_debug_read,
1060 .llseek = generic_file_llseek,
1061};
1062
1063void o2hb_exit(void)
1064{
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001065 kfree(o2hb_db_livenodes);
1066 debugfs_remove(o2hb_debug_livenodes);
1067 debugfs_remove(o2hb_debug_dir);
1068}
1069
1070static struct dentry *o2hb_debug_create(const char *name, struct dentry *dir,
1071 struct o2hb_debug_buf **db, int db_len,
1072 int type, int size, int len, void *data)
1073{
1074 *db = kmalloc(db_len, GFP_KERNEL);
1075 if (!*db)
1076 return NULL;
1077
1078 (*db)->db_type = type;
1079 (*db)->db_size = size;
1080 (*db)->db_len = len;
1081 (*db)->db_data = data;
1082
1083 return debugfs_create_file(name, S_IFREG|S_IRUSR, dir, *db,
1084 &o2hb_debug_fops);
1085}
1086
1087static int o2hb_debug_init(void)
1088{
1089 int ret = -ENOMEM;
1090
1091 o2hb_debug_dir = debugfs_create_dir(O2HB_DEBUG_DIR, NULL);
1092 if (!o2hb_debug_dir) {
1093 mlog_errno(ret);
1094 goto bail;
1095 }
1096
1097 o2hb_debug_livenodes = o2hb_debug_create(O2HB_DEBUG_LIVENODES,
1098 o2hb_debug_dir,
1099 &o2hb_db_livenodes,
1100 sizeof(*o2hb_db_livenodes),
1101 O2HB_DB_TYPE_LIVENODES,
1102 sizeof(o2hb_live_node_bitmap),
1103 O2NM_MAX_NODES,
1104 o2hb_live_node_bitmap);
1105 if (!o2hb_debug_livenodes) {
1106 mlog_errno(ret);
1107 goto bail;
1108 }
1109 ret = 0;
1110bail:
1111 if (ret)
1112 o2hb_exit();
1113
1114 return ret;
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001115}
1116
1117int o2hb_init(void)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001118{
1119 int i;
1120
1121 for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
1122 INIT_LIST_HEAD(&o2hb_callbacks[i].list);
1123
1124 for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
1125 INIT_LIST_HEAD(&o2hb_live_slots[i]);
1126
1127 INIT_LIST_HEAD(&o2hb_node_events);
1128
1129 memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
Sunil Mushran87d3d3f2008-12-17 14:17:42 -08001130
Sunil Mushran8ca8b0b2010-10-07 17:01:27 -07001131 return o2hb_debug_init();
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001132}
1133
1134/* if we're already in a callback then we're already serialized by the sem */
1135static void o2hb_fill_node_map_from_callback(unsigned long *map,
1136 unsigned bytes)
1137{
1138 BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
1139
1140 memcpy(map, &o2hb_live_node_bitmap, bytes);
1141}
1142
1143/*
1144 * get a map of all nodes that are heartbeating in any regions
1145 */
1146void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
1147{
1148 /* callers want to serialize this map and callbacks so that they
1149 * can trust that they don't miss nodes coming to the party */
1150 down_read(&o2hb_callback_sem);
1151 spin_lock(&o2hb_live_lock);
1152 o2hb_fill_node_map_from_callback(map, bytes);
1153 spin_unlock(&o2hb_live_lock);
1154 up_read(&o2hb_callback_sem);
1155}
1156EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
1157
1158/*
1159 * heartbeat configfs bits. The heartbeat set is a default set under
1160 * the cluster set in nodemanager.c.
1161 */
1162
1163static struct o2hb_region *to_o2hb_region(struct config_item *item)
1164{
1165 return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
1166}
1167
1168/* drop_item only drops its ref after killing the thread, nothing should
1169 * be using the region anymore. this has to clean up any state that
1170 * attributes might have built up. */
1171static void o2hb_region_release(struct config_item *item)
1172{
1173 int i;
1174 struct page *page;
1175 struct o2hb_region *reg = to_o2hb_region(item);
1176
1177 if (reg->hr_tmp_block)
1178 kfree(reg->hr_tmp_block);
1179
1180 if (reg->hr_slot_data) {
1181 for (i = 0; i < reg->hr_num_pages; i++) {
1182 page = reg->hr_slot_data[i];
1183 if (page)
1184 __free_page(page);
1185 }
1186 kfree(reg->hr_slot_data);
1187 }
1188
1189 if (reg->hr_bdev)
Al Viro9a1c3542008-02-22 20:40:24 -05001190 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001191
1192 if (reg->hr_slots)
1193 kfree(reg->hr_slots);
1194
1195 spin_lock(&o2hb_live_lock);
1196 list_del(&reg->hr_all_item);
1197 spin_unlock(&o2hb_live_lock);
1198
1199 kfree(reg);
1200}
1201
1202static int o2hb_read_block_input(struct o2hb_region *reg,
1203 const char *page,
1204 size_t count,
1205 unsigned long *ret_bytes,
1206 unsigned int *ret_bits)
1207{
1208 unsigned long bytes;
1209 char *p = (char *)page;
1210
1211 bytes = simple_strtoul(p, &p, 0);
1212 if (!p || (*p && (*p != '\n')))
1213 return -EINVAL;
1214
1215 /* Heartbeat and fs min / max block sizes are the same. */
1216 if (bytes > 4096 || bytes < 512)
1217 return -ERANGE;
1218 if (hweight16(bytes) != 1)
1219 return -EINVAL;
1220
1221 if (ret_bytes)
1222 *ret_bytes = bytes;
1223 if (ret_bits)
1224 *ret_bits = ffs(bytes) - 1;
1225
1226 return 0;
1227}
1228
1229static ssize_t o2hb_region_block_bytes_read(struct o2hb_region *reg,
1230 char *page)
1231{
1232 return sprintf(page, "%u\n", reg->hr_block_bytes);
1233}
1234
1235static ssize_t o2hb_region_block_bytes_write(struct o2hb_region *reg,
1236 const char *page,
1237 size_t count)
1238{
1239 int status;
1240 unsigned long block_bytes;
1241 unsigned int block_bits;
1242
1243 if (reg->hr_bdev)
1244 return -EINVAL;
1245
1246 status = o2hb_read_block_input(reg, page, count,
1247 &block_bytes, &block_bits);
1248 if (status)
1249 return status;
1250
1251 reg->hr_block_bytes = (unsigned int)block_bytes;
1252 reg->hr_block_bits = block_bits;
1253
1254 return count;
1255}
1256
1257static ssize_t o2hb_region_start_block_read(struct o2hb_region *reg,
1258 char *page)
1259{
1260 return sprintf(page, "%llu\n", reg->hr_start_block);
1261}
1262
1263static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
1264 const char *page,
1265 size_t count)
1266{
1267 unsigned long long tmp;
1268 char *p = (char *)page;
1269
1270 if (reg->hr_bdev)
1271 return -EINVAL;
1272
1273 tmp = simple_strtoull(p, &p, 0);
1274 if (!p || (*p && (*p != '\n')))
1275 return -EINVAL;
1276
1277 reg->hr_start_block = tmp;
1278
1279 return count;
1280}
1281
1282static ssize_t o2hb_region_blocks_read(struct o2hb_region *reg,
1283 char *page)
1284{
1285 return sprintf(page, "%d\n", reg->hr_blocks);
1286}
1287
1288static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
1289 const char *page,
1290 size_t count)
1291{
1292 unsigned long tmp;
1293 char *p = (char *)page;
1294
1295 if (reg->hr_bdev)
1296 return -EINVAL;
1297
1298 tmp = simple_strtoul(p, &p, 0);
1299 if (!p || (*p && (*p != '\n')))
1300 return -EINVAL;
1301
1302 if (tmp > O2NM_MAX_NODES || tmp == 0)
1303 return -ERANGE;
1304
1305 reg->hr_blocks = (unsigned int)tmp;
1306
1307 return count;
1308}
1309
1310static ssize_t o2hb_region_dev_read(struct o2hb_region *reg,
1311 char *page)
1312{
1313 unsigned int ret = 0;
1314
1315 if (reg->hr_bdev)
1316 ret = sprintf(page, "%s\n", reg->hr_dev_name);
1317
1318 return ret;
1319}
1320
1321static void o2hb_init_region_params(struct o2hb_region *reg)
1322{
1323 reg->hr_slots_per_page = PAGE_CACHE_SIZE >> reg->hr_block_bits;
1324 reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
1325
1326 mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
1327 reg->hr_start_block, reg->hr_blocks);
1328 mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
1329 reg->hr_block_bytes, reg->hr_block_bits);
1330 mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
1331 mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
1332}
1333
1334static int o2hb_map_slot_data(struct o2hb_region *reg)
1335{
1336 int i, j;
1337 unsigned int last_slot;
1338 unsigned int spp = reg->hr_slots_per_page;
1339 struct page *page;
1340 char *raw;
1341 struct o2hb_disk_slot *slot;
1342
1343 reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
1344 if (reg->hr_tmp_block == NULL) {
1345 mlog_errno(-ENOMEM);
1346 return -ENOMEM;
1347 }
1348
1349 reg->hr_slots = kcalloc(reg->hr_blocks,
1350 sizeof(struct o2hb_disk_slot), GFP_KERNEL);
1351 if (reg->hr_slots == NULL) {
1352 mlog_errno(-ENOMEM);
1353 return -ENOMEM;
1354 }
1355
1356 for(i = 0; i < reg->hr_blocks; i++) {
1357 slot = &reg->hr_slots[i];
1358 slot->ds_node_num = i;
1359 INIT_LIST_HEAD(&slot->ds_live_item);
1360 slot->ds_raw_block = NULL;
1361 }
1362
1363 reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
1364 mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
1365 "at %u blocks per page\n",
1366 reg->hr_num_pages, reg->hr_blocks, spp);
1367
1368 reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
1369 GFP_KERNEL);
1370 if (!reg->hr_slot_data) {
1371 mlog_errno(-ENOMEM);
1372 return -ENOMEM;
1373 }
1374
1375 for(i = 0; i < reg->hr_num_pages; i++) {
1376 page = alloc_page(GFP_KERNEL);
1377 if (!page) {
1378 mlog_errno(-ENOMEM);
1379 return -ENOMEM;
1380 }
1381
1382 reg->hr_slot_data[i] = page;
1383
1384 last_slot = i * spp;
1385 raw = page_address(page);
1386 for (j = 0;
1387 (j < spp) && ((j + last_slot) < reg->hr_blocks);
1388 j++) {
1389 BUG_ON((j + last_slot) >= reg->hr_blocks);
1390
1391 slot = &reg->hr_slots[j + last_slot];
1392 slot->ds_raw_block =
1393 (struct o2hb_disk_heartbeat_block *) raw;
1394
1395 raw += reg->hr_block_bytes;
1396 }
1397 }
1398
1399 return 0;
1400}
1401
1402/* Read in all the slots available and populate the tracking
1403 * structures so that we can start with a baseline idea of what's
1404 * there. */
1405static int o2hb_populate_slot_data(struct o2hb_region *reg)
1406{
1407 int ret, i;
1408 struct o2hb_disk_slot *slot;
1409 struct o2hb_disk_heartbeat_block *hb_block;
1410
1411 mlog_entry_void();
1412
1413 ret = o2hb_read_slots(reg, reg->hr_blocks);
1414 if (ret) {
1415 mlog_errno(ret);
1416 goto out;
1417 }
1418
1419 /* We only want to get an idea of the values initially in each
1420 * slot, so we do no verification - o2hb_check_slot will
1421 * actually determine if each configured slot is valid and
1422 * whether any values have changed. */
1423 for(i = 0; i < reg->hr_blocks; i++) {
1424 slot = &reg->hr_slots[i];
1425 hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
1426
1427 /* Only fill the values that o2hb_check_slot uses to
1428 * determine changing slots */
1429 slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
1430 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
1431 }
1432
1433out:
1434 mlog_exit(ret);
1435 return ret;
1436}
1437
1438/* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1439static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
1440 const char *page,
1441 size_t count)
1442{
Joel Beckere6c352d2007-02-03 03:04:20 -08001443 struct task_struct *hb_task;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001444 long fd;
1445 int sectsize;
1446 char *p = (char *)page;
1447 struct file *filp = NULL;
1448 struct inode *inode = NULL;
1449 ssize_t ret = -EINVAL;
1450
1451 if (reg->hr_bdev)
1452 goto out;
1453
1454 /* We can't heartbeat without having had our node number
1455 * configured yet. */
1456 if (o2nm_this_node() == O2NM_MAX_NODES)
1457 goto out;
1458
1459 fd = simple_strtol(p, &p, 0);
1460 if (!p || (*p && (*p != '\n')))
1461 goto out;
1462
1463 if (fd < 0 || fd >= INT_MAX)
1464 goto out;
1465
1466 filp = fget(fd);
1467 if (filp == NULL)
1468 goto out;
1469
1470 if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
1471 reg->hr_block_bytes == 0)
1472 goto out;
1473
1474 inode = igrab(filp->f_mapping->host);
1475 if (inode == NULL)
1476 goto out;
1477
1478 if (!S_ISBLK(inode->i_mode))
1479 goto out;
1480
1481 reg->hr_bdev = I_BDEV(filp->f_mapping->host);
Al Viro572c4892007-10-08 13:24:05 -04001482 ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001483 if (ret) {
1484 reg->hr_bdev = NULL;
1485 goto out;
1486 }
1487 inode = NULL;
1488
1489 bdevname(reg->hr_bdev, reg->hr_dev_name);
1490
Martin K. Petersene1defc42009-05-22 17:17:49 -04001491 sectsize = bdev_logical_block_size(reg->hr_bdev);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001492 if (sectsize != reg->hr_block_bytes) {
1493 mlog(ML_ERROR,
1494 "blocksize %u incorrect for device, expected %d",
1495 reg->hr_block_bytes, sectsize);
1496 ret = -EINVAL;
1497 goto out;
1498 }
1499
1500 o2hb_init_region_params(reg);
1501
1502 /* Generation of zero is invalid */
1503 do {
1504 get_random_bytes(&reg->hr_generation,
1505 sizeof(reg->hr_generation));
1506 } while (reg->hr_generation == 0);
1507
1508 ret = o2hb_map_slot_data(reg);
1509 if (ret) {
1510 mlog_errno(ret);
1511 goto out;
1512 }
1513
1514 ret = o2hb_populate_slot_data(reg);
1515 if (ret) {
1516 mlog_errno(ret);
1517 goto out;
1518 }
1519
David Howellsc4028952006-11-22 14:57:56 +00001520 INIT_DELAYED_WORK(&reg->hr_write_timeout_work, o2hb_write_timeout);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001521
1522 /*
1523 * A node is considered live after it has beat LIVE_THRESHOLD
1524 * times. We're not steady until we've given them a chance
1525 * _after_ our first read.
1526 */
1527 atomic_set(&reg->hr_steady_iterations, O2HB_LIVE_THRESHOLD + 1);
1528
Joel Beckere6c352d2007-02-03 03:04:20 -08001529 hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
1530 reg->hr_item.ci_name);
1531 if (IS_ERR(hb_task)) {
1532 ret = PTR_ERR(hb_task);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001533 mlog_errno(ret);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001534 goto out;
1535 }
1536
Joel Beckere6c352d2007-02-03 03:04:20 -08001537 spin_lock(&o2hb_live_lock);
1538 reg->hr_task = hb_task;
1539 spin_unlock(&o2hb_live_lock);
1540
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001541 ret = wait_event_interruptible(o2hb_steady_queue,
1542 atomic_read(&reg->hr_steady_iterations) == 0);
1543 if (ret) {
Joel Beckere6df3a62007-02-06 15:45:39 -08001544 /* We got interrupted (hello ptrace!). Clean up */
Joel Beckere6c352d2007-02-03 03:04:20 -08001545 spin_lock(&o2hb_live_lock);
1546 hb_task = reg->hr_task;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001547 reg->hr_task = NULL;
Joel Beckere6c352d2007-02-03 03:04:20 -08001548 spin_unlock(&o2hb_live_lock);
1549
1550 if (hb_task)
1551 kthread_stop(hb_task);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001552 goto out;
1553 }
1554
Joel Beckere6df3a62007-02-06 15:45:39 -08001555 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1556 spin_lock(&o2hb_live_lock);
1557 hb_task = reg->hr_task;
1558 spin_unlock(&o2hb_live_lock);
1559
1560 if (hb_task)
1561 ret = count;
1562 else
1563 ret = -EIO;
1564
Sunil Mushran18c50cb2010-10-06 18:26:59 -07001565 if (hb_task && o2hb_global_heartbeat_active())
1566 printk(KERN_NOTICE "o2hb: Heartbeat started on region %s\n",
1567 config_item_name(&reg->hr_item));
1568
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001569out:
1570 if (filp)
1571 fput(filp);
1572 if (inode)
1573 iput(inode);
1574 if (ret < 0) {
1575 if (reg->hr_bdev) {
Al Viro9a1c3542008-02-22 20:40:24 -05001576 blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001577 reg->hr_bdev = NULL;
1578 }
1579 }
1580 return ret;
1581}
1582
Zhen Wei92efc152006-12-08 00:48:17 -07001583static ssize_t o2hb_region_pid_read(struct o2hb_region *reg,
1584 char *page)
1585{
Joel Beckere6c352d2007-02-03 03:04:20 -08001586 pid_t pid = 0;
1587
1588 spin_lock(&o2hb_live_lock);
1589 if (reg->hr_task)
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001590 pid = task_pid_nr(reg->hr_task);
Joel Beckere6c352d2007-02-03 03:04:20 -08001591 spin_unlock(&o2hb_live_lock);
1592
1593 if (!pid)
Zhen Wei92efc152006-12-08 00:48:17 -07001594 return 0;
1595
Joel Beckere6c352d2007-02-03 03:04:20 -08001596 return sprintf(page, "%u\n", pid);
Zhen Wei92efc152006-12-08 00:48:17 -07001597}
1598
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001599struct o2hb_region_attribute {
1600 struct configfs_attribute attr;
1601 ssize_t (*show)(struct o2hb_region *, char *);
1602 ssize_t (*store)(struct o2hb_region *, const char *, size_t);
1603};
1604
1605static struct o2hb_region_attribute o2hb_region_attr_block_bytes = {
1606 .attr = { .ca_owner = THIS_MODULE,
1607 .ca_name = "block_bytes",
1608 .ca_mode = S_IRUGO | S_IWUSR },
1609 .show = o2hb_region_block_bytes_read,
1610 .store = o2hb_region_block_bytes_write,
1611};
1612
1613static struct o2hb_region_attribute o2hb_region_attr_start_block = {
1614 .attr = { .ca_owner = THIS_MODULE,
1615 .ca_name = "start_block",
1616 .ca_mode = S_IRUGO | S_IWUSR },
1617 .show = o2hb_region_start_block_read,
1618 .store = o2hb_region_start_block_write,
1619};
1620
1621static struct o2hb_region_attribute o2hb_region_attr_blocks = {
1622 .attr = { .ca_owner = THIS_MODULE,
1623 .ca_name = "blocks",
1624 .ca_mode = S_IRUGO | S_IWUSR },
1625 .show = o2hb_region_blocks_read,
1626 .store = o2hb_region_blocks_write,
1627};
1628
1629static struct o2hb_region_attribute o2hb_region_attr_dev = {
1630 .attr = { .ca_owner = THIS_MODULE,
1631 .ca_name = "dev",
1632 .ca_mode = S_IRUGO | S_IWUSR },
1633 .show = o2hb_region_dev_read,
1634 .store = o2hb_region_dev_write,
1635};
1636
Zhen Wei92efc152006-12-08 00:48:17 -07001637static struct o2hb_region_attribute o2hb_region_attr_pid = {
1638 .attr = { .ca_owner = THIS_MODULE,
1639 .ca_name = "pid",
1640 .ca_mode = S_IRUGO | S_IRUSR },
1641 .show = o2hb_region_pid_read,
1642};
1643
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001644static struct configfs_attribute *o2hb_region_attrs[] = {
1645 &o2hb_region_attr_block_bytes.attr,
1646 &o2hb_region_attr_start_block.attr,
1647 &o2hb_region_attr_blocks.attr,
1648 &o2hb_region_attr_dev.attr,
Zhen Wei92efc152006-12-08 00:48:17 -07001649 &o2hb_region_attr_pid.attr,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001650 NULL,
1651};
1652
1653static ssize_t o2hb_region_show(struct config_item *item,
1654 struct configfs_attribute *attr,
1655 char *page)
1656{
1657 struct o2hb_region *reg = to_o2hb_region(item);
1658 struct o2hb_region_attribute *o2hb_region_attr =
1659 container_of(attr, struct o2hb_region_attribute, attr);
1660 ssize_t ret = 0;
1661
1662 if (o2hb_region_attr->show)
1663 ret = o2hb_region_attr->show(reg, page);
1664 return ret;
1665}
1666
1667static ssize_t o2hb_region_store(struct config_item *item,
1668 struct configfs_attribute *attr,
1669 const char *page, size_t count)
1670{
1671 struct o2hb_region *reg = to_o2hb_region(item);
1672 struct o2hb_region_attribute *o2hb_region_attr =
1673 container_of(attr, struct o2hb_region_attribute, attr);
1674 ssize_t ret = -EINVAL;
1675
1676 if (o2hb_region_attr->store)
1677 ret = o2hb_region_attr->store(reg, page, count);
1678 return ret;
1679}
1680
1681static struct configfs_item_operations o2hb_region_item_ops = {
1682 .release = o2hb_region_release,
1683 .show_attribute = o2hb_region_show,
1684 .store_attribute = o2hb_region_store,
1685};
1686
1687static struct config_item_type o2hb_region_type = {
1688 .ct_item_ops = &o2hb_region_item_ops,
1689 .ct_attrs = o2hb_region_attrs,
1690 .ct_owner = THIS_MODULE,
1691};
1692
1693/* heartbeat set */
1694
1695struct o2hb_heartbeat_group {
1696 struct config_group hs_group;
1697 /* some stuff? */
1698};
1699
1700static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
1701{
1702 return group ?
1703 container_of(group, struct o2hb_heartbeat_group, hs_group)
1704 : NULL;
1705}
1706
Joel Beckerf89ab862008-07-17 14:53:48 -07001707static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
1708 const char *name)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001709{
1710 struct o2hb_region *reg = NULL;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001711
Robert P. J. Daycd861282006-12-13 00:34:52 -08001712 reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
Joel Beckerf89ab862008-07-17 14:53:48 -07001713 if (reg == NULL)
Joel Beckera6795e92008-07-17 15:21:29 -07001714 return ERR_PTR(-ENOMEM);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001715
Sunil Mushranb3c85c42010-10-07 14:31:06 -07001716 if (strlen(name) > O2HB_MAX_REGION_NAME_LEN)
1717 return ERR_PTR(-ENAMETOOLONG);
1718
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001719 config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
1720
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001721 spin_lock(&o2hb_live_lock);
1722 list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
1723 spin_unlock(&o2hb_live_lock);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001724
Joel Beckera6795e92008-07-17 15:21:29 -07001725 return &reg->hr_item;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001726}
1727
1728static void o2hb_heartbeat_group_drop_item(struct config_group *group,
1729 struct config_item *item)
1730{
Joel Beckere6c352d2007-02-03 03:04:20 -08001731 struct task_struct *hb_task;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001732 struct o2hb_region *reg = to_o2hb_region(item);
1733
1734 /* stop the thread when the user removes the region dir */
Joel Beckere6c352d2007-02-03 03:04:20 -08001735 spin_lock(&o2hb_live_lock);
1736 hb_task = reg->hr_task;
1737 reg->hr_task = NULL;
1738 spin_unlock(&o2hb_live_lock);
1739
1740 if (hb_task)
1741 kthread_stop(hb_task);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001742
Joel Beckere6df3a62007-02-06 15:45:39 -08001743 /*
1744 * If we're racing a dev_write(), we need to wake them. They will
1745 * check reg->hr_task
1746 */
1747 if (atomic_read(&reg->hr_steady_iterations) != 0) {
1748 atomic_set(&reg->hr_steady_iterations, 0);
1749 wake_up(&o2hb_steady_queue);
1750 }
1751
Sunil Mushran18c50cb2010-10-06 18:26:59 -07001752 if (o2hb_global_heartbeat_active())
1753 printk(KERN_NOTICE "o2hb: Heartbeat stopped on region %s\n",
1754 config_item_name(&reg->hr_item));
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001755 config_item_put(item);
1756}
1757
1758struct o2hb_heartbeat_group_attribute {
1759 struct configfs_attribute attr;
1760 ssize_t (*show)(struct o2hb_heartbeat_group *, char *);
1761 ssize_t (*store)(struct o2hb_heartbeat_group *, const char *, size_t);
1762};
1763
1764static ssize_t o2hb_heartbeat_group_show(struct config_item *item,
1765 struct configfs_attribute *attr,
1766 char *page)
1767{
1768 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1769 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1770 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1771 ssize_t ret = 0;
1772
1773 if (o2hb_heartbeat_group_attr->show)
1774 ret = o2hb_heartbeat_group_attr->show(reg, page);
1775 return ret;
1776}
1777
1778static ssize_t o2hb_heartbeat_group_store(struct config_item *item,
1779 struct configfs_attribute *attr,
1780 const char *page, size_t count)
1781{
1782 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1783 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1784 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1785 ssize_t ret = -EINVAL;
1786
1787 if (o2hb_heartbeat_group_attr->store)
1788 ret = o2hb_heartbeat_group_attr->store(reg, page, count);
1789 return ret;
1790}
1791
1792static ssize_t o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group *group,
1793 char *page)
1794{
1795 return sprintf(page, "%u\n", o2hb_dead_threshold);
1796}
1797
1798static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group *group,
1799 const char *page,
1800 size_t count)
1801{
1802 unsigned long tmp;
1803 char *p = (char *)page;
1804
1805 tmp = simple_strtoul(p, &p, 10);
1806 if (!p || (*p && (*p != '\n')))
1807 return -EINVAL;
1808
1809 /* this will validate ranges for us. */
1810 o2hb_dead_threshold_set((unsigned int) tmp);
1811
1812 return count;
1813}
1814
Sunil Mushran54b51872010-10-07 15:26:08 -07001815static
1816ssize_t o2hb_heartbeat_group_mode_show(struct o2hb_heartbeat_group *group,
1817 char *page)
1818{
1819 return sprintf(page, "%s\n",
1820 o2hb_heartbeat_mode_desc[o2hb_heartbeat_mode]);
1821}
1822
1823static
1824ssize_t o2hb_heartbeat_group_mode_store(struct o2hb_heartbeat_group *group,
1825 const char *page, size_t count)
1826{
1827 unsigned int i;
1828 int ret;
1829 size_t len;
1830
1831 len = (page[count - 1] == '\n') ? count - 1 : count;
1832 if (!len)
1833 return -EINVAL;
1834
1835 for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) {
1836 if (strnicmp(page, o2hb_heartbeat_mode_desc[i], len))
1837 continue;
1838
1839 ret = o2hb_global_hearbeat_mode_set(i);
1840 if (!ret)
Sunil Mushran18c50cb2010-10-06 18:26:59 -07001841 printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
Sunil Mushran54b51872010-10-07 15:26:08 -07001842 o2hb_heartbeat_mode_desc[i]);
1843 return count;
1844 }
1845
1846 return -EINVAL;
1847
1848}
1849
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001850static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold = {
1851 .attr = { .ca_owner = THIS_MODULE,
1852 .ca_name = "dead_threshold",
1853 .ca_mode = S_IRUGO | S_IWUSR },
1854 .show = o2hb_heartbeat_group_threshold_show,
1855 .store = o2hb_heartbeat_group_threshold_store,
1856};
1857
Sunil Mushran54b51872010-10-07 15:26:08 -07001858static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_mode = {
1859 .attr = { .ca_owner = THIS_MODULE,
1860 .ca_name = "mode",
1861 .ca_mode = S_IRUGO | S_IWUSR },
1862 .show = o2hb_heartbeat_group_mode_show,
1863 .store = o2hb_heartbeat_group_mode_store,
1864};
1865
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001866static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
1867 &o2hb_heartbeat_group_attr_threshold.attr,
Sunil Mushran54b51872010-10-07 15:26:08 -07001868 &o2hb_heartbeat_group_attr_mode.attr,
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001869 NULL,
1870};
1871
1872static struct configfs_item_operations o2hb_hearbeat_group_item_ops = {
1873 .show_attribute = o2hb_heartbeat_group_show,
1874 .store_attribute = o2hb_heartbeat_group_store,
1875};
1876
1877static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
1878 .make_item = o2hb_heartbeat_group_make_item,
1879 .drop_item = o2hb_heartbeat_group_drop_item,
1880};
1881
1882static struct config_item_type o2hb_heartbeat_group_type = {
1883 .ct_group_ops = &o2hb_heartbeat_group_group_ops,
1884 .ct_item_ops = &o2hb_hearbeat_group_item_ops,
1885 .ct_attrs = o2hb_heartbeat_group_attrs,
1886 .ct_owner = THIS_MODULE,
1887};
1888
1889/* this is just here to avoid touching group in heartbeat.h which the
1890 * entire damn world #includes */
1891struct config_group *o2hb_alloc_hb_set(void)
1892{
1893 struct o2hb_heartbeat_group *hs = NULL;
1894 struct config_group *ret = NULL;
1895
Robert P. J. Daycd861282006-12-13 00:34:52 -08001896 hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08001897 if (hs == NULL)
1898 goto out;
1899
1900 config_group_init_type_name(&hs->hs_group, "heartbeat",
1901 &o2hb_heartbeat_group_type);
1902
1903 ret = &hs->hs_group;
1904out:
1905 if (ret == NULL)
1906 kfree(hs);
1907 return ret;
1908}
1909
1910void o2hb_free_hb_set(struct config_group *group)
1911{
1912 struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
1913 kfree(hs);
1914}
1915
1916/* hb callback registration and issueing */
1917
1918static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
1919{
1920 if (type == O2HB_NUM_CB)
1921 return ERR_PTR(-EINVAL);
1922
1923 return &o2hb_callbacks[type];
1924}
1925
1926void o2hb_setup_callback(struct o2hb_callback_func *hc,
1927 enum o2hb_callback_type type,
1928 o2hb_cb_func *func,
1929 void *data,
1930 int priority)
1931{
1932 INIT_LIST_HEAD(&hc->hc_item);
1933 hc->hc_func = func;
1934 hc->hc_data = data;
1935 hc->hc_priority = priority;
1936 hc->hc_type = type;
1937 hc->hc_magic = O2HB_CB_MAGIC;
1938}
1939EXPORT_SYMBOL_GPL(o2hb_setup_callback);
1940
Joel Becker14829422007-06-14 21:40:49 -07001941static struct o2hb_region *o2hb_find_region(const char *region_uuid)
1942{
1943 struct o2hb_region *p, *reg = NULL;
1944
1945 assert_spin_locked(&o2hb_live_lock);
1946
1947 list_for_each_entry(p, &o2hb_all_regions, hr_all_item) {
1948 if (!strcmp(region_uuid, config_item_name(&p->hr_item))) {
1949 reg = p;
1950 break;
1951 }
1952 }
1953
1954 return reg;
1955}
1956
1957static int o2hb_region_get(const char *region_uuid)
1958{
1959 int ret = 0;
1960 struct o2hb_region *reg;
1961
1962 spin_lock(&o2hb_live_lock);
1963
1964 reg = o2hb_find_region(region_uuid);
1965 if (!reg)
1966 ret = -ENOENT;
1967 spin_unlock(&o2hb_live_lock);
1968
Joel Becker16c6a4f2007-06-19 11:34:03 -07001969 if (ret)
1970 goto out;
Joel Becker14829422007-06-14 21:40:49 -07001971
Joel Becker16c6a4f2007-06-19 11:34:03 -07001972 ret = o2nm_depend_this_node();
1973 if (ret)
1974 goto out;
1975
1976 ret = o2nm_depend_item(&reg->hr_item);
1977 if (ret)
1978 o2nm_undepend_this_node();
1979
1980out:
Joel Becker14829422007-06-14 21:40:49 -07001981 return ret;
1982}
1983
1984static void o2hb_region_put(const char *region_uuid)
1985{
1986 struct o2hb_region *reg;
1987
1988 spin_lock(&o2hb_live_lock);
1989
1990 reg = o2hb_find_region(region_uuid);
1991
1992 spin_unlock(&o2hb_live_lock);
1993
Joel Becker16c6a4f2007-06-19 11:34:03 -07001994 if (reg) {
Joel Becker14829422007-06-14 21:40:49 -07001995 o2nm_undepend_item(&reg->hr_item);
Joel Becker16c6a4f2007-06-19 11:34:03 -07001996 o2nm_undepend_this_node();
1997 }
Joel Becker14829422007-06-14 21:40:49 -07001998}
1999
2000int o2hb_register_callback(const char *region_uuid,
2001 struct o2hb_callback_func *hc)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002002{
2003 struct o2hb_callback_func *tmp;
2004 struct list_head *iter;
2005 struct o2hb_callback *hbcall;
2006 int ret;
2007
2008 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2009 BUG_ON(!list_empty(&hc->hc_item));
2010
2011 hbcall = hbcall_from_type(hc->hc_type);
2012 if (IS_ERR(hbcall)) {
2013 ret = PTR_ERR(hbcall);
2014 goto out;
2015 }
2016
Joel Becker14829422007-06-14 21:40:49 -07002017 if (region_uuid) {
2018 ret = o2hb_region_get(region_uuid);
2019 if (ret)
2020 goto out;
2021 }
2022
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002023 down_write(&o2hb_callback_sem);
2024
2025 list_for_each(iter, &hbcall->list) {
2026 tmp = list_entry(iter, struct o2hb_callback_func, hc_item);
2027 if (hc->hc_priority < tmp->hc_priority) {
2028 list_add_tail(&hc->hc_item, iter);
2029 break;
2030 }
2031 }
2032 if (list_empty(&hc->hc_item))
2033 list_add_tail(&hc->hc_item, &hbcall->list);
2034
2035 up_write(&o2hb_callback_sem);
2036 ret = 0;
2037out:
2038 mlog(ML_HEARTBEAT, "returning %d on behalf of %p for funcs %p\n",
2039 ret, __builtin_return_address(0), hc);
2040 return ret;
2041}
2042EXPORT_SYMBOL_GPL(o2hb_register_callback);
2043
Joel Becker14829422007-06-14 21:40:49 -07002044void o2hb_unregister_callback(const char *region_uuid,
2045 struct o2hb_callback_func *hc)
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002046{
2047 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2048
2049 mlog(ML_HEARTBEAT, "on behalf of %p for funcs %p\n",
2050 __builtin_return_address(0), hc);
2051
Joel Becker14829422007-06-14 21:40:49 -07002052 /* XXX Can this happen _with_ a region reference? */
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002053 if (list_empty(&hc->hc_item))
Joel Beckerc24f72c2007-02-03 03:14:30 -08002054 return;
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002055
Joel Becker14829422007-06-14 21:40:49 -07002056 if (region_uuid)
2057 o2hb_region_put(region_uuid);
2058
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002059 down_write(&o2hb_callback_sem);
2060
2061 list_del_init(&hc->hc_item);
2062
2063 up_write(&o2hb_callback_sem);
Mark Fasheha7f6a5f2005-12-15 14:31:23 -08002064}
2065EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
2066
2067int o2hb_check_node_heartbeating(u8 node_num)
2068{
2069 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2070
2071 o2hb_fill_node_map(testing_map, sizeof(testing_map));
2072 if (!test_bit(node_num, testing_map)) {
2073 mlog(ML_HEARTBEAT,
2074 "node (%u) does not have heartbeating enabled.\n",
2075 node_num);
2076 return 0;
2077 }
2078
2079 return 1;
2080}
2081EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating);
2082
2083int o2hb_check_node_heartbeating_from_callback(u8 node_num)
2084{
2085 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2086
2087 o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
2088 if (!test_bit(node_num, testing_map)) {
2089 mlog(ML_HEARTBEAT,
2090 "node (%u) does not have heartbeating enabled.\n",
2091 node_num);
2092 return 0;
2093 }
2094
2095 return 1;
2096}
2097EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
2098
2099/* Makes sure our local node is configured with a node number, and is
2100 * heartbeating. */
2101int o2hb_check_local_node_heartbeating(void)
2102{
2103 u8 node_num;
2104
2105 /* if this node was set then we have networking */
2106 node_num = o2nm_this_node();
2107 if (node_num == O2NM_MAX_NODES) {
2108 mlog(ML_HEARTBEAT, "this node has not been configured.\n");
2109 return 0;
2110 }
2111
2112 return o2hb_check_node_heartbeating(node_num);
2113}
2114EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating);
2115
2116/*
2117 * this is just a hack until we get the plumbing which flips file systems
2118 * read only and drops the hb ref instead of killing the node dead.
2119 */
2120void o2hb_stop_all_regions(void)
2121{
2122 struct o2hb_region *reg;
2123
2124 mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
2125
2126 spin_lock(&o2hb_live_lock);
2127
2128 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
2129 reg->hr_unclean_stop = 1;
2130
2131 spin_unlock(&o2hb_live_lock);
2132}
2133EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);
Sunil Mushranb3c85c42010-10-07 14:31:06 -07002134
2135int o2hb_get_all_regions(char *region_uuids, u8 max_regions)
2136{
2137 struct o2hb_region *reg;
2138 int numregs = 0;
2139 char *p;
2140
2141 spin_lock(&o2hb_live_lock);
2142
2143 p = region_uuids;
2144 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2145 mlog(0, "Region: %s\n", config_item_name(&reg->hr_item));
2146 if (numregs < max_regions) {
2147 memcpy(p, config_item_name(&reg->hr_item),
2148 O2HB_MAX_REGION_NAME_LEN);
2149 p += O2HB_MAX_REGION_NAME_LEN;
2150 }
2151 numregs++;
2152 }
2153
2154 spin_unlock(&o2hb_live_lock);
2155
2156 return numregs;
2157}
2158EXPORT_SYMBOL_GPL(o2hb_get_all_regions);
2159
2160int o2hb_global_heartbeat_active(void)
2161{
2162 return 0;
2163}
2164EXPORT_SYMBOL(o2hb_global_heartbeat_active);