2 * Copyright (C) 2015 Shaohua Li <shli@fb.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #include <linux/kernel.h>
15 #include <linux/wait.h>
16 #include <linux/blkdev.h>
17 #include <linux/slab.h>
18 #include <linux/raid/md_p.h>
19 #include <linux/crc32c.h>
20 #include <linux/random.h>
25 * metadata/data stored in disk with 4k size unit (a block) regardless
26 * underneath hardware sector size. only works with PAGE_SIZE == 4096
28 #define BLOCK_SECTORS (8)
31 * reclaim runs every 1/4 disk size or 10G reclaimable space. This can prevent
32 * recovery scans a very long log
34 #define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
35 #define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
42 sector_t device_size; /* log device size, round to
44 sector_t max_free_space; /* reclaim run if free space is at
47 sector_t last_checkpoint; /* log tail. where recovery scan
49 u64 last_cp_seq; /* log tail sequence */
51 sector_t log_start; /* log head. where new data appends */
52 u64 seq; /* log head sequence */
54 struct mutex io_mutex;
55 struct r5l_io_unit *current_io; /* current io_unit accepting new data */
57 spinlock_t io_list_lock;
58 struct list_head running_ios; /* io_units which are still running,
59 * and have not yet been completely
60 * written to the log */
61 struct list_head io_end_ios; /* io_units which have been completely
62 * written to the log but not yet written
64 struct list_head flushing_ios; /* io_units which are waiting for log
66 struct list_head flushed_ios; /* io_units which settle down in log disk */
68 struct list_head stripe_end_ios;/* io_units which have been completely
69 * written to the RAID but have not yet
70 * been considered for updating super */
72 struct kmem_cache *io_kc;
74 struct md_thread *reclaim_thread;
75 unsigned long reclaim_target; /* number of space that need to be
76 * reclaimed. if it's 0, reclaim spaces
77 * used by io_units which are in
78 * IO_UNIT_STRIPE_END state (eg, reclaim
79 * dones't wait for specific io_unit
80 * switching to IO_UNIT_STRIPE_END
82 wait_queue_head_t iounit_wait;
84 struct list_head no_space_stripes; /* pending stripes, log has no space */
85 spinlock_t no_space_stripes_lock;
89 * an IO range starts from a meta data block and end at the next meta data
90 * block. The io unit's the meta data block tracks data/parity followed it. io
91 * unit is written to log disk with normal write, as we always flush log disk
92 * first and then start move data to raid disks, there is no requirement to
93 * write io unit with FLUSH/FUA
98 struct page *meta_page; /* store meta block */
99 int meta_offset; /* current offset in meta_page */
101 struct bio_list bios;
102 atomic_t pending_io; /* pending bios not written to log yet */
103 struct bio *current_bio;/* current_bio accepting new data */
105 atomic_t pending_stripe;/* how many stripes not flushed to raid */
106 u64 seq; /* seq number of the metablock */
107 sector_t log_start; /* where the io_unit starts */
108 sector_t log_end; /* where the io_unit ends */
109 struct list_head log_sibling; /* log->running_ios */
110 struct list_head stripe_list; /* stripes added to the io_unit */
115 /* r5l_io_unit state */
116 enum r5l_io_unit_state {
117 IO_UNIT_RUNNING = 0, /* accepting new IO */
118 IO_UNIT_IO_START = 1, /* io_unit bio start writing to log,
119 * don't accepting new bio */
120 IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */
121 IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
124 static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
127 if (start >= log->device_size)
128 start = start - log->device_size;
132 static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
138 return end + log->device_size - start;
141 static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
145 used_size = r5l_ring_distance(log, log->last_checkpoint,
148 return log->device_size > used_size + size;
151 static struct r5l_io_unit *r5l_alloc_io_unit(struct r5l_log *log)
153 struct r5l_io_unit *io;
154 /* We can't handle memory allocate failure so far */
155 gfp_t gfp = GFP_NOIO | __GFP_NOFAIL;
157 io = kmem_cache_zalloc(log->io_kc, gfp);
159 io->meta_page = alloc_page(gfp | __GFP_ZERO);
161 bio_list_init(&io->bios);
162 INIT_LIST_HEAD(&io->log_sibling);
163 INIT_LIST_HEAD(&io->stripe_list);
164 io->state = IO_UNIT_RUNNING;
168 static void r5l_free_io_unit(struct r5l_log *log, struct r5l_io_unit *io)
170 __free_page(io->meta_page);
171 kmem_cache_free(log->io_kc, io);
174 static void r5l_move_io_unit_list(struct list_head *from, struct list_head *to,
175 enum r5l_io_unit_state state)
177 struct r5l_io_unit *io;
179 while (!list_empty(from)) {
180 io = list_first_entry(from, struct r5l_io_unit, log_sibling);
181 /* don't change list order */
182 if (io->state >= state)
183 list_move_tail(&io->log_sibling, to);
190 * We don't want too many io_units reside in stripe_end_ios list, which will
191 * waste a lot of memory. So we try to remove some. But we must keep at least 2
192 * io_units. The superblock must point to a valid meta, if it's the last meta,
193 * recovery can scan less
195 static void r5l_compress_stripe_end_list(struct r5l_log *log)
197 struct r5l_io_unit *first, *last, *io;
199 first = list_first_entry(&log->stripe_end_ios,
200 struct r5l_io_unit, log_sibling);
201 last = list_last_entry(&log->stripe_end_ios,
202 struct r5l_io_unit, log_sibling);
205 list_del(&first->log_sibling);
206 list_del(&last->log_sibling);
207 while (!list_empty(&log->stripe_end_ios)) {
208 io = list_first_entry(&log->stripe_end_ios,
209 struct r5l_io_unit, log_sibling);
210 list_del(&io->log_sibling);
211 first->log_end = io->log_end;
212 r5l_free_io_unit(log, io);
214 list_add_tail(&first->log_sibling, &log->stripe_end_ios);
215 list_add_tail(&last->log_sibling, &log->stripe_end_ios);
218 static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
219 enum r5l_io_unit_state state)
221 if (WARN_ON(io->state >= state))
226 /* XXX: totally ignores I/O errors */
227 static void r5l_log_endio(struct bio *bio)
229 struct r5l_io_unit *io = bio->bi_private;
230 struct r5l_log *log = io->log;
235 if (!atomic_dec_and_test(&io->pending_io))
238 spin_lock_irqsave(&log->io_list_lock, flags);
239 __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
240 r5l_move_io_unit_list(&log->running_ios, &log->io_end_ios,
242 spin_unlock_irqrestore(&log->io_list_lock, flags);
244 md_wakeup_thread(log->rdev->mddev->thread);
247 static void r5l_submit_current_io(struct r5l_log *log)
249 struct r5l_io_unit *io = log->current_io;
250 struct r5l_meta_block *block;
258 block = page_address(io->meta_page);
259 block->meta_size = cpu_to_le32(io->meta_offset);
260 crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
261 block->checksum = cpu_to_le32(crc);
263 log->current_io = NULL;
264 spin_lock_irqsave(&log->io_list_lock, flags);
265 __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
266 spin_unlock_irqrestore(&log->io_list_lock, flags);
268 while ((bio = bio_list_pop(&io->bios))) {
269 /* all IO must start from rdev->data_offset */
270 bio->bi_iter.bi_sector += log->rdev->data_offset;
271 submit_bio(WRITE, bio);
275 static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
277 struct r5l_io_unit *io;
278 struct r5l_meta_block *block;
281 io = r5l_alloc_io_unit(log);
283 block = page_address(io->meta_page);
284 block->magic = cpu_to_le32(R5LOG_MAGIC);
285 block->version = R5LOG_VERSION;
286 block->seq = cpu_to_le64(log->seq);
287 block->position = cpu_to_le64(log->log_start);
289 io->log_start = log->log_start;
290 io->meta_offset = sizeof(struct r5l_meta_block);
293 bio = bio_kmalloc(GFP_NOIO | __GFP_NOFAIL, BIO_MAX_PAGES);
294 io->current_bio = bio;
296 bio->bi_bdev = log->rdev->bdev;
297 bio->bi_iter.bi_sector = log->log_start;
298 bio_add_page(bio, io->meta_page, PAGE_SIZE, 0);
299 bio->bi_end_io = r5l_log_endio;
300 bio->bi_private = io;
302 bio_list_add(&io->bios, bio);
303 atomic_inc(&io->pending_io);
306 log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
307 io->log_end = log->log_start;
308 /* current bio hit disk end */
309 if (log->log_start == 0)
310 io->current_bio = NULL;
312 spin_lock_irq(&log->io_list_lock);
313 list_add_tail(&io->log_sibling, &log->running_ios);
314 spin_unlock_irq(&log->io_list_lock);
319 static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
321 struct r5l_io_unit *io;
323 io = log->current_io;
324 if (io && io->meta_offset + payload_size > PAGE_SIZE)
325 r5l_submit_current_io(log);
326 io = log->current_io;
330 log->current_io = r5l_new_meta(log);
334 static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
336 u32 checksum1, u32 checksum2,
337 bool checksum2_valid)
339 struct r5l_io_unit *io = log->current_io;
340 struct r5l_payload_data_parity *payload;
342 payload = page_address(io->meta_page) + io->meta_offset;
343 payload->header.type = cpu_to_le16(type);
344 payload->header.flags = cpu_to_le16(0);
345 payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
347 payload->location = cpu_to_le64(location);
348 payload->checksum[0] = cpu_to_le32(checksum1);
350 payload->checksum[1] = cpu_to_le32(checksum2);
352 io->meta_offset += sizeof(struct r5l_payload_data_parity) +
353 sizeof(__le32) * (1 + !!checksum2_valid);
356 static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
358 struct r5l_io_unit *io = log->current_io;
361 if (!io->current_bio) {
364 bio = bio_kmalloc(GFP_NOIO | __GFP_NOFAIL, BIO_MAX_PAGES);
366 bio->bi_bdev = log->rdev->bdev;
367 bio->bi_iter.bi_sector = log->log_start;
368 bio->bi_end_io = r5l_log_endio;
369 bio->bi_private = io;
370 bio_list_add(&io->bios, bio);
371 atomic_inc(&io->pending_io);
372 io->current_bio = bio;
374 if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0)) {
375 io->current_bio = NULL;
378 log->log_start = r5l_ring_add(log, log->log_start,
380 /* current bio hit disk end */
381 if (log->log_start == 0)
382 io->current_bio = NULL;
384 io->log_end = log->log_start;
387 static void r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
388 int data_pages, int parity_pages)
392 struct r5l_io_unit *io;
395 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
397 sizeof(struct r5l_payload_data_parity) +
398 sizeof(__le32) * parity_pages;
400 r5l_get_meta(log, meta_size);
401 io = log->current_io;
403 for (i = 0; i < sh->disks; i++) {
404 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
406 if (i == sh->pd_idx || i == sh->qd_idx)
408 r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
409 raid5_compute_blocknr(sh, i, 0),
410 sh->dev[i].log_checksum, 0, false);
411 r5l_append_payload_page(log, sh->dev[i].page);
414 if (sh->qd_idx >= 0) {
415 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
416 sh->sector, sh->dev[sh->pd_idx].log_checksum,
417 sh->dev[sh->qd_idx].log_checksum, true);
418 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
419 r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
421 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
422 sh->sector, sh->dev[sh->pd_idx].log_checksum,
424 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
427 list_add_tail(&sh->log_list, &io->stripe_list);
428 atomic_inc(&io->pending_stripe);
432 static void r5l_wake_reclaim(struct r5l_log *log, sector_t space);
434 * running in raid5d, where reclaim could wait for raid5d too (when it flushes
435 * data from log to raid disks), so we shouldn't wait for reclaim here
437 int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
440 int data_pages, parity_pages;
447 /* Don't support stripe batch */
448 if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
449 test_bit(STRIPE_SYNCING, &sh->state)) {
450 /* the stripe is written to log, we start writing it to raid */
451 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
455 for (i = 0; i < sh->disks; i++) {
458 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
461 /* checksum is already calculated in last run */
462 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
464 addr = kmap_atomic(sh->dev[i].page);
465 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
469 parity_pages = 1 + !!(sh->qd_idx >= 0);
470 data_pages = write_disks - parity_pages;
473 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
475 sizeof(struct r5l_payload_data_parity) +
476 sizeof(__le32) * parity_pages;
477 /* Doesn't work with very big raid array */
478 if (meta_size + sizeof(struct r5l_meta_block) > PAGE_SIZE)
481 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
482 atomic_inc(&sh->count);
484 mutex_lock(&log->io_mutex);
486 reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
487 if (r5l_has_free_space(log, reserve))
488 r5l_log_stripe(log, sh, data_pages, parity_pages);
490 spin_lock(&log->no_space_stripes_lock);
491 list_add_tail(&sh->log_list, &log->no_space_stripes);
492 spin_unlock(&log->no_space_stripes_lock);
494 r5l_wake_reclaim(log, reserve);
496 mutex_unlock(&log->io_mutex);
501 void r5l_write_stripe_run(struct r5l_log *log)
505 mutex_lock(&log->io_mutex);
506 r5l_submit_current_io(log);
507 mutex_unlock(&log->io_mutex);
510 int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
515 * we flush log disk cache first, then write stripe data to raid disks.
516 * So if bio is finished, the log disk cache is flushed already. The
517 * recovery guarantees we can recovery the bio from log disk, so we
518 * don't need to flush again
520 if (bio->bi_iter.bi_size == 0) {
524 bio->bi_rw &= ~REQ_FLUSH;
528 /* This will run after log space is reclaimed */
529 static void r5l_run_no_space_stripes(struct r5l_log *log)
531 struct stripe_head *sh;
533 spin_lock(&log->no_space_stripes_lock);
534 while (!list_empty(&log->no_space_stripes)) {
535 sh = list_first_entry(&log->no_space_stripes,
536 struct stripe_head, log_list);
537 list_del_init(&sh->log_list);
538 set_bit(STRIPE_HANDLE, &sh->state);
539 raid5_release_stripe(sh);
541 spin_unlock(&log->no_space_stripes_lock);
544 static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
546 struct r5l_log *log = io->log;
547 struct r5l_io_unit *last;
548 sector_t reclaimable_space;
551 spin_lock_irqsave(&log->io_list_lock, flags);
552 __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
553 /* might move 0 entry */
554 r5l_move_io_unit_list(&log->flushed_ios, &log->stripe_end_ios,
556 if (list_empty(&log->stripe_end_ios)) {
557 spin_unlock_irqrestore(&log->io_list_lock, flags);
561 last = list_last_entry(&log->stripe_end_ios,
562 struct r5l_io_unit, log_sibling);
563 reclaimable_space = r5l_ring_distance(log, log->last_checkpoint,
565 if (reclaimable_space >= log->max_free_space)
566 r5l_wake_reclaim(log, 0);
568 r5l_compress_stripe_end_list(log);
569 spin_unlock_irqrestore(&log->io_list_lock, flags);
570 wake_up(&log->iounit_wait);
573 void r5l_stripe_write_finished(struct stripe_head *sh)
575 struct r5l_io_unit *io;
580 if (io && atomic_dec_and_test(&io->pending_stripe))
581 __r5l_stripe_write_finished(io);
584 static void r5l_log_flush_endio(struct bio *bio)
586 struct r5l_log *log = container_of(bio, struct r5l_log,
589 struct r5l_io_unit *io;
590 struct stripe_head *sh;
592 spin_lock_irqsave(&log->io_list_lock, flags);
593 list_for_each_entry(io, &log->flushing_ios, log_sibling) {
594 while (!list_empty(&io->stripe_list)) {
595 sh = list_first_entry(&io->stripe_list,
596 struct stripe_head, log_list);
597 list_del_init(&sh->log_list);
598 set_bit(STRIPE_HANDLE, &sh->state);
599 raid5_release_stripe(sh);
602 list_splice_tail_init(&log->flushing_ios, &log->flushed_ios);
603 spin_unlock_irqrestore(&log->io_list_lock, flags);
607 * Starting dispatch IO to raid.
608 * io_unit(meta) consists of a log. There is one situation we want to avoid. A
609 * broken meta in the middle of a log causes recovery can't find meta at the
610 * head of log. If operations require meta at the head persistent in log, we
611 * must make sure meta before it persistent in log too. A case is:
613 * stripe data/parity is in log, we start write stripe to raid disks. stripe
614 * data/parity must be persistent in log before we do the write to raid disks.
616 * The solution is we restrictly maintain io_unit list order. In this case, we
617 * only write stripes of an io_unit to raid disks till the io_unit is the first
618 * one whose data/parity is in log.
620 void r5l_flush_stripe_to_raid(struct r5l_log *log)
626 spin_lock_irq(&log->io_list_lock);
627 /* flush bio is running */
628 if (!list_empty(&log->flushing_ios)) {
629 spin_unlock_irq(&log->io_list_lock);
632 list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
633 do_flush = !list_empty(&log->flushing_ios);
634 spin_unlock_irq(&log->io_list_lock);
638 bio_reset(&log->flush_bio);
639 log->flush_bio.bi_bdev = log->rdev->bdev;
640 log->flush_bio.bi_end_io = r5l_log_flush_endio;
641 submit_bio(WRITE_FLUSH, &log->flush_bio);
644 static void r5l_kick_io_unit(struct r5l_log *log)
646 md_wakeup_thread(log->rdev->mddev->thread);
647 wait_event_lock_irq(log->iounit_wait, !list_empty(&log->stripe_end_ios),
651 static void r5l_write_super(struct r5l_log *log, sector_t cp);
652 static void r5l_do_reclaim(struct r5l_log *log)
654 struct r5l_io_unit *io, *last;
657 sector_t reclaim_target = xchg(&log->reclaim_target, 0);
659 spin_lock_irq(&log->io_list_lock);
661 * move proper io_unit to reclaim list. We should not change the order.
662 * reclaimable/unreclaimable io_unit can be mixed in the list, we
663 * shouldn't reuse space of an unreclaimable io_unit
666 struct list_head *target_list = NULL;
668 while (!list_empty(&log->stripe_end_ios)) {
669 io = list_first_entry(&log->stripe_end_ios,
670 struct r5l_io_unit, log_sibling);
671 list_move_tail(&io->log_sibling, &list);
672 free += r5l_ring_distance(log, io->log_start,
676 if (free >= reclaim_target ||
677 (list_empty(&log->running_ios) &&
678 list_empty(&log->io_end_ios) &&
679 list_empty(&log->flushing_ios) &&
680 list_empty(&log->flushed_ios)))
683 /* Below waiting mostly happens when we shutdown the raid */
684 if (!list_empty(&log->flushed_ios))
685 target_list = &log->flushed_ios;
686 else if (!list_empty(&log->flushing_ios))
687 target_list = &log->flushing_ios;
688 else if (!list_empty(&log->io_end_ios))
689 target_list = &log->io_end_ios;
690 else if (!list_empty(&log->running_ios))
691 target_list = &log->running_ios;
693 r5l_kick_io_unit(log);
695 spin_unlock_irq(&log->io_list_lock);
697 if (list_empty(&list))
700 /* super always point to last valid meta */
701 last = list_last_entry(&list, struct r5l_io_unit, log_sibling);
703 * write_super will flush cache of each raid disk. We must write super
704 * here, because the log area might be reused soon and we don't want to
707 r5l_write_super(log, last->log_start);
709 mutex_lock(&log->io_mutex);
710 log->last_checkpoint = last->log_start;
711 log->last_cp_seq = last->seq;
712 mutex_unlock(&log->io_mutex);
713 r5l_run_no_space_stripes(log);
715 while (!list_empty(&list)) {
716 io = list_first_entry(&list, struct r5l_io_unit, log_sibling);
717 list_del(&io->log_sibling);
718 r5l_free_io_unit(log, io);
722 static void r5l_reclaim_thread(struct md_thread *thread)
724 struct mddev *mddev = thread->mddev;
725 struct r5conf *conf = mddev->private;
726 struct r5l_log *log = conf->log;
733 static void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
735 unsigned long target;
736 unsigned long new = (unsigned long)space; /* overflow in theory */
739 target = log->reclaim_target;
742 } while (cmpxchg(&log->reclaim_target, target, new) != target);
743 md_wakeup_thread(log->reclaim_thread);
746 struct r5l_recovery_ctx {
747 struct page *meta_page; /* current meta */
748 sector_t meta_total_blocks; /* total size of current meta and data */
749 sector_t pos; /* recovery position */
750 u64 seq; /* recovery position seq */
753 static int r5l_read_meta_block(struct r5l_log *log,
754 struct r5l_recovery_ctx *ctx)
756 struct page *page = ctx->meta_page;
757 struct r5l_meta_block *mb;
760 if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, READ, false))
763 mb = page_address(page);
764 stored_crc = le32_to_cpu(mb->checksum);
767 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
768 le64_to_cpu(mb->seq) != ctx->seq ||
769 mb->version != R5LOG_VERSION ||
770 le64_to_cpu(mb->position) != ctx->pos)
773 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
774 if (stored_crc != crc)
777 if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
780 ctx->meta_total_blocks = BLOCK_SECTORS;
785 static int r5l_recovery_flush_one_stripe(struct r5l_log *log,
786 struct r5l_recovery_ctx *ctx,
787 sector_t stripe_sect,
788 int *offset, sector_t *log_offset)
790 struct r5conf *conf = log->rdev->mddev->private;
791 struct stripe_head *sh;
792 struct r5l_payload_data_parity *payload;
795 sh = raid5_get_active_stripe(conf, stripe_sect, 0, 0, 0);
797 payload = page_address(ctx->meta_page) + *offset;
799 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) {
800 raid5_compute_sector(conf,
801 le64_to_cpu(payload->location), 0,
804 sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
805 sh->dev[disk_index].page, READ, false);
806 sh->dev[disk_index].log_checksum =
807 le32_to_cpu(payload->checksum[0]);
808 set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
809 ctx->meta_total_blocks += BLOCK_SECTORS;
811 disk_index = sh->pd_idx;
812 sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
813 sh->dev[disk_index].page, READ, false);
814 sh->dev[disk_index].log_checksum =
815 le32_to_cpu(payload->checksum[0]);
816 set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
818 if (sh->qd_idx >= 0) {
819 disk_index = sh->qd_idx;
820 sync_page_io(log->rdev,
821 r5l_ring_add(log, *log_offset, BLOCK_SECTORS),
822 PAGE_SIZE, sh->dev[disk_index].page,
824 sh->dev[disk_index].log_checksum =
825 le32_to_cpu(payload->checksum[1]);
826 set_bit(R5_Wantwrite,
827 &sh->dev[disk_index].flags);
829 ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
832 *log_offset = r5l_ring_add(log, *log_offset,
833 le32_to_cpu(payload->size));
834 *offset += sizeof(struct r5l_payload_data_parity) +
836 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
837 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY)
841 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
845 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
847 addr = kmap_atomic(sh->dev[disk_index].page);
848 checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
850 if (checksum != sh->dev[disk_index].log_checksum)
854 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
855 struct md_rdev *rdev, *rrdev;
857 if (!test_and_clear_bit(R5_Wantwrite,
858 &sh->dev[disk_index].flags))
861 /* in case device is broken */
862 rdev = rcu_dereference(conf->disks[disk_index].rdev);
864 sync_page_io(rdev, stripe_sect, PAGE_SIZE,
865 sh->dev[disk_index].page, WRITE, false);
866 rrdev = rcu_dereference(conf->disks[disk_index].replacement);
868 sync_page_io(rrdev, stripe_sect, PAGE_SIZE,
869 sh->dev[disk_index].page, WRITE, false);
871 raid5_release_stripe(sh);
875 for (disk_index = 0; disk_index < sh->disks; disk_index++)
876 sh->dev[disk_index].flags = 0;
877 raid5_release_stripe(sh);
881 static int r5l_recovery_flush_one_meta(struct r5l_log *log,
882 struct r5l_recovery_ctx *ctx)
884 struct r5conf *conf = log->rdev->mddev->private;
885 struct r5l_payload_data_parity *payload;
886 struct r5l_meta_block *mb;
889 sector_t stripe_sector;
891 mb = page_address(ctx->meta_page);
892 offset = sizeof(struct r5l_meta_block);
893 log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
895 while (offset < le32_to_cpu(mb->meta_size)) {
898 payload = (void *)mb + offset;
899 stripe_sector = raid5_compute_sector(conf,
900 le64_to_cpu(payload->location), 0, &dd, NULL);
901 if (r5l_recovery_flush_one_stripe(log, ctx, stripe_sector,
902 &offset, &log_offset))
908 /* copy data/parity from log to raid disks */
909 static void r5l_recovery_flush_log(struct r5l_log *log,
910 struct r5l_recovery_ctx *ctx)
913 if (r5l_read_meta_block(log, ctx))
915 if (r5l_recovery_flush_one_meta(log, ctx))
918 ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
922 static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
926 struct r5l_meta_block *mb;
929 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
932 mb = page_address(page);
933 mb->magic = cpu_to_le32(R5LOG_MAGIC);
934 mb->version = R5LOG_VERSION;
935 mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
936 mb->seq = cpu_to_le64(seq);
937 mb->position = cpu_to_le64(pos);
938 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
939 mb->checksum = cpu_to_le32(crc);
941 if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, WRITE_FUA, false)) {
949 static int r5l_recovery_log(struct r5l_log *log)
951 struct r5l_recovery_ctx ctx;
953 ctx.pos = log->last_checkpoint;
954 ctx.seq = log->last_cp_seq;
955 ctx.meta_page = alloc_page(GFP_KERNEL);
959 r5l_recovery_flush_log(log, &ctx);
960 __free_page(ctx.meta_page);
963 * we did a recovery. Now ctx.pos points to an invalid meta block. New
964 * log will start here. but we can't let superblock point to last valid
965 * meta block. The log might looks like:
966 * | meta 1| meta 2| meta 3|
967 * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
968 * superblock points to meta 1, we write a new valid meta 2n. if crash
969 * happens again, new recovery will start from meta 1. Since meta 2n is
970 * valid now, recovery will think meta 3 is valid, which is wrong.
971 * The solution is we create a new meta in meta2 with its seq == meta
972 * 1's seq + 10 and let superblock points to meta2. The same recovery will
973 * not think meta 3 is a valid meta, because its seq doesn't match
975 if (ctx.seq > log->last_cp_seq + 1) {
978 ret = r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq + 10);
981 log->seq = ctx.seq + 11;
982 log->log_start = r5l_ring_add(log, ctx.pos, BLOCK_SECTORS);
983 r5l_write_super(log, ctx.pos);
985 log->log_start = ctx.pos;
991 static void r5l_write_super(struct r5l_log *log, sector_t cp)
993 struct mddev *mddev = log->rdev->mddev;
995 log->rdev->journal_tail = cp;
996 set_bit(MD_CHANGE_DEVS, &mddev->flags);
999 static int r5l_load_log(struct r5l_log *log)
1001 struct md_rdev *rdev = log->rdev;
1003 struct r5l_meta_block *mb;
1004 sector_t cp = log->rdev->journal_tail;
1005 u32 stored_crc, expected_crc;
1006 bool create_super = false;
1009 /* Make sure it's valid */
1010 if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
1012 page = alloc_page(GFP_KERNEL);
1016 if (!sync_page_io(rdev, cp, PAGE_SIZE, page, READ, false)) {
1020 mb = page_address(page);
1022 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
1023 mb->version != R5LOG_VERSION) {
1024 create_super = true;
1027 stored_crc = le32_to_cpu(mb->checksum);
1029 expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
1030 if (stored_crc != expected_crc) {
1031 create_super = true;
1034 if (le64_to_cpu(mb->position) != cp) {
1035 create_super = true;
1040 log->last_cp_seq = prandom_u32();
1043 * Make sure super points to correct address. Log might have
1044 * data very soon. If super hasn't correct log tail address,
1045 * recovery can't find the log
1047 r5l_write_super(log, cp);
1049 log->last_cp_seq = le64_to_cpu(mb->seq);
1051 log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
1052 log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
1053 if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
1054 log->max_free_space = RECLAIM_MAX_FREE_SPACE;
1055 log->last_checkpoint = cp;
1059 return r5l_recovery_log(log);
1065 int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
1067 struct r5l_log *log;
1069 if (PAGE_SIZE != 4096)
1071 log = kzalloc(sizeof(*log), GFP_KERNEL);
1076 log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
1077 sizeof(rdev->mddev->uuid));
1079 mutex_init(&log->io_mutex);
1081 spin_lock_init(&log->io_list_lock);
1082 INIT_LIST_HEAD(&log->running_ios);
1083 INIT_LIST_HEAD(&log->io_end_ios);
1084 INIT_LIST_HEAD(&log->stripe_end_ios);
1085 INIT_LIST_HEAD(&log->flushing_ios);
1086 INIT_LIST_HEAD(&log->flushed_ios);
1087 bio_init(&log->flush_bio);
1089 log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
1093 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
1094 log->rdev->mddev, "reclaim");
1095 if (!log->reclaim_thread)
1096 goto reclaim_thread;
1097 init_waitqueue_head(&log->iounit_wait);
1099 INIT_LIST_HEAD(&log->no_space_stripes);
1100 spin_lock_init(&log->no_space_stripes_lock);
1102 if (r5l_load_log(log))
1108 md_unregister_thread(&log->reclaim_thread);
1110 kmem_cache_destroy(log->io_kc);
1116 void r5l_exit_log(struct r5l_log *log)
1119 * at this point all stripes are finished, so io_unit is at least in
1122 r5l_wake_reclaim(log, -1L);
1123 md_unregister_thread(&log->reclaim_thread);
1124 r5l_do_reclaim(log);
1126 * force a super update, r5l_do_reclaim might updated the super.
1127 * mddev->thread is already stopped
1129 md_update_sb(log->rdev->mddev, 1);
1131 kmem_cache_destroy(log->io_kc);