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>
32 #include <linux/interval_tree_generic.h>
34 #include <trace/events/block.h>
38 #include "md-bitmap.h"
40 #define UNSUPPORTED_MDDEV_FLAGS \
41 ((1L << MD_HAS_JOURNAL) | \
42 (1L << MD_JOURNAL_CLEAN) | \
43 (1L << MD_HAS_PPL) | \
44 (1L << MD_HAS_MULTIPLE_PPLS))
46 static void allow_barrier(struct r1conf
*conf
, sector_t sector_nr
);
47 static void lower_barrier(struct r1conf
*conf
, sector_t sector_nr
);
49 #define raid1_log(md, fmt, args...) \
50 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid1 " fmt, ##args); } while (0)
54 #define START(node) ((node)->start)
55 #define LAST(node) ((node)->last)
56 INTERVAL_TREE_DEFINE(struct serial_info
, node
, sector_t
, _subtree_last
,
57 START
, LAST
, static inline, raid1_rb
);
59 static int check_and_add_serial(struct md_rdev
*rdev
, struct r1bio
*r1_bio
,
60 struct serial_info
*si
, int idx
)
64 sector_t lo
= r1_bio
->sector
;
65 sector_t hi
= lo
+ r1_bio
->sectors
;
66 struct serial_in_rdev
*serial
= &rdev
->serial
[idx
];
68 spin_lock_irqsave(&serial
->serial_lock
, flags
);
69 /* collision happened */
70 if (raid1_rb_iter_first(&serial
->serial_rb
, lo
, hi
))
75 raid1_rb_insert(si
, &serial
->serial_rb
);
77 spin_unlock_irqrestore(&serial
->serial_lock
, flags
);
82 static void wait_for_serialization(struct md_rdev
*rdev
, struct r1bio
*r1_bio
)
84 struct mddev
*mddev
= rdev
->mddev
;
85 struct serial_info
*si
;
86 int idx
= sector_to_idx(r1_bio
->sector
);
87 struct serial_in_rdev
*serial
= &rdev
->serial
[idx
];
89 if (WARN_ON(!mddev
->serial_info_pool
))
91 si
= mempool_alloc(mddev
->serial_info_pool
, GFP_NOIO
);
92 wait_event(serial
->serial_io_wait
,
93 check_and_add_serial(rdev
, r1_bio
, si
, idx
) == 0);
96 static void remove_serial(struct md_rdev
*rdev
, sector_t lo
, sector_t hi
)
98 struct serial_info
*si
;
101 struct mddev
*mddev
= rdev
->mddev
;
102 int idx
= sector_to_idx(lo
);
103 struct serial_in_rdev
*serial
= &rdev
->serial
[idx
];
105 spin_lock_irqsave(&serial
->serial_lock
, flags
);
106 for (si
= raid1_rb_iter_first(&serial
->serial_rb
, lo
, hi
);
107 si
; si
= raid1_rb_iter_next(si
, lo
, hi
)) {
108 if (si
->start
== lo
&& si
->last
== hi
) {
109 raid1_rb_remove(si
, &serial
->serial_rb
);
110 mempool_free(si
, mddev
->serial_info_pool
);
116 WARN(1, "The write IO is not recorded for serialization\n");
117 spin_unlock_irqrestore(&serial
->serial_lock
, flags
);
118 wake_up(&serial
->serial_io_wait
);
122 * for resync bio, r1bio pointer can be retrieved from the per-bio
123 * 'struct resync_pages'.
125 static inline struct r1bio
*get_resync_r1bio(struct bio
*bio
)
127 return get_resync_pages(bio
)->raid_bio
;
130 static void * r1bio_pool_alloc(gfp_t gfp_flags
, void *data
)
132 struct pool_info
*pi
= data
;
133 int size
= offsetof(struct r1bio
, bios
[pi
->raid_disks
]);
135 /* allocate a r1bio with room for raid_disks entries in the bios array */
136 return kzalloc(size
, gfp_flags
);
139 #define RESYNC_DEPTH 32
140 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
141 #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
142 #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
143 #define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
144 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
146 static void * r1buf_pool_alloc(gfp_t gfp_flags
, void *data
)
148 struct pool_info
*pi
= data
;
149 struct r1bio
*r1_bio
;
153 struct resync_pages
*rps
;
155 r1_bio
= r1bio_pool_alloc(gfp_flags
, pi
);
159 rps
= kmalloc_array(pi
->raid_disks
, sizeof(struct resync_pages
),
165 * Allocate bios : 1 for reading, n-1 for writing
167 for (j
= pi
->raid_disks
; j
-- ; ) {
168 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
171 r1_bio
->bios
[j
] = bio
;
174 * Allocate RESYNC_PAGES data pages and attach them to
176 * If this is a user-requested check/repair, allocate
177 * RESYNC_PAGES for each bio.
179 if (test_bit(MD_RECOVERY_REQUESTED
, &pi
->mddev
->recovery
))
180 need_pages
= pi
->raid_disks
;
183 for (j
= 0; j
< pi
->raid_disks
; j
++) {
184 struct resync_pages
*rp
= &rps
[j
];
186 bio
= r1_bio
->bios
[j
];
188 if (j
< need_pages
) {
189 if (resync_alloc_pages(rp
, gfp_flags
))
192 memcpy(rp
, &rps
[0], sizeof(*rp
));
193 resync_get_all_pages(rp
);
196 rp
->raid_bio
= r1_bio
;
197 bio
->bi_private
= rp
;
200 r1_bio
->master_bio
= NULL
;
206 resync_free_pages(&rps
[j
]);
209 while (++j
< pi
->raid_disks
)
210 bio_put(r1_bio
->bios
[j
]);
214 rbio_pool_free(r1_bio
, data
);
218 static void r1buf_pool_free(void *__r1_bio
, void *data
)
220 struct pool_info
*pi
= data
;
222 struct r1bio
*r1bio
= __r1_bio
;
223 struct resync_pages
*rp
= NULL
;
225 for (i
= pi
->raid_disks
; i
--; ) {
226 rp
= get_resync_pages(r1bio
->bios
[i
]);
227 resync_free_pages(rp
);
228 bio_put(r1bio
->bios
[i
]);
231 /* resync pages array stored in the 1st bio's .bi_private */
234 rbio_pool_free(r1bio
, data
);
237 static void put_all_bios(struct r1conf
*conf
, struct r1bio
*r1_bio
)
241 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
242 struct bio
**bio
= r1_bio
->bios
+ i
;
243 if (!BIO_SPECIAL(*bio
))
249 static void free_r1bio(struct r1bio
*r1_bio
)
251 struct r1conf
*conf
= r1_bio
->mddev
->private;
253 put_all_bios(conf
, r1_bio
);
254 mempool_free(r1_bio
, &conf
->r1bio_pool
);
257 static void put_buf(struct r1bio
*r1_bio
)
259 struct r1conf
*conf
= r1_bio
->mddev
->private;
260 sector_t sect
= r1_bio
->sector
;
263 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
264 struct bio
*bio
= r1_bio
->bios
[i
];
266 rdev_dec_pending(conf
->mirrors
[i
].rdev
, r1_bio
->mddev
);
269 mempool_free(r1_bio
, &conf
->r1buf_pool
);
271 lower_barrier(conf
, sect
);
274 static void reschedule_retry(struct r1bio
*r1_bio
)
277 struct mddev
*mddev
= r1_bio
->mddev
;
278 struct r1conf
*conf
= mddev
->private;
281 idx
= sector_to_idx(r1_bio
->sector
);
282 spin_lock_irqsave(&conf
->device_lock
, flags
);
283 list_add(&r1_bio
->retry_list
, &conf
->retry_list
);
284 atomic_inc(&conf
->nr_queued
[idx
]);
285 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
287 wake_up(&conf
->wait_barrier
);
288 md_wakeup_thread(mddev
->thread
);
292 * raid_end_bio_io() is called when we have finished servicing a mirrored
293 * operation and are ready to return a success/failure code to the buffer
296 static void call_bio_endio(struct r1bio
*r1_bio
)
298 struct bio
*bio
= r1_bio
->master_bio
;
299 struct r1conf
*conf
= r1_bio
->mddev
->private;
301 if (!test_bit(R1BIO_Uptodate
, &r1_bio
->state
))
302 bio
->bi_status
= BLK_STS_IOERR
;
306 * Wake up any possible resync thread that waits for the device
309 allow_barrier(conf
, r1_bio
->sector
);
312 static void raid_end_bio_io(struct r1bio
*r1_bio
)
314 struct bio
*bio
= r1_bio
->master_bio
;
316 /* if nobody has done the final endio yet, do it now */
317 if (!test_and_set_bit(R1BIO_Returned
, &r1_bio
->state
)) {
318 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
319 (bio_data_dir(bio
) == WRITE
) ? "write" : "read",
320 (unsigned long long) bio
->bi_iter
.bi_sector
,
321 (unsigned long long) bio_end_sector(bio
) - 1);
323 call_bio_endio(r1_bio
);
329 * Update disk head position estimator based on IRQ completion info.
331 static inline void update_head_pos(int disk
, struct r1bio
*r1_bio
)
333 struct r1conf
*conf
= r1_bio
->mddev
->private;
335 conf
->mirrors
[disk
].head_position
=
336 r1_bio
->sector
+ (r1_bio
->sectors
);
340 * Find the disk number which triggered given bio
342 static int find_bio_disk(struct r1bio
*r1_bio
, struct bio
*bio
)
345 struct r1conf
*conf
= r1_bio
->mddev
->private;
346 int raid_disks
= conf
->raid_disks
;
348 for (mirror
= 0; mirror
< raid_disks
* 2; mirror
++)
349 if (r1_bio
->bios
[mirror
] == bio
)
352 BUG_ON(mirror
== raid_disks
* 2);
353 update_head_pos(mirror
, r1_bio
);
358 static void raid1_end_read_request(struct bio
*bio
)
360 int uptodate
= !bio
->bi_status
;
361 struct r1bio
*r1_bio
= bio
->bi_private
;
362 struct r1conf
*conf
= r1_bio
->mddev
->private;
363 struct md_rdev
*rdev
= conf
->mirrors
[r1_bio
->read_disk
].rdev
;
366 * this branch is our 'one mirror IO has finished' event handler:
368 update_head_pos(r1_bio
->read_disk
, r1_bio
);
371 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
372 else if (test_bit(FailFast
, &rdev
->flags
) &&
373 test_bit(R1BIO_FailFast
, &r1_bio
->state
))
374 /* This was a fail-fast read so we definitely
378 /* If all other devices have failed, we want to return
379 * the error upwards rather than fail the last device.
380 * Here we redefine "uptodate" to mean "Don't want to retry"
383 spin_lock_irqsave(&conf
->device_lock
, flags
);
384 if (r1_bio
->mddev
->degraded
== conf
->raid_disks
||
385 (r1_bio
->mddev
->degraded
== conf
->raid_disks
-1 &&
386 test_bit(In_sync
, &rdev
->flags
)))
388 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
392 raid_end_bio_io(r1_bio
);
393 rdev_dec_pending(rdev
, conf
->mddev
);
398 char b
[BDEVNAME_SIZE
];
399 pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n",
401 bdevname(rdev
->bdev
, b
),
402 (unsigned long long)r1_bio
->sector
);
403 set_bit(R1BIO_ReadError
, &r1_bio
->state
);
404 reschedule_retry(r1_bio
);
405 /* don't drop the reference on read_disk yet */
409 static void close_write(struct r1bio
*r1_bio
)
411 /* it really is the end of this request */
412 if (test_bit(R1BIO_BehindIO
, &r1_bio
->state
)) {
413 bio_free_pages(r1_bio
->behind_master_bio
);
414 bio_put(r1_bio
->behind_master_bio
);
415 r1_bio
->behind_master_bio
= NULL
;
417 /* clear the bitmap if all writes complete successfully */
418 md_bitmap_endwrite(r1_bio
->mddev
->bitmap
, r1_bio
->sector
,
420 !test_bit(R1BIO_Degraded
, &r1_bio
->state
),
421 test_bit(R1BIO_BehindIO
, &r1_bio
->state
));
422 md_write_end(r1_bio
->mddev
);
425 static void r1_bio_write_done(struct r1bio
*r1_bio
)
427 if (!atomic_dec_and_test(&r1_bio
->remaining
))
430 if (test_bit(R1BIO_WriteError
, &r1_bio
->state
))
431 reschedule_retry(r1_bio
);
434 if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
))
435 reschedule_retry(r1_bio
);
437 raid_end_bio_io(r1_bio
);
441 static void raid1_end_write_request(struct bio
*bio
)
443 struct r1bio
*r1_bio
= bio
->bi_private
;
444 int behind
= test_bit(R1BIO_BehindIO
, &r1_bio
->state
);
445 struct r1conf
*conf
= r1_bio
->mddev
->private;
446 struct bio
*to_put
= NULL
;
447 int mirror
= find_bio_disk(r1_bio
, bio
);
448 struct md_rdev
*rdev
= conf
->mirrors
[mirror
].rdev
;
450 sector_t lo
= r1_bio
->sector
;
451 sector_t hi
= r1_bio
->sector
+ r1_bio
->sectors
;
453 discard_error
= bio
->bi_status
&& bio_op(bio
) == REQ_OP_DISCARD
;
456 * 'one mirror IO has finished' event handler:
458 if (bio
->bi_status
&& !discard_error
) {
459 set_bit(WriteErrorSeen
, &rdev
->flags
);
460 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
461 set_bit(MD_RECOVERY_NEEDED
, &
462 conf
->mddev
->recovery
);
464 if (test_bit(FailFast
, &rdev
->flags
) &&
465 (bio
->bi_opf
& MD_FAILFAST
) &&
466 /* We never try FailFast to WriteMostly devices */
467 !test_bit(WriteMostly
, &rdev
->flags
)) {
468 md_error(r1_bio
->mddev
, rdev
);
472 * When the device is faulty, it is not necessary to
473 * handle write error.
474 * For failfast, this is the only remaining device,
475 * We need to retry the write without FailFast.
477 if (!test_bit(Faulty
, &rdev
->flags
))
478 set_bit(R1BIO_WriteError
, &r1_bio
->state
);
480 /* Finished with this branch */
481 r1_bio
->bios
[mirror
] = NULL
;
486 * Set R1BIO_Uptodate in our master bio, so that we
487 * will return a good error code for to the higher
488 * levels even if IO on some other mirrored buffer
491 * The 'master' represents the composite IO operation
492 * to user-side. So if something waits for IO, then it
493 * will wait for the 'master' bio.
498 r1_bio
->bios
[mirror
] = NULL
;
501 * Do not set R1BIO_Uptodate if the current device is
502 * rebuilding or Faulty. This is because we cannot use
503 * such device for properly reading the data back (we could
504 * potentially use it, if the current write would have felt
505 * before rdev->recovery_offset, but for simplicity we don't
508 if (test_bit(In_sync
, &rdev
->flags
) &&
509 !test_bit(Faulty
, &rdev
->flags
))
510 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
512 /* Maybe we can clear some bad blocks. */
513 if (is_badblock(rdev
, r1_bio
->sector
, r1_bio
->sectors
,
514 &first_bad
, &bad_sectors
) && !discard_error
) {
515 r1_bio
->bios
[mirror
] = IO_MADE_GOOD
;
516 set_bit(R1BIO_MadeGood
, &r1_bio
->state
);
521 if (test_bit(CollisionCheck
, &rdev
->flags
))
522 remove_serial(rdev
, lo
, hi
);
523 if (test_bit(WriteMostly
, &rdev
->flags
))
524 atomic_dec(&r1_bio
->behind_remaining
);
527 * In behind mode, we ACK the master bio once the I/O
528 * has safely reached all non-writemostly
529 * disks. Setting the Returned bit ensures that this
530 * gets done only once -- we don't ever want to return
531 * -EIO here, instead we'll wait
533 if (atomic_read(&r1_bio
->behind_remaining
) >= (atomic_read(&r1_bio
->remaining
)-1) &&
534 test_bit(R1BIO_Uptodate
, &r1_bio
->state
)) {
535 /* Maybe we can return now */
536 if (!test_and_set_bit(R1BIO_Returned
, &r1_bio
->state
)) {
537 struct bio
*mbio
= r1_bio
->master_bio
;
538 pr_debug("raid1: behind end write sectors"
540 (unsigned long long) mbio
->bi_iter
.bi_sector
,
541 (unsigned long long) bio_end_sector(mbio
) - 1);
542 call_bio_endio(r1_bio
);
545 } else if (rdev
->mddev
->serialize_policy
)
546 remove_serial(rdev
, lo
, hi
);
547 if (r1_bio
->bios
[mirror
] == NULL
)
548 rdev_dec_pending(rdev
, conf
->mddev
);
551 * Let's see if all mirrored write operations have finished
554 r1_bio_write_done(r1_bio
);
560 static sector_t
align_to_barrier_unit_end(sector_t start_sector
,
565 WARN_ON(sectors
== 0);
567 * len is the number of sectors from start_sector to end of the
568 * barrier unit which start_sector belongs to.
570 len
= round_up(start_sector
+ 1, BARRIER_UNIT_SECTOR_SIZE
) -
580 * This routine returns the disk from which the requested read should
581 * be done. There is a per-array 'next expected sequential IO' sector
582 * number - if this matches on the next IO then we use the last disk.
583 * There is also a per-disk 'last know head position' sector that is
584 * maintained from IRQ contexts, both the normal and the resync IO
585 * completion handlers update this position correctly. If there is no
586 * perfect sequential match then we pick the disk whose head is closest.
588 * If there are 2 mirrors in the same 2 devices, performance degrades
589 * because position is mirror, not device based.
591 * The rdev for the device selected will have nr_pending incremented.
593 static int read_balance(struct r1conf
*conf
, struct r1bio
*r1_bio
, int *max_sectors
)
595 const sector_t this_sector
= r1_bio
->sector
;
597 int best_good_sectors
;
598 int best_disk
, best_dist_disk
, best_pending_disk
;
602 unsigned int min_pending
;
603 struct md_rdev
*rdev
;
605 int choose_next_idle
;
609 * Check if we can balance. We can balance on the whole
610 * device if no resync is going on, or below the resync window.
611 * We take the first readable disk when above the resync window.
614 sectors
= r1_bio
->sectors
;
617 best_dist
= MaxSector
;
618 best_pending_disk
= -1;
619 min_pending
= UINT_MAX
;
620 best_good_sectors
= 0;
622 choose_next_idle
= 0;
623 clear_bit(R1BIO_FailFast
, &r1_bio
->state
);
625 if ((conf
->mddev
->recovery_cp
< this_sector
+ sectors
) ||
626 (mddev_is_clustered(conf
->mddev
) &&
627 md_cluster_ops
->area_resyncing(conf
->mddev
, READ
, this_sector
,
628 this_sector
+ sectors
)))
633 for (disk
= 0 ; disk
< conf
->raid_disks
* 2 ; disk
++) {
637 unsigned int pending
;
640 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
641 if (r1_bio
->bios
[disk
] == IO_BLOCKED
643 || test_bit(Faulty
, &rdev
->flags
))
645 if (!test_bit(In_sync
, &rdev
->flags
) &&
646 rdev
->recovery_offset
< this_sector
+ sectors
)
648 if (test_bit(WriteMostly
, &rdev
->flags
)) {
649 /* Don't balance among write-mostly, just
650 * use the first as a last resort */
651 if (best_dist_disk
< 0) {
652 if (is_badblock(rdev
, this_sector
, sectors
,
653 &first_bad
, &bad_sectors
)) {
654 if (first_bad
<= this_sector
)
655 /* Cannot use this */
657 best_good_sectors
= first_bad
- this_sector
;
659 best_good_sectors
= sectors
;
660 best_dist_disk
= disk
;
661 best_pending_disk
= disk
;
665 /* This is a reasonable device to use. It might
668 if (is_badblock(rdev
, this_sector
, sectors
,
669 &first_bad
, &bad_sectors
)) {
670 if (best_dist
< MaxSector
)
671 /* already have a better device */
673 if (first_bad
<= this_sector
) {
674 /* cannot read here. If this is the 'primary'
675 * device, then we must not read beyond
676 * bad_sectors from another device..
678 bad_sectors
-= (this_sector
- first_bad
);
679 if (choose_first
&& sectors
> bad_sectors
)
680 sectors
= bad_sectors
;
681 if (best_good_sectors
> sectors
)
682 best_good_sectors
= sectors
;
685 sector_t good_sectors
= first_bad
- this_sector
;
686 if (good_sectors
> best_good_sectors
) {
687 best_good_sectors
= good_sectors
;
695 if ((sectors
> best_good_sectors
) && (best_disk
>= 0))
697 best_good_sectors
= sectors
;
701 /* At least two disks to choose from so failfast is OK */
702 set_bit(R1BIO_FailFast
, &r1_bio
->state
);
704 nonrot
= blk_queue_nonrot(bdev_get_queue(rdev
->bdev
));
705 has_nonrot_disk
|= nonrot
;
706 pending
= atomic_read(&rdev
->nr_pending
);
707 dist
= abs(this_sector
- conf
->mirrors
[disk
].head_position
);
712 /* Don't change to another disk for sequential reads */
713 if (conf
->mirrors
[disk
].next_seq_sect
== this_sector
715 int opt_iosize
= bdev_io_opt(rdev
->bdev
) >> 9;
716 struct raid1_info
*mirror
= &conf
->mirrors
[disk
];
720 * If buffered sequential IO size exceeds optimal
721 * iosize, check if there is idle disk. If yes, choose
722 * the idle disk. read_balance could already choose an
723 * idle disk before noticing it's a sequential IO in
724 * this disk. This doesn't matter because this disk
725 * will idle, next time it will be utilized after the
726 * first disk has IO size exceeds optimal iosize. In
727 * this way, iosize of the first disk will be optimal
728 * iosize at least. iosize of the second disk might be
729 * small, but not a big deal since when the second disk
730 * starts IO, the first disk is likely still busy.
732 if (nonrot
&& opt_iosize
> 0 &&
733 mirror
->seq_start
!= MaxSector
&&
734 mirror
->next_seq_sect
> opt_iosize
&&
735 mirror
->next_seq_sect
- opt_iosize
>=
737 choose_next_idle
= 1;
743 if (choose_next_idle
)
746 if (min_pending
> pending
) {
747 min_pending
= pending
;
748 best_pending_disk
= disk
;
751 if (dist
< best_dist
) {
753 best_dist_disk
= disk
;
758 * If all disks are rotational, choose the closest disk. If any disk is
759 * non-rotational, choose the disk with less pending request even the
760 * disk is rotational, which might/might not be optimal for raids with
761 * mixed ratation/non-rotational disks depending on workload.
763 if (best_disk
== -1) {
764 if (has_nonrot_disk
|| min_pending
== 0)
765 best_disk
= best_pending_disk
;
767 best_disk
= best_dist_disk
;
770 if (best_disk
>= 0) {
771 rdev
= rcu_dereference(conf
->mirrors
[best_disk
].rdev
);
774 atomic_inc(&rdev
->nr_pending
);
775 sectors
= best_good_sectors
;
777 if (conf
->mirrors
[best_disk
].next_seq_sect
!= this_sector
)
778 conf
->mirrors
[best_disk
].seq_start
= this_sector
;
780 conf
->mirrors
[best_disk
].next_seq_sect
= this_sector
+ sectors
;
783 *max_sectors
= sectors
;
788 static int raid1_congested(struct mddev
*mddev
, int bits
)
790 struct r1conf
*conf
= mddev
->private;
793 if ((bits
& (1 << WB_async_congested
)) &&
794 conf
->pending_count
>= max_queued_requests
)
798 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
799 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
800 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
801 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
805 /* Note the '|| 1' - when read_balance prefers
806 * non-congested targets, it can be removed
808 if ((bits
& (1 << WB_async_congested
)) || 1)
809 ret
|= bdi_congested(q
->backing_dev_info
, bits
);
811 ret
&= bdi_congested(q
->backing_dev_info
, bits
);
818 static void flush_bio_list(struct r1conf
*conf
, struct bio
*bio
)
820 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
821 md_bitmap_unplug(conf
->mddev
->bitmap
);
822 wake_up(&conf
->wait_barrier
);
824 while (bio
) { /* submit pending writes */
825 struct bio
*next
= bio
->bi_next
;
826 struct md_rdev
*rdev
= (void *)bio
->bi_disk
;
828 bio_set_dev(bio
, rdev
->bdev
);
829 if (test_bit(Faulty
, &rdev
->flags
)) {
831 } else if (unlikely((bio_op(bio
) == REQ_OP_DISCARD
) &&
832 !blk_queue_discard(bio
->bi_disk
->queue
)))
836 generic_make_request(bio
);
842 static void flush_pending_writes(struct r1conf
*conf
)
844 /* Any writes that have been queued but are awaiting
845 * bitmap updates get flushed here.
847 spin_lock_irq(&conf
->device_lock
);
849 if (conf
->pending_bio_list
.head
) {
850 struct blk_plug plug
;
853 bio
= bio_list_get(&conf
->pending_bio_list
);
854 conf
->pending_count
= 0;
855 spin_unlock_irq(&conf
->device_lock
);
858 * As this is called in a wait_event() loop (see freeze_array),
859 * current->state might be TASK_UNINTERRUPTIBLE which will
860 * cause a warning when we prepare to wait again. As it is
861 * rare that this path is taken, it is perfectly safe to force
862 * us to go around the wait_event() loop again, so the warning
863 * is a false-positive. Silence the warning by resetting
866 __set_current_state(TASK_RUNNING
);
867 blk_start_plug(&plug
);
868 flush_bio_list(conf
, bio
);
869 blk_finish_plug(&plug
);
871 spin_unlock_irq(&conf
->device_lock
);
875 * Sometimes we need to suspend IO while we do something else,
876 * either some resync/recovery, or reconfigure the array.
877 * To do this we raise a 'barrier'.
878 * The 'barrier' is a counter that can be raised multiple times
879 * to count how many activities are happening which preclude
881 * We can only raise the barrier if there is no pending IO.
882 * i.e. if nr_pending == 0.
883 * We choose only to raise the barrier if no-one is waiting for the
884 * barrier to go down. This means that as soon as an IO request
885 * is ready, no other operations which require a barrier will start
886 * until the IO request has had a chance.
888 * So: regular IO calls 'wait_barrier'. When that returns there
889 * is no backgroup IO happening, It must arrange to call
890 * allow_barrier when it has finished its IO.
891 * backgroup IO calls must call raise_barrier. Once that returns
892 * there is no normal IO happeing. It must arrange to call
893 * lower_barrier when the particular background IO completes.
895 * If resync/recovery is interrupted, returns -EINTR;
896 * Otherwise, returns 0.
898 static int raise_barrier(struct r1conf
*conf
, sector_t sector_nr
)
900 int idx
= sector_to_idx(sector_nr
);
902 spin_lock_irq(&conf
->resync_lock
);
904 /* Wait until no block IO is waiting */
905 wait_event_lock_irq(conf
->wait_barrier
,
906 !atomic_read(&conf
->nr_waiting
[idx
]),
909 /* block any new IO from starting */
910 atomic_inc(&conf
->barrier
[idx
]);
912 * In raise_barrier() we firstly increase conf->barrier[idx] then
913 * check conf->nr_pending[idx]. In _wait_barrier() we firstly
914 * increase conf->nr_pending[idx] then check conf->barrier[idx].
915 * A memory barrier here to make sure conf->nr_pending[idx] won't
916 * be fetched before conf->barrier[idx] is increased. Otherwise
917 * there will be a race between raise_barrier() and _wait_barrier().
919 smp_mb__after_atomic();
921 /* For these conditions we must wait:
922 * A: while the array is in frozen state
923 * B: while conf->nr_pending[idx] is not 0, meaning regular I/O
924 * existing in corresponding I/O barrier bucket.
925 * C: while conf->barrier[idx] >= RESYNC_DEPTH, meaning reaches
926 * max resync count which allowed on current I/O barrier bucket.
928 wait_event_lock_irq(conf
->wait_barrier
,
929 (!conf
->array_frozen
&&
930 !atomic_read(&conf
->nr_pending
[idx
]) &&
931 atomic_read(&conf
->barrier
[idx
]) < RESYNC_DEPTH
) ||
932 test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
),
935 if (test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
936 atomic_dec(&conf
->barrier
[idx
]);
937 spin_unlock_irq(&conf
->resync_lock
);
938 wake_up(&conf
->wait_barrier
);
942 atomic_inc(&conf
->nr_sync_pending
);
943 spin_unlock_irq(&conf
->resync_lock
);
948 static void lower_barrier(struct r1conf
*conf
, sector_t sector_nr
)
950 int idx
= sector_to_idx(sector_nr
);
952 BUG_ON(atomic_read(&conf
->barrier
[idx
]) <= 0);
954 atomic_dec(&conf
->barrier
[idx
]);
955 atomic_dec(&conf
->nr_sync_pending
);
956 wake_up(&conf
->wait_barrier
);
959 static void _wait_barrier(struct r1conf
*conf
, int idx
)
962 * We need to increase conf->nr_pending[idx] very early here,
963 * then raise_barrier() can be blocked when it waits for
964 * conf->nr_pending[idx] to be 0. Then we can avoid holding
965 * conf->resync_lock when there is no barrier raised in same
966 * barrier unit bucket. Also if the array is frozen, I/O
967 * should be blocked until array is unfrozen.
969 atomic_inc(&conf
->nr_pending
[idx
]);
971 * In _wait_barrier() we firstly increase conf->nr_pending[idx], then
972 * check conf->barrier[idx]. In raise_barrier() we firstly increase
973 * conf->barrier[idx], then check conf->nr_pending[idx]. A memory
974 * barrier is necessary here to make sure conf->barrier[idx] won't be
975 * fetched before conf->nr_pending[idx] is increased. Otherwise there
976 * will be a race between _wait_barrier() and raise_barrier().
978 smp_mb__after_atomic();
981 * Don't worry about checking two atomic_t variables at same time
982 * here. If during we check conf->barrier[idx], the array is
983 * frozen (conf->array_frozen is 1), and chonf->barrier[idx] is
984 * 0, it is safe to return and make the I/O continue. Because the
985 * array is frozen, all I/O returned here will eventually complete
986 * or be queued, no race will happen. See code comment in
989 if (!READ_ONCE(conf
->array_frozen
) &&
990 !atomic_read(&conf
->barrier
[idx
]))
994 * After holding conf->resync_lock, conf->nr_pending[idx]
995 * should be decreased before waiting for barrier to drop.
996 * Otherwise, we may encounter a race condition because
997 * raise_barrer() might be waiting for conf->nr_pending[idx]
998 * to be 0 at same time.
1000 spin_lock_irq(&conf
->resync_lock
);
1001 atomic_inc(&conf
->nr_waiting
[idx
]);
1002 atomic_dec(&conf
->nr_pending
[idx
]);
1004 * In case freeze_array() is waiting for
1005 * get_unqueued_pending() == extra
1007 wake_up(&conf
->wait_barrier
);
1008 /* Wait for the barrier in same barrier unit bucket to drop. */
1009 wait_event_lock_irq(conf
->wait_barrier
,
1010 !conf
->array_frozen
&&
1011 !atomic_read(&conf
->barrier
[idx
]),
1013 atomic_inc(&conf
->nr_pending
[idx
]);
1014 atomic_dec(&conf
->nr_waiting
[idx
]);
1015 spin_unlock_irq(&conf
->resync_lock
);
1018 static void wait_read_barrier(struct r1conf
*conf
, sector_t sector_nr
)
1020 int idx
= sector_to_idx(sector_nr
);
1023 * Very similar to _wait_barrier(). The difference is, for read
1024 * I/O we don't need wait for sync I/O, but if the whole array
1025 * is frozen, the read I/O still has to wait until the array is
1026 * unfrozen. Since there is no ordering requirement with
1027 * conf->barrier[idx] here, memory barrier is unnecessary as well.
1029 atomic_inc(&conf
->nr_pending
[idx
]);
1031 if (!READ_ONCE(conf
->array_frozen
))
1034 spin_lock_irq(&conf
->resync_lock
);
1035 atomic_inc(&conf
->nr_waiting
[idx
]);
1036 atomic_dec(&conf
->nr_pending
[idx
]);
1038 * In case freeze_array() is waiting for
1039 * get_unqueued_pending() == extra
1041 wake_up(&conf
->wait_barrier
);
1042 /* Wait for array to be unfrozen */
1043 wait_event_lock_irq(conf
->wait_barrier
,
1044 !conf
->array_frozen
,
1046 atomic_inc(&conf
->nr_pending
[idx
]);
1047 atomic_dec(&conf
->nr_waiting
[idx
]);
1048 spin_unlock_irq(&conf
->resync_lock
);
1051 static void wait_barrier(struct r1conf
*conf
, sector_t sector_nr
)
1053 int idx
= sector_to_idx(sector_nr
);
1055 _wait_barrier(conf
, idx
);
1058 static void _allow_barrier(struct r1conf
*conf
, int idx
)
1060 atomic_dec(&conf
->nr_pending
[idx
]);
1061 wake_up(&conf
->wait_barrier
);
1064 static void allow_barrier(struct r1conf
*conf
, sector_t sector_nr
)
1066 int idx
= sector_to_idx(sector_nr
);
1068 _allow_barrier(conf
, idx
);
1071 /* conf->resync_lock should be held */
1072 static int get_unqueued_pending(struct r1conf
*conf
)
1076 ret
= atomic_read(&conf
->nr_sync_pending
);
1077 for (idx
= 0; idx
< BARRIER_BUCKETS_NR
; idx
++)
1078 ret
+= atomic_read(&conf
->nr_pending
[idx
]) -
1079 atomic_read(&conf
->nr_queued
[idx
]);
1084 static void freeze_array(struct r1conf
*conf
, int extra
)
1086 /* Stop sync I/O and normal I/O and wait for everything to
1088 * This is called in two situations:
1089 * 1) management command handlers (reshape, remove disk, quiesce).
1090 * 2) one normal I/O request failed.
1092 * After array_frozen is set to 1, new sync IO will be blocked at
1093 * raise_barrier(), and new normal I/O will blocked at _wait_barrier()
1094 * or wait_read_barrier(). The flying I/Os will either complete or be
1095 * queued. When everything goes quite, there are only queued I/Os left.
1097 * Every flying I/O contributes to a conf->nr_pending[idx], idx is the
1098 * barrier bucket index which this I/O request hits. When all sync and
1099 * normal I/O are queued, sum of all conf->nr_pending[] will match sum
1100 * of all conf->nr_queued[]. But normal I/O failure is an exception,
1101 * in handle_read_error(), we may call freeze_array() before trying to
1102 * fix the read error. In this case, the error read I/O is not queued,
1103 * so get_unqueued_pending() == 1.
1105 * Therefore before this function returns, we need to wait until
1106 * get_unqueued_pendings(conf) gets equal to extra. For
1107 * normal I/O context, extra is 1, in rested situations extra is 0.
1109 spin_lock_irq(&conf
->resync_lock
);
1110 conf
->array_frozen
= 1;
1111 raid1_log(conf
->mddev
, "wait freeze");
1112 wait_event_lock_irq_cmd(
1114 get_unqueued_pending(conf
) == extra
,
1116 flush_pending_writes(conf
));
1117 spin_unlock_irq(&conf
->resync_lock
);
1119 static void unfreeze_array(struct r1conf
*conf
)
1121 /* reverse the effect of the freeze */
1122 spin_lock_irq(&conf
->resync_lock
);
1123 conf
->array_frozen
= 0;
1124 spin_unlock_irq(&conf
->resync_lock
);
1125 wake_up(&conf
->wait_barrier
);
1128 static void alloc_behind_master_bio(struct r1bio
*r1_bio
,
1131 int size
= bio
->bi_iter
.bi_size
;
1132 unsigned vcnt
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1134 struct bio
*behind_bio
= NULL
;
1136 behind_bio
= bio_alloc_mddev(GFP_NOIO
, vcnt
, r1_bio
->mddev
);
1140 /* discard op, we don't support writezero/writesame yet */
1141 if (!bio_has_data(bio
)) {
1142 behind_bio
->bi_iter
.bi_size
= size
;
1146 behind_bio
->bi_write_hint
= bio
->bi_write_hint
;
1148 while (i
< vcnt
&& size
) {
1150 int len
= min_t(int, PAGE_SIZE
, size
);
1152 page
= alloc_page(GFP_NOIO
);
1153 if (unlikely(!page
))
1156 bio_add_page(behind_bio
, page
, len
, 0);
1162 bio_copy_data(behind_bio
, bio
);
1164 r1_bio
->behind_master_bio
= behind_bio
;
1165 set_bit(R1BIO_BehindIO
, &r1_bio
->state
);
1170 pr_debug("%dB behind alloc failed, doing sync I/O\n",
1171 bio
->bi_iter
.bi_size
);
1172 bio_free_pages(behind_bio
);
1173 bio_put(behind_bio
);
1176 struct raid1_plug_cb
{
1177 struct blk_plug_cb cb
;
1178 struct bio_list pending
;
1182 static void raid1_unplug(struct blk_plug_cb
*cb
, bool from_schedule
)
1184 struct raid1_plug_cb
*plug
= container_of(cb
, struct raid1_plug_cb
,
1186 struct mddev
*mddev
= plug
->cb
.data
;
1187 struct r1conf
*conf
= mddev
->private;
1190 if (from_schedule
|| current
->bio_list
) {
1191 spin_lock_irq(&conf
->device_lock
);
1192 bio_list_merge(&conf
->pending_bio_list
, &plug
->pending
);
1193 conf
->pending_count
+= plug
->pending_cnt
;
1194 spin_unlock_irq(&conf
->device_lock
);
1195 wake_up(&conf
->wait_barrier
);
1196 md_wakeup_thread(mddev
->thread
);
1201 /* we aren't scheduling, so we can do the write-out directly. */
1202 bio
= bio_list_get(&plug
->pending
);
1203 flush_bio_list(conf
, bio
);
1207 static void init_r1bio(struct r1bio
*r1_bio
, struct mddev
*mddev
, struct bio
*bio
)
1209 r1_bio
->master_bio
= bio
;
1210 r1_bio
->sectors
= bio_sectors(bio
);
1212 r1_bio
->mddev
= mddev
;
1213 r1_bio
->sector
= bio
->bi_iter
.bi_sector
;
1216 static inline struct r1bio
*
1217 alloc_r1bio(struct mddev
*mddev
, struct bio
*bio
)
1219 struct r1conf
*conf
= mddev
->private;
1220 struct r1bio
*r1_bio
;
1222 r1_bio
= mempool_alloc(&conf
->r1bio_pool
, GFP_NOIO
);
1223 /* Ensure no bio records IO_BLOCKED */
1224 memset(r1_bio
->bios
, 0, conf
->raid_disks
* sizeof(r1_bio
->bios
[0]));
1225 init_r1bio(r1_bio
, mddev
, bio
);
1229 static void raid1_read_request(struct mddev
*mddev
, struct bio
*bio
,
1230 int max_read_sectors
, struct r1bio
*r1_bio
)
1232 struct r1conf
*conf
= mddev
->private;
1233 struct raid1_info
*mirror
;
1234 struct bio
*read_bio
;
1235 struct bitmap
*bitmap
= mddev
->bitmap
;
1236 const int op
= bio_op(bio
);
1237 const unsigned long do_sync
= (bio
->bi_opf
& REQ_SYNC
);
1240 bool print_msg
= !!r1_bio
;
1241 char b
[BDEVNAME_SIZE
];
1244 * If r1_bio is set, we are blocking the raid1d thread
1245 * so there is a tiny risk of deadlock. So ask for
1246 * emergency memory if needed.
1248 gfp_t gfp
= r1_bio
? (GFP_NOIO
| __GFP_HIGH
) : GFP_NOIO
;
1251 /* Need to get the block device name carefully */
1252 struct md_rdev
*rdev
;
1254 rdev
= rcu_dereference(conf
->mirrors
[r1_bio
->read_disk
].rdev
);
1256 bdevname(rdev
->bdev
, b
);
1263 * Still need barrier for READ in case that whole
1266 wait_read_barrier(conf
, bio
->bi_iter
.bi_sector
);
1269 r1_bio
= alloc_r1bio(mddev
, bio
);
1271 init_r1bio(r1_bio
, mddev
, bio
);
1272 r1_bio
->sectors
= max_read_sectors
;
1275 * make_request() can abort the operation when read-ahead is being
1276 * used and no empty request is available.
1278 rdisk
= read_balance(conf
, r1_bio
, &max_sectors
);
1281 /* couldn't find anywhere to read from */
1283 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
1286 (unsigned long long)r1_bio
->sector
);
1288 raid_end_bio_io(r1_bio
);
1291 mirror
= conf
->mirrors
+ rdisk
;
1294 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
1296 (unsigned long long)r1_bio
->sector
,
1297 bdevname(mirror
->rdev
->bdev
, b
));
1299 if (test_bit(WriteMostly
, &mirror
->rdev
->flags
) &&
1302 * Reading from a write-mostly device must take care not to
1303 * over-take any writes that are 'behind'
1305 raid1_log(mddev
, "wait behind writes");
1306 wait_event(bitmap
->behind_wait
,
1307 atomic_read(&bitmap
->behind_writes
) == 0);
1310 if (max_sectors
< bio_sectors(bio
)) {
1311 struct bio
*split
= bio_split(bio
, max_sectors
,
1312 gfp
, &conf
->bio_split
);
1313 bio_chain(split
, bio
);
1314 generic_make_request(bio
);
1316 r1_bio
->master_bio
= bio
;
1317 r1_bio
->sectors
= max_sectors
;
1320 r1_bio
->read_disk
= rdisk
;
1322 read_bio
= bio_clone_fast(bio
, gfp
, &mddev
->bio_set
);
1324 r1_bio
->bios
[rdisk
] = read_bio
;
1326 read_bio
->bi_iter
.bi_sector
= r1_bio
->sector
+
1327 mirror
->rdev
->data_offset
;
1328 bio_set_dev(read_bio
, mirror
->rdev
->bdev
);
1329 read_bio
->bi_end_io
= raid1_end_read_request
;
1330 bio_set_op_attrs(read_bio
, op
, do_sync
);
1331 if (test_bit(FailFast
, &mirror
->rdev
->flags
) &&
1332 test_bit(R1BIO_FailFast
, &r1_bio
->state
))
1333 read_bio
->bi_opf
|= MD_FAILFAST
;
1334 read_bio
->bi_private
= r1_bio
;
1337 trace_block_bio_remap(read_bio
->bi_disk
->queue
, read_bio
,
1338 disk_devt(mddev
->gendisk
), r1_bio
->sector
);
1340 generic_make_request(read_bio
);
1343 static void raid1_write_request(struct mddev
*mddev
, struct bio
*bio
,
1344 int max_write_sectors
)
1346 struct r1conf
*conf
= mddev
->private;
1347 struct r1bio
*r1_bio
;
1349 struct bitmap
*bitmap
= mddev
->bitmap
;
1350 unsigned long flags
;
1351 struct md_rdev
*blocked_rdev
;
1352 struct blk_plug_cb
*cb
;
1353 struct raid1_plug_cb
*plug
= NULL
;
1357 if (mddev_is_clustered(mddev
) &&
1358 md_cluster_ops
->area_resyncing(mddev
, WRITE
,
1359 bio
->bi_iter
.bi_sector
, bio_end_sector(bio
))) {
1363 prepare_to_wait(&conf
->wait_barrier
,
1365 if (!md_cluster_ops
->area_resyncing(mddev
, WRITE
,
1366 bio
->bi_iter
.bi_sector
,
1367 bio_end_sector(bio
)))
1371 finish_wait(&conf
->wait_barrier
, &w
);
1375 * Register the new request and wait if the reconstruction
1376 * thread has put up a bar for new requests.
1377 * Continue immediately if no resync is active currently.
1379 wait_barrier(conf
, bio
->bi_iter
.bi_sector
);
1381 r1_bio
= alloc_r1bio(mddev
, bio
);
1382 r1_bio
->sectors
= max_write_sectors
;
1384 if (conf
->pending_count
>= max_queued_requests
) {
1385 md_wakeup_thread(mddev
->thread
);
1386 raid1_log(mddev
, "wait queued");
1387 wait_event(conf
->wait_barrier
,
1388 conf
->pending_count
< max_queued_requests
);
1390 /* first select target devices under rcu_lock and
1391 * inc refcount on their rdev. Record them by setting
1393 * If there are known/acknowledged bad blocks on any device on
1394 * which we have seen a write error, we want to avoid writing those
1396 * This potentially requires several writes to write around
1397 * the bad blocks. Each set of writes gets it's own r1bio
1398 * with a set of bios attached.
1401 disks
= conf
->raid_disks
* 2;
1403 blocked_rdev
= NULL
;
1405 max_sectors
= r1_bio
->sectors
;
1406 for (i
= 0; i
< disks
; i
++) {
1407 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1408 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
1409 atomic_inc(&rdev
->nr_pending
);
1410 blocked_rdev
= rdev
;
1413 r1_bio
->bios
[i
] = NULL
;
1414 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
)) {
1415 if (i
< conf
->raid_disks
)
1416 set_bit(R1BIO_Degraded
, &r1_bio
->state
);
1420 atomic_inc(&rdev
->nr_pending
);
1421 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
1426 is_bad
= is_badblock(rdev
, r1_bio
->sector
, max_sectors
,
1427 &first_bad
, &bad_sectors
);
1429 /* mustn't write here until the bad block is
1431 set_bit(BlockedBadBlocks
, &rdev
->flags
);
1432 blocked_rdev
= rdev
;
1435 if (is_bad
&& first_bad
<= r1_bio
->sector
) {
1436 /* Cannot write here at all */
1437 bad_sectors
-= (r1_bio
->sector
- first_bad
);
1438 if (bad_sectors
< max_sectors
)
1439 /* mustn't write more than bad_sectors
1440 * to other devices yet
1442 max_sectors
= bad_sectors
;
1443 rdev_dec_pending(rdev
, mddev
);
1444 /* We don't set R1BIO_Degraded as that
1445 * only applies if the disk is
1446 * missing, so it might be re-added,
1447 * and we want to know to recover this
1449 * In this case the device is here,
1450 * and the fact that this chunk is not
1451 * in-sync is recorded in the bad
1457 int good_sectors
= first_bad
- r1_bio
->sector
;
1458 if (good_sectors
< max_sectors
)
1459 max_sectors
= good_sectors
;
1462 r1_bio
->bios
[i
] = bio
;
1466 if (unlikely(blocked_rdev
)) {
1467 /* Wait for this device to become unblocked */
1470 for (j
= 0; j
< i
; j
++)
1471 if (r1_bio
->bios
[j
])
1472 rdev_dec_pending(conf
->mirrors
[j
].rdev
, mddev
);
1474 allow_barrier(conf
, bio
->bi_iter
.bi_sector
);
1475 raid1_log(mddev
, "wait rdev %d blocked", blocked_rdev
->raid_disk
);
1476 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
1477 wait_barrier(conf
, bio
->bi_iter
.bi_sector
);
1481 if (max_sectors
< bio_sectors(bio
)) {
1482 struct bio
*split
= bio_split(bio
, max_sectors
,
1483 GFP_NOIO
, &conf
->bio_split
);
1484 bio_chain(split
, bio
);
1485 generic_make_request(bio
);
1487 r1_bio
->master_bio
= bio
;
1488 r1_bio
->sectors
= max_sectors
;
1491 atomic_set(&r1_bio
->remaining
, 1);
1492 atomic_set(&r1_bio
->behind_remaining
, 0);
1496 for (i
= 0; i
< disks
; i
++) {
1497 struct bio
*mbio
= NULL
;
1498 struct md_rdev
*rdev
= conf
->mirrors
[i
].rdev
;
1499 if (!r1_bio
->bios
[i
])
1504 * Not if there are too many, or cannot
1505 * allocate memory, or a reader on WriteMostly
1506 * is waiting for behind writes to flush */
1508 (atomic_read(&bitmap
->behind_writes
)
1509 < mddev
->bitmap_info
.max_write_behind
) &&
1510 !waitqueue_active(&bitmap
->behind_wait
)) {
1511 alloc_behind_master_bio(r1_bio
, bio
);
1514 md_bitmap_startwrite(bitmap
, r1_bio
->sector
, r1_bio
->sectors
,
1515 test_bit(R1BIO_BehindIO
, &r1_bio
->state
));
1519 if (r1_bio
->behind_master_bio
)
1520 mbio
= bio_clone_fast(r1_bio
->behind_master_bio
,
1521 GFP_NOIO
, &mddev
->bio_set
);
1523 mbio
= bio_clone_fast(bio
, GFP_NOIO
, &mddev
->bio_set
);
1525 if (r1_bio
->behind_master_bio
) {
1526 if (test_bit(CollisionCheck
, &rdev
->flags
))
1527 wait_for_serialization(rdev
, r1_bio
);
1528 if (test_bit(WriteMostly
, &rdev
->flags
))
1529 atomic_inc(&r1_bio
->behind_remaining
);
1530 } else if (mddev
->serialize_policy
)
1531 wait_for_serialization(rdev
, r1_bio
);
1533 r1_bio
->bios
[i
] = mbio
;
1535 mbio
->bi_iter
.bi_sector
= (r1_bio
->sector
+
1536 conf
->mirrors
[i
].rdev
->data_offset
);
1537 bio_set_dev(mbio
, conf
->mirrors
[i
].rdev
->bdev
);
1538 mbio
->bi_end_io
= raid1_end_write_request
;
1539 mbio
->bi_opf
= bio_op(bio
) | (bio
->bi_opf
& (REQ_SYNC
| REQ_FUA
));
1540 if (test_bit(FailFast
, &conf
->mirrors
[i
].rdev
->flags
) &&
1541 !test_bit(WriteMostly
, &conf
->mirrors
[i
].rdev
->flags
) &&
1542 conf
->raid_disks
- mddev
->degraded
> 1)
1543 mbio
->bi_opf
|= MD_FAILFAST
;
1544 mbio
->bi_private
= r1_bio
;
1546 atomic_inc(&r1_bio
->remaining
);
1549 trace_block_bio_remap(mbio
->bi_disk
->queue
,
1550 mbio
, disk_devt(mddev
->gendisk
),
1552 /* flush_pending_writes() needs access to the rdev so...*/
1553 mbio
->bi_disk
= (void *)conf
->mirrors
[i
].rdev
;
1555 cb
= blk_check_plugged(raid1_unplug
, mddev
, sizeof(*plug
));
1557 plug
= container_of(cb
, struct raid1_plug_cb
, cb
);
1561 bio_list_add(&plug
->pending
, mbio
);
1562 plug
->pending_cnt
++;
1564 spin_lock_irqsave(&conf
->device_lock
, flags
);
1565 bio_list_add(&conf
->pending_bio_list
, mbio
);
1566 conf
->pending_count
++;
1567 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1568 md_wakeup_thread(mddev
->thread
);
1572 r1_bio_write_done(r1_bio
);
1574 /* In case raid1d snuck in to freeze_array */
1575 wake_up(&conf
->wait_barrier
);
1578 static bool raid1_make_request(struct mddev
*mddev
, struct bio
*bio
)
1582 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)
1583 && md_flush_request(mddev
, bio
))
1587 * There is a limit to the maximum size, but
1588 * the read/write handler might find a lower limit
1589 * due to bad blocks. To avoid multiple splits,
1590 * we pass the maximum number of sectors down
1591 * and let the lower level perform the split.
1593 sectors
= align_to_barrier_unit_end(
1594 bio
->bi_iter
.bi_sector
, bio_sectors(bio
));
1596 if (bio_data_dir(bio
) == READ
)
1597 raid1_read_request(mddev
, bio
, sectors
, NULL
);
1599 if (!md_write_start(mddev
,bio
))
1601 raid1_write_request(mddev
, bio
, sectors
);
1606 static void raid1_status(struct seq_file
*seq
, struct mddev
*mddev
)
1608 struct r1conf
*conf
= mddev
->private;
1611 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
1612 conf
->raid_disks
- mddev
->degraded
);
1614 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1615 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1616 seq_printf(seq
, "%s",
1617 rdev
&& test_bit(In_sync
, &rdev
->flags
) ? "U" : "_");
1620 seq_printf(seq
, "]");
1623 static void raid1_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
1625 char b
[BDEVNAME_SIZE
];
1626 struct r1conf
*conf
= mddev
->private;
1627 unsigned long flags
;
1630 * If it is not operational, then we have already marked it as dead
1631 * else if it is the last working disks with "fail_last_dev == false",
1632 * ignore the error, let the next level up know.
1633 * else mark the drive as failed
1635 spin_lock_irqsave(&conf
->device_lock
, flags
);
1636 if (test_bit(In_sync
, &rdev
->flags
) && !mddev
->fail_last_dev
1637 && (conf
->raid_disks
- mddev
->degraded
) == 1) {
1639 * Don't fail the drive, act as though we were just a
1640 * normal single drive.
1641 * However don't try a recovery from this drive as
1642 * it is very likely to fail.
1644 conf
->recovery_disabled
= mddev
->recovery_disabled
;
1645 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1648 set_bit(Blocked
, &rdev
->flags
);
1649 if (test_and_clear_bit(In_sync
, &rdev
->flags
))
1651 set_bit(Faulty
, &rdev
->flags
);
1652 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1654 * if recovery is running, make sure it aborts.
1656 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1657 set_mask_bits(&mddev
->sb_flags
, 0,
1658 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
1659 pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
1660 "md/raid1:%s: Operation continuing on %d devices.\n",
1661 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1662 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
);
1665 static void print_conf(struct r1conf
*conf
)
1669 pr_debug("RAID1 conf printout:\n");
1671 pr_debug("(!conf)\n");
1674 pr_debug(" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
1678 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1679 char b
[BDEVNAME_SIZE
];
1680 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1682 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1683 i
, !test_bit(In_sync
, &rdev
->flags
),
1684 !test_bit(Faulty
, &rdev
->flags
),
1685 bdevname(rdev
->bdev
,b
));
1690 static void close_sync(struct r1conf
*conf
)
1694 for (idx
= 0; idx
< BARRIER_BUCKETS_NR
; idx
++) {
1695 _wait_barrier(conf
, idx
);
1696 _allow_barrier(conf
, idx
);
1699 mempool_exit(&conf
->r1buf_pool
);
1702 static int raid1_spare_active(struct mddev
*mddev
)
1705 struct r1conf
*conf
= mddev
->private;
1707 unsigned long flags
;
1710 * Find all failed disks within the RAID1 configuration
1711 * and mark them readable.
1712 * Called under mddev lock, so rcu protection not needed.
1713 * device_lock used to avoid races with raid1_end_read_request
1714 * which expects 'In_sync' flags and ->degraded to be consistent.
1716 spin_lock_irqsave(&conf
->device_lock
, flags
);
1717 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1718 struct md_rdev
*rdev
= conf
->mirrors
[i
].rdev
;
1719 struct md_rdev
*repl
= conf
->mirrors
[conf
->raid_disks
+ i
].rdev
;
1721 && !test_bit(Candidate
, &repl
->flags
)
1722 && repl
->recovery_offset
== MaxSector
1723 && !test_bit(Faulty
, &repl
->flags
)
1724 && !test_and_set_bit(In_sync
, &repl
->flags
)) {
1725 /* replacement has just become active */
1727 !test_and_clear_bit(In_sync
, &rdev
->flags
))
1730 /* Replaced device not technically
1731 * faulty, but we need to be sure
1732 * it gets removed and never re-added
1734 set_bit(Faulty
, &rdev
->flags
);
1735 sysfs_notify_dirent_safe(
1740 && rdev
->recovery_offset
== MaxSector
1741 && !test_bit(Faulty
, &rdev
->flags
)
1742 && !test_and_set_bit(In_sync
, &rdev
->flags
)) {
1744 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
1747 mddev
->degraded
-= count
;
1748 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1754 static int raid1_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1756 struct r1conf
*conf
= mddev
->private;
1759 struct raid1_info
*p
;
1761 int last
= conf
->raid_disks
- 1;
1763 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
1766 if (md_integrity_add_rdev(rdev
, mddev
))
1769 if (rdev
->raid_disk
>= 0)
1770 first
= last
= rdev
->raid_disk
;
1773 * find the disk ... but prefer rdev->saved_raid_disk
1776 if (rdev
->saved_raid_disk
>= 0 &&
1777 rdev
->saved_raid_disk
>= first
&&
1778 rdev
->saved_raid_disk
< conf
->raid_disks
&&
1779 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1780 first
= last
= rdev
->saved_raid_disk
;
1782 for (mirror
= first
; mirror
<= last
; mirror
++) {
1783 p
= conf
->mirrors
+ mirror
;
1786 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1787 rdev
->data_offset
<< 9);
1789 p
->head_position
= 0;
1790 rdev
->raid_disk
= mirror
;
1792 /* As all devices are equivalent, we don't need a full recovery
1793 * if this was recently any drive of the array
1795 if (rdev
->saved_raid_disk
< 0)
1797 rcu_assign_pointer(p
->rdev
, rdev
);
1800 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
1801 p
[conf
->raid_disks
].rdev
== NULL
) {
1802 /* Add this device as a replacement */
1803 clear_bit(In_sync
, &rdev
->flags
);
1804 set_bit(Replacement
, &rdev
->flags
);
1805 rdev
->raid_disk
= mirror
;
1808 rcu_assign_pointer(p
[conf
->raid_disks
].rdev
, rdev
);
1812 if (mddev
->queue
&& blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
1813 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, mddev
->queue
);
1818 static int raid1_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1820 struct r1conf
*conf
= mddev
->private;
1822 int number
= rdev
->raid_disk
;
1823 struct raid1_info
*p
= conf
->mirrors
+ number
;
1825 if (rdev
!= p
->rdev
)
1826 p
= conf
->mirrors
+ conf
->raid_disks
+ number
;
1829 if (rdev
== p
->rdev
) {
1830 if (test_bit(In_sync
, &rdev
->flags
) ||
1831 atomic_read(&rdev
->nr_pending
)) {
1835 /* Only remove non-faulty devices if recovery
1838 if (!test_bit(Faulty
, &rdev
->flags
) &&
1839 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
1840 mddev
->degraded
< conf
->raid_disks
) {
1845 if (!test_bit(RemoveSynchronized
, &rdev
->flags
)) {
1847 if (atomic_read(&rdev
->nr_pending
)) {
1848 /* lost the race, try later */
1854 if (conf
->mirrors
[conf
->raid_disks
+ number
].rdev
) {
1855 /* We just removed a device that is being replaced.
1856 * Move down the replacement. We drain all IO before
1857 * doing this to avoid confusion.
1859 struct md_rdev
*repl
=
1860 conf
->mirrors
[conf
->raid_disks
+ number
].rdev
;
1861 freeze_array(conf
, 0);
1862 if (atomic_read(&repl
->nr_pending
)) {
1863 /* It means that some queued IO of retry_list
1864 * hold repl. Thus, we cannot set replacement
1865 * as NULL, avoiding rdev NULL pointer
1866 * dereference in sync_request_write and
1867 * handle_write_finished.
1870 unfreeze_array(conf
);
1873 clear_bit(Replacement
, &repl
->flags
);
1875 conf
->mirrors
[conf
->raid_disks
+ number
].rdev
= NULL
;
1876 unfreeze_array(conf
);
1879 clear_bit(WantReplacement
, &rdev
->flags
);
1880 err
= md_integrity_register(mddev
);
1888 static void end_sync_read(struct bio
*bio
)
1890 struct r1bio
*r1_bio
= get_resync_r1bio(bio
);
1892 update_head_pos(r1_bio
->read_disk
, r1_bio
);
1895 * we have read a block, now it needs to be re-written,
1896 * or re-read if the read failed.
1897 * We don't do much here, just schedule handling by raid1d
1899 if (!bio
->bi_status
)
1900 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
1902 if (atomic_dec_and_test(&r1_bio
->remaining
))
1903 reschedule_retry(r1_bio
);
1906 static void abort_sync_write(struct mddev
*mddev
, struct r1bio
*r1_bio
)
1908 sector_t sync_blocks
= 0;
1909 sector_t s
= r1_bio
->sector
;
1910 long sectors_to_go
= r1_bio
->sectors
;
1912 /* make sure these bits don't get cleared. */
1914 md_bitmap_end_sync(mddev
->bitmap
, s
, &sync_blocks
, 1);
1916 sectors_to_go
-= sync_blocks
;
1917 } while (sectors_to_go
> 0);
1920 static void put_sync_write_buf(struct r1bio
*r1_bio
, int uptodate
)
1922 if (atomic_dec_and_test(&r1_bio
->remaining
)) {
1923 struct mddev
*mddev
= r1_bio
->mddev
;
1924 int s
= r1_bio
->sectors
;
1926 if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
) ||
1927 test_bit(R1BIO_WriteError
, &r1_bio
->state
))
1928 reschedule_retry(r1_bio
);
1931 md_done_sync(mddev
, s
, uptodate
);
1936 static void end_sync_write(struct bio
*bio
)
1938 int uptodate
= !bio
->bi_status
;
1939 struct r1bio
*r1_bio
= get_resync_r1bio(bio
);
1940 struct mddev
*mddev
= r1_bio
->mddev
;
1941 struct r1conf
*conf
= mddev
->private;
1944 struct md_rdev
*rdev
= conf
->mirrors
[find_bio_disk(r1_bio
, bio
)].rdev
;
1947 abort_sync_write(mddev
, r1_bio
);
1948 set_bit(WriteErrorSeen
, &rdev
->flags
);
1949 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
1950 set_bit(MD_RECOVERY_NEEDED
, &
1952 set_bit(R1BIO_WriteError
, &r1_bio
->state
);
1953 } else if (is_badblock(rdev
, r1_bio
->sector
, r1_bio
->sectors
,
1954 &first_bad
, &bad_sectors
) &&
1955 !is_badblock(conf
->mirrors
[r1_bio
->read_disk
].rdev
,
1958 &first_bad
, &bad_sectors
)
1960 set_bit(R1BIO_MadeGood
, &r1_bio
->state
);
1962 put_sync_write_buf(r1_bio
, uptodate
);
1965 static int r1_sync_page_io(struct md_rdev
*rdev
, sector_t sector
,
1966 int sectors
, struct page
*page
, int rw
)
1968 if (sync_page_io(rdev
, sector
, sectors
<< 9, page
, rw
, 0, false))
1972 set_bit(WriteErrorSeen
, &rdev
->flags
);
1973 if (!test_and_set_bit(WantReplacement
,
1975 set_bit(MD_RECOVERY_NEEDED
, &
1976 rdev
->mddev
->recovery
);
1978 /* need to record an error - either for the block or the device */
1979 if (!rdev_set_badblocks(rdev
, sector
, sectors
, 0))
1980 md_error(rdev
->mddev
, rdev
);
1984 static int fix_sync_read_error(struct r1bio
*r1_bio
)
1986 /* Try some synchronous reads of other devices to get
1987 * good data, much like with normal read errors. Only
1988 * read into the pages we already have so we don't
1989 * need to re-issue the read request.
1990 * We don't need to freeze the array, because being in an
1991 * active sync request, there is no normal IO, and
1992 * no overlapping syncs.
1993 * We don't need to check is_badblock() again as we
1994 * made sure that anything with a bad block in range
1995 * will have bi_end_io clear.
1997 struct mddev
*mddev
= r1_bio
->mddev
;
1998 struct r1conf
*conf
= mddev
->private;
1999 struct bio
*bio
= r1_bio
->bios
[r1_bio
->read_disk
];
2000 struct page
**pages
= get_resync_pages(bio
)->pages
;
2001 sector_t sect
= r1_bio
->sector
;
2002 int sectors
= r1_bio
->sectors
;
2004 struct md_rdev
*rdev
;
2006 rdev
= conf
->mirrors
[r1_bio
->read_disk
].rdev
;
2007 if (test_bit(FailFast
, &rdev
->flags
)) {
2008 /* Don't try recovering from here - just fail it
2009 * ... unless it is the last working device of course */
2010 md_error(mddev
, rdev
);
2011 if (test_bit(Faulty
, &rdev
->flags
))
2012 /* Don't try to read from here, but make sure
2013 * put_buf does it's thing
2015 bio
->bi_end_io
= end_sync_write
;
2020 int d
= r1_bio
->read_disk
;
2024 if (s
> (PAGE_SIZE
>>9))
2027 if (r1_bio
->bios
[d
]->bi_end_io
== end_sync_read
) {
2028 /* No rcu protection needed here devices
2029 * can only be removed when no resync is
2030 * active, and resync is currently active
2032 rdev
= conf
->mirrors
[d
].rdev
;
2033 if (sync_page_io(rdev
, sect
, s
<<9,
2035 REQ_OP_READ
, 0, false)) {
2041 if (d
== conf
->raid_disks
* 2)
2043 } while (!success
&& d
!= r1_bio
->read_disk
);
2046 char b
[BDEVNAME_SIZE
];
2048 /* Cannot read from anywhere, this block is lost.
2049 * Record a bad block on each device. If that doesn't
2050 * work just disable and interrupt the recovery.
2051 * Don't fail devices as that won't really help.
2053 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
2054 mdname(mddev
), bio_devname(bio
, b
),
2055 (unsigned long long)r1_bio
->sector
);
2056 for (d
= 0; d
< conf
->raid_disks
* 2; d
++) {
2057 rdev
= conf
->mirrors
[d
].rdev
;
2058 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
2060 if (!rdev_set_badblocks(rdev
, sect
, s
, 0))
2064 conf
->recovery_disabled
=
2065 mddev
->recovery_disabled
;
2066 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2067 md_done_sync(mddev
, r1_bio
->sectors
, 0);
2079 /* write it back and re-read */
2080 while (d
!= r1_bio
->read_disk
) {
2082 d
= conf
->raid_disks
* 2;
2084 if (r1_bio
->bios
[d
]->bi_end_io
!= end_sync_read
)
2086 rdev
= conf
->mirrors
[d
].rdev
;
2087 if (r1_sync_page_io(rdev
, sect
, s
,
2090 r1_bio
->bios
[d
]->bi_end_io
= NULL
;
2091 rdev_dec_pending(rdev
, mddev
);
2095 while (d
!= r1_bio
->read_disk
) {
2097 d
= conf
->raid_disks
* 2;
2099 if (r1_bio
->bios
[d
]->bi_end_io
!= end_sync_read
)
2101 rdev
= conf
->mirrors
[d
].rdev
;
2102 if (r1_sync_page_io(rdev
, sect
, s
,
2105 atomic_add(s
, &rdev
->corrected_errors
);
2111 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
2116 static void process_checks(struct r1bio
*r1_bio
)
2118 /* We have read all readable devices. If we haven't
2119 * got the block, then there is no hope left.
2120 * If we have, then we want to do a comparison
2121 * and skip the write if everything is the same.
2122 * If any blocks failed to read, then we need to
2123 * attempt an over-write
2125 struct mddev
*mddev
= r1_bio
->mddev
;
2126 struct r1conf
*conf
= mddev
->private;
2131 /* Fix variable parts of all bios */
2132 vcnt
= (r1_bio
->sectors
+ PAGE_SIZE
/ 512 - 1) >> (PAGE_SHIFT
- 9);
2133 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
2134 blk_status_t status
;
2135 struct bio
*b
= r1_bio
->bios
[i
];
2136 struct resync_pages
*rp
= get_resync_pages(b
);
2137 if (b
->bi_end_io
!= end_sync_read
)
2139 /* fixup the bio for reuse, but preserve errno */
2140 status
= b
->bi_status
;
2142 b
->bi_status
= status
;
2143 b
->bi_iter
.bi_sector
= r1_bio
->sector
+
2144 conf
->mirrors
[i
].rdev
->data_offset
;
2145 bio_set_dev(b
, conf
->mirrors
[i
].rdev
->bdev
);
2146 b
->bi_end_io
= end_sync_read
;
2147 rp
->raid_bio
= r1_bio
;
2150 /* initialize bvec table again */
2151 md_bio_reset_resync_pages(b
, rp
, r1_bio
->sectors
<< 9);
2153 for (primary
= 0; primary
< conf
->raid_disks
* 2; primary
++)
2154 if (r1_bio
->bios
[primary
]->bi_end_io
== end_sync_read
&&
2155 !r1_bio
->bios
[primary
]->bi_status
) {
2156 r1_bio
->bios
[primary
]->bi_end_io
= NULL
;
2157 rdev_dec_pending(conf
->mirrors
[primary
].rdev
, mddev
);
2160 r1_bio
->read_disk
= primary
;
2161 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
2163 struct bio
*pbio
= r1_bio
->bios
[primary
];
2164 struct bio
*sbio
= r1_bio
->bios
[i
];
2165 blk_status_t status
= sbio
->bi_status
;
2166 struct page
**ppages
= get_resync_pages(pbio
)->pages
;
2167 struct page
**spages
= get_resync_pages(sbio
)->pages
;
2169 int page_len
[RESYNC_PAGES
] = { 0 };
2170 struct bvec_iter_all iter_all
;
2172 if (sbio
->bi_end_io
!= end_sync_read
)
2174 /* Now we can 'fixup' the error value */
2175 sbio
->bi_status
= 0;
2177 bio_for_each_segment_all(bi
, sbio
, iter_all
)
2178 page_len
[j
++] = bi
->bv_len
;
2181 for (j
= vcnt
; j
-- ; ) {
2182 if (memcmp(page_address(ppages
[j
]),
2183 page_address(spages
[j
]),
2190 atomic64_add(r1_bio
->sectors
, &mddev
->resync_mismatches
);
2191 if (j
< 0 || (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)
2193 /* No need to write to this device. */
2194 sbio
->bi_end_io
= NULL
;
2195 rdev_dec_pending(conf
->mirrors
[i
].rdev
, mddev
);
2199 bio_copy_data(sbio
, pbio
);
2203 static void sync_request_write(struct mddev
*mddev
, struct r1bio
*r1_bio
)
2205 struct r1conf
*conf
= mddev
->private;
2207 int disks
= conf
->raid_disks
* 2;
2210 if (!test_bit(R1BIO_Uptodate
, &r1_bio
->state
))
2211 /* ouch - failed to read all of that. */
2212 if (!fix_sync_read_error(r1_bio
))
2215 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
2216 process_checks(r1_bio
);
2221 atomic_set(&r1_bio
->remaining
, 1);
2222 for (i
= 0; i
< disks
; i
++) {
2223 wbio
= r1_bio
->bios
[i
];
2224 if (wbio
->bi_end_io
== NULL
||
2225 (wbio
->bi_end_io
== end_sync_read
&&
2226 (i
== r1_bio
->read_disk
||
2227 !test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))))
2229 if (test_bit(Faulty
, &conf
->mirrors
[i
].rdev
->flags
)) {
2230 abort_sync_write(mddev
, r1_bio
);
2234 bio_set_op_attrs(wbio
, REQ_OP_WRITE
, 0);
2235 if (test_bit(FailFast
, &conf
->mirrors
[i
].rdev
->flags
))
2236 wbio
->bi_opf
|= MD_FAILFAST
;
2238 wbio
->bi_end_io
= end_sync_write
;
2239 atomic_inc(&r1_bio
->remaining
);
2240 md_sync_acct(conf
->mirrors
[i
].rdev
->bdev
, bio_sectors(wbio
));
2242 generic_make_request(wbio
);
2245 put_sync_write_buf(r1_bio
, 1);
2249 * This is a kernel thread which:
2251 * 1. Retries failed read operations on working mirrors.
2252 * 2. Updates the raid superblock when problems encounter.
2253 * 3. Performs writes following reads for array synchronising.
2256 static void fix_read_error(struct r1conf
*conf
, int read_disk
,
2257 sector_t sect
, int sectors
)
2259 struct mddev
*mddev
= conf
->mddev
;
2265 struct md_rdev
*rdev
;
2267 if (s
> (PAGE_SIZE
>>9))
2275 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2277 (test_bit(In_sync
, &rdev
->flags
) ||
2278 (!test_bit(Faulty
, &rdev
->flags
) &&
2279 rdev
->recovery_offset
>= sect
+ s
)) &&
2280 is_badblock(rdev
, sect
, s
,
2281 &first_bad
, &bad_sectors
) == 0) {
2282 atomic_inc(&rdev
->nr_pending
);
2284 if (sync_page_io(rdev
, sect
, s
<<9,
2285 conf
->tmppage
, REQ_OP_READ
, 0, false))
2287 rdev_dec_pending(rdev
, mddev
);
2293 if (d
== conf
->raid_disks
* 2)
2295 } while (!success
&& d
!= read_disk
);
2298 /* Cannot read from anywhere - mark it bad */
2299 struct md_rdev
*rdev
= conf
->mirrors
[read_disk
].rdev
;
2300 if (!rdev_set_badblocks(rdev
, sect
, s
, 0))
2301 md_error(mddev
, rdev
);
2304 /* write it back and re-read */
2306 while (d
!= read_disk
) {
2308 d
= conf
->raid_disks
* 2;
2311 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2313 !test_bit(Faulty
, &rdev
->flags
)) {
2314 atomic_inc(&rdev
->nr_pending
);
2316 r1_sync_page_io(rdev
, sect
, s
,
2317 conf
->tmppage
, WRITE
);
2318 rdev_dec_pending(rdev
, mddev
);
2323 while (d
!= read_disk
) {
2324 char b
[BDEVNAME_SIZE
];
2326 d
= conf
->raid_disks
* 2;
2329 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2331 !test_bit(Faulty
, &rdev
->flags
)) {
2332 atomic_inc(&rdev
->nr_pending
);
2334 if (r1_sync_page_io(rdev
, sect
, s
,
2335 conf
->tmppage
, READ
)) {
2336 atomic_add(s
, &rdev
->corrected_errors
);
2337 pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
2339 (unsigned long long)(sect
+
2341 bdevname(rdev
->bdev
, b
));
2343 rdev_dec_pending(rdev
, mddev
);
2352 static int narrow_write_error(struct r1bio
*r1_bio
, int i
)
2354 struct mddev
*mddev
= r1_bio
->mddev
;
2355 struct r1conf
*conf
= mddev
->private;
2356 struct md_rdev
*rdev
= conf
->mirrors
[i
].rdev
;
2358 /* bio has the data to be written to device 'i' where
2359 * we just recently had a write error.
2360 * We repeatedly clone the bio and trim down to one block,
2361 * then try the write. Where the write fails we record
2363 * It is conceivable that the bio doesn't exactly align with
2364 * blocks. We must handle this somehow.
2366 * We currently own a reference on the rdev.
2372 int sect_to_write
= r1_bio
->sectors
;
2375 if (rdev
->badblocks
.shift
< 0)
2378 block_sectors
= roundup(1 << rdev
->badblocks
.shift
,
2379 bdev_logical_block_size(rdev
->bdev
) >> 9);
2380 sector
= r1_bio
->sector
;
2381 sectors
= ((sector
+ block_sectors
)
2382 & ~(sector_t
)(block_sectors
- 1))
2385 while (sect_to_write
) {
2387 if (sectors
> sect_to_write
)
2388 sectors
= sect_to_write
;
2389 /* Write at 'sector' for 'sectors'*/
2391 if (test_bit(R1BIO_BehindIO
, &r1_bio
->state
)) {
2392 wbio
= bio_clone_fast(r1_bio
->behind_master_bio
,
2396 wbio
= bio_clone_fast(r1_bio
->master_bio
, GFP_NOIO
,
2400 bio_set_op_attrs(wbio
, REQ_OP_WRITE
, 0);
2401 wbio
->bi_iter
.bi_sector
= r1_bio
->sector
;
2402 wbio
->bi_iter
.bi_size
= r1_bio
->sectors
<< 9;
2404 bio_trim(wbio
, sector
- r1_bio
->sector
, sectors
);
2405 wbio
->bi_iter
.bi_sector
+= rdev
->data_offset
;
2406 bio_set_dev(wbio
, rdev
->bdev
);
2408 if (submit_bio_wait(wbio
) < 0)
2410 ok
= rdev_set_badblocks(rdev
, sector
,
2415 sect_to_write
-= sectors
;
2417 sectors
= block_sectors
;
2422 static void handle_sync_write_finished(struct r1conf
*conf
, struct r1bio
*r1_bio
)
2425 int s
= r1_bio
->sectors
;
2426 for (m
= 0; m
< conf
->raid_disks
* 2 ; m
++) {
2427 struct md_rdev
*rdev
= conf
->mirrors
[m
].rdev
;
2428 struct bio
*bio
= r1_bio
->bios
[m
];
2429 if (bio
->bi_end_io
== NULL
)
2431 if (!bio
->bi_status
&&
2432 test_bit(R1BIO_MadeGood
, &r1_bio
->state
)) {
2433 rdev_clear_badblocks(rdev
, r1_bio
->sector
, s
, 0);
2435 if (bio
->bi_status
&&
2436 test_bit(R1BIO_WriteError
, &r1_bio
->state
)) {
2437 if (!rdev_set_badblocks(rdev
, r1_bio
->sector
, s
, 0))
2438 md_error(conf
->mddev
, rdev
);
2442 md_done_sync(conf
->mddev
, s
, 1);
2445 static void handle_write_finished(struct r1conf
*conf
, struct r1bio
*r1_bio
)
2450 for (m
= 0; m
< conf
->raid_disks
* 2 ; m
++)
2451 if (r1_bio
->bios
[m
] == IO_MADE_GOOD
) {
2452 struct md_rdev
*rdev
= conf
->mirrors
[m
].rdev
;
2453 rdev_clear_badblocks(rdev
,
2455 r1_bio
->sectors
, 0);
2456 rdev_dec_pending(rdev
, conf
->mddev
);
2457 } else if (r1_bio
->bios
[m
] != NULL
) {
2458 /* This drive got a write error. We need to
2459 * narrow down and record precise write
2463 if (!narrow_write_error(r1_bio
, m
)) {
2464 md_error(conf
->mddev
,
2465 conf
->mirrors
[m
].rdev
);
2466 /* an I/O failed, we can't clear the bitmap */
2467 set_bit(R1BIO_Degraded
, &r1_bio
->state
);
2469 rdev_dec_pending(conf
->mirrors
[m
].rdev
,
2473 spin_lock_irq(&conf
->device_lock
);
2474 list_add(&r1_bio
->retry_list
, &conf
->bio_end_io_list
);
2475 idx
= sector_to_idx(r1_bio
->sector
);
2476 atomic_inc(&conf
->nr_queued
[idx
]);
2477 spin_unlock_irq(&conf
->device_lock
);
2479 * In case freeze_array() is waiting for condition
2480 * get_unqueued_pending() == extra to be true.
2482 wake_up(&conf
->wait_barrier
);
2483 md_wakeup_thread(conf
->mddev
->thread
);
2485 if (test_bit(R1BIO_WriteError
, &r1_bio
->state
))
2486 close_write(r1_bio
);
2487 raid_end_bio_io(r1_bio
);
2491 static void handle_read_error(struct r1conf
*conf
, struct r1bio
*r1_bio
)
2493 struct mddev
*mddev
= conf
->mddev
;
2495 struct md_rdev
*rdev
;
2497 clear_bit(R1BIO_ReadError
, &r1_bio
->state
);
2498 /* we got a read error. Maybe the drive is bad. Maybe just
2499 * the block and we can fix it.
2500 * We freeze all other IO, and try reading the block from
2501 * other devices. When we find one, we re-write
2502 * and check it that fixes the read error.
2503 * This is all done synchronously while the array is
2507 bio
= r1_bio
->bios
[r1_bio
->read_disk
];
2509 r1_bio
->bios
[r1_bio
->read_disk
] = NULL
;
2511 rdev
= conf
->mirrors
[r1_bio
->read_disk
].rdev
;
2513 && !test_bit(FailFast
, &rdev
->flags
)) {
2514 freeze_array(conf
, 1);
2515 fix_read_error(conf
, r1_bio
->read_disk
,
2516 r1_bio
->sector
, r1_bio
->sectors
);
2517 unfreeze_array(conf
);
2518 } else if (mddev
->ro
== 0 && test_bit(FailFast
, &rdev
->flags
)) {
2519 md_error(mddev
, rdev
);
2521 r1_bio
->bios
[r1_bio
->read_disk
] = IO_BLOCKED
;
2524 rdev_dec_pending(rdev
, conf
->mddev
);
2525 allow_barrier(conf
, r1_bio
->sector
);
2526 bio
= r1_bio
->master_bio
;
2528 /* Reuse the old r1_bio so that the IO_BLOCKED settings are preserved */
2530 raid1_read_request(mddev
, bio
, r1_bio
->sectors
, r1_bio
);
2533 static void raid1d(struct md_thread
*thread
)
2535 struct mddev
*mddev
= thread
->mddev
;
2536 struct r1bio
*r1_bio
;
2537 unsigned long flags
;
2538 struct r1conf
*conf
= mddev
->private;
2539 struct list_head
*head
= &conf
->retry_list
;
2540 struct blk_plug plug
;
2543 md_check_recovery(mddev
);
2545 if (!list_empty_careful(&conf
->bio_end_io_list
) &&
2546 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
2548 spin_lock_irqsave(&conf
->device_lock
, flags
);
2549 if (!test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
))
2550 list_splice_init(&conf
->bio_end_io_list
, &tmp
);
2551 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2552 while (!list_empty(&tmp
)) {
2553 r1_bio
= list_first_entry(&tmp
, struct r1bio
,
2555 list_del(&r1_bio
->retry_list
);
2556 idx
= sector_to_idx(r1_bio
->sector
);
2557 atomic_dec(&conf
->nr_queued
[idx
]);
2558 if (mddev
->degraded
)
2559 set_bit(R1BIO_Degraded
, &r1_bio
->state
);
2560 if (test_bit(R1BIO_WriteError
, &r1_bio
->state
))
2561 close_write(r1_bio
);
2562 raid_end_bio_io(r1_bio
);
2566 blk_start_plug(&plug
);
2569 flush_pending_writes(conf
);
2571 spin_lock_irqsave(&conf
->device_lock
, flags
);
2572 if (list_empty(head
)) {
2573 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2576 r1_bio
= list_entry(head
->prev
, struct r1bio
, retry_list
);
2577 list_del(head
->prev
);
2578 idx
= sector_to_idx(r1_bio
->sector
);
2579 atomic_dec(&conf
->nr_queued
[idx
]);
2580 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2582 mddev
= r1_bio
->mddev
;
2583 conf
= mddev
->private;
2584 if (test_bit(R1BIO_IsSync
, &r1_bio
->state
)) {
2585 if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
) ||
2586 test_bit(R1BIO_WriteError
, &r1_bio
->state
))
2587 handle_sync_write_finished(conf
, r1_bio
);
2589 sync_request_write(mddev
, r1_bio
);
2590 } else if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
) ||
2591 test_bit(R1BIO_WriteError
, &r1_bio
->state
))
2592 handle_write_finished(conf
, r1_bio
);
2593 else if (test_bit(R1BIO_ReadError
, &r1_bio
->state
))
2594 handle_read_error(conf
, r1_bio
);
2599 if (mddev
->sb_flags
& ~(1<<MD_SB_CHANGE_PENDING
))
2600 md_check_recovery(mddev
);
2602 blk_finish_plug(&plug
);
2605 static int init_resync(struct r1conf
*conf
)
2609 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
2610 BUG_ON(mempool_initialized(&conf
->r1buf_pool
));
2612 return mempool_init(&conf
->r1buf_pool
, buffs
, r1buf_pool_alloc
,
2613 r1buf_pool_free
, conf
->poolinfo
);
2616 static struct r1bio
*raid1_alloc_init_r1buf(struct r1conf
*conf
)
2618 struct r1bio
*r1bio
= mempool_alloc(&conf
->r1buf_pool
, GFP_NOIO
);
2619 struct resync_pages
*rps
;
2623 for (i
= conf
->poolinfo
->raid_disks
; i
--; ) {
2624 bio
= r1bio
->bios
[i
];
2625 rps
= bio
->bi_private
;
2627 bio
->bi_private
= rps
;
2629 r1bio
->master_bio
= NULL
;
2634 * perform a "sync" on one "block"
2636 * We need to make sure that no normal I/O request - particularly write
2637 * requests - conflict with active sync requests.
2639 * This is achieved by tracking pending requests and a 'barrier' concept
2640 * that can be installed to exclude normal IO requests.
2643 static sector_t
raid1_sync_request(struct mddev
*mddev
, sector_t sector_nr
,
2646 struct r1conf
*conf
= mddev
->private;
2647 struct r1bio
*r1_bio
;
2649 sector_t max_sector
, nr_sectors
;
2653 int write_targets
= 0, read_targets
= 0;
2654 sector_t sync_blocks
;
2655 int still_degraded
= 0;
2656 int good_sectors
= RESYNC_SECTORS
;
2657 int min_bad
= 0; /* number of sectors that are bad in all devices */
2658 int idx
= sector_to_idx(sector_nr
);
2661 if (!mempool_initialized(&conf
->r1buf_pool
))
2662 if (init_resync(conf
))
2665 max_sector
= mddev
->dev_sectors
;
2666 if (sector_nr
>= max_sector
) {
2667 /* If we aborted, we need to abort the
2668 * sync on the 'current' bitmap chunk (there will
2669 * only be one in raid1 resync.
2670 * We can find the current addess in mddev->curr_resync
2672 if (mddev
->curr_resync
< max_sector
) /* aborted */
2673 md_bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
2675 else /* completed sync */
2678 md_bitmap_close_sync(mddev
->bitmap
);
2681 if (mddev_is_clustered(mddev
)) {
2682 conf
->cluster_sync_low
= 0;
2683 conf
->cluster_sync_high
= 0;
2688 if (mddev
->bitmap
== NULL
&&
2689 mddev
->recovery_cp
== MaxSector
&&
2690 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
2691 conf
->fullsync
== 0) {
2693 return max_sector
- sector_nr
;
2695 /* before building a request, check if we can skip these blocks..
2696 * This call the bitmap_start_sync doesn't actually record anything
2698 if (!md_bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
2699 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
2700 /* We can skip this block, and probably several more */
2706 * If there is non-resync activity waiting for a turn, then let it
2707 * though before starting on this new sync request.
2709 if (atomic_read(&conf
->nr_waiting
[idx
]))
2710 schedule_timeout_uninterruptible(1);
2712 /* we are incrementing sector_nr below. To be safe, we check against
2713 * sector_nr + two times RESYNC_SECTORS
2716 md_bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
,
2717 mddev_is_clustered(mddev
) && (sector_nr
+ 2 * RESYNC_SECTORS
> conf
->cluster_sync_high
));
2720 if (raise_barrier(conf
, sector_nr
))
2723 r1_bio
= raid1_alloc_init_r1buf(conf
);
2727 * If we get a correctably read error during resync or recovery,
2728 * we might want to read from a different device. So we
2729 * flag all drives that could conceivably be read from for READ,
2730 * and any others (which will be non-In_sync devices) for WRITE.
2731 * If a read fails, we try reading from something else for which READ
2735 r1_bio
->mddev
= mddev
;
2736 r1_bio
->sector
= sector_nr
;
2738 set_bit(R1BIO_IsSync
, &r1_bio
->state
);
2739 /* make sure good_sectors won't go across barrier unit boundary */
2740 good_sectors
= align_to_barrier_unit_end(sector_nr
, good_sectors
);
2742 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
2743 struct md_rdev
*rdev
;
2744 bio
= r1_bio
->bios
[i
];
2746 rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
2748 test_bit(Faulty
, &rdev
->flags
)) {
2749 if (i
< conf
->raid_disks
)
2751 } else if (!test_bit(In_sync
, &rdev
->flags
)) {
2752 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
2753 bio
->bi_end_io
= end_sync_write
;
2756 /* may need to read from here */
2757 sector_t first_bad
= MaxSector
;
2760 if (is_badblock(rdev
, sector_nr
, good_sectors
,
2761 &first_bad
, &bad_sectors
)) {
2762 if (first_bad
> sector_nr
)
2763 good_sectors
= first_bad
- sector_nr
;
2765 bad_sectors
-= (sector_nr
- first_bad
);
2767 min_bad
> bad_sectors
)
2768 min_bad
= bad_sectors
;
2771 if (sector_nr
< first_bad
) {
2772 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2779 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
2780 bio
->bi_end_io
= end_sync_read
;
2782 } else if (!test_bit(WriteErrorSeen
, &rdev
->flags
) &&
2783 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
2784 !test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
2786 * The device is suitable for reading (InSync),
2787 * but has bad block(s) here. Let's try to correct them,
2788 * if we are doing resync or repair. Otherwise, leave
2789 * this device alone for this sync request.
2791 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
2792 bio
->bi_end_io
= end_sync_write
;
2796 if (rdev
&& bio
->bi_end_io
) {
2797 atomic_inc(&rdev
->nr_pending
);
2798 bio
->bi_iter
.bi_sector
= sector_nr
+ rdev
->data_offset
;
2799 bio_set_dev(bio
, rdev
->bdev
);
2800 if (test_bit(FailFast
, &rdev
->flags
))
2801 bio
->bi_opf
|= MD_FAILFAST
;
2807 r1_bio
->read_disk
= disk
;
2809 if (read_targets
== 0 && min_bad
> 0) {
2810 /* These sectors are bad on all InSync devices, so we
2811 * need to mark them bad on all write targets
2814 for (i
= 0 ; i
< conf
->raid_disks
* 2 ; i
++)
2815 if (r1_bio
->bios
[i
]->bi_end_io
== end_sync_write
) {
2816 struct md_rdev
*rdev
= conf
->mirrors
[i
].rdev
;
2817 ok
= rdev_set_badblocks(rdev
, sector_nr
,
2821 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
2826 /* Cannot record the badblocks, so need to
2828 * If there are multiple read targets, could just
2829 * fail the really bad ones ???
2831 conf
->recovery_disabled
= mddev
->recovery_disabled
;
2832 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2838 if (min_bad
> 0 && min_bad
< good_sectors
) {
2839 /* only resync enough to reach the next bad->good
2841 good_sectors
= min_bad
;
2844 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) && read_targets
> 0)
2845 /* extra read targets are also write targets */
2846 write_targets
+= read_targets
-1;
2848 if (write_targets
== 0 || read_targets
== 0) {
2849 /* There is nowhere to write, so all non-sync
2850 * drives must be failed - so we are finished
2854 max_sector
= sector_nr
+ min_bad
;
2855 rv
= max_sector
- sector_nr
;
2861 if (max_sector
> mddev
->resync_max
)
2862 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
2863 if (max_sector
> sector_nr
+ good_sectors
)
2864 max_sector
= sector_nr
+ good_sectors
;
2869 int len
= PAGE_SIZE
;
2870 if (sector_nr
+ (len
>>9) > max_sector
)
2871 len
= (max_sector
- sector_nr
) << 9;
2874 if (sync_blocks
== 0) {
2875 if (!md_bitmap_start_sync(mddev
->bitmap
, sector_nr
,
2876 &sync_blocks
, still_degraded
) &&
2878 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
2880 if ((len
>> 9) > sync_blocks
)
2881 len
= sync_blocks
<<9;
2884 for (i
= 0 ; i
< conf
->raid_disks
* 2; i
++) {
2885 struct resync_pages
*rp
;
2887 bio
= r1_bio
->bios
[i
];
2888 rp
= get_resync_pages(bio
);
2889 if (bio
->bi_end_io
) {
2890 page
= resync_fetch_page(rp
, page_idx
);
2893 * won't fail because the vec table is big
2894 * enough to hold all these pages
2896 bio_add_page(bio
, page
, len
, 0);
2899 nr_sectors
+= len
>>9;
2900 sector_nr
+= len
>>9;
2901 sync_blocks
-= (len
>>9);
2902 } while (++page_idx
< RESYNC_PAGES
);
2904 r1_bio
->sectors
= nr_sectors
;
2906 if (mddev_is_clustered(mddev
) &&
2907 conf
->cluster_sync_high
< sector_nr
+ nr_sectors
) {
2908 conf
->cluster_sync_low
= mddev
->curr_resync_completed
;
2909 conf
->cluster_sync_high
= conf
->cluster_sync_low
+ CLUSTER_RESYNC_WINDOW_SECTORS
;
2910 /* Send resync message */
2911 md_cluster_ops
->resync_info_update(mddev
,
2912 conf
->cluster_sync_low
,
2913 conf
->cluster_sync_high
);
2916 /* For a user-requested sync, we read all readable devices and do a
2919 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
2920 atomic_set(&r1_bio
->remaining
, read_targets
);
2921 for (i
= 0; i
< conf
->raid_disks
* 2 && read_targets
; i
++) {
2922 bio
= r1_bio
->bios
[i
];
2923 if (bio
->bi_end_io
== end_sync_read
) {
2925 md_sync_acct_bio(bio
, nr_sectors
);
2926 if (read_targets
== 1)
2927 bio
->bi_opf
&= ~MD_FAILFAST
;
2928 generic_make_request(bio
);
2932 atomic_set(&r1_bio
->remaining
, 1);
2933 bio
= r1_bio
->bios
[r1_bio
->read_disk
];
2934 md_sync_acct_bio(bio
, nr_sectors
);
2935 if (read_targets
== 1)
2936 bio
->bi_opf
&= ~MD_FAILFAST
;
2937 generic_make_request(bio
);
2942 static sector_t
raid1_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
2947 return mddev
->dev_sectors
;
2950 static struct r1conf
*setup_conf(struct mddev
*mddev
)
2952 struct r1conf
*conf
;
2954 struct raid1_info
*disk
;
2955 struct md_rdev
*rdev
;
2958 conf
= kzalloc(sizeof(struct r1conf
), GFP_KERNEL
);
2962 conf
->nr_pending
= kcalloc(BARRIER_BUCKETS_NR
,
2963 sizeof(atomic_t
), GFP_KERNEL
);
2964 if (!conf
->nr_pending
)
2967 conf
->nr_waiting
= kcalloc(BARRIER_BUCKETS_NR
,
2968 sizeof(atomic_t
), GFP_KERNEL
);
2969 if (!conf
->nr_waiting
)
2972 conf
->nr_queued
= kcalloc(BARRIER_BUCKETS_NR
,
2973 sizeof(atomic_t
), GFP_KERNEL
);
2974 if (!conf
->nr_queued
)
2977 conf
->barrier
= kcalloc(BARRIER_BUCKETS_NR
,
2978 sizeof(atomic_t
), GFP_KERNEL
);
2982 conf
->mirrors
= kzalloc(array3_size(sizeof(struct raid1_info
),
2983 mddev
->raid_disks
, 2),
2988 conf
->tmppage
= alloc_page(GFP_KERNEL
);
2992 conf
->poolinfo
= kzalloc(sizeof(*conf
->poolinfo
), GFP_KERNEL
);
2993 if (!conf
->poolinfo
)
2995 conf
->poolinfo
->raid_disks
= mddev
->raid_disks
* 2;
2996 err
= mempool_init(&conf
->r1bio_pool
, NR_RAID_BIOS
, r1bio_pool_alloc
,
2997 rbio_pool_free
, conf
->poolinfo
);
3001 err
= bioset_init(&conf
->bio_split
, BIO_POOL_SIZE
, 0, 0);
3005 conf
->poolinfo
->mddev
= mddev
;
3008 spin_lock_init(&conf
->device_lock
);
3009 rdev_for_each(rdev
, mddev
) {
3010 int disk_idx
= rdev
->raid_disk
;
3011 if (disk_idx
>= mddev
->raid_disks
3014 if (test_bit(Replacement
, &rdev
->flags
))
3015 disk
= conf
->mirrors
+ mddev
->raid_disks
+ disk_idx
;
3017 disk
= conf
->mirrors
+ disk_idx
;
3022 disk
->head_position
= 0;
3023 disk
->seq_start
= MaxSector
;
3025 conf
->raid_disks
= mddev
->raid_disks
;
3026 conf
->mddev
= mddev
;
3027 INIT_LIST_HEAD(&conf
->retry_list
);
3028 INIT_LIST_HEAD(&conf
->bio_end_io_list
);
3030 spin_lock_init(&conf
->resync_lock
);
3031 init_waitqueue_head(&conf
->wait_barrier
);
3033 bio_list_init(&conf
->pending_bio_list
);
3034 conf
->pending_count
= 0;
3035 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
3038 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
3040 disk
= conf
->mirrors
+ i
;
3042 if (i
< conf
->raid_disks
&&
3043 disk
[conf
->raid_disks
].rdev
) {
3044 /* This slot has a replacement. */
3046 /* No original, just make the replacement
3047 * a recovering spare
3050 disk
[conf
->raid_disks
].rdev
;
3051 disk
[conf
->raid_disks
].rdev
= NULL
;
3052 } else if (!test_bit(In_sync
, &disk
->rdev
->flags
))
3053 /* Original is not in_sync - bad */
3058 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
3059 disk
->head_position
= 0;
3061 (disk
->rdev
->saved_raid_disk
< 0))
3067 conf
->thread
= md_register_thread(raid1d
, mddev
, "raid1");
3075 mempool_exit(&conf
->r1bio_pool
);
3076 kfree(conf
->mirrors
);
3077 safe_put_page(conf
->tmppage
);
3078 kfree(conf
->poolinfo
);
3079 kfree(conf
->nr_pending
);
3080 kfree(conf
->nr_waiting
);
3081 kfree(conf
->nr_queued
);
3082 kfree(conf
->barrier
);
3083 bioset_exit(&conf
->bio_split
);
3086 return ERR_PTR(err
);
3089 static void raid1_free(struct mddev
*mddev
, void *priv
);
3090 static int raid1_run(struct mddev
*mddev
)
3092 struct r1conf
*conf
;
3094 struct md_rdev
*rdev
;
3096 bool discard_supported
= false;
3098 if (mddev
->level
!= 1) {
3099 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
3100 mdname(mddev
), mddev
->level
);
3103 if (mddev
->reshape_position
!= MaxSector
) {
3104 pr_warn("md/raid1:%s: reshape_position set but not supported\n",
3108 if (mddev_init_writes_pending(mddev
) < 0)
3111 * copy the already verified devices into our private RAID1
3112 * bookkeeping area. [whatever we allocate in run(),
3113 * should be freed in raid1_free()]
3115 if (mddev
->private == NULL
)
3116 conf
= setup_conf(mddev
);
3118 conf
= mddev
->private;
3121 return PTR_ERR(conf
);
3124 blk_queue_max_write_same_sectors(mddev
->queue
, 0);
3125 blk_queue_max_write_zeroes_sectors(mddev
->queue
, 0);
3128 rdev_for_each(rdev
, mddev
) {
3129 if (!mddev
->gendisk
)
3131 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
3132 rdev
->data_offset
<< 9);
3133 if (blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
3134 discard_supported
= true;
3137 mddev
->degraded
= 0;
3138 for (i
= 0; i
< conf
->raid_disks
; i
++)
3139 if (conf
->mirrors
[i
].rdev
== NULL
||
3140 !test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ||
3141 test_bit(Faulty
, &conf
->mirrors
[i
].rdev
->flags
))
3144 * RAID1 needs at least one disk in active
3146 if (conf
->raid_disks
- mddev
->degraded
< 1) {
3151 if (conf
->raid_disks
- mddev
->degraded
== 1)
3152 mddev
->recovery_cp
= MaxSector
;
3154 if (mddev
->recovery_cp
!= MaxSector
)
3155 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
3157 pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
3158 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
3162 * Ok, everything is just fine now
3164 mddev
->thread
= conf
->thread
;
3165 conf
->thread
= NULL
;
3166 mddev
->private = conf
;
3167 set_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
);
3169 md_set_array_sectors(mddev
, raid1_size(mddev
, 0, 0));
3172 if (discard_supported
)
3173 blk_queue_flag_set(QUEUE_FLAG_DISCARD
,
3176 blk_queue_flag_clear(QUEUE_FLAG_DISCARD
,
3180 ret
= md_integrity_register(mddev
);
3182 md_unregister_thread(&mddev
->thread
);
3188 raid1_free(mddev
, conf
);
3192 static void raid1_free(struct mddev
*mddev
, void *priv
)
3194 struct r1conf
*conf
= priv
;
3196 mempool_exit(&conf
->r1bio_pool
);
3197 kfree(conf
->mirrors
);
3198 safe_put_page(conf
->tmppage
);
3199 kfree(conf
->poolinfo
);
3200 kfree(conf
->nr_pending
);
3201 kfree(conf
->nr_waiting
);
3202 kfree(conf
->nr_queued
);
3203 kfree(conf
->barrier
);
3204 bioset_exit(&conf
->bio_split
);
3208 static int raid1_resize(struct mddev
*mddev
, sector_t sectors
)
3210 /* no resync is happening, and there is enough space
3211 * on all devices, so we can resize.
3212 * We need to make sure resync covers any new space.
3213 * If the array is shrinking we should possibly wait until
3214 * any io in the removed space completes, but it hardly seems
3217 sector_t newsize
= raid1_size(mddev
, sectors
, 0);
3218 if (mddev
->external_size
&&
3219 mddev
->array_sectors
> newsize
)
3221 if (mddev
->bitmap
) {
3222 int ret
= md_bitmap_resize(mddev
->bitmap
, newsize
, 0, 0);
3226 md_set_array_sectors(mddev
, newsize
);
3227 if (sectors
> mddev
->dev_sectors
&&
3228 mddev
->recovery_cp
> mddev
->dev_sectors
) {
3229 mddev
->recovery_cp
= mddev
->dev_sectors
;
3230 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3232 mddev
->dev_sectors
= sectors
;
3233 mddev
->resync_max_sectors
= sectors
;
3237 static int raid1_reshape(struct mddev
*mddev
)
3240 * 1/ resize the r1bio_pool
3241 * 2/ resize conf->mirrors
3243 * We allocate a new r1bio_pool if we can.
3244 * Then raise a device barrier and wait until all IO stops.
3245 * Then resize conf->mirrors and swap in the new r1bio pool.
3247 * At the same time, we "pack" the devices so that all the missing
3248 * devices have the higher raid_disk numbers.
3250 mempool_t newpool
, oldpool
;
3251 struct pool_info
*newpoolinfo
;
3252 struct raid1_info
*newmirrors
;
3253 struct r1conf
*conf
= mddev
->private;
3254 int cnt
, raid_disks
;
3255 unsigned long flags
;
3259 memset(&newpool
, 0, sizeof(newpool
));
3260 memset(&oldpool
, 0, sizeof(oldpool
));
3262 /* Cannot change chunk_size, layout, or level */
3263 if (mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
||
3264 mddev
->layout
!= mddev
->new_layout
||
3265 mddev
->level
!= mddev
->new_level
) {
3266 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3267 mddev
->new_layout
= mddev
->layout
;
3268 mddev
->new_level
= mddev
->level
;
3272 if (!mddev_is_clustered(mddev
))
3273 md_allow_write(mddev
);
3275 raid_disks
= mddev
->raid_disks
+ mddev
->delta_disks
;
3277 if (raid_disks
< conf
->raid_disks
) {
3279 for (d
= 0; d
< conf
->raid_disks
; d
++)
3280 if (conf
->mirrors
[d
].rdev
)
3282 if (cnt
> raid_disks
)
3286 newpoolinfo
= kmalloc(sizeof(*newpoolinfo
), GFP_KERNEL
);
3289 newpoolinfo
->mddev
= mddev
;
3290 newpoolinfo
->raid_disks
= raid_disks
* 2;
3292 ret
= mempool_init(&newpool
, NR_RAID_BIOS
, r1bio_pool_alloc
,
3293 rbio_pool_free
, newpoolinfo
);
3298 newmirrors
= kzalloc(array3_size(sizeof(struct raid1_info
),
3303 mempool_exit(&newpool
);
3307 freeze_array(conf
, 0);
3309 /* ok, everything is stopped */
3310 oldpool
= conf
->r1bio_pool
;
3311 conf
->r1bio_pool
= newpool
;
3313 for (d
= d2
= 0; d
< conf
->raid_disks
; d
++) {
3314 struct md_rdev
*rdev
= conf
->mirrors
[d
].rdev
;
3315 if (rdev
&& rdev
->raid_disk
!= d2
) {
3316 sysfs_unlink_rdev(mddev
, rdev
);
3317 rdev
->raid_disk
= d2
;
3318 sysfs_unlink_rdev(mddev
, rdev
);
3319 if (sysfs_link_rdev(mddev
, rdev
))
3320 pr_warn("md/raid1:%s: cannot register rd%d\n",
3321 mdname(mddev
), rdev
->raid_disk
);
3324 newmirrors
[d2
++].rdev
= rdev
;
3326 kfree(conf
->mirrors
);
3327 conf
->mirrors
= newmirrors
;
3328 kfree(conf
->poolinfo
);
3329 conf
->poolinfo
= newpoolinfo
;
3331 spin_lock_irqsave(&conf
->device_lock
, flags
);
3332 mddev
->degraded
+= (raid_disks
- conf
->raid_disks
);
3333 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3334 conf
->raid_disks
= mddev
->raid_disks
= raid_disks
;
3335 mddev
->delta_disks
= 0;
3337 unfreeze_array(conf
);
3339 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3340 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3341 md_wakeup_thread(mddev
->thread
);
3343 mempool_exit(&oldpool
);
3347 static void raid1_quiesce(struct mddev
*mddev
, int quiesce
)
3349 struct r1conf
*conf
= mddev
->private;
3352 freeze_array(conf
, 0);
3354 unfreeze_array(conf
);
3357 static void *raid1_takeover(struct mddev
*mddev
)
3359 /* raid1 can take over:
3360 * raid5 with 2 devices, any layout or chunk size
3362 if (mddev
->level
== 5 && mddev
->raid_disks
== 2) {
3363 struct r1conf
*conf
;
3364 mddev
->new_level
= 1;
3365 mddev
->new_layout
= 0;
3366 mddev
->new_chunk_sectors
= 0;
3367 conf
= setup_conf(mddev
);
3368 if (!IS_ERR(conf
)) {
3369 /* Array must appear to be quiesced */
3370 conf
->array_frozen
= 1;
3371 mddev_clear_unsupported_flags(mddev
,
3372 UNSUPPORTED_MDDEV_FLAGS
);
3376 return ERR_PTR(-EINVAL
);
3379 static struct md_personality raid1_personality
=
3383 .owner
= THIS_MODULE
,
3384 .make_request
= raid1_make_request
,
3387 .status
= raid1_status
,
3388 .error_handler
= raid1_error
,
3389 .hot_add_disk
= raid1_add_disk
,
3390 .hot_remove_disk
= raid1_remove_disk
,
3391 .spare_active
= raid1_spare_active
,
3392 .sync_request
= raid1_sync_request
,
3393 .resize
= raid1_resize
,
3395 .check_reshape
= raid1_reshape
,
3396 .quiesce
= raid1_quiesce
,
3397 .takeover
= raid1_takeover
,
3398 .congested
= raid1_congested
,
3401 static int __init
raid_init(void)
3403 return register_md_personality(&raid1_personality
);
3406 static void raid_exit(void)
3408 unregister_md_personality(&raid1_personality
);
3411 module_init(raid_init
);
3412 module_exit(raid_exit
);
3413 MODULE_LICENSE("GPL");
3414 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
3415 MODULE_ALIAS("md-personality-3"); /* RAID1 */
3416 MODULE_ALIAS("md-raid1");
3417 MODULE_ALIAS("md-level-1");
3419 module_param(max_queued_requests
, int, S_IRUGO
|S_IWUSR
);