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