]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/ocfs2/cluster/heartbeat.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[mirror_ubuntu-artful-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>
36
37#include "heartbeat.h"
38#include "tcp.h"
39#include "nodemanager.h"
40#include "quorum.h"
41
42#include "masklog.h"
43
44
45/*
46 * The first heartbeat pass had one global thread that would serialize all hb
47 * callback calls. This global serializing sem should only be removed once
48 * we've made sure that all callees can deal with being called concurrently
49 * from multiple hb region threads.
50 */
51static DECLARE_RWSEM(o2hb_callback_sem);
52
53/*
54 * multiple hb threads are watching multiple regions. A node is live
55 * whenever any of the threads sees activity from the node in its region.
56 */
34af946a 57static DEFINE_SPINLOCK(o2hb_live_lock);
a7f6a5fb
MF
58static struct list_head o2hb_live_slots[O2NM_MAX_NODES];
59static unsigned long o2hb_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
60static LIST_HEAD(o2hb_node_events);
61static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue);
62
63static LIST_HEAD(o2hb_all_regions);
64
65static struct o2hb_callback {
66 struct list_head list;
67} o2hb_callbacks[O2HB_NUM_CB];
68
69static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type);
70
71#define O2HB_DEFAULT_BLOCK_BITS 9
72
73unsigned int o2hb_dead_threshold = O2HB_DEFAULT_DEAD_THRESHOLD;
74
75/* Only sets a new threshold if there are no active regions.
76 *
77 * No locking or otherwise interesting code is required for reading
78 * o2hb_dead_threshold as it can't change once regions are active and
79 * it's not interesting to anyone until then anyway. */
80static void o2hb_dead_threshold_set(unsigned int threshold)
81{
82 if (threshold > O2HB_MIN_DEAD_THRESHOLD) {
83 spin_lock(&o2hb_live_lock);
84 if (list_empty(&o2hb_all_regions))
85 o2hb_dead_threshold = threshold;
86 spin_unlock(&o2hb_live_lock);
87 }
88}
89
90struct o2hb_node_event {
91 struct list_head hn_item;
92 enum o2hb_callback_type hn_event_type;
93 struct o2nm_node *hn_node;
94 int hn_node_num;
95};
96
97struct o2hb_disk_slot {
98 struct o2hb_disk_heartbeat_block *ds_raw_block;
99 u8 ds_node_num;
100 u64 ds_last_time;
101 u64 ds_last_generation;
102 u16 ds_equal_samples;
103 u16 ds_changed_samples;
104 struct list_head ds_live_item;
105};
106
107/* each thread owns a region.. when we're asked to tear down the region
108 * we ask the thread to stop, who cleans up the region */
109struct o2hb_region {
110 struct config_item hr_item;
111
112 struct list_head hr_all_item;
113 unsigned hr_unclean_stop:1;
114
115 /* protected by the hr_callback_sem */
116 struct task_struct *hr_task;
117
118 unsigned int hr_blocks;
119 unsigned long long hr_start_block;
120
121 unsigned int hr_block_bits;
122 unsigned int hr_block_bytes;
123
124 unsigned int hr_slots_per_page;
125 unsigned int hr_num_pages;
126
127 struct page **hr_slot_data;
128 struct block_device *hr_bdev;
129 struct o2hb_disk_slot *hr_slots;
130
131 /* let the person setting up hb wait for it to return until it
132 * has reached a 'steady' state. This will be fixed when we have
133 * a more complete api that doesn't lead to this sort of fragility. */
134 atomic_t hr_steady_iterations;
135
136 char hr_dev_name[BDEVNAME_SIZE];
137
138 unsigned int hr_timeout_ms;
139
140 /* randomized as the region goes up and down so that a node
141 * recognizes a node going up and down in one iteration */
142 u64 hr_generation;
143
c4028958 144 struct delayed_work hr_write_timeout_work;
a7f6a5fb
MF
145 unsigned long hr_last_timeout_start;
146
147 /* Used during o2hb_check_slot to hold a copy of the block
148 * being checked because we temporarily have to zero out the
149 * crc field. */
150 struct o2hb_disk_heartbeat_block *hr_tmp_block;
151};
152
153struct o2hb_bio_wait_ctxt {
154 atomic_t wc_num_reqs;
155 struct completion wc_io_complete;
a9e2ae39 156 int wc_error;
a7f6a5fb
MF
157};
158
c4028958 159static void o2hb_write_timeout(struct work_struct *work)
a7f6a5fb 160{
c4028958
DH
161 struct o2hb_region *reg =
162 container_of(work, struct o2hb_region,
163 hr_write_timeout_work.work);
a7f6a5fb
MF
164
165 mlog(ML_ERROR, "Heartbeat write timeout to device %s after %u "
166 "milliseconds\n", reg->hr_dev_name,
167 jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
168 o2quo_disk_timeout();
169}
170
171static void o2hb_arm_write_timeout(struct o2hb_region *reg)
172{
173 mlog(0, "Queue write timeout for %u ms\n", O2HB_MAX_WRITE_TIMEOUT_MS);
174
175 cancel_delayed_work(&reg->hr_write_timeout_work);
176 reg->hr_last_timeout_start = jiffies;
177 schedule_delayed_work(&reg->hr_write_timeout_work,
178 msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS));
179}
180
181static void o2hb_disarm_write_timeout(struct o2hb_region *reg)
182{
183 cancel_delayed_work(&reg->hr_write_timeout_work);
184 flush_scheduled_work();
185}
186
b559292e 187static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt *wc)
a7f6a5fb 188{
b559292e 189 atomic_set(&wc->wc_num_reqs, 1);
a7f6a5fb 190 init_completion(&wc->wc_io_complete);
a9e2ae39 191 wc->wc_error = 0;
a7f6a5fb
MF
192}
193
194/* Used in error paths too */
195static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt *wc,
196 unsigned int num)
197{
198 /* sadly atomic_sub_and_test() isn't available on all platforms. The
199 * good news is that the fast path only completes one at a time */
200 while(num--) {
201 if (atomic_dec_and_test(&wc->wc_num_reqs)) {
202 BUG_ON(num > 0);
203 complete(&wc->wc_io_complete);
204 }
205 }
206}
207
208static void o2hb_wait_on_io(struct o2hb_region *reg,
209 struct o2hb_bio_wait_ctxt *wc)
210{
211 struct address_space *mapping = reg->hr_bdev->bd_inode->i_mapping;
212
213 blk_run_address_space(mapping);
b559292e 214 o2hb_bio_wait_dec(wc, 1);
a7f6a5fb
MF
215
216 wait_for_completion(&wc->wc_io_complete);
217}
218
219static int o2hb_bio_end_io(struct bio *bio,
a7f6a5fb
MF
220 int error)
221{
222 struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
223
a9e2ae39 224 if (error) {
a7f6a5fb 225 mlog(ML_ERROR, "IO Error %d\n", error);
a9e2ae39
MF
226 wc->wc_error = error;
227 }
a7f6a5fb 228
a7f6a5fb 229 o2hb_bio_wait_dec(wc, 1);
b559292e 230 bio_put(bio);
a7f6a5fb
MF
231 return 0;
232}
233
234/* Setup a Bio to cover I/O against num_slots slots starting at
235 * start_slot. */
236static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
237 struct o2hb_bio_wait_ctxt *wc,
b559292e
PR
238 unsigned int *current_slot,
239 unsigned int max_slots)
a7f6a5fb 240{
b559292e 241 int len, current_page;
a7f6a5fb
MF
242 unsigned int vec_len, vec_start;
243 unsigned int bits = reg->hr_block_bits;
244 unsigned int spp = reg->hr_slots_per_page;
b559292e 245 unsigned int cs = *current_slot;
a7f6a5fb
MF
246 struct bio *bio;
247 struct page *page;
248
a7f6a5fb
MF
249 /* Testing has shown this allocation to take long enough under
250 * GFP_KERNEL that the local node can get fenced. It would be
251 * nicest if we could pre-allocate these bios and avoid this
252 * all together. */
b559292e 253 bio = bio_alloc(GFP_ATOMIC, 16);
a7f6a5fb
MF
254 if (!bio) {
255 mlog(ML_ERROR, "Could not alloc slots BIO!\n");
256 bio = ERR_PTR(-ENOMEM);
257 goto bail;
258 }
259
260 /* Must put everything in 512 byte sectors for the bio... */
b559292e 261 bio->bi_sector = (reg->hr_start_block + cs) << (bits - 9);
a7f6a5fb
MF
262 bio->bi_bdev = reg->hr_bdev;
263 bio->bi_private = wc;
264 bio->bi_end_io = o2hb_bio_end_io;
265
b559292e
PR
266 vec_start = (cs << bits) % PAGE_CACHE_SIZE;
267 while(cs < max_slots) {
268 current_page = cs / spp;
269 page = reg->hr_slot_data[current_page];
a7f6a5fb 270
b559292e
PR
271 vec_len = min(PAGE_CACHE_SIZE,
272 (max_slots-cs) * (PAGE_CACHE_SIZE/spp) );
a7f6a5fb
MF
273
274 mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
b559292e 275 current_page, vec_len, vec_start);
a7f6a5fb
MF
276
277 len = bio_add_page(bio, page, vec_len, vec_start);
b559292e 278 if (len != vec_len) break;
a7f6a5fb 279
b559292e 280 cs += vec_len / (PAGE_CACHE_SIZE/spp);
a7f6a5fb
MF
281 vec_start = 0;
282 }
283
284bail:
b559292e 285 *current_slot = cs;
a7f6a5fb
MF
286 return bio;
287}
288
a7f6a5fb
MF
289static int o2hb_read_slots(struct o2hb_region *reg,
290 unsigned int max_slots)
291{
b559292e
PR
292 unsigned int current_slot=0;
293 int status;
a7f6a5fb 294 struct o2hb_bio_wait_ctxt wc;
a7f6a5fb
MF
295 struct bio *bio;
296
b559292e 297 o2hb_bio_wait_init(&wc);
a7f6a5fb 298
b559292e
PR
299 while(current_slot < max_slots) {
300 bio = o2hb_setup_one_bio(reg, &wc, &current_slot, max_slots);
a7f6a5fb 301 if (IS_ERR(bio)) {
a7f6a5fb
MF
302 status = PTR_ERR(bio);
303 mlog_errno(status);
304 goto bail_and_wait;
305 }
a7f6a5fb 306
b559292e 307 atomic_inc(&wc.wc_num_reqs);
a7f6a5fb
MF
308 submit_bio(READ, bio);
309 }
310
311 status = 0;
312
313bail_and_wait:
314 o2hb_wait_on_io(reg, &wc);
a9e2ae39
MF
315 if (wc.wc_error && !status)
316 status = wc.wc_error;
a7f6a5fb 317
a7f6a5fb
MF
318 return status;
319}
320
321static int o2hb_issue_node_write(struct o2hb_region *reg,
a7f6a5fb
MF
322 struct o2hb_bio_wait_ctxt *write_wc)
323{
324 int status;
325 unsigned int slot;
326 struct bio *bio;
327
b559292e 328 o2hb_bio_wait_init(write_wc);
a7f6a5fb
MF
329
330 slot = o2nm_this_node();
331
b559292e 332 bio = o2hb_setup_one_bio(reg, write_wc, &slot, slot+1);
a7f6a5fb
MF
333 if (IS_ERR(bio)) {
334 status = PTR_ERR(bio);
335 mlog_errno(status);
336 goto bail;
337 }
338
b559292e 339 atomic_inc(&write_wc->wc_num_reqs);
a7f6a5fb
MF
340 submit_bio(WRITE, bio);
341
a7f6a5fb
MF
342 status = 0;
343bail:
344 return status;
345}
346
347static u32 o2hb_compute_block_crc_le(struct o2hb_region *reg,
348 struct o2hb_disk_heartbeat_block *hb_block)
349{
350 __le32 old_cksum;
351 u32 ret;
352
353 /* We want to compute the block crc with a 0 value in the
354 * hb_cksum field. Save it off here and replace after the
355 * crc. */
356 old_cksum = hb_block->hb_cksum;
357 hb_block->hb_cksum = 0;
358
359 ret = crc32_le(0, (unsigned char *) hb_block, reg->hr_block_bytes);
360
361 hb_block->hb_cksum = old_cksum;
362
363 return ret;
364}
365
366static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
367{
70bacbdb
MF
368 mlog(ML_ERROR, "Dump slot information: seq = 0x%llx, node = %u, "
369 "cksum = 0x%x, generation 0x%llx\n",
370 (long long)le64_to_cpu(hb_block->hb_seq),
371 hb_block->hb_node, le32_to_cpu(hb_block->hb_cksum),
372 (long long)le64_to_cpu(hb_block->hb_generation));
a7f6a5fb
MF
373}
374
375static int o2hb_verify_crc(struct o2hb_region *reg,
376 struct o2hb_disk_heartbeat_block *hb_block)
377{
378 u32 read, computed;
379
380 read = le32_to_cpu(hb_block->hb_cksum);
381 computed = o2hb_compute_block_crc_le(reg, hb_block);
382
383 return read == computed;
384}
385
386/* We want to make sure that nobody is heartbeating on top of us --
387 * this will help detect an invalid configuration. */
388static int o2hb_check_last_timestamp(struct o2hb_region *reg)
389{
390 int node_num, ret;
391 struct o2hb_disk_slot *slot;
392 struct o2hb_disk_heartbeat_block *hb_block;
393
394 node_num = o2nm_this_node();
395
396 ret = 1;
397 slot = &reg->hr_slots[node_num];
398 /* Don't check on our 1st timestamp */
399 if (slot->ds_last_time) {
400 hb_block = slot->ds_raw_block;
401
402 if (le64_to_cpu(hb_block->hb_seq) != slot->ds_last_time)
403 ret = 0;
404 }
405
406 return ret;
407}
408
409static inline void o2hb_prepare_block(struct o2hb_region *reg,
410 u64 generation)
411{
412 int node_num;
413 u64 cputime;
414 struct o2hb_disk_slot *slot;
415 struct o2hb_disk_heartbeat_block *hb_block;
416
417 node_num = o2nm_this_node();
418 slot = &reg->hr_slots[node_num];
419
420 hb_block = (struct o2hb_disk_heartbeat_block *)slot->ds_raw_block;
421 memset(hb_block, 0, reg->hr_block_bytes);
422 /* TODO: time stuff */
423 cputime = CURRENT_TIME.tv_sec;
424 if (!cputime)
425 cputime = 1;
426
427 hb_block->hb_seq = cpu_to_le64(cputime);
428 hb_block->hb_node = node_num;
429 hb_block->hb_generation = cpu_to_le64(generation);
0db638f4 430 hb_block->hb_dead_ms = cpu_to_le32(o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS);
a7f6a5fb
MF
431
432 /* This step must always happen last! */
433 hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
434 hb_block));
435
70bacbdb 436 mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x\n",
5fdf1e67 437 (long long)generation,
70bacbdb 438 le32_to_cpu(hb_block->hb_cksum));
a7f6a5fb
MF
439}
440
441static void o2hb_fire_callbacks(struct o2hb_callback *hbcall,
442 struct o2nm_node *node,
443 int idx)
444{
445 struct list_head *iter;
446 struct o2hb_callback_func *f;
447
448 list_for_each(iter, &hbcall->list) {
449 f = list_entry(iter, struct o2hb_callback_func, hc_item);
450 mlog(ML_HEARTBEAT, "calling funcs %p\n", f);
451 (f->hc_func)(node, idx, f->hc_data);
452 }
453}
454
455/* Will run the list in order until we process the passed event */
456static void o2hb_run_event_list(struct o2hb_node_event *queued_event)
457{
458 int empty;
459 struct o2hb_callback *hbcall;
460 struct o2hb_node_event *event;
461
462 spin_lock(&o2hb_live_lock);
463 empty = list_empty(&queued_event->hn_item);
464 spin_unlock(&o2hb_live_lock);
465 if (empty)
466 return;
467
468 /* Holding callback sem assures we don't alter the callback
469 * lists when doing this, and serializes ourselves with other
470 * processes wanting callbacks. */
471 down_write(&o2hb_callback_sem);
472
473 spin_lock(&o2hb_live_lock);
474 while (!list_empty(&o2hb_node_events)
475 && !list_empty(&queued_event->hn_item)) {
476 event = list_entry(o2hb_node_events.next,
477 struct o2hb_node_event,
478 hn_item);
479 list_del_init(&event->hn_item);
480 spin_unlock(&o2hb_live_lock);
481
482 mlog(ML_HEARTBEAT, "Node %s event for %d\n",
483 event->hn_event_type == O2HB_NODE_UP_CB ? "UP" : "DOWN",
484 event->hn_node_num);
485
486 hbcall = hbcall_from_type(event->hn_event_type);
487
488 /* We should *never* have gotten on to the list with a
489 * bad type... This isn't something that we should try
490 * to recover from. */
491 BUG_ON(IS_ERR(hbcall));
492
493 o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num);
494
495 spin_lock(&o2hb_live_lock);
496 }
497 spin_unlock(&o2hb_live_lock);
498
499 up_write(&o2hb_callback_sem);
500}
501
502static void o2hb_queue_node_event(struct o2hb_node_event *event,
503 enum o2hb_callback_type type,
504 struct o2nm_node *node,
505 int node_num)
506{
507 assert_spin_locked(&o2hb_live_lock);
508
509 event->hn_event_type = type;
510 event->hn_node = node;
511 event->hn_node_num = node_num;
512
513 mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
514 type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
515
516 list_add_tail(&event->hn_item, &o2hb_node_events);
517}
518
519static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
520{
521 struct o2hb_node_event event =
522 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
523 struct o2nm_node *node;
524
525 node = o2nm_get_node_by_num(slot->ds_node_num);
526 if (!node)
527 return;
528
529 spin_lock(&o2hb_live_lock);
530 if (!list_empty(&slot->ds_live_item)) {
531 mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
532 slot->ds_node_num);
533
534 list_del_init(&slot->ds_live_item);
535
536 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
537 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
538
539 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
540 slot->ds_node_num);
541 }
542 }
543 spin_unlock(&o2hb_live_lock);
544
545 o2hb_run_event_list(&event);
546
547 o2nm_node_put(node);
548}
549
550static int o2hb_check_slot(struct o2hb_region *reg,
551 struct o2hb_disk_slot *slot)
552{
553 int changed = 0, gen_changed = 0;
554 struct o2hb_node_event event =
555 { .hn_item = LIST_HEAD_INIT(event.hn_item), };
556 struct o2nm_node *node;
557 struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
558 u64 cputime;
0db638f4
MF
559 unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
560 unsigned int slot_dead_ms;
a7f6a5fb
MF
561
562 memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
563
564 /* Is this correct? Do we assume that the node doesn't exist
565 * if we're not configured for him? */
566 node = o2nm_get_node_by_num(slot->ds_node_num);
567 if (!node)
568 return 0;
569
570 if (!o2hb_verify_crc(reg, hb_block)) {
571 /* all paths from here will drop o2hb_live_lock for
572 * us. */
573 spin_lock(&o2hb_live_lock);
574
575 /* Don't print an error on the console in this case -
576 * a freshly formatted heartbeat area will not have a
577 * crc set on it. */
578 if (list_empty(&slot->ds_live_item))
579 goto out;
580
581 /* The node is live but pushed out a bad crc. We
582 * consider it a transient miss but don't populate any
583 * other values as they may be junk. */
584 mlog(ML_ERROR, "Node %d has written a bad crc to %s\n",
585 slot->ds_node_num, reg->hr_dev_name);
586 o2hb_dump_slot(hb_block);
587
588 slot->ds_equal_samples++;
589 goto fire_callbacks;
590 }
591
592 /* we don't care if these wrap.. the state transitions below
593 * clear at the right places */
594 cputime = le64_to_cpu(hb_block->hb_seq);
595 if (slot->ds_last_time != cputime)
596 slot->ds_changed_samples++;
597 else
598 slot->ds_equal_samples++;
599 slot->ds_last_time = cputime;
600
601 /* The node changed heartbeat generations. We assume this to
602 * mean it dropped off but came back before we timed out. We
603 * want to consider it down for the time being but don't want
604 * to lose any changed_samples state we might build up to
605 * considering it live again. */
606 if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
607 gen_changed = 1;
608 slot->ds_equal_samples = 0;
70bacbdb
MF
609 mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
610 "to 0x%llx)\n", slot->ds_node_num,
611 (long long)slot->ds_last_generation,
612 (long long)le64_to_cpu(hb_block->hb_generation));
a7f6a5fb
MF
613 }
614
615 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
616
70bacbdb
MF
617 mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
618 "seq %llu last %llu changed %u equal %u\n",
619 slot->ds_node_num, (long long)slot->ds_last_generation,
620 le32_to_cpu(hb_block->hb_cksum),
621 (unsigned long long)le64_to_cpu(hb_block->hb_seq),
622 (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
a7f6a5fb
MF
623 slot->ds_equal_samples);
624
625 spin_lock(&o2hb_live_lock);
626
627fire_callbacks:
628 /* dead nodes only come to life after some number of
629 * changes at any time during their dead time */
630 if (list_empty(&slot->ds_live_item) &&
631 slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
70bacbdb
MF
632 mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
633 slot->ds_node_num, (long long)slot->ds_last_generation);
a7f6a5fb
MF
634
635 /* first on the list generates a callback */
636 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
637 set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
638
639 o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
640 slot->ds_node_num);
641
642 changed = 1;
643 }
644
645 list_add_tail(&slot->ds_live_item,
646 &o2hb_live_slots[slot->ds_node_num]);
647
648 slot->ds_equal_samples = 0;
0db638f4
MF
649
650 /* We want to be sure that all nodes agree on the
651 * number of milliseconds before a node will be
652 * considered dead. The self-fencing timeout is
653 * computed from this value, and a discrepancy might
654 * result in heartbeat calling a node dead when it
655 * hasn't self-fenced yet. */
656 slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
657 if (slot_dead_ms && slot_dead_ms != dead_ms) {
658 /* TODO: Perhaps we can fail the region here. */
659 mlog(ML_ERROR, "Node %d on device %s has a dead count "
660 "of %u ms, but our count is %u ms.\n"
661 "Please double check your configuration values "
662 "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
663 slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
664 dead_ms);
665 }
a7f6a5fb
MF
666 goto out;
667 }
668
669 /* if the list is dead, we're done.. */
670 if (list_empty(&slot->ds_live_item))
671 goto out;
672
673 /* live nodes only go dead after enough consequtive missed
674 * samples.. reset the missed counter whenever we see
675 * activity */
676 if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
677 mlog(ML_HEARTBEAT, "Node %d left my region\n",
678 slot->ds_node_num);
679
680 /* last off the live_slot generates a callback */
681 list_del_init(&slot->ds_live_item);
682 if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
683 clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
684
685 o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
686 slot->ds_node_num);
687
688 changed = 1;
689 }
690
691 /* We don't clear this because the node is still
692 * actually writing new blocks. */
693 if (!gen_changed)
694 slot->ds_changed_samples = 0;
695 goto out;
696 }
697 if (slot->ds_changed_samples) {
698 slot->ds_changed_samples = 0;
699 slot->ds_equal_samples = 0;
700 }
701out:
702 spin_unlock(&o2hb_live_lock);
703
704 o2hb_run_event_list(&event);
705
706 o2nm_node_put(node);
707 return changed;
708}
709
710/* This could be faster if we just implmented a find_last_bit, but I
711 * don't think the circumstances warrant it. */
712static int o2hb_highest_node(unsigned long *nodes,
713 int numbits)
714{
715 int highest, node;
716
717 highest = numbits;
718 node = -1;
719 while ((node = find_next_bit(nodes, numbits, node + 1)) != -1) {
720 if (node >= numbits)
721 break;
722
723 highest = node;
724 }
725
726 return highest;
727}
728
a9e2ae39 729static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
a7f6a5fb
MF
730{
731 int i, ret, highest_node, change = 0;
732 unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
a7f6a5fb
MF
733 struct o2hb_bio_wait_ctxt write_wc;
734
a9e2ae39
MF
735 ret = o2nm_configured_node_map(configured_nodes,
736 sizeof(configured_nodes));
737 if (ret) {
738 mlog_errno(ret);
739 return ret;
740 }
a7f6a5fb
MF
741
742 highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
743 if (highest_node >= O2NM_MAX_NODES) {
744 mlog(ML_NOTICE, "ocfs2_heartbeat: no configured nodes found!\n");
a9e2ae39 745 return -EINVAL;
a7f6a5fb
MF
746 }
747
748 /* No sense in reading the slots of nodes that don't exist
749 * yet. Of course, if the node definitions have holes in them
750 * then we're reading an empty slot anyway... Consider this
751 * best-effort. */
752 ret = o2hb_read_slots(reg, highest_node + 1);
753 if (ret < 0) {
754 mlog_errno(ret);
a9e2ae39 755 return ret;
a7f6a5fb
MF
756 }
757
758 /* With an up to date view of the slots, we can check that no
759 * other node has been improperly configured to heartbeat in
760 * our slot. */
761 if (!o2hb_check_last_timestamp(reg))
762 mlog(ML_ERROR, "Device \"%s\": another node is heartbeating "
763 "in our slot!\n", reg->hr_dev_name);
764
765 /* fill in the proper info for our next heartbeat */
766 o2hb_prepare_block(reg, reg->hr_generation);
767
768 /* And fire off the write. Note that we don't wait on this I/O
769 * until later. */
b559292e 770 ret = o2hb_issue_node_write(reg, &write_wc);
a7f6a5fb
MF
771 if (ret < 0) {
772 mlog_errno(ret);
a9e2ae39 773 return ret;
a7f6a5fb
MF
774 }
775
776 i = -1;
777 while((i = find_next_bit(configured_nodes, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
778
779 change |= o2hb_check_slot(reg, &reg->hr_slots[i]);
780 }
781
782 /*
783 * We have to be sure we've advertised ourselves on disk
784 * before we can go to steady state. This ensures that
785 * people we find in our steady state have seen us.
786 */
787 o2hb_wait_on_io(reg, &write_wc);
a9e2ae39
MF
788 if (write_wc.wc_error) {
789 /* Do not re-arm the write timeout on I/O error - we
790 * can't be sure that the new block ever made it to
791 * disk */
792 mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
793 write_wc.wc_error, reg->hr_dev_name);
794 return write_wc.wc_error;
795 }
796
a7f6a5fb
MF
797 o2hb_arm_write_timeout(reg);
798
799 /* let the person who launched us know when things are steady */
800 if (!change && (atomic_read(&reg->hr_steady_iterations) != 0)) {
801 if (atomic_dec_and_test(&reg->hr_steady_iterations))
802 wake_up(&o2hb_steady_queue);
803 }
a9e2ae39
MF
804
805 return 0;
a7f6a5fb
MF
806}
807
808/* Subtract b from a, storing the result in a. a *must* have a larger
809 * value than b. */
810static void o2hb_tv_subtract(struct timeval *a,
811 struct timeval *b)
812{
813 /* just return 0 when a is after b */
814 if (a->tv_sec < b->tv_sec ||
815 (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) {
816 a->tv_sec = 0;
817 a->tv_usec = 0;
818 return;
819 }
820
821 a->tv_sec -= b->tv_sec;
822 a->tv_usec -= b->tv_usec;
823 while ( a->tv_usec < 0 ) {
824 a->tv_sec--;
825 a->tv_usec += 1000000;
826 }
827}
828
829static unsigned int o2hb_elapsed_msecs(struct timeval *start,
830 struct timeval *end)
831{
832 struct timeval res = *end;
833
834 o2hb_tv_subtract(&res, start);
835
836 return res.tv_sec * 1000 + res.tv_usec / 1000;
837}
838
839/*
840 * we ride the region ref that the region dir holds. before the region
841 * dir is removed and drops it ref it will wait to tear down this
842 * thread.
843 */
844static int o2hb_thread(void *data)
845{
846 int i, ret;
847 struct o2hb_region *reg = data;
a7f6a5fb
MF
848 struct o2hb_bio_wait_ctxt write_wc;
849 struct timeval before_hb, after_hb;
850 unsigned int elapsed_msec;
851
852 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
853
854 set_user_nice(current, -20);
855
856 while (!kthread_should_stop() && !reg->hr_unclean_stop) {
857 /* We track the time spent inside
858 * o2hb_do_disk_heartbeat so that we avoid more then
859 * hr_timeout_ms between disk writes. On busy systems
860 * this should result in a heartbeat which is less
861 * likely to time itself out. */
862 do_gettimeofday(&before_hb);
863
a9e2ae39
MF
864 i = 0;
865 do {
866 ret = o2hb_do_disk_heartbeat(reg);
867 } while (ret && ++i < 2);
a7f6a5fb
MF
868
869 do_gettimeofday(&after_hb);
870 elapsed_msec = o2hb_elapsed_msecs(&before_hb, &after_hb);
871
872 mlog(0, "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
215c7f9f
MF
873 before_hb.tv_sec, (unsigned long) before_hb.tv_usec,
874 after_hb.tv_sec, (unsigned long) after_hb.tv_usec,
875 elapsed_msec);
a7f6a5fb
MF
876
877 if (elapsed_msec < reg->hr_timeout_ms) {
878 /* the kthread api has blocked signals for us so no
879 * need to record the return value. */
880 msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
881 }
882 }
883
884 o2hb_disarm_write_timeout(reg);
885
886 /* unclean stop is only used in very bad situation */
887 for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
888 o2hb_shutdown_slot(&reg->hr_slots[i]);
889
890 /* Explicit down notification - avoid forcing the other nodes
891 * to timeout on this region when we could just as easily
892 * write a clear generation - thus indicating to them that
893 * this node has left this region.
894 *
895 * XXX: Should we skip this on unclean_stop? */
896 o2hb_prepare_block(reg, 0);
b559292e 897 ret = o2hb_issue_node_write(reg, &write_wc);
a7f6a5fb
MF
898 if (ret == 0) {
899 o2hb_wait_on_io(reg, &write_wc);
a7f6a5fb
MF
900 } else {
901 mlog_errno(ret);
902 }
903
904 mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread exiting\n");
905
906 return 0;
907}
908
909void o2hb_init(void)
910{
911 int i;
912
913 for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
914 INIT_LIST_HEAD(&o2hb_callbacks[i].list);
915
916 for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
917 INIT_LIST_HEAD(&o2hb_live_slots[i]);
918
919 INIT_LIST_HEAD(&o2hb_node_events);
920
921 memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
922}
923
924/* if we're already in a callback then we're already serialized by the sem */
925static void o2hb_fill_node_map_from_callback(unsigned long *map,
926 unsigned bytes)
927{
928 BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
929
930 memcpy(map, &o2hb_live_node_bitmap, bytes);
931}
932
933/*
934 * get a map of all nodes that are heartbeating in any regions
935 */
936void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
937{
938 /* callers want to serialize this map and callbacks so that they
939 * can trust that they don't miss nodes coming to the party */
940 down_read(&o2hb_callback_sem);
941 spin_lock(&o2hb_live_lock);
942 o2hb_fill_node_map_from_callback(map, bytes);
943 spin_unlock(&o2hb_live_lock);
944 up_read(&o2hb_callback_sem);
945}
946EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
947
948/*
949 * heartbeat configfs bits. The heartbeat set is a default set under
950 * the cluster set in nodemanager.c.
951 */
952
953static struct o2hb_region *to_o2hb_region(struct config_item *item)
954{
955 return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
956}
957
958/* drop_item only drops its ref after killing the thread, nothing should
959 * be using the region anymore. this has to clean up any state that
960 * attributes might have built up. */
961static void o2hb_region_release(struct config_item *item)
962{
963 int i;
964 struct page *page;
965 struct o2hb_region *reg = to_o2hb_region(item);
966
967 if (reg->hr_tmp_block)
968 kfree(reg->hr_tmp_block);
969
970 if (reg->hr_slot_data) {
971 for (i = 0; i < reg->hr_num_pages; i++) {
972 page = reg->hr_slot_data[i];
973 if (page)
974 __free_page(page);
975 }
976 kfree(reg->hr_slot_data);
977 }
978
979 if (reg->hr_bdev)
980 blkdev_put(reg->hr_bdev);
981
982 if (reg->hr_slots)
983 kfree(reg->hr_slots);
984
985 spin_lock(&o2hb_live_lock);
986 list_del(&reg->hr_all_item);
987 spin_unlock(&o2hb_live_lock);
988
989 kfree(reg);
990}
991
992static int o2hb_read_block_input(struct o2hb_region *reg,
993 const char *page,
994 size_t count,
995 unsigned long *ret_bytes,
996 unsigned int *ret_bits)
997{
998 unsigned long bytes;
999 char *p = (char *)page;
1000
1001 bytes = simple_strtoul(p, &p, 0);
1002 if (!p || (*p && (*p != '\n')))
1003 return -EINVAL;
1004
1005 /* Heartbeat and fs min / max block sizes are the same. */
1006 if (bytes > 4096 || bytes < 512)
1007 return -ERANGE;
1008 if (hweight16(bytes) != 1)
1009 return -EINVAL;
1010
1011 if (ret_bytes)
1012 *ret_bytes = bytes;
1013 if (ret_bits)
1014 *ret_bits = ffs(bytes) - 1;
1015
1016 return 0;
1017}
1018
1019static ssize_t o2hb_region_block_bytes_read(struct o2hb_region *reg,
1020 char *page)
1021{
1022 return sprintf(page, "%u\n", reg->hr_block_bytes);
1023}
1024
1025static ssize_t o2hb_region_block_bytes_write(struct o2hb_region *reg,
1026 const char *page,
1027 size_t count)
1028{
1029 int status;
1030 unsigned long block_bytes;
1031 unsigned int block_bits;
1032
1033 if (reg->hr_bdev)
1034 return -EINVAL;
1035
1036 status = o2hb_read_block_input(reg, page, count,
1037 &block_bytes, &block_bits);
1038 if (status)
1039 return status;
1040
1041 reg->hr_block_bytes = (unsigned int)block_bytes;
1042 reg->hr_block_bits = block_bits;
1043
1044 return count;
1045}
1046
1047static ssize_t o2hb_region_start_block_read(struct o2hb_region *reg,
1048 char *page)
1049{
1050 return sprintf(page, "%llu\n", reg->hr_start_block);
1051}
1052
1053static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
1054 const char *page,
1055 size_t count)
1056{
1057 unsigned long long tmp;
1058 char *p = (char *)page;
1059
1060 if (reg->hr_bdev)
1061 return -EINVAL;
1062
1063 tmp = simple_strtoull(p, &p, 0);
1064 if (!p || (*p && (*p != '\n')))
1065 return -EINVAL;
1066
1067 reg->hr_start_block = tmp;
1068
1069 return count;
1070}
1071
1072static ssize_t o2hb_region_blocks_read(struct o2hb_region *reg,
1073 char *page)
1074{
1075 return sprintf(page, "%d\n", reg->hr_blocks);
1076}
1077
1078static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
1079 const char *page,
1080 size_t count)
1081{
1082 unsigned long tmp;
1083 char *p = (char *)page;
1084
1085 if (reg->hr_bdev)
1086 return -EINVAL;
1087
1088 tmp = simple_strtoul(p, &p, 0);
1089 if (!p || (*p && (*p != '\n')))
1090 return -EINVAL;
1091
1092 if (tmp > O2NM_MAX_NODES || tmp == 0)
1093 return -ERANGE;
1094
1095 reg->hr_blocks = (unsigned int)tmp;
1096
1097 return count;
1098}
1099
1100static ssize_t o2hb_region_dev_read(struct o2hb_region *reg,
1101 char *page)
1102{
1103 unsigned int ret = 0;
1104
1105 if (reg->hr_bdev)
1106 ret = sprintf(page, "%s\n", reg->hr_dev_name);
1107
1108 return ret;
1109}
1110
1111static void o2hb_init_region_params(struct o2hb_region *reg)
1112{
1113 reg->hr_slots_per_page = PAGE_CACHE_SIZE >> reg->hr_block_bits;
1114 reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
1115
1116 mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
1117 reg->hr_start_block, reg->hr_blocks);
1118 mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
1119 reg->hr_block_bytes, reg->hr_block_bits);
1120 mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
1121 mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
1122}
1123
1124static int o2hb_map_slot_data(struct o2hb_region *reg)
1125{
1126 int i, j;
1127 unsigned int last_slot;
1128 unsigned int spp = reg->hr_slots_per_page;
1129 struct page *page;
1130 char *raw;
1131 struct o2hb_disk_slot *slot;
1132
1133 reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
1134 if (reg->hr_tmp_block == NULL) {
1135 mlog_errno(-ENOMEM);
1136 return -ENOMEM;
1137 }
1138
1139 reg->hr_slots = kcalloc(reg->hr_blocks,
1140 sizeof(struct o2hb_disk_slot), GFP_KERNEL);
1141 if (reg->hr_slots == NULL) {
1142 mlog_errno(-ENOMEM);
1143 return -ENOMEM;
1144 }
1145
1146 for(i = 0; i < reg->hr_blocks; i++) {
1147 slot = &reg->hr_slots[i];
1148 slot->ds_node_num = i;
1149 INIT_LIST_HEAD(&slot->ds_live_item);
1150 slot->ds_raw_block = NULL;
1151 }
1152
1153 reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
1154 mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
1155 "at %u blocks per page\n",
1156 reg->hr_num_pages, reg->hr_blocks, spp);
1157
1158 reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
1159 GFP_KERNEL);
1160 if (!reg->hr_slot_data) {
1161 mlog_errno(-ENOMEM);
1162 return -ENOMEM;
1163 }
1164
1165 for(i = 0; i < reg->hr_num_pages; i++) {
1166 page = alloc_page(GFP_KERNEL);
1167 if (!page) {
1168 mlog_errno(-ENOMEM);
1169 return -ENOMEM;
1170 }
1171
1172 reg->hr_slot_data[i] = page;
1173
1174 last_slot = i * spp;
1175 raw = page_address(page);
1176 for (j = 0;
1177 (j < spp) && ((j + last_slot) < reg->hr_blocks);
1178 j++) {
1179 BUG_ON((j + last_slot) >= reg->hr_blocks);
1180
1181 slot = &reg->hr_slots[j + last_slot];
1182 slot->ds_raw_block =
1183 (struct o2hb_disk_heartbeat_block *) raw;
1184
1185 raw += reg->hr_block_bytes;
1186 }
1187 }
1188
1189 return 0;
1190}
1191
1192/* Read in all the slots available and populate the tracking
1193 * structures so that we can start with a baseline idea of what's
1194 * there. */
1195static int o2hb_populate_slot_data(struct o2hb_region *reg)
1196{
1197 int ret, i;
1198 struct o2hb_disk_slot *slot;
1199 struct o2hb_disk_heartbeat_block *hb_block;
1200
1201 mlog_entry_void();
1202
1203 ret = o2hb_read_slots(reg, reg->hr_blocks);
1204 if (ret) {
1205 mlog_errno(ret);
1206 goto out;
1207 }
1208
1209 /* We only want to get an idea of the values initially in each
1210 * slot, so we do no verification - o2hb_check_slot will
1211 * actually determine if each configured slot is valid and
1212 * whether any values have changed. */
1213 for(i = 0; i < reg->hr_blocks; i++) {
1214 slot = &reg->hr_slots[i];
1215 hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
1216
1217 /* Only fill the values that o2hb_check_slot uses to
1218 * determine changing slots */
1219 slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
1220 slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
1221 }
1222
1223out:
1224 mlog_exit(ret);
1225 return ret;
1226}
1227
1228/* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
1229static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
1230 const char *page,
1231 size_t count)
1232{
e6c352db 1233 struct task_struct *hb_task;
a7f6a5fb
MF
1234 long fd;
1235 int sectsize;
1236 char *p = (char *)page;
1237 struct file *filp = NULL;
1238 struct inode *inode = NULL;
1239 ssize_t ret = -EINVAL;
1240
1241 if (reg->hr_bdev)
1242 goto out;
1243
1244 /* We can't heartbeat without having had our node number
1245 * configured yet. */
1246 if (o2nm_this_node() == O2NM_MAX_NODES)
1247 goto out;
1248
1249 fd = simple_strtol(p, &p, 0);
1250 if (!p || (*p && (*p != '\n')))
1251 goto out;
1252
1253 if (fd < 0 || fd >= INT_MAX)
1254 goto out;
1255
1256 filp = fget(fd);
1257 if (filp == NULL)
1258 goto out;
1259
1260 if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
1261 reg->hr_block_bytes == 0)
1262 goto out;
1263
1264 inode = igrab(filp->f_mapping->host);
1265 if (inode == NULL)
1266 goto out;
1267
1268 if (!S_ISBLK(inode->i_mode))
1269 goto out;
1270
1271 reg->hr_bdev = I_BDEV(filp->f_mapping->host);
1272 ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ, 0);
1273 if (ret) {
1274 reg->hr_bdev = NULL;
1275 goto out;
1276 }
1277 inode = NULL;
1278
1279 bdevname(reg->hr_bdev, reg->hr_dev_name);
1280
1281 sectsize = bdev_hardsect_size(reg->hr_bdev);
1282 if (sectsize != reg->hr_block_bytes) {
1283 mlog(ML_ERROR,
1284 "blocksize %u incorrect for device, expected %d",
1285 reg->hr_block_bytes, sectsize);
1286 ret = -EINVAL;
1287 goto out;
1288 }
1289
1290 o2hb_init_region_params(reg);
1291
1292 /* Generation of zero is invalid */
1293 do {
1294 get_random_bytes(&reg->hr_generation,
1295 sizeof(reg->hr_generation));
1296 } while (reg->hr_generation == 0);
1297
1298 ret = o2hb_map_slot_data(reg);
1299 if (ret) {
1300 mlog_errno(ret);
1301 goto out;
1302 }
1303
1304 ret = o2hb_populate_slot_data(reg);
1305 if (ret) {
1306 mlog_errno(ret);
1307 goto out;
1308 }
1309
c4028958 1310 INIT_DELAYED_WORK(&reg->hr_write_timeout_work, o2hb_write_timeout);
a7f6a5fb
MF
1311
1312 /*
1313 * A node is considered live after it has beat LIVE_THRESHOLD
1314 * times. We're not steady until we've given them a chance
1315 * _after_ our first read.
1316 */
1317 atomic_set(&reg->hr_steady_iterations, O2HB_LIVE_THRESHOLD + 1);
1318
e6c352db
JB
1319 hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
1320 reg->hr_item.ci_name);
1321 if (IS_ERR(hb_task)) {
1322 ret = PTR_ERR(hb_task);
a7f6a5fb 1323 mlog_errno(ret);
a7f6a5fb
MF
1324 goto out;
1325 }
1326
e6c352db
JB
1327 spin_lock(&o2hb_live_lock);
1328 reg->hr_task = hb_task;
1329 spin_unlock(&o2hb_live_lock);
1330
a7f6a5fb
MF
1331 ret = wait_event_interruptible(o2hb_steady_queue,
1332 atomic_read(&reg->hr_steady_iterations) == 0);
1333 if (ret) {
e6df3a66 1334 /* We got interrupted (hello ptrace!). Clean up */
e6c352db
JB
1335 spin_lock(&o2hb_live_lock);
1336 hb_task = reg->hr_task;
a7f6a5fb 1337 reg->hr_task = NULL;
e6c352db
JB
1338 spin_unlock(&o2hb_live_lock);
1339
1340 if (hb_task)
1341 kthread_stop(hb_task);
a7f6a5fb
MF
1342 goto out;
1343 }
1344
e6df3a66
JB
1345 /* Ok, we were woken. Make sure it wasn't by drop_item() */
1346 spin_lock(&o2hb_live_lock);
1347 hb_task = reg->hr_task;
1348 spin_unlock(&o2hb_live_lock);
1349
1350 if (hb_task)
1351 ret = count;
1352 else
1353 ret = -EIO;
1354
a7f6a5fb
MF
1355out:
1356 if (filp)
1357 fput(filp);
1358 if (inode)
1359 iput(inode);
1360 if (ret < 0) {
1361 if (reg->hr_bdev) {
1362 blkdev_put(reg->hr_bdev);
1363 reg->hr_bdev = NULL;
1364 }
1365 }
1366 return ret;
1367}
1368
92efc152
ZW
1369static ssize_t o2hb_region_pid_read(struct o2hb_region *reg,
1370 char *page)
1371{
e6c352db
JB
1372 pid_t pid = 0;
1373
1374 spin_lock(&o2hb_live_lock);
1375 if (reg->hr_task)
1376 pid = reg->hr_task->pid;
1377 spin_unlock(&o2hb_live_lock);
1378
1379 if (!pid)
92efc152
ZW
1380 return 0;
1381
e6c352db 1382 return sprintf(page, "%u\n", pid);
92efc152
ZW
1383}
1384
a7f6a5fb
MF
1385struct o2hb_region_attribute {
1386 struct configfs_attribute attr;
1387 ssize_t (*show)(struct o2hb_region *, char *);
1388 ssize_t (*store)(struct o2hb_region *, const char *, size_t);
1389};
1390
1391static struct o2hb_region_attribute o2hb_region_attr_block_bytes = {
1392 .attr = { .ca_owner = THIS_MODULE,
1393 .ca_name = "block_bytes",
1394 .ca_mode = S_IRUGO | S_IWUSR },
1395 .show = o2hb_region_block_bytes_read,
1396 .store = o2hb_region_block_bytes_write,
1397};
1398
1399static struct o2hb_region_attribute o2hb_region_attr_start_block = {
1400 .attr = { .ca_owner = THIS_MODULE,
1401 .ca_name = "start_block",
1402 .ca_mode = S_IRUGO | S_IWUSR },
1403 .show = o2hb_region_start_block_read,
1404 .store = o2hb_region_start_block_write,
1405};
1406
1407static struct o2hb_region_attribute o2hb_region_attr_blocks = {
1408 .attr = { .ca_owner = THIS_MODULE,
1409 .ca_name = "blocks",
1410 .ca_mode = S_IRUGO | S_IWUSR },
1411 .show = o2hb_region_blocks_read,
1412 .store = o2hb_region_blocks_write,
1413};
1414
1415static struct o2hb_region_attribute o2hb_region_attr_dev = {
1416 .attr = { .ca_owner = THIS_MODULE,
1417 .ca_name = "dev",
1418 .ca_mode = S_IRUGO | S_IWUSR },
1419 .show = o2hb_region_dev_read,
1420 .store = o2hb_region_dev_write,
1421};
1422
92efc152
ZW
1423static struct o2hb_region_attribute o2hb_region_attr_pid = {
1424 .attr = { .ca_owner = THIS_MODULE,
1425 .ca_name = "pid",
1426 .ca_mode = S_IRUGO | S_IRUSR },
1427 .show = o2hb_region_pid_read,
1428};
1429
a7f6a5fb
MF
1430static struct configfs_attribute *o2hb_region_attrs[] = {
1431 &o2hb_region_attr_block_bytes.attr,
1432 &o2hb_region_attr_start_block.attr,
1433 &o2hb_region_attr_blocks.attr,
1434 &o2hb_region_attr_dev.attr,
92efc152 1435 &o2hb_region_attr_pid.attr,
a7f6a5fb
MF
1436 NULL,
1437};
1438
1439static ssize_t o2hb_region_show(struct config_item *item,
1440 struct configfs_attribute *attr,
1441 char *page)
1442{
1443 struct o2hb_region *reg = to_o2hb_region(item);
1444 struct o2hb_region_attribute *o2hb_region_attr =
1445 container_of(attr, struct o2hb_region_attribute, attr);
1446 ssize_t ret = 0;
1447
1448 if (o2hb_region_attr->show)
1449 ret = o2hb_region_attr->show(reg, page);
1450 return ret;
1451}
1452
1453static ssize_t o2hb_region_store(struct config_item *item,
1454 struct configfs_attribute *attr,
1455 const char *page, size_t count)
1456{
1457 struct o2hb_region *reg = to_o2hb_region(item);
1458 struct o2hb_region_attribute *o2hb_region_attr =
1459 container_of(attr, struct o2hb_region_attribute, attr);
1460 ssize_t ret = -EINVAL;
1461
1462 if (o2hb_region_attr->store)
1463 ret = o2hb_region_attr->store(reg, page, count);
1464 return ret;
1465}
1466
1467static struct configfs_item_operations o2hb_region_item_ops = {
1468 .release = o2hb_region_release,
1469 .show_attribute = o2hb_region_show,
1470 .store_attribute = o2hb_region_store,
1471};
1472
1473static struct config_item_type o2hb_region_type = {
1474 .ct_item_ops = &o2hb_region_item_ops,
1475 .ct_attrs = o2hb_region_attrs,
1476 .ct_owner = THIS_MODULE,
1477};
1478
1479/* heartbeat set */
1480
1481struct o2hb_heartbeat_group {
1482 struct config_group hs_group;
1483 /* some stuff? */
1484};
1485
1486static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
1487{
1488 return group ?
1489 container_of(group, struct o2hb_heartbeat_group, hs_group)
1490 : NULL;
1491}
1492
1493static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
1494 const char *name)
1495{
1496 struct o2hb_region *reg = NULL;
1497 struct config_item *ret = NULL;
1498
cd861280 1499 reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
a7f6a5fb
MF
1500 if (reg == NULL)
1501 goto out; /* ENOMEM */
1502
1503 config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
1504
1505 ret = &reg->hr_item;
1506
1507 spin_lock(&o2hb_live_lock);
1508 list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
1509 spin_unlock(&o2hb_live_lock);
1510out:
1511 if (ret == NULL)
1512 kfree(reg);
1513
1514 return ret;
1515}
1516
1517static void o2hb_heartbeat_group_drop_item(struct config_group *group,
1518 struct config_item *item)
1519{
e6c352db 1520 struct task_struct *hb_task;
a7f6a5fb
MF
1521 struct o2hb_region *reg = to_o2hb_region(item);
1522
1523 /* stop the thread when the user removes the region dir */
e6c352db
JB
1524 spin_lock(&o2hb_live_lock);
1525 hb_task = reg->hr_task;
1526 reg->hr_task = NULL;
1527 spin_unlock(&o2hb_live_lock);
1528
1529 if (hb_task)
1530 kthread_stop(hb_task);
a7f6a5fb 1531
e6df3a66
JB
1532 /*
1533 * If we're racing a dev_write(), we need to wake them. They will
1534 * check reg->hr_task
1535 */
1536 if (atomic_read(&reg->hr_steady_iterations) != 0) {
1537 atomic_set(&reg->hr_steady_iterations, 0);
1538 wake_up(&o2hb_steady_queue);
1539 }
1540
a7f6a5fb
MF
1541 config_item_put(item);
1542}
1543
1544struct o2hb_heartbeat_group_attribute {
1545 struct configfs_attribute attr;
1546 ssize_t (*show)(struct o2hb_heartbeat_group *, char *);
1547 ssize_t (*store)(struct o2hb_heartbeat_group *, const char *, size_t);
1548};
1549
1550static ssize_t o2hb_heartbeat_group_show(struct config_item *item,
1551 struct configfs_attribute *attr,
1552 char *page)
1553{
1554 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1555 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1556 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1557 ssize_t ret = 0;
1558
1559 if (o2hb_heartbeat_group_attr->show)
1560 ret = o2hb_heartbeat_group_attr->show(reg, page);
1561 return ret;
1562}
1563
1564static ssize_t o2hb_heartbeat_group_store(struct config_item *item,
1565 struct configfs_attribute *attr,
1566 const char *page, size_t count)
1567{
1568 struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
1569 struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
1570 container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
1571 ssize_t ret = -EINVAL;
1572
1573 if (o2hb_heartbeat_group_attr->store)
1574 ret = o2hb_heartbeat_group_attr->store(reg, page, count);
1575 return ret;
1576}
1577
1578static ssize_t o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group *group,
1579 char *page)
1580{
1581 return sprintf(page, "%u\n", o2hb_dead_threshold);
1582}
1583
1584static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group *group,
1585 const char *page,
1586 size_t count)
1587{
1588 unsigned long tmp;
1589 char *p = (char *)page;
1590
1591 tmp = simple_strtoul(p, &p, 10);
1592 if (!p || (*p && (*p != '\n')))
1593 return -EINVAL;
1594
1595 /* this will validate ranges for us. */
1596 o2hb_dead_threshold_set((unsigned int) tmp);
1597
1598 return count;
1599}
1600
1601static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold = {
1602 .attr = { .ca_owner = THIS_MODULE,
1603 .ca_name = "dead_threshold",
1604 .ca_mode = S_IRUGO | S_IWUSR },
1605 .show = o2hb_heartbeat_group_threshold_show,
1606 .store = o2hb_heartbeat_group_threshold_store,
1607};
1608
1609static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
1610 &o2hb_heartbeat_group_attr_threshold.attr,
1611 NULL,
1612};
1613
1614static struct configfs_item_operations o2hb_hearbeat_group_item_ops = {
1615 .show_attribute = o2hb_heartbeat_group_show,
1616 .store_attribute = o2hb_heartbeat_group_store,
1617};
1618
1619static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
1620 .make_item = o2hb_heartbeat_group_make_item,
1621 .drop_item = o2hb_heartbeat_group_drop_item,
1622};
1623
1624static struct config_item_type o2hb_heartbeat_group_type = {
1625 .ct_group_ops = &o2hb_heartbeat_group_group_ops,
1626 .ct_item_ops = &o2hb_hearbeat_group_item_ops,
1627 .ct_attrs = o2hb_heartbeat_group_attrs,
1628 .ct_owner = THIS_MODULE,
1629};
1630
1631/* this is just here to avoid touching group in heartbeat.h which the
1632 * entire damn world #includes */
1633struct config_group *o2hb_alloc_hb_set(void)
1634{
1635 struct o2hb_heartbeat_group *hs = NULL;
1636 struct config_group *ret = NULL;
1637
cd861280 1638 hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
a7f6a5fb
MF
1639 if (hs == NULL)
1640 goto out;
1641
1642 config_group_init_type_name(&hs->hs_group, "heartbeat",
1643 &o2hb_heartbeat_group_type);
1644
1645 ret = &hs->hs_group;
1646out:
1647 if (ret == NULL)
1648 kfree(hs);
1649 return ret;
1650}
1651
1652void o2hb_free_hb_set(struct config_group *group)
1653{
1654 struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
1655 kfree(hs);
1656}
1657
1658/* hb callback registration and issueing */
1659
1660static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
1661{
1662 if (type == O2HB_NUM_CB)
1663 return ERR_PTR(-EINVAL);
1664
1665 return &o2hb_callbacks[type];
1666}
1667
1668void o2hb_setup_callback(struct o2hb_callback_func *hc,
1669 enum o2hb_callback_type type,
1670 o2hb_cb_func *func,
1671 void *data,
1672 int priority)
1673{
1674 INIT_LIST_HEAD(&hc->hc_item);
1675 hc->hc_func = func;
1676 hc->hc_data = data;
1677 hc->hc_priority = priority;
1678 hc->hc_type = type;
1679 hc->hc_magic = O2HB_CB_MAGIC;
1680}
1681EXPORT_SYMBOL_GPL(o2hb_setup_callback);
1682
14829422
JB
1683static struct o2hb_region *o2hb_find_region(const char *region_uuid)
1684{
1685 struct o2hb_region *p, *reg = NULL;
1686
1687 assert_spin_locked(&o2hb_live_lock);
1688
1689 list_for_each_entry(p, &o2hb_all_regions, hr_all_item) {
1690 if (!strcmp(region_uuid, config_item_name(&p->hr_item))) {
1691 reg = p;
1692 break;
1693 }
1694 }
1695
1696 return reg;
1697}
1698
1699static int o2hb_region_get(const char *region_uuid)
1700{
1701 int ret = 0;
1702 struct o2hb_region *reg;
1703
1704 spin_lock(&o2hb_live_lock);
1705
1706 reg = o2hb_find_region(region_uuid);
1707 if (!reg)
1708 ret = -ENOENT;
1709 spin_unlock(&o2hb_live_lock);
1710
16c6a4f2
JB
1711 if (ret)
1712 goto out;
1713
1714 ret = o2nm_depend_this_node();
1715 if (ret)
1716 goto out;
14829422 1717
16c6a4f2
JB
1718 ret = o2nm_depend_item(&reg->hr_item);
1719 if (ret)
1720 o2nm_undepend_this_node();
1721
1722out:
14829422
JB
1723 return ret;
1724}
1725
1726static void o2hb_region_put(const char *region_uuid)
1727{
1728 struct o2hb_region *reg;
1729
1730 spin_lock(&o2hb_live_lock);
1731
1732 reg = o2hb_find_region(region_uuid);
1733
1734 spin_unlock(&o2hb_live_lock);
1735
16c6a4f2 1736 if (reg) {
14829422 1737 o2nm_undepend_item(&reg->hr_item);
16c6a4f2
JB
1738 o2nm_undepend_this_node();
1739 }
14829422
JB
1740}
1741
1742int o2hb_register_callback(const char *region_uuid,
1743 struct o2hb_callback_func *hc)
a7f6a5fb
MF
1744{
1745 struct o2hb_callback_func *tmp;
1746 struct list_head *iter;
1747 struct o2hb_callback *hbcall;
1748 int ret;
1749
1750 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
1751 BUG_ON(!list_empty(&hc->hc_item));
1752
1753 hbcall = hbcall_from_type(hc->hc_type);
1754 if (IS_ERR(hbcall)) {
1755 ret = PTR_ERR(hbcall);
1756 goto out;
1757 }
1758
14829422
JB
1759 if (region_uuid) {
1760 ret = o2hb_region_get(region_uuid);
1761 if (ret)
1762 goto out;
1763 }
1764
a7f6a5fb
MF
1765 down_write(&o2hb_callback_sem);
1766
1767 list_for_each(iter, &hbcall->list) {
1768 tmp = list_entry(iter, struct o2hb_callback_func, hc_item);
1769 if (hc->hc_priority < tmp->hc_priority) {
1770 list_add_tail(&hc->hc_item, iter);
1771 break;
1772 }
1773 }
1774 if (list_empty(&hc->hc_item))
1775 list_add_tail(&hc->hc_item, &hbcall->list);
1776
1777 up_write(&o2hb_callback_sem);
1778 ret = 0;
1779out:
1780 mlog(ML_HEARTBEAT, "returning %d on behalf of %p for funcs %p\n",
1781 ret, __builtin_return_address(0), hc);
1782 return ret;
1783}
1784EXPORT_SYMBOL_GPL(o2hb_register_callback);
1785
14829422
JB
1786void o2hb_unregister_callback(const char *region_uuid,
1787 struct o2hb_callback_func *hc)
a7f6a5fb
MF
1788{
1789 BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
1790
1791 mlog(ML_HEARTBEAT, "on behalf of %p for funcs %p\n",
1792 __builtin_return_address(0), hc);
1793
14829422 1794 /* XXX Can this happen _with_ a region reference? */
a7f6a5fb 1795 if (list_empty(&hc->hc_item))
c24f72cc 1796 return;
a7f6a5fb 1797
14829422
JB
1798 if (region_uuid)
1799 o2hb_region_put(region_uuid);
1800
a7f6a5fb
MF
1801 down_write(&o2hb_callback_sem);
1802
1803 list_del_init(&hc->hc_item);
1804
1805 up_write(&o2hb_callback_sem);
a7f6a5fb
MF
1806}
1807EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
1808
1809int o2hb_check_node_heartbeating(u8 node_num)
1810{
1811 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1812
1813 o2hb_fill_node_map(testing_map, sizeof(testing_map));
1814 if (!test_bit(node_num, testing_map)) {
1815 mlog(ML_HEARTBEAT,
1816 "node (%u) does not have heartbeating enabled.\n",
1817 node_num);
1818 return 0;
1819 }
1820
1821 return 1;
1822}
1823EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating);
1824
1825int o2hb_check_node_heartbeating_from_callback(u8 node_num)
1826{
1827 unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1828
1829 o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
1830 if (!test_bit(node_num, testing_map)) {
1831 mlog(ML_HEARTBEAT,
1832 "node (%u) does not have heartbeating enabled.\n",
1833 node_num);
1834 return 0;
1835 }
1836
1837 return 1;
1838}
1839EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
1840
1841/* Makes sure our local node is configured with a node number, and is
1842 * heartbeating. */
1843int o2hb_check_local_node_heartbeating(void)
1844{
1845 u8 node_num;
1846
1847 /* if this node was set then we have networking */
1848 node_num = o2nm_this_node();
1849 if (node_num == O2NM_MAX_NODES) {
1850 mlog(ML_HEARTBEAT, "this node has not been configured.\n");
1851 return 0;
1852 }
1853
1854 return o2hb_check_node_heartbeating(node_num);
1855}
1856EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating);
1857
1858/*
1859 * this is just a hack until we get the plumbing which flips file systems
1860 * read only and drops the hb ref instead of killing the node dead.
1861 */
1862void o2hb_stop_all_regions(void)
1863{
1864 struct o2hb_region *reg;
1865
1866 mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
1867
1868 spin_lock(&o2hb_live_lock);
1869
1870 list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
1871 reg->hr_unclean_stop = 1;
1872
1873 spin_unlock(&o2hb_live_lock);
1874}
1875EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);