@@ -76,6 +76,7 @@ static void scrub_rbio_work_locked(struct work_struct *work);
76
76
77
77
static void free_raid_bio_pointers (struct btrfs_raid_bio * rbio )
78
78
{
79
+ bitmap_free (rbio -> error_bitmap );
79
80
kfree (rbio -> stripe_pages );
80
81
kfree (rbio -> bio_sectors );
81
82
kfree (rbio -> stripe_sectors );
@@ -950,9 +951,10 @@ static struct btrfs_raid_bio *alloc_rbio(struct btrfs_fs_info *fs_info,
950
951
rbio -> stripe_sectors = kcalloc (num_sectors , sizeof (struct sector_ptr ),
951
952
GFP_NOFS );
952
953
rbio -> finish_pointers = kcalloc (real_stripes , sizeof (void * ), GFP_NOFS );
954
+ rbio -> error_bitmap = bitmap_zalloc (num_sectors , GFP_NOFS );
953
955
954
956
if (!rbio -> stripe_pages || !rbio -> bio_sectors || !rbio -> stripe_sectors ||
955
- !rbio -> finish_pointers ) {
957
+ !rbio -> finish_pointers || ! rbio -> error_bitmap ) {
956
958
free_raid_bio_pointers (rbio );
957
959
kfree (rbio );
958
960
return ERR_PTR (- ENOMEM );
@@ -1044,8 +1046,11 @@ static int rbio_add_io_sector(struct btrfs_raid_bio *rbio,
1044
1046
disk_start = stripe -> physical + sector_nr * sectorsize ;
1045
1047
1046
1048
/* if the device is missing, just fail this stripe */
1047
- if (!stripe -> dev -> bdev )
1049
+ if (!stripe -> dev -> bdev ) {
1050
+ set_bit (stripe_nr * rbio -> stripe_nsectors + sector_nr ,
1051
+ rbio -> error_bitmap );
1048
1052
return fail_rbio_index (rbio , stripe_nr );
1053
+ }
1049
1054
1050
1055
/* see if we can add this page onto our existing bio */
1051
1056
if (last ) {
@@ -1209,6 +1214,7 @@ static int rmw_assemble_write_bios(struct btrfs_raid_bio *rbio,
1209
1214
* write.
1210
1215
*/
1211
1216
atomic_set (& rbio -> error , 0 );
1217
+ bitmap_clear (rbio -> error_bitmap , 0 , rbio -> nr_sectors );
1212
1218
rbio -> faila = -1 ;
1213
1219
rbio -> failb = -1 ;
1214
1220
@@ -1332,6 +1338,40 @@ static int find_logical_bio_stripe(struct btrfs_raid_bio *rbio,
1332
1338
return -1 ;
1333
1339
}
1334
1340
1341
+ static void set_rbio_range_error (struct btrfs_raid_bio * rbio , struct bio * bio )
1342
+ {
1343
+ struct btrfs_fs_info * fs_info = rbio -> bioc -> fs_info ;
1344
+ u32 offset = (bio -> bi_iter .bi_sector << SECTOR_SHIFT ) -
1345
+ rbio -> bioc -> raid_map [0 ];
1346
+ int total_nr_sector = offset >> fs_info -> sectorsize_bits ;
1347
+
1348
+ ASSERT (total_nr_sector < rbio -> nr_data * rbio -> stripe_nsectors );
1349
+
1350
+ bitmap_set (rbio -> error_bitmap , total_nr_sector ,
1351
+ bio -> bi_iter .bi_size >> fs_info -> sectorsize_bits );
1352
+
1353
+ /*
1354
+ * Special handling for raid56_alloc_missing_rbio() used by
1355
+ * scrub/replace. Unlike call path in raid56_parity_recover(), they
1356
+ * pass an empty bio here. Thus we have to find out the missing device
1357
+ * and mark the stripe error instead.
1358
+ */
1359
+ if (bio -> bi_iter .bi_size == 0 ) {
1360
+ bool found_missing = false;
1361
+ int stripe_nr ;
1362
+
1363
+ for (stripe_nr = 0 ; stripe_nr < rbio -> real_stripes ; stripe_nr ++ ) {
1364
+ if (!rbio -> bioc -> stripes [stripe_nr ].dev -> bdev ) {
1365
+ found_missing = true;
1366
+ bitmap_set (rbio -> error_bitmap ,
1367
+ stripe_nr * rbio -> stripe_nsectors ,
1368
+ rbio -> stripe_nsectors );
1369
+ }
1370
+ }
1371
+ ASSERT (found_missing );
1372
+ }
1373
+ }
1374
+
1335
1375
/*
1336
1376
* returns -EIO if we had too many failures
1337
1377
*/
@@ -1423,14 +1463,49 @@ static void set_bio_pages_uptodate(struct btrfs_raid_bio *rbio, struct bio *bio)
1423
1463
}
1424
1464
}
1425
1465
1466
+ static int get_bio_sector_nr (struct btrfs_raid_bio * rbio , struct bio * bio )
1467
+ {
1468
+ struct bio_vec * bv = bio_first_bvec_all (bio );
1469
+ int i ;
1470
+
1471
+ for (i = 0 ; i < rbio -> nr_sectors ; i ++ ) {
1472
+ struct sector_ptr * sector ;
1473
+
1474
+ sector = & rbio -> stripe_sectors [i ];
1475
+ if (sector -> page == bv -> bv_page && sector -> pgoff == bv -> bv_offset )
1476
+ break ;
1477
+ sector = & rbio -> bio_sectors [i ];
1478
+ if (sector -> page == bv -> bv_page && sector -> pgoff == bv -> bv_offset )
1479
+ break ;
1480
+ }
1481
+ ASSERT (i < rbio -> nr_sectors );
1482
+ return i ;
1483
+ }
1484
+
1485
+ static void rbio_update_error_bitmap (struct btrfs_raid_bio * rbio , struct bio * bio )
1486
+ {
1487
+ int total_sector_nr = get_bio_sector_nr (rbio , bio );
1488
+ u32 bio_size = 0 ;
1489
+ struct bio_vec * bvec ;
1490
+ struct bvec_iter_all iter_all ;
1491
+
1492
+ bio_for_each_segment_all (bvec , bio , iter_all )
1493
+ bio_size += bvec -> bv_len ;
1494
+
1495
+ bitmap_set (rbio -> error_bitmap , total_sector_nr ,
1496
+ bio_size >> rbio -> bioc -> fs_info -> sectorsize_bits );
1497
+ }
1498
+
1426
1499
static void raid_wait_read_end_io (struct bio * bio )
1427
1500
{
1428
1501
struct btrfs_raid_bio * rbio = bio -> bi_private ;
1429
1502
1430
- if (bio -> bi_status )
1503
+ if (bio -> bi_status ) {
1431
1504
fail_bio_stripe (rbio , bio );
1432
- else
1505
+ rbio_update_error_bitmap (rbio , bio );
1506
+ } else {
1433
1507
set_bio_pages_uptodate (rbio , bio );
1508
+ }
1434
1509
1435
1510
bio_put (bio );
1436
1511
if (atomic_dec_and_test (& rbio -> stripes_pending ))
@@ -1863,10 +1938,10 @@ static int recover_assemble_read_bios(struct btrfs_raid_bio *rbio,
1863
1938
struct sector_ptr * sector ;
1864
1939
1865
1940
if (rbio -> faila == stripe || rbio -> failb == stripe ) {
1866
- atomic_inc (& rbio -> error );
1867
1941
/* Skip the current stripe. */
1868
1942
ASSERT (sectornr == 0 );
1869
1943
total_sector_nr += rbio -> stripe_nsectors - 1 ;
1944
+ atomic_inc (& rbio -> error );
1870
1945
continue ;
1871
1946
}
1872
1947
sector = rbio_stripe_sector (rbio , stripe , sectornr );
@@ -1891,9 +1966,10 @@ static int recover_rbio(struct btrfs_raid_bio *rbio)
1891
1966
1892
1967
/*
1893
1968
* Either we're doing recover for a read failure or degraded write,
1894
- * caller should have set faila/b correctly.
1969
+ * caller should have set faila/b and error bitmap correctly.
1895
1970
*/
1896
1971
ASSERT (rbio -> faila >= 0 || rbio -> failb >= 0 );
1972
+ ASSERT (bitmap_weight (rbio -> error_bitmap , rbio -> nr_sectors ));
1897
1973
bio_list_init (& bio_list );
1898
1974
1899
1975
/*
@@ -1978,6 +2054,8 @@ void raid56_parity_recover(struct bio *bio, struct btrfs_io_context *bioc,
1978
2054
rbio -> operation = BTRFS_RBIO_READ_REBUILD ;
1979
2055
rbio_add_bio (rbio , bio );
1980
2056
2057
+ set_rbio_range_error (rbio , bio );
2058
+
1981
2059
rbio -> faila = find_logical_bio_stripe (rbio , bio );
1982
2060
if (rbio -> faila == -1 ) {
1983
2061
btrfs_warn (fs_info ,
@@ -2038,8 +2116,10 @@ static void raid_wait_write_end_io(struct bio *bio)
2038
2116
struct btrfs_raid_bio * rbio = bio -> bi_private ;
2039
2117
blk_status_t err = bio -> bi_status ;
2040
2118
2041
- if (err )
2119
+ if (err ) {
2042
2120
fail_bio_stripe (rbio , bio );
2121
+ rbio_update_error_bitmap (rbio , bio );
2122
+ }
2043
2123
bio_put (bio );
2044
2124
if (atomic_dec_and_test (& rbio -> stripes_pending ))
2045
2125
wake_up (& rbio -> io_wait );
@@ -2117,6 +2197,7 @@ static int rmw_rbio(struct btrfs_raid_bio *rbio)
2117
2197
spin_unlock_irq (& rbio -> bio_list_lock );
2118
2198
2119
2199
atomic_set (& rbio -> error , 0 );
2200
+ bitmap_clear (rbio -> error_bitmap , 0 , rbio -> nr_sectors );
2120
2201
2121
2202
index_rbio_pages (rbio );
2122
2203
@@ -2328,6 +2409,7 @@ static int finish_parity_scrub(struct btrfs_raid_bio *rbio, int need_check)
2328
2409
}
2329
2410
2330
2411
atomic_set (& rbio -> error , 0 );
2412
+ bitmap_clear (rbio -> error_bitmap , 0 , rbio -> nr_sectors );
2331
2413
2332
2414
/* Map the parity stripe just once */
2333
2415
pointers [nr_data ] = kmap_local_page (p_sector .page );
@@ -2533,6 +2615,8 @@ static int scrub_rbio(struct btrfs_raid_bio *rbio)
2533
2615
goto cleanup ;
2534
2616
2535
2617
atomic_set (& rbio -> error , 0 );
2618
+ bitmap_clear (rbio -> error_bitmap , 0 , rbio -> nr_sectors );
2619
+
2536
2620
ret = scrub_assemble_read_bios (rbio , & bio_list );
2537
2621
if (ret < 0 )
2538
2622
goto cleanup ;
@@ -2612,6 +2696,7 @@ raid56_alloc_missing_rbio(struct bio *bio, struct btrfs_io_context *bioc)
2612
2696
*/
2613
2697
ASSERT (!bio -> bi_iter .bi_size );
2614
2698
2699
+ set_rbio_range_error (rbio , bio );
2615
2700
rbio -> faila = find_logical_bio_stripe (rbio , bio );
2616
2701
if (rbio -> faila == -1 ) {
2617
2702
btrfs_warn_rl (fs_info ,
0 commit comments