Skip to content

Commit 9f3ced7

Browse files
YuKuai-huaweiliu-song-6
authored andcommitted
md/raid1: factor out choose_bb_rdev() from read_balance()
read_balance() is hard to understand because there are too many status and branches, and it's overlong. This patch factor out the case to read the rdev with bad blocks from read_balance(), there are no functional changes. Co-developed-by: Paul Luse <[email protected]> Signed-off-by: Paul Luse <[email protected]> Signed-off-by: Yu Kuai <[email protected]> Reviewed-by: Xiao Ni <[email protected]> Signed-off-by: Song Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent dfa8ecd commit 9f3ced7

File tree

1 file changed

+48
-31
lines changed

1 file changed

+48
-31
lines changed

drivers/md/raid1.c

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,44 @@ static int choose_first_rdev(struct r1conf *conf, struct r1bio *r1_bio,
620620
return -1;
621621
}
622622

623+
static int choose_bb_rdev(struct r1conf *conf, struct r1bio *r1_bio,
624+
int *max_sectors)
625+
{
626+
sector_t this_sector = r1_bio->sector;
627+
int best_disk = -1;
628+
int best_len = 0;
629+
int disk;
630+
631+
for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
632+
struct md_rdev *rdev;
633+
int len;
634+
int read_len;
635+
636+
if (r1_bio->bios[disk] == IO_BLOCKED)
637+
continue;
638+
639+
rdev = conf->mirrors[disk].rdev;
640+
if (!rdev || test_bit(Faulty, &rdev->flags) ||
641+
test_bit(WriteMostly, &rdev->flags))
642+
continue;
643+
644+
/* keep track of the disk with the most readable sectors. */
645+
len = r1_bio->sectors;
646+
read_len = raid1_check_read_range(rdev, this_sector, &len);
647+
if (read_len > best_len) {
648+
best_disk = disk;
649+
best_len = read_len;
650+
}
651+
}
652+
653+
if (best_disk != -1) {
654+
*max_sectors = best_len;
655+
update_read_sectors(conf, best_disk, this_sector, best_len);
656+
}
657+
658+
return best_disk;
659+
}
660+
623661
static int choose_slow_rdev(struct r1conf *conf, struct r1bio *r1_bio,
624662
int *max_sectors)
625663
{
@@ -708,8 +746,6 @@ static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sect
708746

709747
for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
710748
sector_t dist;
711-
sector_t first_bad;
712-
int bad_sectors;
713749
unsigned int pending;
714750

715751
rdev = conf->mirrors[disk].rdev;
@@ -722,36 +758,8 @@ static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sect
722758
continue;
723759
if (test_bit(WriteMostly, &rdev->flags))
724760
continue;
725-
/* This is a reasonable device to use. It might
726-
* even be best.
727-
*/
728-
if (is_badblock(rdev, this_sector, sectors,
729-
&first_bad, &bad_sectors)) {
730-
if (best_dist < MaxSector)
731-
/* already have a better device */
732-
continue;
733-
if (first_bad <= this_sector) {
734-
/* cannot read here. If this is the 'primary'
735-
* device, then we must not read beyond
736-
* bad_sectors from another device..
737-
*/
738-
bad_sectors -= (this_sector - first_bad);
739-
if (best_good_sectors > sectors)
740-
best_good_sectors = sectors;
741-
742-
} else {
743-
sector_t good_sectors = first_bad - this_sector;
744-
if (good_sectors > best_good_sectors) {
745-
best_good_sectors = good_sectors;
746-
best_disk = disk;
747-
}
748-
}
761+
if (rdev_has_badblock(rdev, this_sector, sectors))
749762
continue;
750-
} else {
751-
if ((sectors > best_good_sectors) && (best_disk >= 0))
752-
best_disk = -1;
753-
best_good_sectors = sectors;
754-
}
755763

756764
if (best_disk >= 0)
757765
/* At least two disks to choose from so failfast is OK */
@@ -843,6 +851,15 @@ static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sect
843851
if (best_disk >= 0)
844852
return best_disk;
845853

854+
/*
855+
* If we are here it means we didn't find a perfectly good disk so
856+
* now spend a bit more time trying to find one with the most good
857+
* sectors.
858+
*/
859+
disk = choose_bb_rdev(conf, r1_bio, max_sectors);
860+
if (disk >= 0)
861+
return disk;
862+
846863
return choose_slow_rdev(conf, r1_bio, max_sectors);
847864
}
848865

0 commit comments

Comments
 (0)