2 * raid1.c : Multiple Devices driver for Linux
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
8 * RAID-1 management functions.
10 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
12 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
13 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
15 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16 * bitmapped intelligence in resync:
18 * - bitmap marked during normal i/o
19 * - bitmap used to skip nondirty blocks during sync
21 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22 * - persistent bitmap code
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2, or (at your option)
29 * You should have received a copy of the GNU General Public License
30 * (for example /usr/src/linux/COPYING); if not, write to the Free
31 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/slab.h>
35 #include <linux/delay.h>
36 #include <linux/blkdev.h>
37 #include <linux/module.h>
38 #include <linux/seq_file.h>
39 #include <linux/ratelimit.h>
40 #include <trace/events/block.h>
46 * Number of guaranteed r1bios in case of extreme VM load:
48 #define NR_RAID1_BIOS 256
50 /* when we get a read error on a read-only array, we redirect to another
51 * device without failing the first device, or trying to over-write to
52 * correct the read error. To keep track of bad blocks on a per-bio
53 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
55 #define IO_BLOCKED ((struct bio *)1)
56 /* When we successfully write to a known bad-block, we need to remove the
57 * bad-block marking which must be done from process context. So we record
58 * the success by setting devs[n].bio to IO_MADE_GOOD
60 #define IO_MADE_GOOD ((struct bio *)2)
62 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
64 /* When there are this many requests queue to be written by
65 * the raid1 thread, we become 'congested' to provide back-pressure
68 static int max_queued_requests = 1024;
70 static void allow_barrier(struct r1conf *conf, sector_t start_next_window,
72 static void lower_barrier(struct r1conf *conf);
74 #define raid1_log(md, fmt, args...) \
75 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid1 " fmt, ##args); } while (0)
77 static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
79 struct pool_info *pi = data;
80 int size = offsetof(struct r1bio, bios[pi->raid_disks]);
82 /* allocate a r1bio with room for raid_disks entries in the bios array */
83 return kzalloc(size, gfp_flags);
86 static void r1bio_pool_free(void *r1_bio, void *data)
91 #define RESYNC_BLOCK_SIZE (64*1024)
92 #define RESYNC_DEPTH 32
93 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
94 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
95 #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
96 #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
97 #define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
98 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
99 #define NEXT_NORMALIO_DISTANCE (3 * RESYNC_WINDOW_SECTORS)
101 static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
103 struct pool_info *pi = data;
104 struct r1bio *r1_bio;
109 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
114 * Allocate bios : 1 for reading, n-1 for writing
116 for (j = pi->raid_disks ; j-- ; ) {
117 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
120 r1_bio->bios[j] = bio;
123 * Allocate RESYNC_PAGES data pages and attach them to
125 * If this is a user-requested check/repair, allocate
126 * RESYNC_PAGES for each bio.
128 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
129 need_pages = pi->raid_disks;
132 for (j = 0; j < need_pages; j++) {
133 bio = r1_bio->bios[j];
134 bio->bi_vcnt = RESYNC_PAGES;
136 if (bio_alloc_pages(bio, gfp_flags))
139 /* If not user-requests, copy the page pointers to all bios */
140 if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
141 for (i=0; i<RESYNC_PAGES ; i++)
142 for (j=1; j<pi->raid_disks; j++)
143 r1_bio->bios[j]->bi_io_vec[i].bv_page =
144 r1_bio->bios[0]->bi_io_vec[i].bv_page;
147 r1_bio->master_bio = NULL;
153 bio_free_pages(r1_bio->bios[j]);
156 while (++j < pi->raid_disks)
157 bio_put(r1_bio->bios[j]);
158 r1bio_pool_free(r1_bio, data);
162 static void r1buf_pool_free(void *__r1_bio, void *data)
164 struct pool_info *pi = data;
166 struct r1bio *r1bio = __r1_bio;
168 for (i = 0; i < RESYNC_PAGES; i++)
169 for (j = pi->raid_disks; j-- ;) {
171 r1bio->bios[j]->bi_io_vec[i].bv_page !=
172 r1bio->bios[0]->bi_io_vec[i].bv_page)
173 safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
175 for (i=0 ; i < pi->raid_disks; i++)
176 bio_put(r1bio->bios[i]);
178 r1bio_pool_free(r1bio, data);
181 static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
185 for (i = 0; i < conf->raid_disks * 2; i++) {
186 struct bio **bio = r1_bio->bios + i;
187 if (!BIO_SPECIAL(*bio))
193 static void free_r1bio(struct r1bio *r1_bio)
195 struct r1conf *conf = r1_bio->mddev->private;
197 put_all_bios(conf, r1_bio);
198 mempool_free(r1_bio, conf->r1bio_pool);
201 static void put_buf(struct r1bio *r1_bio)
203 struct r1conf *conf = r1_bio->mddev->private;
206 for (i = 0; i < conf->raid_disks * 2; i++) {
207 struct bio *bio = r1_bio->bios[i];
209 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
212 mempool_free(r1_bio, conf->r1buf_pool);
217 static void reschedule_retry(struct r1bio *r1_bio)
220 struct mddev *mddev = r1_bio->mddev;
221 struct r1conf *conf = mddev->private;
223 spin_lock_irqsave(&conf->device_lock, flags);
224 list_add(&r1_bio->retry_list, &conf->retry_list);
226 spin_unlock_irqrestore(&conf->device_lock, flags);
228 wake_up(&conf->wait_barrier);
229 md_wakeup_thread(mddev->thread);
233 * raid_end_bio_io() is called when we have finished servicing a mirrored
234 * operation and are ready to return a success/failure code to the buffer
237 static void call_bio_endio(struct r1bio *r1_bio)
239 struct bio *bio = r1_bio->master_bio;
241 struct r1conf *conf = r1_bio->mddev->private;
242 sector_t start_next_window = r1_bio->start_next_window;
243 sector_t bi_sector = bio->bi_iter.bi_sector;
245 if (bio->bi_phys_segments) {
247 spin_lock_irqsave(&conf->device_lock, flags);
248 bio->bi_phys_segments--;
249 done = (bio->bi_phys_segments == 0);
250 spin_unlock_irqrestore(&conf->device_lock, flags);
252 * make_request() might be waiting for
253 * bi_phys_segments to decrease
255 wake_up(&conf->wait_barrier);
259 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
260 bio->bi_error = -EIO;
265 * Wake up any possible resync thread that waits for the device
268 allow_barrier(conf, start_next_window, bi_sector);
272 static void raid_end_bio_io(struct r1bio *r1_bio)
274 struct bio *bio = r1_bio->master_bio;
276 /* if nobody has done the final endio yet, do it now */
277 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
278 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
279 (bio_data_dir(bio) == WRITE) ? "write" : "read",
280 (unsigned long long) bio->bi_iter.bi_sector,
281 (unsigned long long) bio_end_sector(bio) - 1);
283 call_bio_endio(r1_bio);
289 * Update disk head position estimator based on IRQ completion info.
291 static inline void update_head_pos(int disk, struct r1bio *r1_bio)
293 struct r1conf *conf = r1_bio->mddev->private;
295 conf->mirrors[disk].head_position =
296 r1_bio->sector + (r1_bio->sectors);
300 * Find the disk number which triggered given bio
302 static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
305 struct r1conf *conf = r1_bio->mddev->private;
306 int raid_disks = conf->raid_disks;
308 for (mirror = 0; mirror < raid_disks * 2; mirror++)
309 if (r1_bio->bios[mirror] == bio)
312 BUG_ON(mirror == raid_disks * 2);
313 update_head_pos(mirror, r1_bio);
318 static void raid1_end_read_request(struct bio *bio)
320 int uptodate = !bio->bi_error;
321 struct r1bio *r1_bio = bio->bi_private;
322 struct r1conf *conf = r1_bio->mddev->private;
323 struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
326 * this branch is our 'one mirror IO has finished' event handler:
328 update_head_pos(r1_bio->read_disk, r1_bio);
331 set_bit(R1BIO_Uptodate, &r1_bio->state);
333 /* If all other devices have failed, we want to return
334 * the error upwards rather than fail the last device.
335 * Here we redefine "uptodate" to mean "Don't want to retry"
338 spin_lock_irqsave(&conf->device_lock, flags);
339 if (r1_bio->mddev->degraded == conf->raid_disks ||
340 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
341 test_bit(In_sync, &rdev->flags)))
343 spin_unlock_irqrestore(&conf->device_lock, flags);
347 raid_end_bio_io(r1_bio);
348 rdev_dec_pending(rdev, conf->mddev);
353 char b[BDEVNAME_SIZE];
354 pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n",
356 bdevname(rdev->bdev, b),
357 (unsigned long long)r1_bio->sector);
358 set_bit(R1BIO_ReadError, &r1_bio->state);
359 reschedule_retry(r1_bio);
360 /* don't drop the reference on read_disk yet */
364 static void close_write(struct r1bio *r1_bio)
366 /* it really is the end of this request */
367 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
368 /* free extra copy of the data pages */
369 int i = r1_bio->behind_page_count;
371 safe_put_page(r1_bio->behind_bvecs[i].bv_page);
372 kfree(r1_bio->behind_bvecs);
373 r1_bio->behind_bvecs = NULL;
375 /* clear the bitmap if all writes complete successfully */
376 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
378 !test_bit(R1BIO_Degraded, &r1_bio->state),
379 test_bit(R1BIO_BehindIO, &r1_bio->state));
380 md_write_end(r1_bio->mddev);
383 static void r1_bio_write_done(struct r1bio *r1_bio)
385 if (!atomic_dec_and_test(&r1_bio->remaining))
388 if (test_bit(R1BIO_WriteError, &r1_bio->state))
389 reschedule_retry(r1_bio);
392 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
393 reschedule_retry(r1_bio);
395 raid_end_bio_io(r1_bio);
399 static void raid1_end_write_request(struct bio *bio)
401 struct r1bio *r1_bio = bio->bi_private;
402 int behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
403 struct r1conf *conf = r1_bio->mddev->private;
404 struct bio *to_put = NULL;
405 int mirror = find_bio_disk(r1_bio, bio);
406 struct md_rdev *rdev = conf->mirrors[mirror].rdev;
409 discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
412 * 'one mirror IO has finished' event handler:
414 if (bio->bi_error && !discard_error) {
415 set_bit(WriteErrorSeen, &rdev->flags);
416 if (!test_and_set_bit(WantReplacement, &rdev->flags))
417 set_bit(MD_RECOVERY_NEEDED, &
418 conf->mddev->recovery);
420 set_bit(R1BIO_WriteError, &r1_bio->state);
423 * Set R1BIO_Uptodate in our master bio, so that we
424 * will return a good error code for to the higher
425 * levels even if IO on some other mirrored buffer
428 * The 'master' represents the composite IO operation
429 * to user-side. So if something waits for IO, then it
430 * will wait for the 'master' bio.
435 r1_bio->bios[mirror] = NULL;
438 * Do not set R1BIO_Uptodate if the current device is
439 * rebuilding or Faulty. This is because we cannot use
440 * such device for properly reading the data back (we could
441 * potentially use it, if the current write would have felt
442 * before rdev->recovery_offset, but for simplicity we don't
445 if (test_bit(In_sync, &rdev->flags) &&
446 !test_bit(Faulty, &rdev->flags))
447 set_bit(R1BIO_Uptodate, &r1_bio->state);
449 /* Maybe we can clear some bad blocks. */
450 if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
451 &first_bad, &bad_sectors) && !discard_error) {
452 r1_bio->bios[mirror] = IO_MADE_GOOD;
453 set_bit(R1BIO_MadeGood, &r1_bio->state);
458 if (test_bit(WriteMostly, &rdev->flags))
459 atomic_dec(&r1_bio->behind_remaining);
462 * In behind mode, we ACK the master bio once the I/O
463 * has safely reached all non-writemostly
464 * disks. Setting the Returned bit ensures that this
465 * gets done only once -- we don't ever want to return
466 * -EIO here, instead we'll wait
468 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
469 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
470 /* Maybe we can return now */
471 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
472 struct bio *mbio = r1_bio->master_bio;
473 pr_debug("raid1: behind end write sectors"
475 (unsigned long long) mbio->bi_iter.bi_sector,
476 (unsigned long long) bio_end_sector(mbio) - 1);
477 call_bio_endio(r1_bio);
481 if (r1_bio->bios[mirror] == NULL)
482 rdev_dec_pending(rdev, conf->mddev);
485 * Let's see if all mirrored write operations have finished
488 r1_bio_write_done(r1_bio);
495 * This routine returns the disk from which the requested read should
496 * be done. There is a per-array 'next expected sequential IO' sector
497 * number - if this matches on the next IO then we use the last disk.
498 * There is also a per-disk 'last know head position' sector that is
499 * maintained from IRQ contexts, both the normal and the resync IO
500 * completion handlers update this position correctly. If there is no
501 * perfect sequential match then we pick the disk whose head is closest.
503 * If there are 2 mirrors in the same 2 devices, performance degrades
504 * because position is mirror, not device based.
506 * The rdev for the device selected will have nr_pending incremented.
508 static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
510 const sector_t this_sector = r1_bio->sector;
512 int best_good_sectors;
513 int best_disk, best_dist_disk, best_pending_disk;
517 unsigned int min_pending;
518 struct md_rdev *rdev;
520 int choose_next_idle;
524 * Check if we can balance. We can balance on the whole
525 * device if no resync is going on, or below the resync window.
526 * We take the first readable disk when above the resync window.
529 sectors = r1_bio->sectors;
532 best_dist = MaxSector;
533 best_pending_disk = -1;
534 min_pending = UINT_MAX;
535 best_good_sectors = 0;
537 choose_next_idle = 0;
539 if ((conf->mddev->recovery_cp < this_sector + sectors) ||
540 (mddev_is_clustered(conf->mddev) &&
541 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
542 this_sector + sectors)))
547 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
551 unsigned int pending;
554 rdev = rcu_dereference(conf->mirrors[disk].rdev);
555 if (r1_bio->bios[disk] == IO_BLOCKED
557 || test_bit(Faulty, &rdev->flags))
559 if (!test_bit(In_sync, &rdev->flags) &&
560 rdev->recovery_offset < this_sector + sectors)
562 if (test_bit(WriteMostly, &rdev->flags)) {
563 /* Don't balance among write-mostly, just
564 * use the first as a last resort */
565 if (best_dist_disk < 0) {
566 if (is_badblock(rdev, this_sector, sectors,
567 &first_bad, &bad_sectors)) {
568 if (first_bad <= this_sector)
569 /* Cannot use this */
571 best_good_sectors = first_bad - this_sector;
573 best_good_sectors = sectors;
574 best_dist_disk = disk;
575 best_pending_disk = disk;
579 /* This is a reasonable device to use. It might
582 if (is_badblock(rdev, this_sector, sectors,
583 &first_bad, &bad_sectors)) {
584 if (best_dist < MaxSector)
585 /* already have a better device */
587 if (first_bad <= this_sector) {
588 /* cannot read here. If this is the 'primary'
589 * device, then we must not read beyond
590 * bad_sectors from another device..
592 bad_sectors -= (this_sector - first_bad);
593 if (choose_first && sectors > bad_sectors)
594 sectors = bad_sectors;
595 if (best_good_sectors > sectors)
596 best_good_sectors = sectors;
599 sector_t good_sectors = first_bad - this_sector;
600 if (good_sectors > best_good_sectors) {
601 best_good_sectors = good_sectors;
609 best_good_sectors = sectors;
611 nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
612 has_nonrot_disk |= nonrot;
613 pending = atomic_read(&rdev->nr_pending);
614 dist = abs(this_sector - conf->mirrors[disk].head_position);
619 /* Don't change to another disk for sequential reads */
620 if (conf->mirrors[disk].next_seq_sect == this_sector
622 int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
623 struct raid1_info *mirror = &conf->mirrors[disk];
627 * If buffered sequential IO size exceeds optimal
628 * iosize, check if there is idle disk. If yes, choose
629 * the idle disk. read_balance could already choose an
630 * idle disk before noticing it's a sequential IO in
631 * this disk. This doesn't matter because this disk
632 * will idle, next time it will be utilized after the
633 * first disk has IO size exceeds optimal iosize. In
634 * this way, iosize of the first disk will be optimal
635 * iosize at least. iosize of the second disk might be
636 * small, but not a big deal since when the second disk
637 * starts IO, the first disk is likely still busy.
639 if (nonrot && opt_iosize > 0 &&
640 mirror->seq_start != MaxSector &&
641 mirror->next_seq_sect > opt_iosize &&
642 mirror->next_seq_sect - opt_iosize >=
644 choose_next_idle = 1;
649 /* If device is idle, use it */
655 if (choose_next_idle)
658 if (min_pending > pending) {
659 min_pending = pending;
660 best_pending_disk = disk;
663 if (dist < best_dist) {
665 best_dist_disk = disk;
670 * If all disks are rotational, choose the closest disk. If any disk is
671 * non-rotational, choose the disk with less pending request even the
672 * disk is rotational, which might/might not be optimal for raids with
673 * mixed ratation/non-rotational disks depending on workload.
675 if (best_disk == -1) {
677 best_disk = best_pending_disk;
679 best_disk = best_dist_disk;
682 if (best_disk >= 0) {
683 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
686 atomic_inc(&rdev->nr_pending);
687 sectors = best_good_sectors;
689 if (conf->mirrors[best_disk].next_seq_sect != this_sector)
690 conf->mirrors[best_disk].seq_start = this_sector;
692 conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
695 *max_sectors = sectors;
700 static int raid1_congested(struct mddev *mddev, int bits)
702 struct r1conf *conf = mddev->private;
705 if ((bits & (1 << WB_async_congested)) &&
706 conf->pending_count >= max_queued_requests)
710 for (i = 0; i < conf->raid_disks * 2; i++) {
711 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
712 if (rdev && !test_bit(Faulty, &rdev->flags)) {
713 struct request_queue *q = bdev_get_queue(rdev->bdev);
717 /* Note the '|| 1' - when read_balance prefers
718 * non-congested targets, it can be removed
720 if ((bits & (1 << WB_async_congested)) || 1)
721 ret |= bdi_congested(&q->backing_dev_info, bits);
723 ret &= bdi_congested(&q->backing_dev_info, bits);
730 static void flush_pending_writes(struct r1conf *conf)
732 /* Any writes that have been queued but are awaiting
733 * bitmap updates get flushed here.
735 spin_lock_irq(&conf->device_lock);
737 if (conf->pending_bio_list.head) {
739 bio = bio_list_get(&conf->pending_bio_list);
740 conf->pending_count = 0;
741 spin_unlock_irq(&conf->device_lock);
742 /* flush any pending bitmap writes to
743 * disk before proceeding w/ I/O */
744 bitmap_unplug(conf->mddev->bitmap);
745 wake_up(&conf->wait_barrier);
747 while (bio) { /* submit pending writes */
748 struct bio *next = bio->bi_next;
749 struct md_rdev *rdev = (void*)bio->bi_bdev;
751 bio->bi_bdev = rdev->bdev;
752 if (test_bit(Faulty, &rdev->flags)) {
753 bio->bi_error = -EIO;
755 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
756 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
760 generic_make_request(bio);
764 spin_unlock_irq(&conf->device_lock);
768 * Sometimes we need to suspend IO while we do something else,
769 * either some resync/recovery, or reconfigure the array.
770 * To do this we raise a 'barrier'.
771 * The 'barrier' is a counter that can be raised multiple times
772 * to count how many activities are happening which preclude
774 * We can only raise the barrier if there is no pending IO.
775 * i.e. if nr_pending == 0.
776 * We choose only to raise the barrier if no-one is waiting for the
777 * barrier to go down. This means that as soon as an IO request
778 * is ready, no other operations which require a barrier will start
779 * until the IO request has had a chance.
781 * So: regular IO calls 'wait_barrier'. When that returns there
782 * is no backgroup IO happening, It must arrange to call
783 * allow_barrier when it has finished its IO.
784 * backgroup IO calls must call raise_barrier. Once that returns
785 * there is no normal IO happeing. It must arrange to call
786 * lower_barrier when the particular background IO completes.
788 static void raise_barrier(struct r1conf *conf, sector_t sector_nr)
790 spin_lock_irq(&conf->resync_lock);
792 /* Wait until no block IO is waiting */
793 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
796 /* block any new IO from starting */
798 conf->next_resync = sector_nr;
800 /* For these conditions we must wait:
801 * A: while the array is in frozen state
802 * B: while barrier >= RESYNC_DEPTH, meaning resync reach
803 * the max count which allowed.
804 * C: next_resync + RESYNC_SECTORS > start_next_window, meaning
805 * next resync will reach to the window which normal bios are
807 * D: while there are any active requests in the current window.
809 wait_event_lock_irq(conf->wait_barrier,
810 !conf->array_frozen &&
811 conf->barrier < RESYNC_DEPTH &&
812 conf->current_window_requests == 0 &&
813 (conf->start_next_window >=
814 conf->next_resync + RESYNC_SECTORS),
818 spin_unlock_irq(&conf->resync_lock);
821 static void lower_barrier(struct r1conf *conf)
824 BUG_ON(conf->barrier <= 0);
825 spin_lock_irqsave(&conf->resync_lock, flags);
828 spin_unlock_irqrestore(&conf->resync_lock, flags);
829 wake_up(&conf->wait_barrier);
832 static bool need_to_wait_for_sync(struct r1conf *conf, struct bio *bio)
836 if (conf->array_frozen || !bio)
838 else if (conf->barrier && bio_data_dir(bio) == WRITE) {
839 if ((conf->mddev->curr_resync_completed
840 >= bio_end_sector(bio)) ||
841 (conf->start_next_window + NEXT_NORMALIO_DISTANCE
842 <= bio->bi_iter.bi_sector))
851 static sector_t wait_barrier(struct r1conf *conf, struct bio *bio)
855 spin_lock_irq(&conf->resync_lock);
856 if (need_to_wait_for_sync(conf, bio)) {
858 /* Wait for the barrier to drop.
859 * However if there are already pending
860 * requests (preventing the barrier from
861 * rising completely), and the
862 * per-process bio queue isn't empty,
863 * then don't wait, as we need to empty
864 * that queue to allow conf->start_next_window
867 raid1_log(conf->mddev, "wait barrier");
868 wait_event_lock_irq(conf->wait_barrier,
869 !conf->array_frozen &&
871 ((conf->start_next_window <
872 conf->next_resync + RESYNC_SECTORS) &&
874 !bio_list_empty(current->bio_list))),
879 if (bio && bio_data_dir(bio) == WRITE) {
880 if (bio->bi_iter.bi_sector >= conf->next_resync) {
881 if (conf->start_next_window == MaxSector)
882 conf->start_next_window =
884 NEXT_NORMALIO_DISTANCE;
886 if ((conf->start_next_window + NEXT_NORMALIO_DISTANCE)
887 <= bio->bi_iter.bi_sector)
888 conf->next_window_requests++;
890 conf->current_window_requests++;
891 sector = conf->start_next_window;
896 spin_unlock_irq(&conf->resync_lock);
900 static void allow_barrier(struct r1conf *conf, sector_t start_next_window,
905 spin_lock_irqsave(&conf->resync_lock, flags);
907 if (start_next_window) {
908 if (start_next_window == conf->start_next_window) {
909 if (conf->start_next_window + NEXT_NORMALIO_DISTANCE
911 conf->next_window_requests--;
913 conf->current_window_requests--;
915 conf->current_window_requests--;
917 if (!conf->current_window_requests) {
918 if (conf->next_window_requests) {
919 conf->current_window_requests =
920 conf->next_window_requests;
921 conf->next_window_requests = 0;
922 conf->start_next_window +=
923 NEXT_NORMALIO_DISTANCE;
925 conf->start_next_window = MaxSector;
928 spin_unlock_irqrestore(&conf->resync_lock, flags);
929 wake_up(&conf->wait_barrier);
932 static void freeze_array(struct r1conf *conf, int extra)
934 /* stop syncio and normal IO and wait for everything to
936 * We wait until nr_pending match nr_queued+extra
937 * This is called in the context of one normal IO request
938 * that has failed. Thus any sync request that might be pending
939 * will be blocked by nr_pending, and we need to wait for
940 * pending IO requests to complete or be queued for re-try.
941 * Thus the number queued (nr_queued) plus this request (extra)
942 * must match the number of pending IOs (nr_pending) before
945 spin_lock_irq(&conf->resync_lock);
946 conf->array_frozen = 1;
947 raid1_log(conf->mddev, "wait freeze");
948 wait_event_lock_irq_cmd(conf->wait_barrier,
949 conf->nr_pending == conf->nr_queued+extra,
951 flush_pending_writes(conf));
952 spin_unlock_irq(&conf->resync_lock);
954 static void unfreeze_array(struct r1conf *conf)
956 /* reverse the effect of the freeze */
957 spin_lock_irq(&conf->resync_lock);
958 conf->array_frozen = 0;
959 wake_up(&conf->wait_barrier);
960 spin_unlock_irq(&conf->resync_lock);
963 /* duplicate the data pages for behind I/O
965 static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio)
968 struct bio_vec *bvec;
969 struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
971 if (unlikely(!bvecs))
974 bio_for_each_segment_all(bvec, bio, i) {
976 bvecs[i].bv_page = alloc_page(GFP_NOIO);
977 if (unlikely(!bvecs[i].bv_page))
979 memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
980 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
981 kunmap(bvecs[i].bv_page);
982 kunmap(bvec->bv_page);
984 r1_bio->behind_bvecs = bvecs;
985 r1_bio->behind_page_count = bio->bi_vcnt;
986 set_bit(R1BIO_BehindIO, &r1_bio->state);
990 for (i = 0; i < bio->bi_vcnt; i++)
991 if (bvecs[i].bv_page)
992 put_page(bvecs[i].bv_page);
994 pr_debug("%dB behind alloc failed, doing sync I/O\n",
995 bio->bi_iter.bi_size);
998 struct raid1_plug_cb {
999 struct blk_plug_cb cb;
1000 struct bio_list pending;
1004 static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
1006 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
1008 struct mddev *mddev = plug->cb.data;
1009 struct r1conf *conf = mddev->private;
1012 if (from_schedule || current->bio_list) {
1013 spin_lock_irq(&conf->device_lock);
1014 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1015 conf->pending_count += plug->pending_cnt;
1016 spin_unlock_irq(&conf->device_lock);
1017 wake_up(&conf->wait_barrier);
1018 md_wakeup_thread(mddev->thread);
1023 /* we aren't scheduling, so we can do the write-out directly. */
1024 bio = bio_list_get(&plug->pending);
1025 bitmap_unplug(mddev->bitmap);
1026 wake_up(&conf->wait_barrier);
1028 while (bio) { /* submit pending writes */
1029 struct bio *next = bio->bi_next;
1030 struct md_rdev *rdev = (void*)bio->bi_bdev;
1031 bio->bi_next = NULL;
1032 bio->bi_bdev = rdev->bdev;
1033 if (test_bit(Faulty, &rdev->flags)) {
1034 bio->bi_error = -EIO;
1036 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1037 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
1038 /* Just ignore it */
1041 generic_make_request(bio);
1047 static void raid1_make_request(struct mddev *mddev, struct bio * bio)
1049 struct r1conf *conf = mddev->private;
1050 struct raid1_info *mirror;
1051 struct r1bio *r1_bio;
1052 struct bio *read_bio;
1054 struct bitmap *bitmap;
1055 unsigned long flags;
1056 const int op = bio_op(bio);
1057 const int rw = bio_data_dir(bio);
1058 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1059 const unsigned long do_flush_fua = (bio->bi_opf &
1060 (REQ_PREFLUSH | REQ_FUA));
1061 struct md_rdev *blocked_rdev;
1062 struct blk_plug_cb *cb;
1063 struct raid1_plug_cb *plug = NULL;
1065 int sectors_handled;
1067 sector_t start_next_window;
1070 * Register the new request and wait if the reconstruction
1071 * thread has put up a bar for new requests.
1072 * Continue immediately if no resync is active currently.
1075 md_write_start(mddev, bio); /* wait on superblock update early */
1077 if (bio_data_dir(bio) == WRITE &&
1078 ((bio_end_sector(bio) > mddev->suspend_lo &&
1079 bio->bi_iter.bi_sector < mddev->suspend_hi) ||
1080 (mddev_is_clustered(mddev) &&
1081 md_cluster_ops->area_resyncing(mddev, WRITE,
1082 bio->bi_iter.bi_sector, bio_end_sector(bio))))) {
1083 /* As the suspend_* range is controlled by
1084 * userspace, we want an interruptible
1089 flush_signals(current);
1090 prepare_to_wait(&conf->wait_barrier,
1091 &w, TASK_INTERRUPTIBLE);
1092 if (bio_end_sector(bio) <= mddev->suspend_lo ||
1093 bio->bi_iter.bi_sector >= mddev->suspend_hi ||
1094 (mddev_is_clustered(mddev) &&
1095 !md_cluster_ops->area_resyncing(mddev, WRITE,
1096 bio->bi_iter.bi_sector, bio_end_sector(bio))))
1100 finish_wait(&conf->wait_barrier, &w);
1103 start_next_window = wait_barrier(conf, bio);
1105 bitmap = mddev->bitmap;
1108 * make_request() can abort the operation when read-ahead is being
1109 * used and no empty request is available.
1112 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1114 r1_bio->master_bio = bio;
1115 r1_bio->sectors = bio_sectors(bio);
1117 r1_bio->mddev = mddev;
1118 r1_bio->sector = bio->bi_iter.bi_sector;
1120 /* We might need to issue multiple reads to different
1121 * devices if there are bad blocks around, so we keep
1122 * track of the number of reads in bio->bi_phys_segments.
1123 * If this is 0, there is only one r1_bio and no locking
1124 * will be needed when requests complete. If it is
1125 * non-zero, then it is the number of not-completed requests.
1127 bio->bi_phys_segments = 0;
1128 bio_clear_flag(bio, BIO_SEG_VALID);
1132 * read balancing logic:
1137 rdisk = read_balance(conf, r1_bio, &max_sectors);
1140 /* couldn't find anywhere to read from */
1141 raid_end_bio_io(r1_bio);
1144 mirror = conf->mirrors + rdisk;
1146 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
1148 /* Reading from a write-mostly device must
1149 * take care not to over-take any writes
1152 raid1_log(mddev, "wait behind writes");
1153 wait_event(bitmap->behind_wait,
1154 atomic_read(&bitmap->behind_writes) == 0);
1156 r1_bio->read_disk = rdisk;
1157 r1_bio->start_next_window = 0;
1159 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1160 bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector,
1163 r1_bio->bios[rdisk] = read_bio;
1165 read_bio->bi_iter.bi_sector = r1_bio->sector +
1166 mirror->rdev->data_offset;
1167 read_bio->bi_bdev = mirror->rdev->bdev;
1168 read_bio->bi_end_io = raid1_end_read_request;
1169 bio_set_op_attrs(read_bio, op, do_sync);
1170 read_bio->bi_private = r1_bio;
1173 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1174 read_bio, disk_devt(mddev->gendisk),
1177 if (max_sectors < r1_bio->sectors) {
1178 /* could not read all from this device, so we will
1179 * need another r1_bio.
1182 sectors_handled = (r1_bio->sector + max_sectors
1183 - bio->bi_iter.bi_sector);
1184 r1_bio->sectors = max_sectors;
1185 spin_lock_irq(&conf->device_lock);
1186 if (bio->bi_phys_segments == 0)
1187 bio->bi_phys_segments = 2;
1189 bio->bi_phys_segments++;
1190 spin_unlock_irq(&conf->device_lock);
1191 /* Cannot call generic_make_request directly
1192 * as that will be queued in __make_request
1193 * and subsequent mempool_alloc might block waiting
1194 * for it. So hand bio over to raid1d.
1196 reschedule_retry(r1_bio);
1198 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1200 r1_bio->master_bio = bio;
1201 r1_bio->sectors = bio_sectors(bio) - sectors_handled;
1203 r1_bio->mddev = mddev;
1204 r1_bio->sector = bio->bi_iter.bi_sector +
1208 generic_make_request(read_bio);
1215 if (conf->pending_count >= max_queued_requests) {
1216 md_wakeup_thread(mddev->thread);
1217 raid1_log(mddev, "wait queued");
1218 wait_event(conf->wait_barrier,
1219 conf->pending_count < max_queued_requests);
1221 /* first select target devices under rcu_lock and
1222 * inc refcount on their rdev. Record them by setting
1224 * If there are known/acknowledged bad blocks on any device on
1225 * which we have seen a write error, we want to avoid writing those
1227 * This potentially requires several writes to write around
1228 * the bad blocks. Each set of writes gets it's own r1bio
1229 * with a set of bios attached.
1232 disks = conf->raid_disks * 2;
1234 r1_bio->start_next_window = start_next_window;
1235 blocked_rdev = NULL;
1237 max_sectors = r1_bio->sectors;
1238 for (i = 0; i < disks; i++) {
1239 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1240 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1241 atomic_inc(&rdev->nr_pending);
1242 blocked_rdev = rdev;
1245 r1_bio->bios[i] = NULL;
1246 if (!rdev || test_bit(Faulty, &rdev->flags)) {
1247 if (i < conf->raid_disks)
1248 set_bit(R1BIO_Degraded, &r1_bio->state);
1252 atomic_inc(&rdev->nr_pending);
1253 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1258 is_bad = is_badblock(rdev, r1_bio->sector,
1260 &first_bad, &bad_sectors);
1262 /* mustn't write here until the bad block is
1264 set_bit(BlockedBadBlocks, &rdev->flags);
1265 blocked_rdev = rdev;
1268 if (is_bad && first_bad <= r1_bio->sector) {
1269 /* Cannot write here at all */
1270 bad_sectors -= (r1_bio->sector - first_bad);
1271 if (bad_sectors < max_sectors)
1272 /* mustn't write more than bad_sectors
1273 * to other devices yet
1275 max_sectors = bad_sectors;
1276 rdev_dec_pending(rdev, mddev);
1277 /* We don't set R1BIO_Degraded as that
1278 * only applies if the disk is
1279 * missing, so it might be re-added,
1280 * and we want to know to recover this
1282 * In this case the device is here,
1283 * and the fact that this chunk is not
1284 * in-sync is recorded in the bad
1290 int good_sectors = first_bad - r1_bio->sector;
1291 if (good_sectors < max_sectors)
1292 max_sectors = good_sectors;
1295 r1_bio->bios[i] = bio;
1299 if (unlikely(blocked_rdev)) {
1300 /* Wait for this device to become unblocked */
1302 sector_t old = start_next_window;
1304 for (j = 0; j < i; j++)
1305 if (r1_bio->bios[j])
1306 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
1308 allow_barrier(conf, start_next_window, bio->bi_iter.bi_sector);
1309 raid1_log(mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
1310 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1311 start_next_window = wait_barrier(conf, bio);
1313 * We must make sure the multi r1bios of bio have
1314 * the same value of bi_phys_segments
1316 if (bio->bi_phys_segments && old &&
1317 old != start_next_window)
1318 /* Wait for the former r1bio(s) to complete */
1319 wait_event(conf->wait_barrier,
1320 bio->bi_phys_segments == 1);
1324 if (max_sectors < r1_bio->sectors) {
1325 /* We are splitting this write into multiple parts, so
1326 * we need to prepare for allocating another r1_bio.
1328 r1_bio->sectors = max_sectors;
1329 spin_lock_irq(&conf->device_lock);
1330 if (bio->bi_phys_segments == 0)
1331 bio->bi_phys_segments = 2;
1333 bio->bi_phys_segments++;
1334 spin_unlock_irq(&conf->device_lock);
1336 sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector;
1338 atomic_set(&r1_bio->remaining, 1);
1339 atomic_set(&r1_bio->behind_remaining, 0);
1342 for (i = 0; i < disks; i++) {
1344 if (!r1_bio->bios[i])
1347 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1348 bio_trim(mbio, r1_bio->sector - bio->bi_iter.bi_sector, max_sectors);
1352 * Not if there are too many, or cannot
1353 * allocate memory, or a reader on WriteMostly
1354 * is waiting for behind writes to flush */
1356 (atomic_read(&bitmap->behind_writes)
1357 < mddev->bitmap_info.max_write_behind) &&
1358 !waitqueue_active(&bitmap->behind_wait))
1359 alloc_behind_pages(mbio, r1_bio);
1361 bitmap_startwrite(bitmap, r1_bio->sector,
1363 test_bit(R1BIO_BehindIO,
1367 if (r1_bio->behind_bvecs) {
1368 struct bio_vec *bvec;
1372 * We trimmed the bio, so _all is legit
1374 bio_for_each_segment_all(bvec, mbio, j)
1375 bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
1376 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1377 atomic_inc(&r1_bio->behind_remaining);
1380 r1_bio->bios[i] = mbio;
1382 mbio->bi_iter.bi_sector = (r1_bio->sector +
1383 conf->mirrors[i].rdev->data_offset);
1384 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1385 mbio->bi_end_io = raid1_end_write_request;
1386 bio_set_op_attrs(mbio, op, do_flush_fua | do_sync);
1387 mbio->bi_private = r1_bio;
1389 atomic_inc(&r1_bio->remaining);
1392 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1393 mbio, disk_devt(mddev->gendisk),
1395 /* flush_pending_writes() needs access to the rdev so...*/
1396 mbio->bi_bdev = (void*)conf->mirrors[i].rdev;
1398 cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
1400 plug = container_of(cb, struct raid1_plug_cb, cb);
1403 spin_lock_irqsave(&conf->device_lock, flags);
1405 bio_list_add(&plug->pending, mbio);
1406 plug->pending_cnt++;
1408 bio_list_add(&conf->pending_bio_list, mbio);
1409 conf->pending_count++;
1411 spin_unlock_irqrestore(&conf->device_lock, flags);
1413 md_wakeup_thread(mddev->thread);
1415 /* Mustn't call r1_bio_write_done before this next test,
1416 * as it could result in the bio being freed.
1418 if (sectors_handled < bio_sectors(bio)) {
1419 r1_bio_write_done(r1_bio);
1420 /* We need another r1_bio. It has already been counted
1421 * in bio->bi_phys_segments
1423 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1424 r1_bio->master_bio = bio;
1425 r1_bio->sectors = bio_sectors(bio) - sectors_handled;
1427 r1_bio->mddev = mddev;
1428 r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
1432 r1_bio_write_done(r1_bio);
1434 /* In case raid1d snuck in to freeze_array */
1435 wake_up(&conf->wait_barrier);
1438 static void raid1_status(struct seq_file *seq, struct mddev *mddev)
1440 struct r1conf *conf = mddev->private;
1443 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1444 conf->raid_disks - mddev->degraded);
1446 for (i = 0; i < conf->raid_disks; i++) {
1447 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1448 seq_printf(seq, "%s",
1449 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1452 seq_printf(seq, "]");
1455 static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
1457 char b[BDEVNAME_SIZE];
1458 struct r1conf *conf = mddev->private;
1459 unsigned long flags;
1462 * If it is not operational, then we have already marked it as dead
1463 * else if it is the last working disks, ignore the error, let the
1464 * next level up know.
1465 * else mark the drive as failed
1467 if (test_bit(In_sync, &rdev->flags)
1468 && (conf->raid_disks - mddev->degraded) == 1) {
1470 * Don't fail the drive, act as though we were just a
1471 * normal single drive.
1472 * However don't try a recovery from this drive as
1473 * it is very likely to fail.
1475 conf->recovery_disabled = mddev->recovery_disabled;
1478 set_bit(Blocked, &rdev->flags);
1479 spin_lock_irqsave(&conf->device_lock, flags);
1480 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1482 set_bit(Faulty, &rdev->flags);
1484 set_bit(Faulty, &rdev->flags);
1485 spin_unlock_irqrestore(&conf->device_lock, flags);
1487 * if recovery is running, make sure it aborts.
1489 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1490 set_mask_bits(&mddev->flags, 0,
1491 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
1492 pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
1493 "md/raid1:%s: Operation continuing on %d devices.\n",
1494 mdname(mddev), bdevname(rdev->bdev, b),
1495 mdname(mddev), conf->raid_disks - mddev->degraded);
1498 static void print_conf(struct r1conf *conf)
1502 pr_debug("RAID1 conf printout:\n");
1504 pr_debug("(!conf)\n");
1507 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1511 for (i = 0; i < conf->raid_disks; i++) {
1512 char b[BDEVNAME_SIZE];
1513 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1515 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1516 i, !test_bit(In_sync, &rdev->flags),
1517 !test_bit(Faulty, &rdev->flags),
1518 bdevname(rdev->bdev,b));
1523 static void close_sync(struct r1conf *conf)
1525 wait_barrier(conf, NULL);
1526 allow_barrier(conf, 0, 0);
1528 mempool_destroy(conf->r1buf_pool);
1529 conf->r1buf_pool = NULL;
1531 spin_lock_irq(&conf->resync_lock);
1532 conf->next_resync = MaxSector - 2 * NEXT_NORMALIO_DISTANCE;
1533 conf->start_next_window = MaxSector;
1534 conf->current_window_requests +=
1535 conf->next_window_requests;
1536 conf->next_window_requests = 0;
1537 spin_unlock_irq(&conf->resync_lock);
1540 static int raid1_spare_active(struct mddev *mddev)
1543 struct r1conf *conf = mddev->private;
1545 unsigned long flags;
1548 * Find all failed disks within the RAID1 configuration
1549 * and mark them readable.
1550 * Called under mddev lock, so rcu protection not needed.
1551 * device_lock used to avoid races with raid1_end_read_request
1552 * which expects 'In_sync' flags and ->degraded to be consistent.
1554 spin_lock_irqsave(&conf->device_lock, flags);
1555 for (i = 0; i < conf->raid_disks; i++) {
1556 struct md_rdev *rdev = conf->mirrors[i].rdev;
1557 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
1559 && !test_bit(Candidate, &repl->flags)
1560 && repl->recovery_offset == MaxSector
1561 && !test_bit(Faulty, &repl->flags)
1562 && !test_and_set_bit(In_sync, &repl->flags)) {
1563 /* replacement has just become active */
1565 !test_and_clear_bit(In_sync, &rdev->flags))
1568 /* Replaced device not technically
1569 * faulty, but we need to be sure
1570 * it gets removed and never re-added
1572 set_bit(Faulty, &rdev->flags);
1573 sysfs_notify_dirent_safe(
1578 && rdev->recovery_offset == MaxSector
1579 && !test_bit(Faulty, &rdev->flags)
1580 && !test_and_set_bit(In_sync, &rdev->flags)) {
1582 sysfs_notify_dirent_safe(rdev->sysfs_state);
1585 mddev->degraded -= count;
1586 spin_unlock_irqrestore(&conf->device_lock, flags);
1592 static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1594 struct r1conf *conf = mddev->private;
1597 struct raid1_info *p;
1599 int last = conf->raid_disks - 1;
1601 if (mddev->recovery_disabled == conf->recovery_disabled)
1604 if (md_integrity_add_rdev(rdev, mddev))
1607 if (rdev->raid_disk >= 0)
1608 first = last = rdev->raid_disk;
1611 * find the disk ... but prefer rdev->saved_raid_disk
1614 if (rdev->saved_raid_disk >= 0 &&
1615 rdev->saved_raid_disk >= first &&
1616 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1617 first = last = rdev->saved_raid_disk;
1619 for (mirror = first; mirror <= last; mirror++) {
1620 p = conf->mirrors+mirror;
1624 disk_stack_limits(mddev->gendisk, rdev->bdev,
1625 rdev->data_offset << 9);
1627 p->head_position = 0;
1628 rdev->raid_disk = mirror;
1630 /* As all devices are equivalent, we don't need a full recovery
1631 * if this was recently any drive of the array
1633 if (rdev->saved_raid_disk < 0)
1635 rcu_assign_pointer(p->rdev, rdev);
1638 if (test_bit(WantReplacement, &p->rdev->flags) &&
1639 p[conf->raid_disks].rdev == NULL) {
1640 /* Add this device as a replacement */
1641 clear_bit(In_sync, &rdev->flags);
1642 set_bit(Replacement, &rdev->flags);
1643 rdev->raid_disk = mirror;
1646 rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
1650 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
1651 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1656 static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1658 struct r1conf *conf = mddev->private;
1660 int number = rdev->raid_disk;
1661 struct raid1_info *p = conf->mirrors + number;
1663 if (rdev != p->rdev)
1664 p = conf->mirrors + conf->raid_disks + number;
1667 if (rdev == p->rdev) {
1668 if (test_bit(In_sync, &rdev->flags) ||
1669 atomic_read(&rdev->nr_pending)) {
1673 /* Only remove non-faulty devices if recovery
1676 if (!test_bit(Faulty, &rdev->flags) &&
1677 mddev->recovery_disabled != conf->recovery_disabled &&
1678 mddev->degraded < conf->raid_disks) {
1683 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1685 if (atomic_read(&rdev->nr_pending)) {
1686 /* lost the race, try later */
1692 if (conf->mirrors[conf->raid_disks + number].rdev) {
1693 /* We just removed a device that is being replaced.
1694 * Move down the replacement. We drain all IO before
1695 * doing this to avoid confusion.
1697 struct md_rdev *repl =
1698 conf->mirrors[conf->raid_disks + number].rdev;
1699 freeze_array(conf, 0);
1700 clear_bit(Replacement, &repl->flags);
1702 conf->mirrors[conf->raid_disks + number].rdev = NULL;
1703 unfreeze_array(conf);
1704 clear_bit(WantReplacement, &rdev->flags);
1706 clear_bit(WantReplacement, &rdev->flags);
1707 err = md_integrity_register(mddev);
1715 static void end_sync_read(struct bio *bio)
1717 struct r1bio *r1_bio = bio->bi_private;
1719 update_head_pos(r1_bio->read_disk, r1_bio);
1722 * we have read a block, now it needs to be re-written,
1723 * or re-read if the read failed.
1724 * We don't do much here, just schedule handling by raid1d
1727 set_bit(R1BIO_Uptodate, &r1_bio->state);
1729 if (atomic_dec_and_test(&r1_bio->remaining))
1730 reschedule_retry(r1_bio);
1733 static void end_sync_write(struct bio *bio)
1735 int uptodate = !bio->bi_error;
1736 struct r1bio *r1_bio = bio->bi_private;
1737 struct mddev *mddev = r1_bio->mddev;
1738 struct r1conf *conf = mddev->private;
1741 struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
1744 sector_t sync_blocks = 0;
1745 sector_t s = r1_bio->sector;
1746 long sectors_to_go = r1_bio->sectors;
1747 /* make sure these bits doesn't get cleared. */
1749 bitmap_end_sync(mddev->bitmap, s,
1752 sectors_to_go -= sync_blocks;
1753 } while (sectors_to_go > 0);
1754 set_bit(WriteErrorSeen, &rdev->flags);
1755 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1756 set_bit(MD_RECOVERY_NEEDED, &
1758 set_bit(R1BIO_WriteError, &r1_bio->state);
1759 } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
1760 &first_bad, &bad_sectors) &&
1761 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1764 &first_bad, &bad_sectors)
1766 set_bit(R1BIO_MadeGood, &r1_bio->state);
1768 if (atomic_dec_and_test(&r1_bio->remaining)) {
1769 int s = r1_bio->sectors;
1770 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1771 test_bit(R1BIO_WriteError, &r1_bio->state))
1772 reschedule_retry(r1_bio);
1775 md_done_sync(mddev, s, uptodate);
1780 static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
1781 int sectors, struct page *page, int rw)
1783 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
1787 set_bit(WriteErrorSeen, &rdev->flags);
1788 if (!test_and_set_bit(WantReplacement,
1790 set_bit(MD_RECOVERY_NEEDED, &
1791 rdev->mddev->recovery);
1793 /* need to record an error - either for the block or the device */
1794 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1795 md_error(rdev->mddev, rdev);
1799 static int fix_sync_read_error(struct r1bio *r1_bio)
1801 /* Try some synchronous reads of other devices to get
1802 * good data, much like with normal read errors. Only
1803 * read into the pages we already have so we don't
1804 * need to re-issue the read request.
1805 * We don't need to freeze the array, because being in an
1806 * active sync request, there is no normal IO, and
1807 * no overlapping syncs.
1808 * We don't need to check is_badblock() again as we
1809 * made sure that anything with a bad block in range
1810 * will have bi_end_io clear.
1812 struct mddev *mddev = r1_bio->mddev;
1813 struct r1conf *conf = mddev->private;
1814 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1815 sector_t sect = r1_bio->sector;
1816 int sectors = r1_bio->sectors;
1821 int d = r1_bio->read_disk;
1823 struct md_rdev *rdev;
1826 if (s > (PAGE_SIZE>>9))
1829 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1830 /* No rcu protection needed here devices
1831 * can only be removed when no resync is
1832 * active, and resync is currently active
1834 rdev = conf->mirrors[d].rdev;
1835 if (sync_page_io(rdev, sect, s<<9,
1836 bio->bi_io_vec[idx].bv_page,
1837 REQ_OP_READ, 0, false)) {
1843 if (d == conf->raid_disks * 2)
1845 } while (!success && d != r1_bio->read_disk);
1848 char b[BDEVNAME_SIZE];
1850 /* Cannot read from anywhere, this block is lost.
1851 * Record a bad block on each device. If that doesn't
1852 * work just disable and interrupt the recovery.
1853 * Don't fail devices as that won't really help.
1855 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
1857 bdevname(bio->bi_bdev, b),
1858 (unsigned long long)r1_bio->sector);
1859 for (d = 0; d < conf->raid_disks * 2; d++) {
1860 rdev = conf->mirrors[d].rdev;
1861 if (!rdev || test_bit(Faulty, &rdev->flags))
1863 if (!rdev_set_badblocks(rdev, sect, s, 0))
1867 conf->recovery_disabled =
1868 mddev->recovery_disabled;
1869 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1870 md_done_sync(mddev, r1_bio->sectors, 0);
1882 /* write it back and re-read */
1883 while (d != r1_bio->read_disk) {
1885 d = conf->raid_disks * 2;
1887 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1889 rdev = conf->mirrors[d].rdev;
1890 if (r1_sync_page_io(rdev, sect, s,
1891 bio->bi_io_vec[idx].bv_page,
1893 r1_bio->bios[d]->bi_end_io = NULL;
1894 rdev_dec_pending(rdev, mddev);
1898 while (d != r1_bio->read_disk) {
1900 d = conf->raid_disks * 2;
1902 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1904 rdev = conf->mirrors[d].rdev;
1905 if (r1_sync_page_io(rdev, sect, s,
1906 bio->bi_io_vec[idx].bv_page,
1908 atomic_add(s, &rdev->corrected_errors);
1914 set_bit(R1BIO_Uptodate, &r1_bio->state);
1919 static void process_checks(struct r1bio *r1_bio)
1921 /* We have read all readable devices. If we haven't
1922 * got the block, then there is no hope left.
1923 * If we have, then we want to do a comparison
1924 * and skip the write if everything is the same.
1925 * If any blocks failed to read, then we need to
1926 * attempt an over-write
1928 struct mddev *mddev = r1_bio->mddev;
1929 struct r1conf *conf = mddev->private;
1934 /* Fix variable parts of all bios */
1935 vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
1936 for (i = 0; i < conf->raid_disks * 2; i++) {
1940 struct bio *b = r1_bio->bios[i];
1941 if (b->bi_end_io != end_sync_read)
1943 /* fixup the bio for reuse, but preserve errno */
1944 error = b->bi_error;
1946 b->bi_error = error;
1948 b->bi_iter.bi_size = r1_bio->sectors << 9;
1949 b->bi_iter.bi_sector = r1_bio->sector +
1950 conf->mirrors[i].rdev->data_offset;
1951 b->bi_bdev = conf->mirrors[i].rdev->bdev;
1952 b->bi_end_io = end_sync_read;
1953 b->bi_private = r1_bio;
1955 size = b->bi_iter.bi_size;
1956 for (j = 0; j < vcnt ; j++) {
1958 bi = &b->bi_io_vec[j];
1960 if (size > PAGE_SIZE)
1961 bi->bv_len = PAGE_SIZE;
1967 for (primary = 0; primary < conf->raid_disks * 2; primary++)
1968 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1969 !r1_bio->bios[primary]->bi_error) {
1970 r1_bio->bios[primary]->bi_end_io = NULL;
1971 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1974 r1_bio->read_disk = primary;
1975 for (i = 0; i < conf->raid_disks * 2; i++) {
1977 struct bio *pbio = r1_bio->bios[primary];
1978 struct bio *sbio = r1_bio->bios[i];
1979 int error = sbio->bi_error;
1981 if (sbio->bi_end_io != end_sync_read)
1983 /* Now we can 'fixup' the error value */
1987 for (j = vcnt; j-- ; ) {
1989 p = pbio->bi_io_vec[j].bv_page;
1990 s = sbio->bi_io_vec[j].bv_page;
1991 if (memcmp(page_address(p),
1993 sbio->bi_io_vec[j].bv_len))
1999 atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
2000 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
2002 /* No need to write to this device. */
2003 sbio->bi_end_io = NULL;
2004 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
2008 bio_copy_data(sbio, pbio);
2012 static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
2014 struct r1conf *conf = mddev->private;
2016 int disks = conf->raid_disks * 2;
2017 struct bio *bio, *wbio;
2019 bio = r1_bio->bios[r1_bio->read_disk];
2021 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
2022 /* ouch - failed to read all of that. */
2023 if (!fix_sync_read_error(r1_bio))
2026 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2027 process_checks(r1_bio);
2032 atomic_set(&r1_bio->remaining, 1);
2033 for (i = 0; i < disks ; i++) {
2034 wbio = r1_bio->bios[i];
2035 if (wbio->bi_end_io == NULL ||
2036 (wbio->bi_end_io == end_sync_read &&
2037 (i == r1_bio->read_disk ||
2038 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
2041 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2042 wbio->bi_end_io = end_sync_write;
2043 atomic_inc(&r1_bio->remaining);
2044 md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
2046 generic_make_request(wbio);
2049 if (atomic_dec_and_test(&r1_bio->remaining)) {
2050 /* if we're here, all write(s) have completed, so clean up */
2051 int s = r1_bio->sectors;
2052 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2053 test_bit(R1BIO_WriteError, &r1_bio->state))
2054 reschedule_retry(r1_bio);
2057 md_done_sync(mddev, s, 1);
2063 * This is a kernel thread which:
2065 * 1. Retries failed read operations on working mirrors.
2066 * 2. Updates the raid superblock when problems encounter.
2067 * 3. Performs writes following reads for array synchronising.
2070 static void fix_read_error(struct r1conf *conf, int read_disk,
2071 sector_t sect, int sectors)
2073 struct mddev *mddev = conf->mddev;
2079 struct md_rdev *rdev;
2081 if (s > (PAGE_SIZE>>9))
2089 rdev = rcu_dereference(conf->mirrors[d].rdev);
2091 (test_bit(In_sync, &rdev->flags) ||
2092 (!test_bit(Faulty, &rdev->flags) &&
2093 rdev->recovery_offset >= sect + s)) &&
2094 is_badblock(rdev, sect, s,
2095 &first_bad, &bad_sectors) == 0) {
2096 atomic_inc(&rdev->nr_pending);
2098 if (sync_page_io(rdev, sect, s<<9,
2099 conf->tmppage, REQ_OP_READ, 0, false))
2101 rdev_dec_pending(rdev, mddev);
2107 if (d == conf->raid_disks * 2)
2109 } while (!success && d != read_disk);
2112 /* Cannot read from anywhere - mark it bad */
2113 struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
2114 if (!rdev_set_badblocks(rdev, sect, s, 0))
2115 md_error(mddev, rdev);
2118 /* write it back and re-read */
2120 while (d != read_disk) {
2122 d = conf->raid_disks * 2;
2125 rdev = rcu_dereference(conf->mirrors[d].rdev);
2127 !test_bit(Faulty, &rdev->flags)) {
2128 atomic_inc(&rdev->nr_pending);
2130 r1_sync_page_io(rdev, sect, s,
2131 conf->tmppage, WRITE);
2132 rdev_dec_pending(rdev, mddev);
2137 while (d != read_disk) {
2138 char b[BDEVNAME_SIZE];
2140 d = conf->raid_disks * 2;
2143 rdev = rcu_dereference(conf->mirrors[d].rdev);
2145 !test_bit(Faulty, &rdev->flags)) {
2146 atomic_inc(&rdev->nr_pending);
2148 if (r1_sync_page_io(rdev, sect, s,
2149 conf->tmppage, READ)) {
2150 atomic_add(s, &rdev->corrected_errors);
2151 pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
2153 (unsigned long long)(sect +
2155 bdevname(rdev->bdev, b));
2157 rdev_dec_pending(rdev, mddev);
2166 static int narrow_write_error(struct r1bio *r1_bio, int i)
2168 struct mddev *mddev = r1_bio->mddev;
2169 struct r1conf *conf = mddev->private;
2170 struct md_rdev *rdev = conf->mirrors[i].rdev;
2172 /* bio has the data to be written to device 'i' where
2173 * we just recently had a write error.
2174 * We repeatedly clone the bio and trim down to one block,
2175 * then try the write. Where the write fails we record
2177 * It is conceivable that the bio doesn't exactly align with
2178 * blocks. We must handle this somehow.
2180 * We currently own a reference on the rdev.
2186 int sect_to_write = r1_bio->sectors;
2189 if (rdev->badblocks.shift < 0)
2192 block_sectors = roundup(1 << rdev->badblocks.shift,
2193 bdev_logical_block_size(rdev->bdev) >> 9);
2194 sector = r1_bio->sector;
2195 sectors = ((sector + block_sectors)
2196 & ~(sector_t)(block_sectors - 1))
2199 while (sect_to_write) {
2201 if (sectors > sect_to_write)
2202 sectors = sect_to_write;
2203 /* Write at 'sector' for 'sectors'*/
2205 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
2206 unsigned vcnt = r1_bio->behind_page_count;
2207 struct bio_vec *vec = r1_bio->behind_bvecs;
2209 while (!vec->bv_page) {
2214 wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
2215 memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
2217 wbio->bi_vcnt = vcnt;
2219 wbio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
2222 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2223 wbio->bi_iter.bi_sector = r1_bio->sector;
2224 wbio->bi_iter.bi_size = r1_bio->sectors << 9;
2226 bio_trim(wbio, sector - r1_bio->sector, sectors);
2227 wbio->bi_iter.bi_sector += rdev->data_offset;
2228 wbio->bi_bdev = rdev->bdev;
2230 if (submit_bio_wait(wbio) < 0)
2232 ok = rdev_set_badblocks(rdev, sector,
2237 sect_to_write -= sectors;
2239 sectors = block_sectors;
2244 static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
2247 int s = r1_bio->sectors;
2248 for (m = 0; m < conf->raid_disks * 2 ; m++) {
2249 struct md_rdev *rdev = conf->mirrors[m].rdev;
2250 struct bio *bio = r1_bio->bios[m];
2251 if (bio->bi_end_io == NULL)
2253 if (!bio->bi_error &&
2254 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
2255 rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
2257 if (bio->bi_error &&
2258 test_bit(R1BIO_WriteError, &r1_bio->state)) {
2259 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
2260 md_error(conf->mddev, rdev);
2264 md_done_sync(conf->mddev, s, 1);
2267 static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
2271 for (m = 0; m < conf->raid_disks * 2 ; m++)
2272 if (r1_bio->bios[m] == IO_MADE_GOOD) {
2273 struct md_rdev *rdev = conf->mirrors[m].rdev;
2274 rdev_clear_badblocks(rdev,
2276 r1_bio->sectors, 0);
2277 rdev_dec_pending(rdev, conf->mddev);
2278 } else if (r1_bio->bios[m] != NULL) {
2279 /* This drive got a write error. We need to
2280 * narrow down and record precise write
2284 if (!narrow_write_error(r1_bio, m)) {
2285 md_error(conf->mddev,
2286 conf->mirrors[m].rdev);
2287 /* an I/O failed, we can't clear the bitmap */
2288 set_bit(R1BIO_Degraded, &r1_bio->state);
2290 rdev_dec_pending(conf->mirrors[m].rdev,
2294 spin_lock_irq(&conf->device_lock);
2295 list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
2297 spin_unlock_irq(&conf->device_lock);
2298 md_wakeup_thread(conf->mddev->thread);
2300 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2301 close_write(r1_bio);
2302 raid_end_bio_io(r1_bio);
2306 static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
2310 struct mddev *mddev = conf->mddev;
2312 char b[BDEVNAME_SIZE];
2313 struct md_rdev *rdev;
2315 sector_t bio_sector;
2317 clear_bit(R1BIO_ReadError, &r1_bio->state);
2318 /* we got a read error. Maybe the drive is bad. Maybe just
2319 * the block and we can fix it.
2320 * We freeze all other IO, and try reading the block from
2321 * other devices. When we find one, we re-write
2322 * and check it that fixes the read error.
2323 * This is all done synchronously while the array is
2327 bio = r1_bio->bios[r1_bio->read_disk];
2328 bdevname(bio->bi_bdev, b);
2329 bio_dev = bio->bi_bdev->bd_dev;
2330 bio_sector = conf->mirrors[r1_bio->read_disk].rdev->data_offset + r1_bio->sector;
2332 r1_bio->bios[r1_bio->read_disk] = NULL;
2334 if (mddev->ro == 0) {
2335 freeze_array(conf, 1);
2336 fix_read_error(conf, r1_bio->read_disk,
2337 r1_bio->sector, r1_bio->sectors);
2338 unfreeze_array(conf);
2340 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
2343 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
2346 disk = read_balance(conf, r1_bio, &max_sectors);
2348 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
2349 mdname(mddev), b, (unsigned long long)r1_bio->sector);
2350 raid_end_bio_io(r1_bio);
2352 const unsigned long do_sync
2353 = r1_bio->master_bio->bi_opf & REQ_SYNC;
2354 r1_bio->read_disk = disk;
2355 bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
2356 bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector,
2358 r1_bio->bios[r1_bio->read_disk] = bio;
2359 rdev = conf->mirrors[disk].rdev;
2360 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
2362 (unsigned long long)r1_bio->sector,
2363 bdevname(rdev->bdev, b));
2364 bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset;
2365 bio->bi_bdev = rdev->bdev;
2366 bio->bi_end_io = raid1_end_read_request;
2367 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
2368 bio->bi_private = r1_bio;
2369 if (max_sectors < r1_bio->sectors) {
2370 /* Drat - have to split this up more */
2371 struct bio *mbio = r1_bio->master_bio;
2372 int sectors_handled = (r1_bio->sector + max_sectors
2373 - mbio->bi_iter.bi_sector);
2374 r1_bio->sectors = max_sectors;
2375 spin_lock_irq(&conf->device_lock);
2376 if (mbio->bi_phys_segments == 0)
2377 mbio->bi_phys_segments = 2;
2379 mbio->bi_phys_segments++;
2380 spin_unlock_irq(&conf->device_lock);
2381 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2382 bio, bio_dev, bio_sector);
2383 generic_make_request(bio);
2386 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
2388 r1_bio->master_bio = mbio;
2389 r1_bio->sectors = bio_sectors(mbio) - sectors_handled;
2391 set_bit(R1BIO_ReadError, &r1_bio->state);
2392 r1_bio->mddev = mddev;
2393 r1_bio->sector = mbio->bi_iter.bi_sector +
2398 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2399 bio, bio_dev, bio_sector);
2400 generic_make_request(bio);
2405 static void raid1d(struct md_thread *thread)
2407 struct mddev *mddev = thread->mddev;
2408 struct r1bio *r1_bio;
2409 unsigned long flags;
2410 struct r1conf *conf = mddev->private;
2411 struct list_head *head = &conf->retry_list;
2412 struct blk_plug plug;
2414 md_check_recovery(mddev);
2416 if (!list_empty_careful(&conf->bio_end_io_list) &&
2417 !test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
2419 spin_lock_irqsave(&conf->device_lock, flags);
2420 if (!test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
2421 while (!list_empty(&conf->bio_end_io_list)) {
2422 list_move(conf->bio_end_io_list.prev, &tmp);
2426 spin_unlock_irqrestore(&conf->device_lock, flags);
2427 while (!list_empty(&tmp)) {
2428 r1_bio = list_first_entry(&tmp, struct r1bio,
2430 list_del(&r1_bio->retry_list);
2431 if (mddev->degraded)
2432 set_bit(R1BIO_Degraded, &r1_bio->state);
2433 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2434 close_write(r1_bio);
2435 raid_end_bio_io(r1_bio);
2439 blk_start_plug(&plug);
2442 flush_pending_writes(conf);
2444 spin_lock_irqsave(&conf->device_lock, flags);
2445 if (list_empty(head)) {
2446 spin_unlock_irqrestore(&conf->device_lock, flags);
2449 r1_bio = list_entry(head->prev, struct r1bio, retry_list);
2450 list_del(head->prev);
2452 spin_unlock_irqrestore(&conf->device_lock, flags);
2454 mddev = r1_bio->mddev;
2455 conf = mddev->private;
2456 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
2457 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2458 test_bit(R1BIO_WriteError, &r1_bio->state))
2459 handle_sync_write_finished(conf, r1_bio);
2461 sync_request_write(mddev, r1_bio);
2462 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2463 test_bit(R1BIO_WriteError, &r1_bio->state))
2464 handle_write_finished(conf, r1_bio);
2465 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2466 handle_read_error(conf, r1_bio);
2468 /* just a partial read to be scheduled from separate
2471 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
2474 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2475 md_check_recovery(mddev);
2477 blk_finish_plug(&plug);
2480 static int init_resync(struct r1conf *conf)
2484 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2485 BUG_ON(conf->r1buf_pool);
2486 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2488 if (!conf->r1buf_pool)
2490 conf->next_resync = 0;
2495 * perform a "sync" on one "block"
2497 * We need to make sure that no normal I/O request - particularly write
2498 * requests - conflict with active sync requests.
2500 * This is achieved by tracking pending requests and a 'barrier' concept
2501 * that can be installed to exclude normal IO requests.
2504 static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
2507 struct r1conf *conf = mddev->private;
2508 struct r1bio *r1_bio;
2510 sector_t max_sector, nr_sectors;
2514 int write_targets = 0, read_targets = 0;
2515 sector_t sync_blocks;
2516 int still_degraded = 0;
2517 int good_sectors = RESYNC_SECTORS;
2518 int min_bad = 0; /* number of sectors that are bad in all devices */
2520 if (!conf->r1buf_pool)
2521 if (init_resync(conf))
2524 max_sector = mddev->dev_sectors;
2525 if (sector_nr >= max_sector) {
2526 /* If we aborted, we need to abort the
2527 * sync on the 'current' bitmap chunk (there will
2528 * only be one in raid1 resync.
2529 * We can find the current addess in mddev->curr_resync
2531 if (mddev->curr_resync < max_sector) /* aborted */
2532 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2534 else /* completed sync */
2537 bitmap_close_sync(mddev->bitmap);
2540 if (mddev_is_clustered(mddev)) {
2541 conf->cluster_sync_low = 0;
2542 conf->cluster_sync_high = 0;
2547 if (mddev->bitmap == NULL &&
2548 mddev->recovery_cp == MaxSector &&
2549 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2550 conf->fullsync == 0) {
2552 return max_sector - sector_nr;
2554 /* before building a request, check if we can skip these blocks..
2555 * This call the bitmap_start_sync doesn't actually record anything
2557 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
2558 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2559 /* We can skip this block, and probably several more */
2565 * If there is non-resync activity waiting for a turn, then let it
2566 * though before starting on this new sync request.
2568 if (conf->nr_waiting)
2569 schedule_timeout_uninterruptible(1);
2571 /* we are incrementing sector_nr below. To be safe, we check against
2572 * sector_nr + two times RESYNC_SECTORS
2575 bitmap_cond_end_sync(mddev->bitmap, sector_nr,
2576 mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
2577 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
2579 raise_barrier(conf, sector_nr);
2583 * If we get a correctably read error during resync or recovery,
2584 * we might want to read from a different device. So we
2585 * flag all drives that could conceivably be read from for READ,
2586 * and any others (which will be non-In_sync devices) for WRITE.
2587 * If a read fails, we try reading from something else for which READ
2591 r1_bio->mddev = mddev;
2592 r1_bio->sector = sector_nr;
2594 set_bit(R1BIO_IsSync, &r1_bio->state);
2596 for (i = 0; i < conf->raid_disks * 2; i++) {
2597 struct md_rdev *rdev;
2598 bio = r1_bio->bios[i];
2601 rdev = rcu_dereference(conf->mirrors[i].rdev);
2603 test_bit(Faulty, &rdev->flags)) {
2604 if (i < conf->raid_disks)
2606 } else if (!test_bit(In_sync, &rdev->flags)) {
2607 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
2608 bio->bi_end_io = end_sync_write;
2611 /* may need to read from here */
2612 sector_t first_bad = MaxSector;
2615 if (is_badblock(rdev, sector_nr, good_sectors,
2616 &first_bad, &bad_sectors)) {
2617 if (first_bad > sector_nr)
2618 good_sectors = first_bad - sector_nr;
2620 bad_sectors -= (sector_nr - first_bad);
2622 min_bad > bad_sectors)
2623 min_bad = bad_sectors;
2626 if (sector_nr < first_bad) {
2627 if (test_bit(WriteMostly, &rdev->flags)) {
2634 bio_set_op_attrs(bio, REQ_OP_READ, 0);
2635 bio->bi_end_io = end_sync_read;
2637 } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
2638 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2639 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
2641 * The device is suitable for reading (InSync),
2642 * but has bad block(s) here. Let's try to correct them,
2643 * if we are doing resync or repair. Otherwise, leave
2644 * this device alone for this sync request.
2646 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
2647 bio->bi_end_io = end_sync_write;
2651 if (bio->bi_end_io) {
2652 atomic_inc(&rdev->nr_pending);
2653 bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
2654 bio->bi_bdev = rdev->bdev;
2655 bio->bi_private = r1_bio;
2661 r1_bio->read_disk = disk;
2663 if (read_targets == 0 && min_bad > 0) {
2664 /* These sectors are bad on all InSync devices, so we
2665 * need to mark them bad on all write targets
2668 for (i = 0 ; i < conf->raid_disks * 2 ; i++)
2669 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
2670 struct md_rdev *rdev = conf->mirrors[i].rdev;
2671 ok = rdev_set_badblocks(rdev, sector_nr,
2675 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2680 /* Cannot record the badblocks, so need to
2682 * If there are multiple read targets, could just
2683 * fail the really bad ones ???
2685 conf->recovery_disabled = mddev->recovery_disabled;
2686 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2692 if (min_bad > 0 && min_bad < good_sectors) {
2693 /* only resync enough to reach the next bad->good
2695 good_sectors = min_bad;
2698 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2699 /* extra read targets are also write targets */
2700 write_targets += read_targets-1;
2702 if (write_targets == 0 || read_targets == 0) {
2703 /* There is nowhere to write, so all non-sync
2704 * drives must be failed - so we are finished
2708 max_sector = sector_nr + min_bad;
2709 rv = max_sector - sector_nr;
2715 if (max_sector > mddev->resync_max)
2716 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2717 if (max_sector > sector_nr + good_sectors)
2718 max_sector = sector_nr + good_sectors;
2723 int len = PAGE_SIZE;
2724 if (sector_nr + (len>>9) > max_sector)
2725 len = (max_sector - sector_nr) << 9;
2728 if (sync_blocks == 0) {
2729 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2730 &sync_blocks, still_degraded) &&
2732 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2734 if ((len >> 9) > sync_blocks)
2735 len = sync_blocks<<9;
2738 for (i = 0 ; i < conf->raid_disks * 2; i++) {
2739 bio = r1_bio->bios[i];
2740 if (bio->bi_end_io) {
2741 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2742 if (bio_add_page(bio, page, len, 0) == 0) {
2744 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2747 bio = r1_bio->bios[i];
2748 if (bio->bi_end_io==NULL)
2750 /* remove last page from this bio */
2752 bio->bi_iter.bi_size -= len;
2753 bio_clear_flag(bio, BIO_SEG_VALID);
2759 nr_sectors += len>>9;
2760 sector_nr += len>>9;
2761 sync_blocks -= (len>>9);
2762 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2764 r1_bio->sectors = nr_sectors;
2766 if (mddev_is_clustered(mddev) &&
2767 conf->cluster_sync_high < sector_nr + nr_sectors) {
2768 conf->cluster_sync_low = mddev->curr_resync_completed;
2769 conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
2770 /* Send resync message */
2771 md_cluster_ops->resync_info_update(mddev,
2772 conf->cluster_sync_low,
2773 conf->cluster_sync_high);
2776 /* For a user-requested sync, we read all readable devices and do a
2779 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2780 atomic_set(&r1_bio->remaining, read_targets);
2781 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
2782 bio = r1_bio->bios[i];
2783 if (bio->bi_end_io == end_sync_read) {
2785 md_sync_acct(bio->bi_bdev, nr_sectors);
2786 generic_make_request(bio);
2790 atomic_set(&r1_bio->remaining, 1);
2791 bio = r1_bio->bios[r1_bio->read_disk];
2792 md_sync_acct(bio->bi_bdev, nr_sectors);
2793 generic_make_request(bio);
2799 static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
2804 return mddev->dev_sectors;
2807 static struct r1conf *setup_conf(struct mddev *mddev)
2809 struct r1conf *conf;
2811 struct raid1_info *disk;
2812 struct md_rdev *rdev;
2815 conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
2819 conf->mirrors = kzalloc(sizeof(struct raid1_info)
2820 * mddev->raid_disks * 2,
2825 conf->tmppage = alloc_page(GFP_KERNEL);
2829 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
2830 if (!conf->poolinfo)
2832 conf->poolinfo->raid_disks = mddev->raid_disks * 2;
2833 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2836 if (!conf->r1bio_pool)
2839 conf->poolinfo->mddev = mddev;
2842 spin_lock_init(&conf->device_lock);
2843 rdev_for_each(rdev, mddev) {
2844 struct request_queue *q;
2845 int disk_idx = rdev->raid_disk;
2846 if (disk_idx >= mddev->raid_disks
2849 if (test_bit(Replacement, &rdev->flags))
2850 disk = conf->mirrors + mddev->raid_disks + disk_idx;
2852 disk = conf->mirrors + disk_idx;
2857 q = bdev_get_queue(rdev->bdev);
2859 disk->head_position = 0;
2860 disk->seq_start = MaxSector;
2862 conf->raid_disks = mddev->raid_disks;
2863 conf->mddev = mddev;
2864 INIT_LIST_HEAD(&conf->retry_list);
2865 INIT_LIST_HEAD(&conf->bio_end_io_list);
2867 spin_lock_init(&conf->resync_lock);
2868 init_waitqueue_head(&conf->wait_barrier);
2870 bio_list_init(&conf->pending_bio_list);
2871 conf->pending_count = 0;
2872 conf->recovery_disabled = mddev->recovery_disabled - 1;
2874 conf->start_next_window = MaxSector;
2875 conf->current_window_requests = conf->next_window_requests = 0;
2878 for (i = 0; i < conf->raid_disks * 2; i++) {
2880 disk = conf->mirrors + i;
2882 if (i < conf->raid_disks &&
2883 disk[conf->raid_disks].rdev) {
2884 /* This slot has a replacement. */
2886 /* No original, just make the replacement
2887 * a recovering spare
2890 disk[conf->raid_disks].rdev;
2891 disk[conf->raid_disks].rdev = NULL;
2892 } else if (!test_bit(In_sync, &disk->rdev->flags))
2893 /* Original is not in_sync - bad */
2898 !test_bit(In_sync, &disk->rdev->flags)) {
2899 disk->head_position = 0;
2901 (disk->rdev->saved_raid_disk < 0))
2907 conf->thread = md_register_thread(raid1d, mddev, "raid1");
2915 mempool_destroy(conf->r1bio_pool);
2916 kfree(conf->mirrors);
2917 safe_put_page(conf->tmppage);
2918 kfree(conf->poolinfo);
2921 return ERR_PTR(err);
2924 static void raid1_free(struct mddev *mddev, void *priv);
2925 static int raid1_run(struct mddev *mddev)
2927 struct r1conf *conf;
2929 struct md_rdev *rdev;
2931 bool discard_supported = false;
2933 if (mddev->level != 1) {
2934 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
2935 mdname(mddev), mddev->level);
2938 if (mddev->reshape_position != MaxSector) {
2939 pr_warn("md/raid1:%s: reshape_position set but not supported\n",
2944 * copy the already verified devices into our private RAID1
2945 * bookkeeping area. [whatever we allocate in run(),
2946 * should be freed in raid1_free()]
2948 if (mddev->private == NULL)
2949 conf = setup_conf(mddev);
2951 conf = mddev->private;
2954 return PTR_ERR(conf);
2957 blk_queue_max_write_same_sectors(mddev->queue, 0);
2959 rdev_for_each(rdev, mddev) {
2960 if (!mddev->gendisk)
2962 disk_stack_limits(mddev->gendisk, rdev->bdev,
2963 rdev->data_offset << 9);
2964 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
2965 discard_supported = true;
2968 mddev->degraded = 0;
2969 for (i=0; i < conf->raid_disks; i++)
2970 if (conf->mirrors[i].rdev == NULL ||
2971 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2972 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2975 if (conf->raid_disks - mddev->degraded == 1)
2976 mddev->recovery_cp = MaxSector;
2978 if (mddev->recovery_cp != MaxSector)
2979 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
2981 pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
2982 mdname(mddev), mddev->raid_disks - mddev->degraded,
2986 * Ok, everything is just fine now
2988 mddev->thread = conf->thread;
2989 conf->thread = NULL;
2990 mddev->private = conf;
2991 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
2993 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
2996 if (discard_supported)
2997 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3000 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3004 ret = md_integrity_register(mddev);
3006 md_unregister_thread(&mddev->thread);
3007 raid1_free(mddev, conf);
3012 static void raid1_free(struct mddev *mddev, void *priv)
3014 struct r1conf *conf = priv;
3016 mempool_destroy(conf->r1bio_pool);
3017 kfree(conf->mirrors);
3018 safe_put_page(conf->tmppage);
3019 kfree(conf->poolinfo);
3023 static int raid1_resize(struct mddev *mddev, sector_t sectors)
3025 /* no resync is happening, and there is enough space
3026 * on all devices, so we can resize.
3027 * We need to make sure resync covers any new space.
3028 * If the array is shrinking we should possibly wait until
3029 * any io in the removed space completes, but it hardly seems
3032 sector_t newsize = raid1_size(mddev, sectors, 0);
3033 if (mddev->external_size &&
3034 mddev->array_sectors > newsize)
3036 if (mddev->bitmap) {
3037 int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
3041 md_set_array_sectors(mddev, newsize);
3042 set_capacity(mddev->gendisk, mddev->array_sectors);
3043 revalidate_disk(mddev->gendisk);
3044 if (sectors > mddev->dev_sectors &&
3045 mddev->recovery_cp > mddev->dev_sectors) {
3046 mddev->recovery_cp = mddev->dev_sectors;
3047 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3049 mddev->dev_sectors = sectors;
3050 mddev->resync_max_sectors = sectors;
3054 static int raid1_reshape(struct mddev *mddev)
3057 * 1/ resize the r1bio_pool
3058 * 2/ resize conf->mirrors
3060 * We allocate a new r1bio_pool if we can.
3061 * Then raise a device barrier and wait until all IO stops.
3062 * Then resize conf->mirrors and swap in the new r1bio pool.
3064 * At the same time, we "pack" the devices so that all the missing
3065 * devices have the higher raid_disk numbers.
3067 mempool_t *newpool, *oldpool;
3068 struct pool_info *newpoolinfo;
3069 struct raid1_info *newmirrors;
3070 struct r1conf *conf = mddev->private;
3071 int cnt, raid_disks;
3072 unsigned long flags;
3075 /* Cannot change chunk_size, layout, or level */
3076 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
3077 mddev->layout != mddev->new_layout ||
3078 mddev->level != mddev->new_level) {
3079 mddev->new_chunk_sectors = mddev->chunk_sectors;
3080 mddev->new_layout = mddev->layout;
3081 mddev->new_level = mddev->level;
3085 if (!mddev_is_clustered(mddev)) {
3086 err = md_allow_write(mddev);
3091 raid_disks = mddev->raid_disks + mddev->delta_disks;
3093 if (raid_disks < conf->raid_disks) {
3095 for (d= 0; d < conf->raid_disks; d++)
3096 if (conf->mirrors[d].rdev)
3098 if (cnt > raid_disks)
3102 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
3105 newpoolinfo->mddev = mddev;
3106 newpoolinfo->raid_disks = raid_disks * 2;
3108 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
3109 r1bio_pool_free, newpoolinfo);
3114 newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2,
3118 mempool_destroy(newpool);
3122 freeze_array(conf, 0);
3124 /* ok, everything is stopped */
3125 oldpool = conf->r1bio_pool;
3126 conf->r1bio_pool = newpool;
3128 for (d = d2 = 0; d < conf->raid_disks; d++) {
3129 struct md_rdev *rdev = conf->mirrors[d].rdev;
3130 if (rdev && rdev->raid_disk != d2) {
3131 sysfs_unlink_rdev(mddev, rdev);
3132 rdev->raid_disk = d2;
3133 sysfs_unlink_rdev(mddev, rdev);
3134 if (sysfs_link_rdev(mddev, rdev))
3135 pr_warn("md/raid1:%s: cannot register rd%d\n",
3136 mdname(mddev), rdev->raid_disk);
3139 newmirrors[d2++].rdev = rdev;
3141 kfree(conf->mirrors);
3142 conf->mirrors = newmirrors;
3143 kfree(conf->poolinfo);
3144 conf->poolinfo = newpoolinfo;
3146 spin_lock_irqsave(&conf->device_lock, flags);
3147 mddev->degraded += (raid_disks - conf->raid_disks);
3148 spin_unlock_irqrestore(&conf->device_lock, flags);
3149 conf->raid_disks = mddev->raid_disks = raid_disks;
3150 mddev->delta_disks = 0;
3152 unfreeze_array(conf);
3154 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3155 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3156 md_wakeup_thread(mddev->thread);
3158 mempool_destroy(oldpool);
3162 static void raid1_quiesce(struct mddev *mddev, int state)
3164 struct r1conf *conf = mddev->private;
3167 case 2: /* wake for suspend */
3168 wake_up(&conf->wait_barrier);
3171 freeze_array(conf, 0);
3174 unfreeze_array(conf);
3179 static void *raid1_takeover(struct mddev *mddev)
3181 /* raid1 can take over:
3182 * raid5 with 2 devices, any layout or chunk size
3184 if (mddev->level == 5 && mddev->raid_disks == 2) {
3185 struct r1conf *conf;
3186 mddev->new_level = 1;
3187 mddev->new_layout = 0;
3188 mddev->new_chunk_sectors = 0;
3189 conf = setup_conf(mddev);
3191 /* Array must appear to be quiesced */
3192 conf->array_frozen = 1;
3195 return ERR_PTR(-EINVAL);
3198 static struct md_personality raid1_personality =
3202 .owner = THIS_MODULE,
3203 .make_request = raid1_make_request,
3206 .status = raid1_status,
3207 .error_handler = raid1_error,
3208 .hot_add_disk = raid1_add_disk,
3209 .hot_remove_disk= raid1_remove_disk,
3210 .spare_active = raid1_spare_active,
3211 .sync_request = raid1_sync_request,
3212 .resize = raid1_resize,
3214 .check_reshape = raid1_reshape,
3215 .quiesce = raid1_quiesce,
3216 .takeover = raid1_takeover,
3217 .congested = raid1_congested,
3220 static int __init raid_init(void)
3222 return register_md_personality(&raid1_personality);
3225 static void raid_exit(void)
3227 unregister_md_personality(&raid1_personality);
3230 module_init(raid_init);
3231 module_exit(raid_exit);
3232 MODULE_LICENSE("GPL");
3233 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
3234 MODULE_ALIAS("md-personality-3"); /* RAID1 */
3235 MODULE_ALIAS("md-raid1");
3236 MODULE_ALIAS("md-level-1");
3238 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);