Skip to content

Commit 31a7333

Browse files
YuKuai-huaweiliu-song-6
authored andcommitted
md/raid1: factor out read_first_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 first rdev 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 f109207 commit 31a7333

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

drivers/md/raid1.c

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,47 @@ static sector_t align_to_barrier_unit_end(sector_t start_sector,
579579
return len;
580580
}
581581

582+
static void update_read_sectors(struct r1conf *conf, int disk,
583+
sector_t this_sector, int len)
584+
{
585+
struct raid1_info *info = &conf->mirrors[disk];
586+
587+
atomic_inc(&info->rdev->nr_pending);
588+
if (info->next_seq_sect != this_sector)
589+
info->seq_start = this_sector;
590+
info->next_seq_sect = this_sector + len;
591+
}
592+
593+
static int choose_first_rdev(struct r1conf *conf, struct r1bio *r1_bio,
594+
int *max_sectors)
595+
{
596+
sector_t this_sector = r1_bio->sector;
597+
int len = r1_bio->sectors;
598+
int disk;
599+
600+
for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
601+
struct md_rdev *rdev;
602+
int read_len;
603+
604+
if (r1_bio->bios[disk] == IO_BLOCKED)
605+
continue;
606+
607+
rdev = conf->mirrors[disk].rdev;
608+
if (!rdev || test_bit(Faulty, &rdev->flags))
609+
continue;
610+
611+
/* choose the first disk even if it has some bad blocks. */
612+
read_len = raid1_check_read_range(rdev, this_sector, &len);
613+
if (read_len > 0) {
614+
update_read_sectors(conf, disk, this_sector, read_len);
615+
*max_sectors = read_len;
616+
return disk;
617+
}
618+
}
619+
620+
return -1;
621+
}
622+
582623
/*
583624
* This routine returns the disk from which the requested read should
584625
* be done. There is a per-array 'next expected sequential IO' sector
@@ -603,7 +644,6 @@ static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sect
603644
sector_t best_dist;
604645
unsigned int min_pending;
605646
struct md_rdev *rdev;
606-
int choose_first;
607647

608648
retry:
609649
sectors = r1_bio->sectors;
@@ -614,10 +654,11 @@ static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sect
614654
best_pending_disk = -1;
615655
min_pending = UINT_MAX;
616656
best_good_sectors = 0;
617-
choose_first = raid1_should_read_first(conf->mddev, this_sector,
618-
sectors);
619657
clear_bit(R1BIO_FailFast, &r1_bio->state);
620658

659+
if (raid1_should_read_first(conf->mddev, this_sector, sectors))
660+
return choose_first_rdev(conf, r1_bio, max_sectors);
661+
621662
for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
622663
sector_t dist;
623664
sector_t first_bad;
@@ -663,8 +704,6 @@ static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sect
663704
* bad_sectors from another device..
664705
*/
665706
bad_sectors -= (this_sector - first_bad);
666-
if (choose_first && sectors > bad_sectors)
667-
sectors = bad_sectors;
668707
if (best_good_sectors > sectors)
669708
best_good_sectors = sectors;
670709

@@ -674,8 +713,6 @@ static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sect
674713
best_good_sectors = good_sectors;
675714
best_disk = disk;
676715
}
677-
if (choose_first)
678-
break;
679716
}
680717
continue;
681718
} else {
@@ -690,10 +727,6 @@ static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sect
690727

691728
pending = atomic_read(&rdev->nr_pending);
692729
dist = abs(this_sector - conf->mirrors[disk].head_position);
693-
if (choose_first) {
694-
best_disk = disk;
695-
break;
696-
}
697730
/* Don't change to another disk for sequential reads */
698731
if (conf->mirrors[disk].next_seq_sect == this_sector
699732
|| dist == 0) {
@@ -769,13 +802,9 @@ static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sect
769802
rdev = conf->mirrors[best_disk].rdev;
770803
if (!rdev)
771804
goto retry;
772-
atomic_inc(&rdev->nr_pending);
773-
sectors = best_good_sectors;
774-
775-
if (conf->mirrors[best_disk].next_seq_sect != this_sector)
776-
conf->mirrors[best_disk].seq_start = this_sector;
777805

778-
conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
806+
sectors = best_good_sectors;
807+
update_read_sectors(conf, disk, this_sector, sectors);
779808
}
780809
*max_sectors = sectors;
781810

0 commit comments

Comments
 (0)