1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * raid1.c : Multiple Devices driver for Linux
5 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
7 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
9 * RAID-1 management functions.
11 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
13 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
14 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
16 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
17 * bitmapped intelligence in resync:
19 * - bitmap marked during normal i/o
20 * - bitmap used to skip nondirty blocks during sync
22 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
23 * - persistent bitmap code
26 #include <linux/slab.h>
27 #include <linux/delay.h>
28 #include <linux/blkdev.h>
29 #include <linux/module.h>
30 #include <linux/seq_file.h>
31 #include <linux/ratelimit.h>
33 #include <trace/events/block.h>
37 #include "md-bitmap.h"
39 #define UNSUPPORTED_MDDEV_FLAGS \
40 ((1L << MD_HAS_JOURNAL) | \
41 (1L << MD_JOURNAL_CLEAN) | \
42 (1L << MD_HAS_PPL) | \
43 (1L << MD_HAS_MULTIPLE_PPLS))
45 static void allow_barrier(struct r1conf *conf, sector_t sector_nr);
46 static void lower_barrier(struct r1conf *conf, sector_t sector_nr);
48 #define raid1_log(md, fmt, args...) \
49 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid1 " fmt, ##args); } while (0)
53 static int check_and_add_wb(struct md_rdev *rdev, sector_t lo, sector_t hi)
55 struct wb_info *wi, *temp_wi;
58 struct mddev *mddev = rdev->mddev;
60 wi = mempool_alloc(mddev->wb_info_pool, GFP_NOIO);
62 spin_lock_irqsave(&rdev->wb_list_lock, flags);
63 list_for_each_entry(temp_wi, &rdev->wb_list, list) {
64 /* collision happened */
65 if (hi > temp_wi->lo && lo < temp_wi->hi) {
74 list_add(&wi->list, &rdev->wb_list);
76 mempool_free(wi, mddev->wb_info_pool);
77 spin_unlock_irqrestore(&rdev->wb_list_lock, flags);
82 static void remove_wb(struct md_rdev *rdev, sector_t lo, sector_t hi)
87 struct mddev *mddev = rdev->mddev;
89 spin_lock_irqsave(&rdev->wb_list_lock, flags);
90 list_for_each_entry(wi, &rdev->wb_list, list)
91 if (hi == wi->hi && lo == wi->lo) {
93 mempool_free(wi, mddev->wb_info_pool);
99 WARN(1, "The write behind IO is not recorded\n");
100 spin_unlock_irqrestore(&rdev->wb_list_lock, flags);
101 wake_up(&rdev->wb_io_wait);
105 * for resync bio, r1bio pointer can be retrieved from the per-bio
106 * 'struct resync_pages'.
108 static inline struct r1bio *get_resync_r1bio(struct bio *bio)
110 return get_resync_pages(bio)->raid_bio;
113 static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
115 struct pool_info *pi = data;
116 int size = offsetof(struct r1bio, bios[pi->raid_disks]);
118 /* allocate a r1bio with room for raid_disks entries in the bios array */
119 return kzalloc(size, gfp_flags);
122 #define RESYNC_DEPTH 32
123 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
124 #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
125 #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
126 #define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
127 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
129 static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
131 struct pool_info *pi = data;
132 struct r1bio *r1_bio;
136 struct resync_pages *rps;
138 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
142 rps = kmalloc_array(pi->raid_disks, sizeof(struct resync_pages),
148 * Allocate bios : 1 for reading, n-1 for writing
150 for (j = pi->raid_disks ; j-- ; ) {
151 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
154 r1_bio->bios[j] = bio;
157 * Allocate RESYNC_PAGES data pages and attach them to
159 * If this is a user-requested check/repair, allocate
160 * RESYNC_PAGES for each bio.
162 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
163 need_pages = pi->raid_disks;
166 for (j = 0; j < pi->raid_disks; j++) {
167 struct resync_pages *rp = &rps[j];
169 bio = r1_bio->bios[j];
171 if (j < need_pages) {
172 if (resync_alloc_pages(rp, gfp_flags))
175 memcpy(rp, &rps[0], sizeof(*rp));
176 resync_get_all_pages(rp);
179 rp->raid_bio = r1_bio;
180 bio->bi_private = rp;
183 r1_bio->master_bio = NULL;
189 resync_free_pages(&rps[j]);
192 while (++j < pi->raid_disks)
193 bio_put(r1_bio->bios[j]);
197 rbio_pool_free(r1_bio, data);
201 static void r1buf_pool_free(void *__r1_bio, void *data)
203 struct pool_info *pi = data;
205 struct r1bio *r1bio = __r1_bio;
206 struct resync_pages *rp = NULL;
208 for (i = pi->raid_disks; i--; ) {
209 rp = get_resync_pages(r1bio->bios[i]);
210 resync_free_pages(rp);
211 bio_put(r1bio->bios[i]);
214 /* resync pages array stored in the 1st bio's .bi_private */
217 rbio_pool_free(r1bio, data);
220 static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
224 for (i = 0; i < conf->raid_disks * 2; i++) {
225 struct bio **bio = r1_bio->bios + i;
226 if (!BIO_SPECIAL(*bio))
232 static void free_r1bio(struct r1bio *r1_bio)
234 struct r1conf *conf = r1_bio->mddev->private;
236 put_all_bios(conf, r1_bio);
237 mempool_free(r1_bio, &conf->r1bio_pool);
240 static void put_buf(struct r1bio *r1_bio)
242 struct r1conf *conf = r1_bio->mddev->private;
243 sector_t sect = r1_bio->sector;
246 for (i = 0; i < conf->raid_disks * 2; i++) {
247 struct bio *bio = r1_bio->bios[i];
249 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
252 mempool_free(r1_bio, &conf->r1buf_pool);
254 lower_barrier(conf, sect);
257 static void reschedule_retry(struct r1bio *r1_bio)
260 struct mddev *mddev = r1_bio->mddev;
261 struct r1conf *conf = mddev->private;
264 idx = sector_to_idx(r1_bio->sector);
265 spin_lock_irqsave(&conf->device_lock, flags);
266 list_add(&r1_bio->retry_list, &conf->retry_list);
267 atomic_inc(&conf->nr_queued[idx]);
268 spin_unlock_irqrestore(&conf->device_lock, flags);
270 wake_up(&conf->wait_barrier);
271 md_wakeup_thread(mddev->thread);
275 * raid_end_bio_io() is called when we have finished servicing a mirrored
276 * operation and are ready to return a success/failure code to the buffer
279 static void call_bio_endio(struct r1bio *r1_bio)
281 struct bio *bio = r1_bio->master_bio;
282 struct r1conf *conf = r1_bio->mddev->private;
284 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
285 bio->bi_status = BLK_STS_IOERR;
289 * Wake up any possible resync thread that waits for the device
292 allow_barrier(conf, r1_bio->sector);
295 static void raid_end_bio_io(struct r1bio *r1_bio)
297 struct bio *bio = r1_bio->master_bio;
299 /* if nobody has done the final endio yet, do it now */
300 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
301 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
302 (bio_data_dir(bio) == WRITE) ? "write" : "read",
303 (unsigned long long) bio->bi_iter.bi_sector,
304 (unsigned long long) bio_end_sector(bio) - 1);
306 call_bio_endio(r1_bio);
312 * Update disk head position estimator based on IRQ completion info.
314 static inline void update_head_pos(int disk, struct r1bio *r1_bio)
316 struct r1conf *conf = r1_bio->mddev->private;
318 conf->mirrors[disk].head_position =
319 r1_bio->sector + (r1_bio->sectors);
323 * Find the disk number which triggered given bio
325 static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
328 struct r1conf *conf = r1_bio->mddev->private;
329 int raid_disks = conf->raid_disks;
331 for (mirror = 0; mirror < raid_disks * 2; mirror++)
332 if (r1_bio->bios[mirror] == bio)
335 BUG_ON(mirror == raid_disks * 2);
336 update_head_pos(mirror, r1_bio);
341 static void raid1_end_read_request(struct bio *bio)
343 int uptodate = !bio->bi_status;
344 struct r1bio *r1_bio = bio->bi_private;
345 struct r1conf *conf = r1_bio->mddev->private;
346 struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
349 * this branch is our 'one mirror IO has finished' event handler:
351 update_head_pos(r1_bio->read_disk, r1_bio);
354 set_bit(R1BIO_Uptodate, &r1_bio->state);
355 else if (test_bit(FailFast, &rdev->flags) &&
356 test_bit(R1BIO_FailFast, &r1_bio->state))
357 /* This was a fail-fast read so we definitely
361 /* If all other devices have failed, we want to return
362 * the error upwards rather than fail the last device.
363 * Here we redefine "uptodate" to mean "Don't want to retry"
366 spin_lock_irqsave(&conf->device_lock, flags);
367 if (r1_bio->mddev->degraded == conf->raid_disks ||
368 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
369 test_bit(In_sync, &rdev->flags)))
371 spin_unlock_irqrestore(&conf->device_lock, flags);
375 raid_end_bio_io(r1_bio);
376 rdev_dec_pending(rdev, conf->mddev);
381 char b[BDEVNAME_SIZE];
382 pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n",
384 bdevname(rdev->bdev, b),
385 (unsigned long long)r1_bio->sector);
386 set_bit(R1BIO_ReadError, &r1_bio->state);
387 reschedule_retry(r1_bio);
388 /* don't drop the reference on read_disk yet */
392 static void close_write(struct r1bio *r1_bio)
394 /* it really is the end of this request */
395 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
396 bio_free_pages(r1_bio->behind_master_bio);
397 bio_put(r1_bio->behind_master_bio);
398 r1_bio->behind_master_bio = NULL;
400 /* clear the bitmap if all writes complete successfully */
401 md_bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
403 !test_bit(R1BIO_Degraded, &r1_bio->state),
404 test_bit(R1BIO_BehindIO, &r1_bio->state));
405 md_write_end(r1_bio->mddev);
408 static void r1_bio_write_done(struct r1bio *r1_bio)
410 if (!atomic_dec_and_test(&r1_bio->remaining))
413 if (test_bit(R1BIO_WriteError, &r1_bio->state))
414 reschedule_retry(r1_bio);
417 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
418 reschedule_retry(r1_bio);
420 raid_end_bio_io(r1_bio);
424 static void raid1_end_write_request(struct bio *bio)
426 struct r1bio *r1_bio = bio->bi_private;
427 int behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
428 struct r1conf *conf = r1_bio->mddev->private;
429 struct bio *to_put = NULL;
430 int mirror = find_bio_disk(r1_bio, bio);
431 struct md_rdev *rdev = conf->mirrors[mirror].rdev;
434 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
437 * 'one mirror IO has finished' event handler:
439 if (bio->bi_status && !discard_error) {
440 set_bit(WriteErrorSeen, &rdev->flags);
441 if (!test_and_set_bit(WantReplacement, &rdev->flags))
442 set_bit(MD_RECOVERY_NEEDED, &
443 conf->mddev->recovery);
445 if (test_bit(FailFast, &rdev->flags) &&
446 (bio->bi_opf & MD_FAILFAST) &&
447 /* We never try FailFast to WriteMostly devices */
448 !test_bit(WriteMostly, &rdev->flags)) {
449 md_error(r1_bio->mddev, rdev);
450 if (!test_bit(Faulty, &rdev->flags))
451 /* This is the only remaining device,
452 * We need to retry the write without
455 set_bit(R1BIO_WriteError, &r1_bio->state);
457 /* Finished with this branch */
458 r1_bio->bios[mirror] = NULL;
462 set_bit(R1BIO_WriteError, &r1_bio->state);
465 * Set R1BIO_Uptodate in our master bio, so that we
466 * will return a good error code for to the higher
467 * levels even if IO on some other mirrored buffer
470 * The 'master' represents the composite IO operation
471 * to user-side. So if something waits for IO, then it
472 * will wait for the 'master' bio.
477 r1_bio->bios[mirror] = NULL;
480 * Do not set R1BIO_Uptodate if the current device is
481 * rebuilding or Faulty. This is because we cannot use
482 * such device for properly reading the data back (we could
483 * potentially use it, if the current write would have felt
484 * before rdev->recovery_offset, but for simplicity we don't
487 if (test_bit(In_sync, &rdev->flags) &&
488 !test_bit(Faulty, &rdev->flags))
489 set_bit(R1BIO_Uptodate, &r1_bio->state);
491 /* Maybe we can clear some bad blocks. */
492 if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
493 &first_bad, &bad_sectors) && !discard_error) {
494 r1_bio->bios[mirror] = IO_MADE_GOOD;
495 set_bit(R1BIO_MadeGood, &r1_bio->state);
500 if (test_bit(WBCollisionCheck, &rdev->flags)) {
501 sector_t lo = r1_bio->sector;
502 sector_t hi = r1_bio->sector + r1_bio->sectors;
504 remove_wb(rdev, lo, hi);
506 if (test_bit(WriteMostly, &rdev->flags))
507 atomic_dec(&r1_bio->behind_remaining);
510 * In behind mode, we ACK the master bio once the I/O
511 * has safely reached all non-writemostly
512 * disks. Setting the Returned bit ensures that this
513 * gets done only once -- we don't ever want to return
514 * -EIO here, instead we'll wait
516 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
517 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
518 /* Maybe we can return now */
519 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
520 struct bio *mbio = r1_bio->master_bio;
521 pr_debug("raid1: behind end write sectors"
523 (unsigned long long) mbio->bi_iter.bi_sector,
524 (unsigned long long) bio_end_sector(mbio) - 1);
525 call_bio_endio(r1_bio);
529 if (r1_bio->bios[mirror] == NULL)
530 rdev_dec_pending(rdev, conf->mddev);
533 * Let's see if all mirrored write operations have finished
536 r1_bio_write_done(r1_bio);
542 static sector_t align_to_barrier_unit_end(sector_t start_sector,
547 WARN_ON(sectors == 0);
549 * len is the number of sectors from start_sector to end of the
550 * barrier unit which start_sector belongs to.
552 len = round_up(start_sector + 1, BARRIER_UNIT_SECTOR_SIZE) -
562 * This routine returns the disk from which the requested read should
563 * be done. There is a per-array 'next expected sequential IO' sector
564 * number - if this matches on the next IO then we use the last disk.
565 * There is also a per-disk 'last know head position' sector that is
566 * maintained from IRQ contexts, both the normal and the resync IO
567 * completion handlers update this position correctly. If there is no
568 * perfect sequential match then we pick the disk whose head is closest.
570 * If there are 2 mirrors in the same 2 devices, performance degrades
571 * because position is mirror, not device based.
573 * The rdev for the device selected will have nr_pending incremented.
575 static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
577 const sector_t this_sector = r1_bio->sector;
579 int best_good_sectors;
580 int best_disk, best_dist_disk, best_pending_disk;
584 unsigned int min_pending;
585 struct md_rdev *rdev;
587 int choose_next_idle;
591 * Check if we can balance. We can balance on the whole
592 * device if no resync is going on, or below the resync window.
593 * We take the first readable disk when above the resync window.
596 sectors = r1_bio->sectors;
599 best_dist = MaxSector;
600 best_pending_disk = -1;
601 min_pending = UINT_MAX;
602 best_good_sectors = 0;
604 choose_next_idle = 0;
605 clear_bit(R1BIO_FailFast, &r1_bio->state);
607 if ((conf->mddev->recovery_cp < this_sector + sectors) ||
608 (mddev_is_clustered(conf->mddev) &&
609 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
610 this_sector + sectors)))
615 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
619 unsigned int pending;
622 rdev = rcu_dereference(conf->mirrors[disk].rdev);
623 if (r1_bio->bios[disk] == IO_BLOCKED
625 || test_bit(Faulty, &rdev->flags))
627 if (!test_bit(In_sync, &rdev->flags) &&
628 rdev->recovery_offset < this_sector + sectors)
630 if (test_bit(WriteMostly, &rdev->flags)) {
631 /* Don't balance among write-mostly, just
632 * use the first as a last resort */
633 if (best_dist_disk < 0) {
634 if (is_badblock(rdev, this_sector, sectors,
635 &first_bad, &bad_sectors)) {
636 if (first_bad <= this_sector)
637 /* Cannot use this */
639 best_good_sectors = first_bad - this_sector;
641 best_good_sectors = sectors;
642 best_dist_disk = disk;
643 best_pending_disk = disk;
647 /* This is a reasonable device to use. It might
650 if (is_badblock(rdev, this_sector, sectors,
651 &first_bad, &bad_sectors)) {
652 if (best_dist < MaxSector)
653 /* already have a better device */
655 if (first_bad <= this_sector) {
656 /* cannot read here. If this is the 'primary'
657 * device, then we must not read beyond
658 * bad_sectors from another device..
660 bad_sectors -= (this_sector - first_bad);
661 if (choose_first && sectors > bad_sectors)
662 sectors = bad_sectors;
663 if (best_good_sectors > sectors)
664 best_good_sectors = sectors;
667 sector_t good_sectors = first_bad - this_sector;
668 if (good_sectors > best_good_sectors) {
669 best_good_sectors = good_sectors;
677 if ((sectors > best_good_sectors) && (best_disk >= 0))
679 best_good_sectors = sectors;
683 /* At least two disks to choose from so failfast is OK */
684 set_bit(R1BIO_FailFast, &r1_bio->state);
686 nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
687 has_nonrot_disk |= nonrot;
688 pending = atomic_read(&rdev->nr_pending);
689 dist = abs(this_sector - conf->mirrors[disk].head_position);
694 /* Don't change to another disk for sequential reads */
695 if (conf->mirrors[disk].next_seq_sect == this_sector
697 int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
698 struct raid1_info *mirror = &conf->mirrors[disk];
702 * If buffered sequential IO size exceeds optimal
703 * iosize, check if there is idle disk. If yes, choose
704 * the idle disk. read_balance could already choose an
705 * idle disk before noticing it's a sequential IO in
706 * this disk. This doesn't matter because this disk
707 * will idle, next time it will be utilized after the
708 * first disk has IO size exceeds optimal iosize. In
709 * this way, iosize of the first disk will be optimal
710 * iosize at least. iosize of the second disk might be
711 * small, but not a big deal since when the second disk
712 * starts IO, the first disk is likely still busy.
714 if (nonrot && opt_iosize > 0 &&
715 mirror->seq_start != MaxSector &&
716 mirror->next_seq_sect > opt_iosize &&
717 mirror->next_seq_sect - opt_iosize >=
719 choose_next_idle = 1;
725 if (choose_next_idle)
728 if (min_pending > pending) {
729 min_pending = pending;
730 best_pending_disk = disk;
733 if (dist < best_dist) {
735 best_dist_disk = disk;
740 * If all disks are rotational, choose the closest disk. If any disk is
741 * non-rotational, choose the disk with less pending request even the
742 * disk is rotational, which might/might not be optimal for raids with
743 * mixed ratation/non-rotational disks depending on workload.
745 if (best_disk == -1) {
746 if (has_nonrot_disk || min_pending == 0)
747 best_disk = best_pending_disk;
749 best_disk = best_dist_disk;
752 if (best_disk >= 0) {
753 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
756 atomic_inc(&rdev->nr_pending);
757 sectors = best_good_sectors;
759 if (conf->mirrors[best_disk].next_seq_sect != this_sector)
760 conf->mirrors[best_disk].seq_start = this_sector;
762 conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
765 *max_sectors = sectors;
770 static int raid1_congested(struct mddev *mddev, int bits)
772 struct r1conf *conf = mddev->private;
775 if ((bits & (1 << WB_async_congested)) &&
776 conf->pending_count >= max_queued_requests)
780 for (i = 0; i < conf->raid_disks * 2; i++) {
781 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
782 if (rdev && !test_bit(Faulty, &rdev->flags)) {
783 struct request_queue *q = bdev_get_queue(rdev->bdev);
787 /* Note the '|| 1' - when read_balance prefers
788 * non-congested targets, it can be removed
790 if ((bits & (1 << WB_async_congested)) || 1)
791 ret |= bdi_congested(q->backing_dev_info, bits);
793 ret &= bdi_congested(q->backing_dev_info, bits);
800 static void flush_bio_list(struct r1conf *conf, struct bio *bio)
802 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
803 md_bitmap_unplug(conf->mddev->bitmap);
804 wake_up(&conf->wait_barrier);
806 while (bio) { /* submit pending writes */
807 struct bio *next = bio->bi_next;
808 struct md_rdev *rdev = (void *)bio->bi_disk;
810 bio_set_dev(bio, rdev->bdev);
811 if (test_bit(Faulty, &rdev->flags)) {
813 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
814 !blk_queue_discard(bio->bi_disk->queue)))
818 generic_make_request(bio);
823 static void flush_pending_writes(struct r1conf *conf)
825 /* Any writes that have been queued but are awaiting
826 * bitmap updates get flushed here.
828 spin_lock_irq(&conf->device_lock);
830 if (conf->pending_bio_list.head) {
831 struct blk_plug plug;
834 bio = bio_list_get(&conf->pending_bio_list);
835 conf->pending_count = 0;
836 spin_unlock_irq(&conf->device_lock);
839 * As this is called in a wait_event() loop (see freeze_array),
840 * current->state might be TASK_UNINTERRUPTIBLE which will
841 * cause a warning when we prepare to wait again. As it is
842 * rare that this path is taken, it is perfectly safe to force
843 * us to go around the wait_event() loop again, so the warning
844 * is a false-positive. Silence the warning by resetting
847 __set_current_state(TASK_RUNNING);
848 blk_start_plug(&plug);
849 flush_bio_list(conf, bio);
850 blk_finish_plug(&plug);
852 spin_unlock_irq(&conf->device_lock);
856 * Sometimes we need to suspend IO while we do something else,
857 * either some resync/recovery, or reconfigure the array.
858 * To do this we raise a 'barrier'.
859 * The 'barrier' is a counter that can be raised multiple times
860 * to count how many activities are happening which preclude
862 * We can only raise the barrier if there is no pending IO.
863 * i.e. if nr_pending == 0.
864 * We choose only to raise the barrier if no-one is waiting for the
865 * barrier to go down. This means that as soon as an IO request
866 * is ready, no other operations which require a barrier will start
867 * until the IO request has had a chance.
869 * So: regular IO calls 'wait_barrier'. When that returns there
870 * is no backgroup IO happening, It must arrange to call
871 * allow_barrier when it has finished its IO.
872 * backgroup IO calls must call raise_barrier. Once that returns
873 * there is no normal IO happeing. It must arrange to call
874 * lower_barrier when the particular background IO completes.
876 static sector_t raise_barrier(struct r1conf *conf, sector_t sector_nr)
878 int idx = sector_to_idx(sector_nr);
880 spin_lock_irq(&conf->resync_lock);
882 /* Wait until no block IO is waiting */
883 wait_event_lock_irq(conf->wait_barrier,
884 !atomic_read(&conf->nr_waiting[idx]),
887 /* block any new IO from starting */
888 atomic_inc(&conf->barrier[idx]);
890 * In raise_barrier() we firstly increase conf->barrier[idx] then
891 * check conf->nr_pending[idx]. In _wait_barrier() we firstly
892 * increase conf->nr_pending[idx] then check conf->barrier[idx].
893 * A memory barrier here to make sure conf->nr_pending[idx] won't
894 * be fetched before conf->barrier[idx] is increased. Otherwise
895 * there will be a race between raise_barrier() and _wait_barrier().
897 smp_mb__after_atomic();
899 /* For these conditions we must wait:
900 * A: while the array is in frozen state
901 * B: while conf->nr_pending[idx] is not 0, meaning regular I/O
902 * existing in corresponding I/O barrier bucket.
903 * C: while conf->barrier[idx] >= RESYNC_DEPTH, meaning reaches
904 * max resync count which allowed on current I/O barrier bucket.
906 wait_event_lock_irq(conf->wait_barrier,
907 (!conf->array_frozen &&
908 !atomic_read(&conf->nr_pending[idx]) &&
909 atomic_read(&conf->barrier[idx]) < RESYNC_DEPTH) ||
910 test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery),
913 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
914 atomic_dec(&conf->barrier[idx]);
915 spin_unlock_irq(&conf->resync_lock);
916 wake_up(&conf->wait_barrier);
920 atomic_inc(&conf->nr_sync_pending);
921 spin_unlock_irq(&conf->resync_lock);
926 static void lower_barrier(struct r1conf *conf, sector_t sector_nr)
928 int idx = sector_to_idx(sector_nr);
930 BUG_ON(atomic_read(&conf->barrier[idx]) <= 0);
932 atomic_dec(&conf->barrier[idx]);
933 atomic_dec(&conf->nr_sync_pending);
934 wake_up(&conf->wait_barrier);
937 static void _wait_barrier(struct r1conf *conf, int idx)
940 * We need to increase conf->nr_pending[idx] very early here,
941 * then raise_barrier() can be blocked when it waits for
942 * conf->nr_pending[idx] to be 0. Then we can avoid holding
943 * conf->resync_lock when there is no barrier raised in same
944 * barrier unit bucket. Also if the array is frozen, I/O
945 * should be blocked until array is unfrozen.
947 atomic_inc(&conf->nr_pending[idx]);
949 * In _wait_barrier() we firstly increase conf->nr_pending[idx], then
950 * check conf->barrier[idx]. In raise_barrier() we firstly increase
951 * conf->barrier[idx], then check conf->nr_pending[idx]. A memory
952 * barrier is necessary here to make sure conf->barrier[idx] won't be
953 * fetched before conf->nr_pending[idx] is increased. Otherwise there
954 * will be a race between _wait_barrier() and raise_barrier().
956 smp_mb__after_atomic();
959 * Don't worry about checking two atomic_t variables at same time
960 * here. If during we check conf->barrier[idx], the array is
961 * frozen (conf->array_frozen is 1), and chonf->barrier[idx] is
962 * 0, it is safe to return and make the I/O continue. Because the
963 * array is frozen, all I/O returned here will eventually complete
964 * or be queued, no race will happen. See code comment in
967 if (!READ_ONCE(conf->array_frozen) &&
968 !atomic_read(&conf->barrier[idx]))
972 * After holding conf->resync_lock, conf->nr_pending[idx]
973 * should be decreased before waiting for barrier to drop.
974 * Otherwise, we may encounter a race condition because
975 * raise_barrer() might be waiting for conf->nr_pending[idx]
976 * to be 0 at same time.
978 spin_lock_irq(&conf->resync_lock);
979 atomic_inc(&conf->nr_waiting[idx]);
980 atomic_dec(&conf->nr_pending[idx]);
982 * In case freeze_array() is waiting for
983 * get_unqueued_pending() == extra
985 wake_up(&conf->wait_barrier);
986 /* Wait for the barrier in same barrier unit bucket to drop. */
987 wait_event_lock_irq(conf->wait_barrier,
988 !conf->array_frozen &&
989 !atomic_read(&conf->barrier[idx]),
991 atomic_inc(&conf->nr_pending[idx]);
992 atomic_dec(&conf->nr_waiting[idx]);
993 spin_unlock_irq(&conf->resync_lock);
996 static void wait_read_barrier(struct r1conf *conf, sector_t sector_nr)
998 int idx = sector_to_idx(sector_nr);
1001 * Very similar to _wait_barrier(). The difference is, for read
1002 * I/O we don't need wait for sync I/O, but if the whole array
1003 * is frozen, the read I/O still has to wait until the array is
1004 * unfrozen. Since there is no ordering requirement with
1005 * conf->barrier[idx] here, memory barrier is unnecessary as well.
1007 atomic_inc(&conf->nr_pending[idx]);
1009 if (!READ_ONCE(conf->array_frozen))
1012 spin_lock_irq(&conf->resync_lock);
1013 atomic_inc(&conf->nr_waiting[idx]);
1014 atomic_dec(&conf->nr_pending[idx]);
1016 * In case freeze_array() is waiting for
1017 * get_unqueued_pending() == extra
1019 wake_up(&conf->wait_barrier);
1020 /* Wait for array to be unfrozen */
1021 wait_event_lock_irq(conf->wait_barrier,
1022 !conf->array_frozen,
1024 atomic_inc(&conf->nr_pending[idx]);
1025 atomic_dec(&conf->nr_waiting[idx]);
1026 spin_unlock_irq(&conf->resync_lock);
1029 static void wait_barrier(struct r1conf *conf, sector_t sector_nr)
1031 int idx = sector_to_idx(sector_nr);
1033 _wait_barrier(conf, idx);
1036 static void _allow_barrier(struct r1conf *conf, int idx)
1038 atomic_dec(&conf->nr_pending[idx]);
1039 wake_up(&conf->wait_barrier);
1042 static void allow_barrier(struct r1conf *conf, sector_t sector_nr)
1044 int idx = sector_to_idx(sector_nr);
1046 _allow_barrier(conf, idx);
1049 /* conf->resync_lock should be held */
1050 static int get_unqueued_pending(struct r1conf *conf)
1054 ret = atomic_read(&conf->nr_sync_pending);
1055 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
1056 ret += atomic_read(&conf->nr_pending[idx]) -
1057 atomic_read(&conf->nr_queued[idx]);
1062 static void freeze_array(struct r1conf *conf, int extra)
1064 /* Stop sync I/O and normal I/O and wait for everything to
1066 * This is called in two situations:
1067 * 1) management command handlers (reshape, remove disk, quiesce).
1068 * 2) one normal I/O request failed.
1070 * After array_frozen is set to 1, new sync IO will be blocked at
1071 * raise_barrier(), and new normal I/O will blocked at _wait_barrier()
1072 * or wait_read_barrier(). The flying I/Os will either complete or be
1073 * queued. When everything goes quite, there are only queued I/Os left.
1075 * Every flying I/O contributes to a conf->nr_pending[idx], idx is the
1076 * barrier bucket index which this I/O request hits. When all sync and
1077 * normal I/O are queued, sum of all conf->nr_pending[] will match sum
1078 * of all conf->nr_queued[]. But normal I/O failure is an exception,
1079 * in handle_read_error(), we may call freeze_array() before trying to
1080 * fix the read error. In this case, the error read I/O is not queued,
1081 * so get_unqueued_pending() == 1.
1083 * Therefore before this function returns, we need to wait until
1084 * get_unqueued_pendings(conf) gets equal to extra. For
1085 * normal I/O context, extra is 1, in rested situations extra is 0.
1087 spin_lock_irq(&conf->resync_lock);
1088 conf->array_frozen = 1;
1089 raid1_log(conf->mddev, "wait freeze");
1090 wait_event_lock_irq_cmd(
1092 get_unqueued_pending(conf) == extra,
1094 flush_pending_writes(conf));
1095 spin_unlock_irq(&conf->resync_lock);
1097 static void unfreeze_array(struct r1conf *conf)
1099 /* reverse the effect of the freeze */
1100 spin_lock_irq(&conf->resync_lock);
1101 conf->array_frozen = 0;
1102 spin_unlock_irq(&conf->resync_lock);
1103 wake_up(&conf->wait_barrier);
1106 static void alloc_behind_master_bio(struct r1bio *r1_bio,
1109 int size = bio->bi_iter.bi_size;
1110 unsigned vcnt = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1112 struct bio *behind_bio = NULL;
1114 behind_bio = bio_alloc_mddev(GFP_NOIO, vcnt, r1_bio->mddev);
1118 /* discard op, we don't support writezero/writesame yet */
1119 if (!bio_has_data(bio)) {
1120 behind_bio->bi_iter.bi_size = size;
1124 behind_bio->bi_write_hint = bio->bi_write_hint;
1126 while (i < vcnt && size) {
1128 int len = min_t(int, PAGE_SIZE, size);
1130 page = alloc_page(GFP_NOIO);
1131 if (unlikely(!page))
1134 bio_add_page(behind_bio, page, len, 0);
1140 bio_copy_data(behind_bio, bio);
1142 r1_bio->behind_master_bio = behind_bio;
1143 set_bit(R1BIO_BehindIO, &r1_bio->state);
1148 pr_debug("%dB behind alloc failed, doing sync I/O\n",
1149 bio->bi_iter.bi_size);
1150 bio_free_pages(behind_bio);
1151 bio_put(behind_bio);
1154 struct raid1_plug_cb {
1155 struct blk_plug_cb cb;
1156 struct bio_list pending;
1160 static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
1162 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
1164 struct mddev *mddev = plug->cb.data;
1165 struct r1conf *conf = mddev->private;
1168 if (from_schedule || current->bio_list) {
1169 spin_lock_irq(&conf->device_lock);
1170 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1171 conf->pending_count += plug->pending_cnt;
1172 spin_unlock_irq(&conf->device_lock);
1173 wake_up(&conf->wait_barrier);
1174 md_wakeup_thread(mddev->thread);
1179 /* we aren't scheduling, so we can do the write-out directly. */
1180 bio = bio_list_get(&plug->pending);
1181 flush_bio_list(conf, bio);
1185 static void init_r1bio(struct r1bio *r1_bio, struct mddev *mddev, struct bio *bio)
1187 r1_bio->master_bio = bio;
1188 r1_bio->sectors = bio_sectors(bio);
1190 r1_bio->mddev = mddev;
1191 r1_bio->sector = bio->bi_iter.bi_sector;
1194 static inline struct r1bio *
1195 alloc_r1bio(struct mddev *mddev, struct bio *bio)
1197 struct r1conf *conf = mddev->private;
1198 struct r1bio *r1_bio;
1200 r1_bio = mempool_alloc(&conf->r1bio_pool, GFP_NOIO);
1201 /* Ensure no bio records IO_BLOCKED */
1202 memset(r1_bio->bios, 0, conf->raid_disks * sizeof(r1_bio->bios[0]));
1203 init_r1bio(r1_bio, mddev, bio);
1207 static void raid1_read_request(struct mddev *mddev, struct bio *bio,
1208 int max_read_sectors, struct r1bio *r1_bio)
1210 struct r1conf *conf = mddev->private;
1211 struct raid1_info *mirror;
1212 struct bio *read_bio;
1213 struct bitmap *bitmap = mddev->bitmap;
1214 const int op = bio_op(bio);
1215 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1218 bool print_msg = !!r1_bio;
1219 char b[BDEVNAME_SIZE];
1222 * If r1_bio is set, we are blocking the raid1d thread
1223 * so there is a tiny risk of deadlock. So ask for
1224 * emergency memory if needed.
1226 gfp_t gfp = r1_bio ? (GFP_NOIO | __GFP_HIGH) : GFP_NOIO;
1229 /* Need to get the block device name carefully */
1230 struct md_rdev *rdev;
1232 rdev = rcu_dereference(conf->mirrors[r1_bio->read_disk].rdev);
1234 bdevname(rdev->bdev, b);
1241 * Still need barrier for READ in case that whole
1244 wait_read_barrier(conf, bio->bi_iter.bi_sector);
1247 r1_bio = alloc_r1bio(mddev, bio);
1249 init_r1bio(r1_bio, mddev, bio);
1250 r1_bio->sectors = max_read_sectors;
1253 * make_request() can abort the operation when read-ahead is being
1254 * used and no empty request is available.
1256 rdisk = read_balance(conf, r1_bio, &max_sectors);
1259 /* couldn't find anywhere to read from */
1261 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
1264 (unsigned long long)r1_bio->sector);
1266 raid_end_bio_io(r1_bio);
1269 mirror = conf->mirrors + rdisk;
1272 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
1274 (unsigned long long)r1_bio->sector,
1275 bdevname(mirror->rdev->bdev, b));
1277 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
1280 * Reading from a write-mostly device must take care not to
1281 * over-take any writes that are 'behind'
1283 raid1_log(mddev, "wait behind writes");
1284 wait_event(bitmap->behind_wait,
1285 atomic_read(&bitmap->behind_writes) == 0);
1288 if (max_sectors < bio_sectors(bio)) {
1289 struct bio *split = bio_split(bio, max_sectors,
1290 gfp, &conf->bio_split);
1291 bio_chain(split, bio);
1292 generic_make_request(bio);
1294 r1_bio->master_bio = bio;
1295 r1_bio->sectors = max_sectors;
1298 r1_bio->read_disk = rdisk;
1300 read_bio = bio_clone_fast(bio, gfp, &mddev->bio_set);
1302 r1_bio->bios[rdisk] = read_bio;
1304 read_bio->bi_iter.bi_sector = r1_bio->sector +
1305 mirror->rdev->data_offset;
1306 bio_set_dev(read_bio, mirror->rdev->bdev);
1307 read_bio->bi_end_io = raid1_end_read_request;
1308 bio_set_op_attrs(read_bio, op, do_sync);
1309 if (test_bit(FailFast, &mirror->rdev->flags) &&
1310 test_bit(R1BIO_FailFast, &r1_bio->state))
1311 read_bio->bi_opf |= MD_FAILFAST;
1312 read_bio->bi_private = r1_bio;
1315 trace_block_bio_remap(read_bio->bi_disk->queue, read_bio,
1316 disk_devt(mddev->gendisk), r1_bio->sector);
1318 generic_make_request(read_bio);
1321 static void raid1_write_request(struct mddev *mddev, struct bio *bio,
1322 int max_write_sectors)
1324 struct r1conf *conf = mddev->private;
1325 struct r1bio *r1_bio;
1327 struct bitmap *bitmap = mddev->bitmap;
1328 unsigned long flags;
1329 struct md_rdev *blocked_rdev;
1330 struct blk_plug_cb *cb;
1331 struct raid1_plug_cb *plug = NULL;
1335 if (mddev_is_clustered(mddev) &&
1336 md_cluster_ops->area_resyncing(mddev, WRITE,
1337 bio->bi_iter.bi_sector, bio_end_sector(bio))) {
1341 prepare_to_wait(&conf->wait_barrier,
1343 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1344 bio->bi_iter.bi_sector,
1345 bio_end_sector(bio)))
1349 finish_wait(&conf->wait_barrier, &w);
1353 * Register the new request and wait if the reconstruction
1354 * thread has put up a bar for new requests.
1355 * Continue immediately if no resync is active currently.
1357 wait_barrier(conf, bio->bi_iter.bi_sector);
1359 r1_bio = alloc_r1bio(mddev, bio);
1360 r1_bio->sectors = max_write_sectors;
1362 if (conf->pending_count >= max_queued_requests) {
1363 md_wakeup_thread(mddev->thread);
1364 raid1_log(mddev, "wait queued");
1365 wait_event(conf->wait_barrier,
1366 conf->pending_count < max_queued_requests);
1368 /* first select target devices under rcu_lock and
1369 * inc refcount on their rdev. Record them by setting
1371 * If there are known/acknowledged bad blocks on any device on
1372 * which we have seen a write error, we want to avoid writing those
1374 * This potentially requires several writes to write around
1375 * the bad blocks. Each set of writes gets it's own r1bio
1376 * with a set of bios attached.
1379 disks = conf->raid_disks * 2;
1381 blocked_rdev = NULL;
1383 max_sectors = r1_bio->sectors;
1384 for (i = 0; i < disks; i++) {
1385 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1386 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1387 atomic_inc(&rdev->nr_pending);
1388 blocked_rdev = rdev;
1391 r1_bio->bios[i] = NULL;
1392 if (!rdev || test_bit(Faulty, &rdev->flags)) {
1393 if (i < conf->raid_disks)
1394 set_bit(R1BIO_Degraded, &r1_bio->state);
1398 atomic_inc(&rdev->nr_pending);
1399 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1404 is_bad = is_badblock(rdev, r1_bio->sector, max_sectors,
1405 &first_bad, &bad_sectors);
1407 /* mustn't write here until the bad block is
1409 set_bit(BlockedBadBlocks, &rdev->flags);
1410 blocked_rdev = rdev;
1413 if (is_bad && first_bad <= r1_bio->sector) {
1414 /* Cannot write here at all */
1415 bad_sectors -= (r1_bio->sector - first_bad);
1416 if (bad_sectors < max_sectors)
1417 /* mustn't write more than bad_sectors
1418 * to other devices yet
1420 max_sectors = bad_sectors;
1421 rdev_dec_pending(rdev, mddev);
1422 /* We don't set R1BIO_Degraded as that
1423 * only applies if the disk is
1424 * missing, so it might be re-added,
1425 * and we want to know to recover this
1427 * In this case the device is here,
1428 * and the fact that this chunk is not
1429 * in-sync is recorded in the bad
1435 int good_sectors = first_bad - r1_bio->sector;
1436 if (good_sectors < max_sectors)
1437 max_sectors = good_sectors;
1440 r1_bio->bios[i] = bio;
1444 if (unlikely(blocked_rdev)) {
1445 /* Wait for this device to become unblocked */
1448 for (j = 0; j < i; j++)
1449 if (r1_bio->bios[j])
1450 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
1452 allow_barrier(conf, bio->bi_iter.bi_sector);
1453 raid1_log(mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
1454 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1455 wait_barrier(conf, bio->bi_iter.bi_sector);
1459 if (max_sectors < bio_sectors(bio)) {
1460 struct bio *split = bio_split(bio, max_sectors,
1461 GFP_NOIO, &conf->bio_split);
1462 bio_chain(split, bio);
1463 generic_make_request(bio);
1465 r1_bio->master_bio = bio;
1466 r1_bio->sectors = max_sectors;
1469 atomic_set(&r1_bio->remaining, 1);
1470 atomic_set(&r1_bio->behind_remaining, 0);
1474 for (i = 0; i < disks; i++) {
1475 struct bio *mbio = NULL;
1476 if (!r1_bio->bios[i])
1481 * Not if there are too many, or cannot
1482 * allocate memory, or a reader on WriteMostly
1483 * is waiting for behind writes to flush */
1485 (atomic_read(&bitmap->behind_writes)
1486 < mddev->bitmap_info.max_write_behind) &&
1487 !waitqueue_active(&bitmap->behind_wait)) {
1488 alloc_behind_master_bio(r1_bio, bio);
1491 md_bitmap_startwrite(bitmap, r1_bio->sector, r1_bio->sectors,
1492 test_bit(R1BIO_BehindIO, &r1_bio->state));
1496 if (r1_bio->behind_master_bio)
1497 mbio = bio_clone_fast(r1_bio->behind_master_bio,
1498 GFP_NOIO, &mddev->bio_set);
1500 mbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
1502 if (r1_bio->behind_master_bio) {
1503 struct md_rdev *rdev = conf->mirrors[i].rdev;
1505 if (test_bit(WBCollisionCheck, &rdev->flags)) {
1506 sector_t lo = r1_bio->sector;
1507 sector_t hi = r1_bio->sector + r1_bio->sectors;
1509 wait_event(rdev->wb_io_wait,
1510 check_and_add_wb(rdev, lo, hi) == 0);
1512 if (test_bit(WriteMostly, &rdev->flags))
1513 atomic_inc(&r1_bio->behind_remaining);
1516 r1_bio->bios[i] = mbio;
1518 mbio->bi_iter.bi_sector = (r1_bio->sector +
1519 conf->mirrors[i].rdev->data_offset);
1520 bio_set_dev(mbio, conf->mirrors[i].rdev->bdev);
1521 mbio->bi_end_io = raid1_end_write_request;
1522 mbio->bi_opf = bio_op(bio) | (bio->bi_opf & (REQ_SYNC | REQ_FUA));
1523 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags) &&
1524 !test_bit(WriteMostly, &conf->mirrors[i].rdev->flags) &&
1525 conf->raid_disks - mddev->degraded > 1)
1526 mbio->bi_opf |= MD_FAILFAST;
1527 mbio->bi_private = r1_bio;
1529 atomic_inc(&r1_bio->remaining);
1532 trace_block_bio_remap(mbio->bi_disk->queue,
1533 mbio, disk_devt(mddev->gendisk),
1535 /* flush_pending_writes() needs access to the rdev so...*/
1536 mbio->bi_disk = (void *)conf->mirrors[i].rdev;
1538 cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
1540 plug = container_of(cb, struct raid1_plug_cb, cb);
1544 bio_list_add(&plug->pending, mbio);
1545 plug->pending_cnt++;
1547 spin_lock_irqsave(&conf->device_lock, flags);
1548 bio_list_add(&conf->pending_bio_list, mbio);
1549 conf->pending_count++;
1550 spin_unlock_irqrestore(&conf->device_lock, flags);
1551 md_wakeup_thread(mddev->thread);
1555 r1_bio_write_done(r1_bio);
1557 /* In case raid1d snuck in to freeze_array */
1558 wake_up(&conf->wait_barrier);
1561 static bool raid1_make_request(struct mddev *mddev, struct bio *bio)
1565 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1566 md_flush_request(mddev, bio);
1571 * There is a limit to the maximum size, but
1572 * the read/write handler might find a lower limit
1573 * due to bad blocks. To avoid multiple splits,
1574 * we pass the maximum number of sectors down
1575 * and let the lower level perform the split.
1577 sectors = align_to_barrier_unit_end(
1578 bio->bi_iter.bi_sector, bio_sectors(bio));
1580 if (bio_data_dir(bio) == READ)
1581 raid1_read_request(mddev, bio, sectors, NULL);
1583 if (!md_write_start(mddev,bio))
1585 raid1_write_request(mddev, bio, sectors);
1590 static void raid1_status(struct seq_file *seq, struct mddev *mddev)
1592 struct r1conf *conf = mddev->private;
1595 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1596 conf->raid_disks - mddev->degraded);
1598 for (i = 0; i < conf->raid_disks; i++) {
1599 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1600 seq_printf(seq, "%s",
1601 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1604 seq_printf(seq, "]");
1607 static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
1609 char b[BDEVNAME_SIZE];
1610 struct r1conf *conf = mddev->private;
1611 unsigned long flags;
1614 * If it is not operational, then we have already marked it as dead
1615 * else if it is the last working disks, ignore the error, let the
1616 * next level up know.
1617 * else mark the drive as failed
1619 spin_lock_irqsave(&conf->device_lock, flags);
1620 if (test_bit(In_sync, &rdev->flags)
1621 && (conf->raid_disks - mddev->degraded) == 1) {
1623 * Don't fail the drive, act as though we were just a
1624 * normal single drive.
1625 * However don't try a recovery from this drive as
1626 * it is very likely to fail.
1628 conf->recovery_disabled = mddev->recovery_disabled;
1629 spin_unlock_irqrestore(&conf->device_lock, flags);
1632 set_bit(Blocked, &rdev->flags);
1633 if (test_and_clear_bit(In_sync, &rdev->flags))
1635 set_bit(Faulty, &rdev->flags);
1636 spin_unlock_irqrestore(&conf->device_lock, flags);
1638 * if recovery is running, make sure it aborts.
1640 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1641 set_mask_bits(&mddev->sb_flags, 0,
1642 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1643 pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
1644 "md/raid1:%s: Operation continuing on %d devices.\n",
1645 mdname(mddev), bdevname(rdev->bdev, b),
1646 mdname(mddev), conf->raid_disks - mddev->degraded);
1649 static void print_conf(struct r1conf *conf)
1653 pr_debug("RAID1 conf printout:\n");
1655 pr_debug("(!conf)\n");
1658 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1662 for (i = 0; i < conf->raid_disks; i++) {
1663 char b[BDEVNAME_SIZE];
1664 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1666 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1667 i, !test_bit(In_sync, &rdev->flags),
1668 !test_bit(Faulty, &rdev->flags),
1669 bdevname(rdev->bdev,b));
1674 static void close_sync(struct r1conf *conf)
1678 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++) {
1679 _wait_barrier(conf, idx);
1680 _allow_barrier(conf, idx);
1683 mempool_exit(&conf->r1buf_pool);
1686 static int raid1_spare_active(struct mddev *mddev)
1689 struct r1conf *conf = mddev->private;
1691 unsigned long flags;
1694 * Find all failed disks within the RAID1 configuration
1695 * and mark them readable.
1696 * Called under mddev lock, so rcu protection not needed.
1697 * device_lock used to avoid races with raid1_end_read_request
1698 * which expects 'In_sync' flags and ->degraded to be consistent.
1700 spin_lock_irqsave(&conf->device_lock, flags);
1701 for (i = 0; i < conf->raid_disks; i++) {
1702 struct md_rdev *rdev = conf->mirrors[i].rdev;
1703 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
1705 && !test_bit(Candidate, &repl->flags)
1706 && repl->recovery_offset == MaxSector
1707 && !test_bit(Faulty, &repl->flags)
1708 && !test_and_set_bit(In_sync, &repl->flags)) {
1709 /* replacement has just become active */
1711 !test_and_clear_bit(In_sync, &rdev->flags))
1714 /* Replaced device not technically
1715 * faulty, but we need to be sure
1716 * it gets removed and never re-added
1718 set_bit(Faulty, &rdev->flags);
1719 sysfs_notify_dirent_safe(
1724 && rdev->recovery_offset == MaxSector
1725 && !test_bit(Faulty, &rdev->flags)
1726 && !test_and_set_bit(In_sync, &rdev->flags)) {
1728 sysfs_notify_dirent_safe(rdev->sysfs_state);
1731 mddev->degraded -= count;
1732 spin_unlock_irqrestore(&conf->device_lock, flags);
1738 static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1740 struct r1conf *conf = mddev->private;
1743 struct raid1_info *p;
1745 int last = conf->raid_disks - 1;
1747 if (mddev->recovery_disabled == conf->recovery_disabled)
1750 if (md_integrity_add_rdev(rdev, mddev))
1753 if (rdev->raid_disk >= 0)
1754 first = last = rdev->raid_disk;
1757 * find the disk ... but prefer rdev->saved_raid_disk
1760 if (rdev->saved_raid_disk >= 0 &&
1761 rdev->saved_raid_disk >= first &&
1762 rdev->saved_raid_disk < conf->raid_disks &&
1763 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1764 first = last = rdev->saved_raid_disk;
1766 for (mirror = first; mirror <= last; mirror++) {
1767 p = conf->mirrors + mirror;
1770 disk_stack_limits(mddev->gendisk, rdev->bdev,
1771 rdev->data_offset << 9);
1773 p->head_position = 0;
1774 rdev->raid_disk = mirror;
1776 /* As all devices are equivalent, we don't need a full recovery
1777 * if this was recently any drive of the array
1779 if (rdev->saved_raid_disk < 0)
1781 rcu_assign_pointer(p->rdev, rdev);
1784 if (test_bit(WantReplacement, &p->rdev->flags) &&
1785 p[conf->raid_disks].rdev == NULL) {
1786 /* Add this device as a replacement */
1787 clear_bit(In_sync, &rdev->flags);
1788 set_bit(Replacement, &rdev->flags);
1789 rdev->raid_disk = mirror;
1792 rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
1796 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
1797 blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
1802 static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1804 struct r1conf *conf = mddev->private;
1806 int number = rdev->raid_disk;
1807 struct raid1_info *p = conf->mirrors + number;
1809 if (rdev != p->rdev)
1810 p = conf->mirrors + conf->raid_disks + number;
1813 if (rdev == p->rdev) {
1814 if (test_bit(In_sync, &rdev->flags) ||
1815 atomic_read(&rdev->nr_pending)) {
1819 /* Only remove non-faulty devices if recovery
1822 if (!test_bit(Faulty, &rdev->flags) &&
1823 mddev->recovery_disabled != conf->recovery_disabled &&
1824 mddev->degraded < conf->raid_disks) {
1829 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1831 if (atomic_read(&rdev->nr_pending)) {
1832 /* lost the race, try later */
1838 if (conf->mirrors[conf->raid_disks + number].rdev) {
1839 /* We just removed a device that is being replaced.
1840 * Move down the replacement. We drain all IO before
1841 * doing this to avoid confusion.
1843 struct md_rdev *repl =
1844 conf->mirrors[conf->raid_disks + number].rdev;
1845 freeze_array(conf, 0);
1846 if (atomic_read(&repl->nr_pending)) {
1847 /* It means that some queued IO of retry_list
1848 * hold repl. Thus, we cannot set replacement
1849 * as NULL, avoiding rdev NULL pointer
1850 * dereference in sync_request_write and
1851 * handle_write_finished.
1854 unfreeze_array(conf);
1857 clear_bit(Replacement, &repl->flags);
1859 conf->mirrors[conf->raid_disks + number].rdev = NULL;
1860 unfreeze_array(conf);
1863 clear_bit(WantReplacement, &rdev->flags);
1864 err = md_integrity_register(mddev);
1872 static void end_sync_read(struct bio *bio)
1874 struct r1bio *r1_bio = get_resync_r1bio(bio);
1876 update_head_pos(r1_bio->read_disk, r1_bio);
1879 * we have read a block, now it needs to be re-written,
1880 * or re-read if the read failed.
1881 * We don't do much here, just schedule handling by raid1d
1883 if (!bio->bi_status)
1884 set_bit(R1BIO_Uptodate, &r1_bio->state);
1886 if (atomic_dec_and_test(&r1_bio->remaining))
1887 reschedule_retry(r1_bio);
1890 static void abort_sync_write(struct mddev *mddev, struct r1bio *r1_bio)
1892 sector_t sync_blocks = 0;
1893 sector_t s = r1_bio->sector;
1894 long sectors_to_go = r1_bio->sectors;
1896 /* make sure these bits don't get cleared. */
1898 md_bitmap_end_sync(mddev->bitmap, s, &sync_blocks, 1);
1900 sectors_to_go -= sync_blocks;
1901 } while (sectors_to_go > 0);
1904 static void end_sync_write(struct bio *bio)
1906 int uptodate = !bio->bi_status;
1907 struct r1bio *r1_bio = get_resync_r1bio(bio);
1908 struct mddev *mddev = r1_bio->mddev;
1909 struct r1conf *conf = mddev->private;
1912 struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
1915 abort_sync_write(mddev, r1_bio);
1916 set_bit(WriteErrorSeen, &rdev->flags);
1917 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1918 set_bit(MD_RECOVERY_NEEDED, &
1920 set_bit(R1BIO_WriteError, &r1_bio->state);
1921 } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
1922 &first_bad, &bad_sectors) &&
1923 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1926 &first_bad, &bad_sectors)
1928 set_bit(R1BIO_MadeGood, &r1_bio->state);
1930 if (atomic_dec_and_test(&r1_bio->remaining)) {
1931 int s = r1_bio->sectors;
1932 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1933 test_bit(R1BIO_WriteError, &r1_bio->state))
1934 reschedule_retry(r1_bio);
1937 md_done_sync(mddev, s, uptodate);
1942 static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
1943 int sectors, struct page *page, int rw)
1945 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
1949 set_bit(WriteErrorSeen, &rdev->flags);
1950 if (!test_and_set_bit(WantReplacement,
1952 set_bit(MD_RECOVERY_NEEDED, &
1953 rdev->mddev->recovery);
1955 /* need to record an error - either for the block or the device */
1956 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1957 md_error(rdev->mddev, rdev);
1961 static int fix_sync_read_error(struct r1bio *r1_bio)
1963 /* Try some synchronous reads of other devices to get
1964 * good data, much like with normal read errors. Only
1965 * read into the pages we already have so we don't
1966 * need to re-issue the read request.
1967 * We don't need to freeze the array, because being in an
1968 * active sync request, there is no normal IO, and
1969 * no overlapping syncs.
1970 * We don't need to check is_badblock() again as we
1971 * made sure that anything with a bad block in range
1972 * will have bi_end_io clear.
1974 struct mddev *mddev = r1_bio->mddev;
1975 struct r1conf *conf = mddev->private;
1976 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1977 struct page **pages = get_resync_pages(bio)->pages;
1978 sector_t sect = r1_bio->sector;
1979 int sectors = r1_bio->sectors;
1981 struct md_rdev *rdev;
1983 rdev = conf->mirrors[r1_bio->read_disk].rdev;
1984 if (test_bit(FailFast, &rdev->flags)) {
1985 /* Don't try recovering from here - just fail it
1986 * ... unless it is the last working device of course */
1987 md_error(mddev, rdev);
1988 if (test_bit(Faulty, &rdev->flags))
1989 /* Don't try to read from here, but make sure
1990 * put_buf does it's thing
1992 bio->bi_end_io = end_sync_write;
1997 int d = r1_bio->read_disk;
2001 if (s > (PAGE_SIZE>>9))
2004 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
2005 /* No rcu protection needed here devices
2006 * can only be removed when no resync is
2007 * active, and resync is currently active
2009 rdev = conf->mirrors[d].rdev;
2010 if (sync_page_io(rdev, sect, s<<9,
2012 REQ_OP_READ, 0, false)) {
2018 if (d == conf->raid_disks * 2)
2020 } while (!success && d != r1_bio->read_disk);
2023 char b[BDEVNAME_SIZE];
2025 /* Cannot read from anywhere, this block is lost.
2026 * Record a bad block on each device. If that doesn't
2027 * work just disable and interrupt the recovery.
2028 * Don't fail devices as that won't really help.
2030 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
2031 mdname(mddev), bio_devname(bio, b),
2032 (unsigned long long)r1_bio->sector);
2033 for (d = 0; d < conf->raid_disks * 2; d++) {
2034 rdev = conf->mirrors[d].rdev;
2035 if (!rdev || test_bit(Faulty, &rdev->flags))
2037 if (!rdev_set_badblocks(rdev, sect, s, 0))
2041 conf->recovery_disabled =
2042 mddev->recovery_disabled;
2043 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2044 md_done_sync(mddev, r1_bio->sectors, 0);
2056 /* write it back and re-read */
2057 while (d != r1_bio->read_disk) {
2059 d = conf->raid_disks * 2;
2061 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
2063 rdev = conf->mirrors[d].rdev;
2064 if (r1_sync_page_io(rdev, sect, s,
2067 r1_bio->bios[d]->bi_end_io = NULL;
2068 rdev_dec_pending(rdev, mddev);
2072 while (d != r1_bio->read_disk) {
2074 d = conf->raid_disks * 2;
2076 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
2078 rdev = conf->mirrors[d].rdev;
2079 if (r1_sync_page_io(rdev, sect, s,
2082 atomic_add(s, &rdev->corrected_errors);
2088 set_bit(R1BIO_Uptodate, &r1_bio->state);
2093 static void process_checks(struct r1bio *r1_bio)
2095 /* We have read all readable devices. If we haven't
2096 * got the block, then there is no hope left.
2097 * If we have, then we want to do a comparison
2098 * and skip the write if everything is the same.
2099 * If any blocks failed to read, then we need to
2100 * attempt an over-write
2102 struct mddev *mddev = r1_bio->mddev;
2103 struct r1conf *conf = mddev->private;
2108 /* Fix variable parts of all bios */
2109 vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
2110 for (i = 0; i < conf->raid_disks * 2; i++) {
2111 blk_status_t status;
2112 struct bio *b = r1_bio->bios[i];
2113 struct resync_pages *rp = get_resync_pages(b);
2114 if (b->bi_end_io != end_sync_read)
2116 /* fixup the bio for reuse, but preserve errno */
2117 status = b->bi_status;
2119 b->bi_status = status;
2120 b->bi_iter.bi_sector = r1_bio->sector +
2121 conf->mirrors[i].rdev->data_offset;
2122 bio_set_dev(b, conf->mirrors[i].rdev->bdev);
2123 b->bi_end_io = end_sync_read;
2124 rp->raid_bio = r1_bio;
2127 /* initialize bvec table again */
2128 md_bio_reset_resync_pages(b, rp, r1_bio->sectors << 9);
2130 for (primary = 0; primary < conf->raid_disks * 2; primary++)
2131 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
2132 !r1_bio->bios[primary]->bi_status) {
2133 r1_bio->bios[primary]->bi_end_io = NULL;
2134 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
2137 r1_bio->read_disk = primary;
2138 for (i = 0; i < conf->raid_disks * 2; i++) {
2140 struct bio *pbio = r1_bio->bios[primary];
2141 struct bio *sbio = r1_bio->bios[i];
2142 blk_status_t status = sbio->bi_status;
2143 struct page **ppages = get_resync_pages(pbio)->pages;
2144 struct page **spages = get_resync_pages(sbio)->pages;
2146 int page_len[RESYNC_PAGES] = { 0 };
2147 struct bvec_iter_all iter_all;
2149 if (sbio->bi_end_io != end_sync_read)
2151 /* Now we can 'fixup' the error value */
2152 sbio->bi_status = 0;
2154 bio_for_each_segment_all(bi, sbio, iter_all)
2155 page_len[j++] = bi->bv_len;
2158 for (j = vcnt; j-- ; ) {
2159 if (memcmp(page_address(ppages[j]),
2160 page_address(spages[j]),
2167 atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
2168 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
2170 /* No need to write to this device. */
2171 sbio->bi_end_io = NULL;
2172 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
2176 bio_copy_data(sbio, pbio);
2180 static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
2182 struct r1conf *conf = mddev->private;
2184 int disks = conf->raid_disks * 2;
2187 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
2188 /* ouch - failed to read all of that. */
2189 if (!fix_sync_read_error(r1_bio))
2192 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2193 process_checks(r1_bio);
2198 atomic_set(&r1_bio->remaining, 1);
2199 for (i = 0; i < disks ; i++) {
2200 wbio = r1_bio->bios[i];
2201 if (wbio->bi_end_io == NULL ||
2202 (wbio->bi_end_io == end_sync_read &&
2203 (i == r1_bio->read_disk ||
2204 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
2206 if (test_bit(Faulty, &conf->mirrors[i].rdev->flags)) {
2207 abort_sync_write(mddev, r1_bio);
2211 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2212 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags))
2213 wbio->bi_opf |= MD_FAILFAST;
2215 wbio->bi_end_io = end_sync_write;
2216 atomic_inc(&r1_bio->remaining);
2217 md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
2219 generic_make_request(wbio);
2222 if (atomic_dec_and_test(&r1_bio->remaining)) {
2223 /* if we're here, all write(s) have completed, so clean up */
2224 int s = r1_bio->sectors;
2225 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2226 test_bit(R1BIO_WriteError, &r1_bio->state))
2227 reschedule_retry(r1_bio);
2230 md_done_sync(mddev, s, 1);
2236 * This is a kernel thread which:
2238 * 1. Retries failed read operations on working mirrors.
2239 * 2. Updates the raid superblock when problems encounter.
2240 * 3. Performs writes following reads for array synchronising.
2243 static void fix_read_error(struct r1conf *conf, int read_disk,
2244 sector_t sect, int sectors)
2246 struct mddev *mddev = conf->mddev;
2252 struct md_rdev *rdev;
2254 if (s > (PAGE_SIZE>>9))
2262 rdev = rcu_dereference(conf->mirrors[d].rdev);
2264 (test_bit(In_sync, &rdev->flags) ||
2265 (!test_bit(Faulty, &rdev->flags) &&
2266 rdev->recovery_offset >= sect + s)) &&
2267 is_badblock(rdev, sect, s,
2268 &first_bad, &bad_sectors) == 0) {
2269 atomic_inc(&rdev->nr_pending);
2271 if (sync_page_io(rdev, sect, s<<9,
2272 conf->tmppage, REQ_OP_READ, 0, false))
2274 rdev_dec_pending(rdev, mddev);
2280 if (d == conf->raid_disks * 2)
2282 } while (!success && d != read_disk);
2285 /* Cannot read from anywhere - mark it bad */
2286 struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
2287 if (!rdev_set_badblocks(rdev, sect, s, 0))
2288 md_error(mddev, rdev);
2291 /* write it back and re-read */
2293 while (d != read_disk) {
2295 d = conf->raid_disks * 2;
2298 rdev = rcu_dereference(conf->mirrors[d].rdev);
2300 !test_bit(Faulty, &rdev->flags)) {
2301 atomic_inc(&rdev->nr_pending);
2303 r1_sync_page_io(rdev, sect, s,
2304 conf->tmppage, WRITE);
2305 rdev_dec_pending(rdev, mddev);
2310 while (d != read_disk) {
2311 char b[BDEVNAME_SIZE];
2313 d = conf->raid_disks * 2;
2316 rdev = rcu_dereference(conf->mirrors[d].rdev);
2318 !test_bit(Faulty, &rdev->flags)) {
2319 atomic_inc(&rdev->nr_pending);
2321 if (r1_sync_page_io(rdev, sect, s,
2322 conf->tmppage, READ)) {
2323 atomic_add(s, &rdev->corrected_errors);
2324 pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
2326 (unsigned long long)(sect +
2328 bdevname(rdev->bdev, b));
2330 rdev_dec_pending(rdev, mddev);
2339 static int narrow_write_error(struct r1bio *r1_bio, int i)
2341 struct mddev *mddev = r1_bio->mddev;
2342 struct r1conf *conf = mddev->private;
2343 struct md_rdev *rdev = conf->mirrors[i].rdev;
2345 /* bio has the data to be written to device 'i' where
2346 * we just recently had a write error.
2347 * We repeatedly clone the bio and trim down to one block,
2348 * then try the write. Where the write fails we record
2350 * It is conceivable that the bio doesn't exactly align with
2351 * blocks. We must handle this somehow.
2353 * We currently own a reference on the rdev.
2359 int sect_to_write = r1_bio->sectors;
2362 if (rdev->badblocks.shift < 0)
2365 block_sectors = roundup(1 << rdev->badblocks.shift,
2366 bdev_logical_block_size(rdev->bdev) >> 9);
2367 sector = r1_bio->sector;
2368 sectors = ((sector + block_sectors)
2369 & ~(sector_t)(block_sectors - 1))
2372 while (sect_to_write) {
2374 if (sectors > sect_to_write)
2375 sectors = sect_to_write;
2376 /* Write at 'sector' for 'sectors'*/
2378 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
2379 wbio = bio_clone_fast(r1_bio->behind_master_bio,
2383 wbio = bio_clone_fast(r1_bio->master_bio, GFP_NOIO,
2387 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2388 wbio->bi_iter.bi_sector = r1_bio->sector;
2389 wbio->bi_iter.bi_size = r1_bio->sectors << 9;
2391 bio_trim(wbio, sector - r1_bio->sector, sectors);
2392 wbio->bi_iter.bi_sector += rdev->data_offset;
2393 bio_set_dev(wbio, rdev->bdev);
2395 if (submit_bio_wait(wbio) < 0)
2397 ok = rdev_set_badblocks(rdev, sector,
2402 sect_to_write -= sectors;
2404 sectors = block_sectors;
2409 static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
2412 int s = r1_bio->sectors;
2413 for (m = 0; m < conf->raid_disks * 2 ; m++) {
2414 struct md_rdev *rdev = conf->mirrors[m].rdev;
2415 struct bio *bio = r1_bio->bios[m];
2416 if (bio->bi_end_io == NULL)
2418 if (!bio->bi_status &&
2419 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
2420 rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
2422 if (bio->bi_status &&
2423 test_bit(R1BIO_WriteError, &r1_bio->state)) {
2424 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
2425 md_error(conf->mddev, rdev);
2429 md_done_sync(conf->mddev, s, 1);
2432 static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
2437 for (m = 0; m < conf->raid_disks * 2 ; m++)
2438 if (r1_bio->bios[m] == IO_MADE_GOOD) {
2439 struct md_rdev *rdev = conf->mirrors[m].rdev;
2440 rdev_clear_badblocks(rdev,
2442 r1_bio->sectors, 0);
2443 rdev_dec_pending(rdev, conf->mddev);
2444 } else if (r1_bio->bios[m] != NULL) {
2445 /* This drive got a write error. We need to
2446 * narrow down and record precise write
2450 if (!narrow_write_error(r1_bio, m)) {
2451 md_error(conf->mddev,
2452 conf->mirrors[m].rdev);
2453 /* an I/O failed, we can't clear the bitmap */
2454 set_bit(R1BIO_Degraded, &r1_bio->state);
2456 rdev_dec_pending(conf->mirrors[m].rdev,
2460 spin_lock_irq(&conf->device_lock);
2461 list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
2462 idx = sector_to_idx(r1_bio->sector);
2463 atomic_inc(&conf->nr_queued[idx]);
2464 spin_unlock_irq(&conf->device_lock);
2466 * In case freeze_array() is waiting for condition
2467 * get_unqueued_pending() == extra to be true.
2469 wake_up(&conf->wait_barrier);
2470 md_wakeup_thread(conf->mddev->thread);
2472 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2473 close_write(r1_bio);
2474 raid_end_bio_io(r1_bio);
2478 static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
2480 struct mddev *mddev = conf->mddev;
2482 struct md_rdev *rdev;
2484 clear_bit(R1BIO_ReadError, &r1_bio->state);
2485 /* we got a read error. Maybe the drive is bad. Maybe just
2486 * the block and we can fix it.
2487 * We freeze all other IO, and try reading the block from
2488 * other devices. When we find one, we re-write
2489 * and check it that fixes the read error.
2490 * This is all done synchronously while the array is
2494 bio = r1_bio->bios[r1_bio->read_disk];
2496 r1_bio->bios[r1_bio->read_disk] = NULL;
2498 rdev = conf->mirrors[r1_bio->read_disk].rdev;
2500 && !test_bit(FailFast, &rdev->flags)) {
2501 freeze_array(conf, 1);
2502 fix_read_error(conf, r1_bio->read_disk,
2503 r1_bio->sector, r1_bio->sectors);
2504 unfreeze_array(conf);
2505 } else if (mddev->ro == 0 && test_bit(FailFast, &rdev->flags)) {
2506 md_error(mddev, rdev);
2508 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
2511 rdev_dec_pending(rdev, conf->mddev);
2512 allow_barrier(conf, r1_bio->sector);
2513 bio = r1_bio->master_bio;
2515 /* Reuse the old r1_bio so that the IO_BLOCKED settings are preserved */
2517 raid1_read_request(mddev, bio, r1_bio->sectors, r1_bio);
2520 static void raid1d(struct md_thread *thread)
2522 struct mddev *mddev = thread->mddev;
2523 struct r1bio *r1_bio;
2524 unsigned long flags;
2525 struct r1conf *conf = mddev->private;
2526 struct list_head *head = &conf->retry_list;
2527 struct blk_plug plug;
2530 md_check_recovery(mddev);
2532 if (!list_empty_careful(&conf->bio_end_io_list) &&
2533 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
2535 spin_lock_irqsave(&conf->device_lock, flags);
2536 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
2537 list_splice_init(&conf->bio_end_io_list, &tmp);
2538 spin_unlock_irqrestore(&conf->device_lock, flags);
2539 while (!list_empty(&tmp)) {
2540 r1_bio = list_first_entry(&tmp, struct r1bio,
2542 list_del(&r1_bio->retry_list);
2543 idx = sector_to_idx(r1_bio->sector);
2544 atomic_dec(&conf->nr_queued[idx]);
2545 if (mddev->degraded)
2546 set_bit(R1BIO_Degraded, &r1_bio->state);
2547 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2548 close_write(r1_bio);
2549 raid_end_bio_io(r1_bio);
2553 blk_start_plug(&plug);
2556 flush_pending_writes(conf);
2558 spin_lock_irqsave(&conf->device_lock, flags);
2559 if (list_empty(head)) {
2560 spin_unlock_irqrestore(&conf->device_lock, flags);
2563 r1_bio = list_entry(head->prev, struct r1bio, retry_list);
2564 list_del(head->prev);
2565 idx = sector_to_idx(r1_bio->sector);
2566 atomic_dec(&conf->nr_queued[idx]);
2567 spin_unlock_irqrestore(&conf->device_lock, flags);
2569 mddev = r1_bio->mddev;
2570 conf = mddev->private;
2571 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
2572 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2573 test_bit(R1BIO_WriteError, &r1_bio->state))
2574 handle_sync_write_finished(conf, r1_bio);
2576 sync_request_write(mddev, r1_bio);
2577 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2578 test_bit(R1BIO_WriteError, &r1_bio->state))
2579 handle_write_finished(conf, r1_bio);
2580 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2581 handle_read_error(conf, r1_bio);
2586 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
2587 md_check_recovery(mddev);
2589 blk_finish_plug(&plug);
2592 static int init_resync(struct r1conf *conf)
2596 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2597 BUG_ON(mempool_initialized(&conf->r1buf_pool));
2599 return mempool_init(&conf->r1buf_pool, buffs, r1buf_pool_alloc,
2600 r1buf_pool_free, conf->poolinfo);
2603 static struct r1bio *raid1_alloc_init_r1buf(struct r1conf *conf)
2605 struct r1bio *r1bio = mempool_alloc(&conf->r1buf_pool, GFP_NOIO);
2606 struct resync_pages *rps;
2610 for (i = conf->poolinfo->raid_disks; i--; ) {
2611 bio = r1bio->bios[i];
2612 rps = bio->bi_private;
2614 bio->bi_private = rps;
2616 r1bio->master_bio = NULL;
2621 * perform a "sync" on one "block"
2623 * We need to make sure that no normal I/O request - particularly write
2624 * requests - conflict with active sync requests.
2626 * This is achieved by tracking pending requests and a 'barrier' concept
2627 * that can be installed to exclude normal IO requests.
2630 static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
2633 struct r1conf *conf = mddev->private;
2634 struct r1bio *r1_bio;
2636 sector_t max_sector, nr_sectors;
2640 int write_targets = 0, read_targets = 0;
2641 sector_t sync_blocks;
2642 int still_degraded = 0;
2643 int good_sectors = RESYNC_SECTORS;
2644 int min_bad = 0; /* number of sectors that are bad in all devices */
2645 int idx = sector_to_idx(sector_nr);
2648 if (!mempool_initialized(&conf->r1buf_pool))
2649 if (init_resync(conf))
2652 max_sector = mddev->dev_sectors;
2653 if (sector_nr >= max_sector) {
2654 /* If we aborted, we need to abort the
2655 * sync on the 'current' bitmap chunk (there will
2656 * only be one in raid1 resync.
2657 * We can find the current addess in mddev->curr_resync
2659 if (mddev->curr_resync < max_sector) /* aborted */
2660 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2662 else /* completed sync */
2665 md_bitmap_close_sync(mddev->bitmap);
2668 if (mddev_is_clustered(mddev)) {
2669 conf->cluster_sync_low = 0;
2670 conf->cluster_sync_high = 0;
2675 if (mddev->bitmap == NULL &&
2676 mddev->recovery_cp == MaxSector &&
2677 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2678 conf->fullsync == 0) {
2680 return max_sector - sector_nr;
2682 /* before building a request, check if we can skip these blocks..
2683 * This call the bitmap_start_sync doesn't actually record anything
2685 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
2686 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2687 /* We can skip this block, and probably several more */
2693 * If there is non-resync activity waiting for a turn, then let it
2694 * though before starting on this new sync request.
2696 if (atomic_read(&conf->nr_waiting[idx]))
2697 schedule_timeout_uninterruptible(1);
2699 /* we are incrementing sector_nr below. To be safe, we check against
2700 * sector_nr + two times RESYNC_SECTORS
2703 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
2704 mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
2707 if (raise_barrier(conf, sector_nr))
2710 r1_bio = raid1_alloc_init_r1buf(conf);
2714 * If we get a correctably read error during resync or recovery,
2715 * we might want to read from a different device. So we
2716 * flag all drives that could conceivably be read from for READ,
2717 * and any others (which will be non-In_sync devices) for WRITE.
2718 * If a read fails, we try reading from something else for which READ
2722 r1_bio->mddev = mddev;
2723 r1_bio->sector = sector_nr;
2725 set_bit(R1BIO_IsSync, &r1_bio->state);
2726 /* make sure good_sectors won't go across barrier unit boundary */
2727 good_sectors = align_to_barrier_unit_end(sector_nr, good_sectors);
2729 for (i = 0; i < conf->raid_disks * 2; i++) {
2730 struct md_rdev *rdev;
2731 bio = r1_bio->bios[i];
2733 rdev = rcu_dereference(conf->mirrors[i].rdev);
2735 test_bit(Faulty, &rdev->flags)) {
2736 if (i < conf->raid_disks)
2738 } else if (!test_bit(In_sync, &rdev->flags)) {
2739 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
2740 bio->bi_end_io = end_sync_write;
2743 /* may need to read from here */
2744 sector_t first_bad = MaxSector;
2747 if (is_badblock(rdev, sector_nr, good_sectors,
2748 &first_bad, &bad_sectors)) {
2749 if (first_bad > sector_nr)
2750 good_sectors = first_bad - sector_nr;
2752 bad_sectors -= (sector_nr - first_bad);
2754 min_bad > bad_sectors)
2755 min_bad = bad_sectors;
2758 if (sector_nr < first_bad) {
2759 if (test_bit(WriteMostly, &rdev->flags)) {
2766 bio_set_op_attrs(bio, REQ_OP_READ, 0);
2767 bio->bi_end_io = end_sync_read;
2769 } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
2770 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2771 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
2773 * The device is suitable for reading (InSync),
2774 * but has bad block(s) here. Let's try to correct them,
2775 * if we are doing resync or repair. Otherwise, leave
2776 * this device alone for this sync request.
2778 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
2779 bio->bi_end_io = end_sync_write;
2783 if (bio->bi_end_io) {
2784 atomic_inc(&rdev->nr_pending);
2785 bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
2786 bio_set_dev(bio, rdev->bdev);
2787 if (test_bit(FailFast, &rdev->flags))
2788 bio->bi_opf |= MD_FAILFAST;
2794 r1_bio->read_disk = disk;
2796 if (read_targets == 0 && min_bad > 0) {
2797 /* These sectors are bad on all InSync devices, so we
2798 * need to mark them bad on all write targets
2801 for (i = 0 ; i < conf->raid_disks * 2 ; i++)
2802 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
2803 struct md_rdev *rdev = conf->mirrors[i].rdev;
2804 ok = rdev_set_badblocks(rdev, sector_nr,
2808 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2813 /* Cannot record the badblocks, so need to
2815 * If there are multiple read targets, could just
2816 * fail the really bad ones ???
2818 conf->recovery_disabled = mddev->recovery_disabled;
2819 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2825 if (min_bad > 0 && min_bad < good_sectors) {
2826 /* only resync enough to reach the next bad->good
2828 good_sectors = min_bad;
2831 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2832 /* extra read targets are also write targets */
2833 write_targets += read_targets-1;
2835 if (write_targets == 0 || read_targets == 0) {
2836 /* There is nowhere to write, so all non-sync
2837 * drives must be failed - so we are finished
2841 max_sector = sector_nr + min_bad;
2842 rv = max_sector - sector_nr;
2848 if (max_sector > mddev->resync_max)
2849 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2850 if (max_sector > sector_nr + good_sectors)
2851 max_sector = sector_nr + good_sectors;
2856 int len = PAGE_SIZE;
2857 if (sector_nr + (len>>9) > max_sector)
2858 len = (max_sector - sector_nr) << 9;
2861 if (sync_blocks == 0) {
2862 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
2863 &sync_blocks, still_degraded) &&
2865 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2867 if ((len >> 9) > sync_blocks)
2868 len = sync_blocks<<9;
2871 for (i = 0 ; i < conf->raid_disks * 2; i++) {
2872 struct resync_pages *rp;
2874 bio = r1_bio->bios[i];
2875 rp = get_resync_pages(bio);
2876 if (bio->bi_end_io) {
2877 page = resync_fetch_page(rp, page_idx);
2880 * won't fail because the vec table is big
2881 * enough to hold all these pages
2883 bio_add_page(bio, page, len, 0);
2886 nr_sectors += len>>9;
2887 sector_nr += len>>9;
2888 sync_blocks -= (len>>9);
2889 } while (++page_idx < RESYNC_PAGES);
2891 r1_bio->sectors = nr_sectors;
2893 if (mddev_is_clustered(mddev) &&
2894 conf->cluster_sync_high < sector_nr + nr_sectors) {
2895 conf->cluster_sync_low = mddev->curr_resync_completed;
2896 conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
2897 /* Send resync message */
2898 md_cluster_ops->resync_info_update(mddev,
2899 conf->cluster_sync_low,
2900 conf->cluster_sync_high);
2903 /* For a user-requested sync, we read all readable devices and do a
2906 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2907 atomic_set(&r1_bio->remaining, read_targets);
2908 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
2909 bio = r1_bio->bios[i];
2910 if (bio->bi_end_io == end_sync_read) {
2912 md_sync_acct_bio(bio, nr_sectors);
2913 if (read_targets == 1)
2914 bio->bi_opf &= ~MD_FAILFAST;
2915 generic_make_request(bio);
2919 atomic_set(&r1_bio->remaining, 1);
2920 bio = r1_bio->bios[r1_bio->read_disk];
2921 md_sync_acct_bio(bio, nr_sectors);
2922 if (read_targets == 1)
2923 bio->bi_opf &= ~MD_FAILFAST;
2924 generic_make_request(bio);
2929 static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
2934 return mddev->dev_sectors;
2937 static struct r1conf *setup_conf(struct mddev *mddev)
2939 struct r1conf *conf;
2941 struct raid1_info *disk;
2942 struct md_rdev *rdev;
2945 conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
2949 conf->nr_pending = kcalloc(BARRIER_BUCKETS_NR,
2950 sizeof(atomic_t), GFP_KERNEL);
2951 if (!conf->nr_pending)
2954 conf->nr_waiting = kcalloc(BARRIER_BUCKETS_NR,
2955 sizeof(atomic_t), GFP_KERNEL);
2956 if (!conf->nr_waiting)
2959 conf->nr_queued = kcalloc(BARRIER_BUCKETS_NR,
2960 sizeof(atomic_t), GFP_KERNEL);
2961 if (!conf->nr_queued)
2964 conf->barrier = kcalloc(BARRIER_BUCKETS_NR,
2965 sizeof(atomic_t), GFP_KERNEL);
2969 conf->mirrors = kzalloc(array3_size(sizeof(struct raid1_info),
2970 mddev->raid_disks, 2),
2975 conf->tmppage = alloc_page(GFP_KERNEL);
2979 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
2980 if (!conf->poolinfo)
2982 conf->poolinfo->raid_disks = mddev->raid_disks * 2;
2983 err = mempool_init(&conf->r1bio_pool, NR_RAID_BIOS, r1bio_pool_alloc,
2984 rbio_pool_free, conf->poolinfo);
2988 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
2992 conf->poolinfo->mddev = mddev;
2995 spin_lock_init(&conf->device_lock);
2996 rdev_for_each(rdev, mddev) {
2997 int disk_idx = rdev->raid_disk;
2998 if (disk_idx >= mddev->raid_disks
3001 if (test_bit(Replacement, &rdev->flags))
3002 disk = conf->mirrors + mddev->raid_disks + disk_idx;
3004 disk = conf->mirrors + disk_idx;
3009 disk->head_position = 0;
3010 disk->seq_start = MaxSector;
3012 conf->raid_disks = mddev->raid_disks;
3013 conf->mddev = mddev;
3014 INIT_LIST_HEAD(&conf->retry_list);
3015 INIT_LIST_HEAD(&conf->bio_end_io_list);
3017 spin_lock_init(&conf->resync_lock);
3018 init_waitqueue_head(&conf->wait_barrier);
3020 bio_list_init(&conf->pending_bio_list);
3021 conf->pending_count = 0;
3022 conf->recovery_disabled = mddev->recovery_disabled - 1;
3025 for (i = 0; i < conf->raid_disks * 2; i++) {
3027 disk = conf->mirrors + i;
3029 if (i < conf->raid_disks &&
3030 disk[conf->raid_disks].rdev) {
3031 /* This slot has a replacement. */
3033 /* No original, just make the replacement
3034 * a recovering spare
3037 disk[conf->raid_disks].rdev;
3038 disk[conf->raid_disks].rdev = NULL;
3039 } else if (!test_bit(In_sync, &disk->rdev->flags))
3040 /* Original is not in_sync - bad */
3045 !test_bit(In_sync, &disk->rdev->flags)) {
3046 disk->head_position = 0;
3048 (disk->rdev->saved_raid_disk < 0))
3054 conf->thread = md_register_thread(raid1d, mddev, "raid1");
3062 mempool_exit(&conf->r1bio_pool);
3063 kfree(conf->mirrors);
3064 safe_put_page(conf->tmppage);
3065 kfree(conf->poolinfo);
3066 kfree(conf->nr_pending);
3067 kfree(conf->nr_waiting);
3068 kfree(conf->nr_queued);
3069 kfree(conf->barrier);
3070 bioset_exit(&conf->bio_split);
3073 return ERR_PTR(err);
3076 static void raid1_free(struct mddev *mddev, void *priv);
3077 static int raid1_run(struct mddev *mddev)
3079 struct r1conf *conf;
3081 struct md_rdev *rdev;
3083 bool discard_supported = false;
3085 if (mddev->level != 1) {
3086 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
3087 mdname(mddev), mddev->level);
3090 if (mddev->reshape_position != MaxSector) {
3091 pr_warn("md/raid1:%s: reshape_position set but not supported\n",
3095 if (mddev_init_writes_pending(mddev) < 0)
3098 * copy the already verified devices into our private RAID1
3099 * bookkeeping area. [whatever we allocate in run(),
3100 * should be freed in raid1_free()]
3102 if (mddev->private == NULL)
3103 conf = setup_conf(mddev);
3105 conf = mddev->private;
3108 return PTR_ERR(conf);
3111 blk_queue_max_write_same_sectors(mddev->queue, 0);
3112 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
3115 rdev_for_each(rdev, mddev) {
3116 if (!mddev->gendisk)
3118 disk_stack_limits(mddev->gendisk, rdev->bdev,
3119 rdev->data_offset << 9);
3120 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3121 discard_supported = true;
3124 mddev->degraded = 0;
3125 for (i = 0; i < conf->raid_disks; i++)
3126 if (conf->mirrors[i].rdev == NULL ||
3127 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
3128 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
3131 if (conf->raid_disks - mddev->degraded == 1)
3132 mddev->recovery_cp = MaxSector;
3134 if (mddev->recovery_cp != MaxSector)
3135 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
3137 pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
3138 mdname(mddev), mddev->raid_disks - mddev->degraded,
3142 * Ok, everything is just fine now
3144 mddev->thread = conf->thread;
3145 conf->thread = NULL;
3146 mddev->private = conf;
3147 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
3149 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
3152 if (discard_supported)
3153 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
3156 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
3160 ret = md_integrity_register(mddev);
3162 md_unregister_thread(&mddev->thread);
3163 raid1_free(mddev, conf);
3168 static void raid1_free(struct mddev *mddev, void *priv)
3170 struct r1conf *conf = priv;
3172 mempool_exit(&conf->r1bio_pool);
3173 kfree(conf->mirrors);
3174 safe_put_page(conf->tmppage);
3175 kfree(conf->poolinfo);
3176 kfree(conf->nr_pending);
3177 kfree(conf->nr_waiting);
3178 kfree(conf->nr_queued);
3179 kfree(conf->barrier);
3180 bioset_exit(&conf->bio_split);
3184 static int raid1_resize(struct mddev *mddev, sector_t sectors)
3186 /* no resync is happening, and there is enough space
3187 * on all devices, so we can resize.
3188 * We need to make sure resync covers any new space.
3189 * If the array is shrinking we should possibly wait until
3190 * any io in the removed space completes, but it hardly seems
3193 sector_t newsize = raid1_size(mddev, sectors, 0);
3194 if (mddev->external_size &&
3195 mddev->array_sectors > newsize)
3197 if (mddev->bitmap) {
3198 int ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
3202 md_set_array_sectors(mddev, newsize);
3203 if (sectors > mddev->dev_sectors &&
3204 mddev->recovery_cp > mddev->dev_sectors) {
3205 mddev->recovery_cp = mddev->dev_sectors;
3206 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3208 mddev->dev_sectors = sectors;
3209 mddev->resync_max_sectors = sectors;
3213 static int raid1_reshape(struct mddev *mddev)
3216 * 1/ resize the r1bio_pool
3217 * 2/ resize conf->mirrors
3219 * We allocate a new r1bio_pool if we can.
3220 * Then raise a device barrier and wait until all IO stops.
3221 * Then resize conf->mirrors and swap in the new r1bio pool.
3223 * At the same time, we "pack" the devices so that all the missing
3224 * devices have the higher raid_disk numbers.
3226 mempool_t newpool, oldpool;
3227 struct pool_info *newpoolinfo;
3228 struct raid1_info *newmirrors;
3229 struct r1conf *conf = mddev->private;
3230 int cnt, raid_disks;
3231 unsigned long flags;
3235 memset(&newpool, 0, sizeof(newpool));
3236 memset(&oldpool, 0, sizeof(oldpool));
3238 /* Cannot change chunk_size, layout, or level */
3239 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
3240 mddev->layout != mddev->new_layout ||
3241 mddev->level != mddev->new_level) {
3242 mddev->new_chunk_sectors = mddev->chunk_sectors;
3243 mddev->new_layout = mddev->layout;
3244 mddev->new_level = mddev->level;
3248 if (!mddev_is_clustered(mddev))
3249 md_allow_write(mddev);
3251 raid_disks = mddev->raid_disks + mddev->delta_disks;
3253 if (raid_disks < conf->raid_disks) {
3255 for (d= 0; d < conf->raid_disks; d++)
3256 if (conf->mirrors[d].rdev)
3258 if (cnt > raid_disks)
3262 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
3265 newpoolinfo->mddev = mddev;
3266 newpoolinfo->raid_disks = raid_disks * 2;
3268 ret = mempool_init(&newpool, NR_RAID_BIOS, r1bio_pool_alloc,
3269 rbio_pool_free, newpoolinfo);
3274 newmirrors = kzalloc(array3_size(sizeof(struct raid1_info),
3279 mempool_exit(&newpool);
3283 freeze_array(conf, 0);
3285 /* ok, everything is stopped */
3286 oldpool = conf->r1bio_pool;
3287 conf->r1bio_pool = newpool;
3289 for (d = d2 = 0; d < conf->raid_disks; d++) {
3290 struct md_rdev *rdev = conf->mirrors[d].rdev;
3291 if (rdev && rdev->raid_disk != d2) {
3292 sysfs_unlink_rdev(mddev, rdev);
3293 rdev->raid_disk = d2;
3294 sysfs_unlink_rdev(mddev, rdev);
3295 if (sysfs_link_rdev(mddev, rdev))
3296 pr_warn("md/raid1:%s: cannot register rd%d\n",
3297 mdname(mddev), rdev->raid_disk);
3300 newmirrors[d2++].rdev = rdev;
3302 kfree(conf->mirrors);
3303 conf->mirrors = newmirrors;
3304 kfree(conf->poolinfo);
3305 conf->poolinfo = newpoolinfo;
3307 spin_lock_irqsave(&conf->device_lock, flags);
3308 mddev->degraded += (raid_disks - conf->raid_disks);
3309 spin_unlock_irqrestore(&conf->device_lock, flags);
3310 conf->raid_disks = mddev->raid_disks = raid_disks;
3311 mddev->delta_disks = 0;
3313 unfreeze_array(conf);
3315 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3316 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3317 md_wakeup_thread(mddev->thread);
3319 mempool_exit(&oldpool);
3323 static void raid1_quiesce(struct mddev *mddev, int quiesce)
3325 struct r1conf *conf = mddev->private;
3328 freeze_array(conf, 0);
3330 unfreeze_array(conf);
3333 static void *raid1_takeover(struct mddev *mddev)
3335 /* raid1 can take over:
3336 * raid5 with 2 devices, any layout or chunk size
3338 if (mddev->level == 5 && mddev->raid_disks == 2) {
3339 struct r1conf *conf;
3340 mddev->new_level = 1;
3341 mddev->new_layout = 0;
3342 mddev->new_chunk_sectors = 0;
3343 conf = setup_conf(mddev);
3344 if (!IS_ERR(conf)) {
3345 /* Array must appear to be quiesced */
3346 conf->array_frozen = 1;
3347 mddev_clear_unsupported_flags(mddev,
3348 UNSUPPORTED_MDDEV_FLAGS);
3352 return ERR_PTR(-EINVAL);
3355 static struct md_personality raid1_personality =
3359 .owner = THIS_MODULE,
3360 .make_request = raid1_make_request,
3363 .status = raid1_status,
3364 .error_handler = raid1_error,
3365 .hot_add_disk = raid1_add_disk,
3366 .hot_remove_disk= raid1_remove_disk,
3367 .spare_active = raid1_spare_active,
3368 .sync_request = raid1_sync_request,
3369 .resize = raid1_resize,
3371 .check_reshape = raid1_reshape,
3372 .quiesce = raid1_quiesce,
3373 .takeover = raid1_takeover,
3374 .congested = raid1_congested,
3377 static int __init raid_init(void)
3379 return register_md_personality(&raid1_personality);
3382 static void raid_exit(void)
3384 unregister_md_personality(&raid1_personality);
3387 module_init(raid_init);
3388 module_exit(raid_exit);
3389 MODULE_LICENSE("GPL");
3390 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
3391 MODULE_ALIAS("md-personality-3"); /* RAID1 */
3392 MODULE_ALIAS("md-raid1");
3393 MODULE_ALIAS("md-level-1");
3395 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);