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