Skip to content

Commit 06f6038

Browse files
committed
md/raid1: avoid reading known bad blocks during resync
When performing resync/etc, keep the size of the request small enough that it doesn't overlap any known bad blocks. Devices with badblocks at the start of the request are completely excluded. If there is nowhere to read from due to bad blocks, record a bad block on each target device. Now that we never read from known-bad-blocks we can allow devices with known-bad-blocks into a RAID1. Signed-off-by: NeilBrown <neilb@suse.de>
1 parent d2eb35a commit 06f6038

1 file changed

Lines changed: 75 additions & 22 deletions

File tree

‎drivers/md/raid1.c‎

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,9 +1155,6 @@ static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
11551155
if (mddev->recovery_disabled == conf->recovery_disabled)
11561156
return -EBUSY;
11571157

1158-
if (rdev->badblocks.count)
1159-
return -EINVAL;
1160-
11611158
if (rdev->raid_disk >= 0)
11621159
first = last = rdev->raid_disk;
11631160

@@ -1303,6 +1300,9 @@ static int fix_sync_read_error(r1bio_t *r1_bio)
13031300
* We don't need to freeze the array, because being in an
13041301
* active sync request, there is no normal IO, and
13051302
* no overlapping syncs.
1303+
* We don't need to check is_badblock() again as we
1304+
* made sure that anything with a bad block in range
1305+
* will have bi_end_io clear.
13061306
*/
13071307
mddev_t *mddev = r1_bio->mddev;
13081308
conf_t *conf = mddev->private;
@@ -1792,6 +1792,8 @@ static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, i
17921792
int write_targets = 0, read_targets = 0;
17931793
sector_t sync_blocks;
17941794
int still_degraded = 0;
1795+
int good_sectors = RESYNC_SECTORS;
1796+
int min_bad = 0; /* number of sectors that are bad in all devices */
17951797

17961798
if (!conf->r1buf_pool)
17971799
if (init_resync(conf))
@@ -1879,36 +1881,89 @@ static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, i
18791881

18801882
rdev = rcu_dereference(conf->mirrors[i].rdev);
18811883
if (rdev == NULL ||
1882-
test_bit(Faulty, &rdev->flags)) {
1884+
test_bit(Faulty, &rdev->flags)) {
18831885
still_degraded = 1;
1884-
continue;
18851886
} else if (!test_bit(In_sync, &rdev->flags)) {
18861887
bio->bi_rw = WRITE;
18871888
bio->bi_end_io = end_sync_write;
18881889
write_targets ++;
18891890
} else {
18901891
/* may need to read from here */
1891-
bio->bi_rw = READ;
1892-
bio->bi_end_io = end_sync_read;
1893-
if (test_bit(WriteMostly, &rdev->flags)) {
1894-
if (wonly < 0)
1895-
wonly = i;
1896-
} else {
1897-
if (disk < 0)
1898-
disk = i;
1892+
sector_t first_bad = MaxSector;
1893+
int bad_sectors;
1894+
1895+
if (is_badblock(rdev, sector_nr, good_sectors,
1896+
&first_bad, &bad_sectors)) {
1897+
if (first_bad > sector_nr)
1898+
good_sectors = first_bad - sector_nr;
1899+
else {
1900+
bad_sectors -= (sector_nr - first_bad);
1901+
if (min_bad == 0 ||
1902+
min_bad > bad_sectors)
1903+
min_bad = bad_sectors;
1904+
}
1905+
}
1906+
if (sector_nr < first_bad) {
1907+
if (test_bit(WriteMostly, &rdev->flags)) {
1908+
if (wonly < 0)
1909+
wonly = i;
1910+
} else {
1911+
if (disk < 0)
1912+
disk = i;
1913+
}
1914+
bio->bi_rw = READ;
1915+
bio->bi_end_io = end_sync_read;
1916+
read_targets++;
18991917
}
1900-
read_targets++;
19011918
}
1902-
atomic_inc(&rdev->nr_pending);
1903-
bio->bi_sector = sector_nr + rdev->data_offset;
1904-
bio->bi_bdev = rdev->bdev;
1905-
bio->bi_private = r1_bio;
1919+
if (bio->bi_end_io) {
1920+
atomic_inc(&rdev->nr_pending);
1921+
bio->bi_sector = sector_nr + rdev->data_offset;
1922+
bio->bi_bdev = rdev->bdev;
1923+
bio->bi_private = r1_bio;
1924+
}
19061925
}
19071926
rcu_read_unlock();
19081927
if (disk < 0)
19091928
disk = wonly;
19101929
r1_bio->read_disk = disk;
19111930

1931+
if (read_targets == 0 && min_bad > 0) {
1932+
/* These sectors are bad on all InSync devices, so we
1933+
* need to mark them bad on all write targets
1934+
*/
1935+
int ok = 1;
1936+
for (i = 0 ; i < conf->raid_disks ; i++)
1937+
if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
1938+
mdk_rdev_t *rdev =
1939+
rcu_dereference(conf->mirrors[i].rdev);
1940+
ok = rdev_set_badblocks(rdev, sector_nr,
1941+
min_bad, 0
1942+
) && ok;
1943+
}
1944+
set_bit(MD_CHANGE_DEVS, &mddev->flags);
1945+
*skipped = 1;
1946+
put_buf(r1_bio);
1947+
1948+
if (!ok) {
1949+
/* Cannot record the badblocks, so need to
1950+
* abort the resync.
1951+
* If there are multiple read targets, could just
1952+
* fail the really bad ones ???
1953+
*/
1954+
conf->recovery_disabled = mddev->recovery_disabled;
1955+
set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1956+
return 0;
1957+
} else
1958+
return min_bad;
1959+
1960+
}
1961+
if (min_bad > 0 && min_bad < good_sectors) {
1962+
/* only resync enough to reach the next bad->good
1963+
* transition */
1964+
good_sectors = min_bad;
1965+
}
1966+
19121967
if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
19131968
/* extra read targets are also write targets */
19141969
write_targets += read_targets-1;
@@ -1925,6 +1980,8 @@ static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, i
19251980

19261981
if (max_sector > mddev->resync_max)
19271982
max_sector = mddev->resync_max; /* Don't do IO beyond here */
1983+
if (max_sector > sector_nr + good_sectors)
1984+
max_sector = sector_nr + good_sectors;
19281985
nr_sectors = 0;
19291986
sync_blocks = 0;
19301987
do {
@@ -2147,10 +2204,6 @@ static int run(mddev_t *mddev)
21472204
blk_queue_segment_boundary(mddev->queue,
21482205
PAGE_CACHE_SIZE - 1);
21492206
}
2150-
if (rdev->badblocks.count) {
2151-
printk(KERN_ERR "md/raid1: Cannot handle bad blocks yet\n");
2152-
return -EINVAL;
2153-
}
21542207
}
21552208

21562209
mddev->degraded = 0;

0 commit comments

Comments
 (0)