Skip to content

Commit 442a4f6

Browse files
Stefan BehrensJosef Bacik
authored andcommitted
Btrfs: add device counters for detected IO and checksum errors
The goal is to detect when drives start to get an increased error rate, when drives should be replaced soon. Therefore statistic counters are added that count IO errors (read, write and flush). Additionally, the software detected errors like checksum errors and corrupted blocks are counted. Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de>
1 parent d07eb91 commit 442a4f6

6 files changed

Lines changed: 230 additions & 24 deletions

File tree

‎fs/btrfs/disk-io.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,18 +2557,19 @@ int open_ctree(struct super_block *sb,
25572557

25582558
static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
25592559
{
2560-
char b[BDEVNAME_SIZE];
2561-
25622560
if (uptodate) {
25632561
set_buffer_uptodate(bh);
25642562
} else {
2563+
struct btrfs_device *device = (struct btrfs_device *)
2564+
bh->b_private;
2565+
25652566
printk_ratelimited(KERN_WARNING "lost page write due to "
2566-
"I/O error on %s\n",
2567-
bdevname(bh->b_bdev, b));
2567+
"I/O error on %s\n", device->name);
25682568
/* note, we dont' set_buffer_write_io_error because we have
25692569
* our own ways of dealing with the IO errors
25702570
*/
25712571
clear_buffer_uptodate(bh);
2572+
btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS);
25722573
}
25732574
unlock_buffer(bh);
25742575
put_bh(bh);
@@ -2683,6 +2684,7 @@ static int write_dev_supers(struct btrfs_device *device,
26832684
set_buffer_uptodate(bh);
26842685
lock_buffer(bh);
26852686
bh->b_end_io = btrfs_end_buffer_write_sync;
2687+
bh->b_private = device;
26862688
}
26872689

26882690
/*
@@ -2741,6 +2743,9 @@ static int write_dev_flush(struct btrfs_device *device, int wait)
27412743
}
27422744
if (!bio_flagged(bio, BIO_UPTODATE)) {
27432745
ret = -EIO;
2746+
if (!bio_flagged(bio, BIO_EOPNOTSUPP))
2747+
btrfs_dev_stat_inc_and_print(device,
2748+
BTRFS_DEV_STAT_FLUSH_ERRS);
27442749
}
27452750

27462751
/* drop the reference from the wait == 0 run */

‎fs/btrfs/extent_io.c‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,7 @@ int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
19131913
if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
19141914
/* try to remap that extent elsewhere? */
19151915
bio_put(bio);
1916+
btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
19161917
return -EIO;
19171918
}
19181919

@@ -2327,10 +2328,23 @@ static void end_bio_extent_readpage(struct bio *bio, int err)
23272328
if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
23282329
ret = tree->ops->readpage_end_io_hook(page, start, end,
23292330
state, mirror);
2330-
if (ret)
2331+
if (ret) {
2332+
/* no IO indicated but software detected errors
2333+
* in the block, either checksum errors or
2334+
* issues with the contents */
2335+
struct btrfs_root *root =
2336+
BTRFS_I(page->mapping->host)->root;
2337+
struct btrfs_device *device;
2338+
23312339
uptodate = 0;
2332-
else
2340+
device = btrfs_find_device_for_logical(
2341+
root, start, mirror);
2342+
if (device)
2343+
btrfs_dev_stat_inc_and_print(device,
2344+
BTRFS_DEV_STAT_CORRUPTION_ERRS);
2345+
} else {
23332346
clean_io_failure(start, page);
2347+
}
23342348
}
23352349

23362350
if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {

‎fs/btrfs/ioctl.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,25 @@ struct btrfs_ioctl_logical_ino_args {
266266
__u64 inodes;
267267
};
268268

269+
enum btrfs_dev_stat_values {
270+
/* disk I/O failure stats */
271+
BTRFS_DEV_STAT_WRITE_ERRS, /* EIO or EREMOTEIO from lower layers */
272+
BTRFS_DEV_STAT_READ_ERRS, /* EIO or EREMOTEIO from lower layers */
273+
BTRFS_DEV_STAT_FLUSH_ERRS, /* EIO or EREMOTEIO from lower layers */
274+
275+
/* stats for indirect indications for I/O failures */
276+
BTRFS_DEV_STAT_CORRUPTION_ERRS, /* checksum error, bytenr error or
277+
* contents is illegal: this is an
278+
* indication that the block was damaged
279+
* during read or write, or written to
280+
* wrong location or read from wrong
281+
* location */
282+
BTRFS_DEV_STAT_GENERATION_ERRS, /* an indication that blocks have not
283+
* been written */
284+
285+
BTRFS_DEV_STAT_VALUES_MAX
286+
};
287+
269288
#define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
270289
struct btrfs_ioctl_vol_args)
271290
#define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \

‎fs/btrfs/scrub.c‎

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct scrub_dev;
5050
struct scrub_page {
5151
struct scrub_block *sblock;
5252
struct page *page;
53-
struct block_device *bdev;
53+
struct btrfs_device *dev;
5454
u64 flags; /* extent flags */
5555
u64 generation;
5656
u64 logical;
@@ -86,6 +86,7 @@ struct scrub_block {
8686
unsigned int header_error:1;
8787
unsigned int checksum_error:1;
8888
unsigned int no_io_error_seen:1;
89+
unsigned int generation_error:1; /* also sets header_error */
8990
};
9091
};
9192

@@ -675,6 +676,8 @@ static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
675676
sdev->stat.read_errors++;
676677
sdev->stat.uncorrectable_errors++;
677678
spin_unlock(&sdev->stat_lock);
679+
btrfs_dev_stat_inc_and_print(sdev->dev,
680+
BTRFS_DEV_STAT_READ_ERRS);
678681
goto out;
679682
}
680683

@@ -686,6 +689,8 @@ static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
686689
sdev->stat.read_errors++;
687690
sdev->stat.uncorrectable_errors++;
688691
spin_unlock(&sdev->stat_lock);
692+
btrfs_dev_stat_inc_and_print(sdev->dev,
693+
BTRFS_DEV_STAT_READ_ERRS);
689694
goto out;
690695
}
691696
BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
@@ -699,6 +704,8 @@ static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
699704
sdev->stat.read_errors++;
700705
sdev->stat.uncorrectable_errors++;
701706
spin_unlock(&sdev->stat_lock);
707+
btrfs_dev_stat_inc_and_print(sdev->dev,
708+
BTRFS_DEV_STAT_READ_ERRS);
702709
goto out;
703710
}
704711

@@ -725,19 +732,29 @@ static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
725732
spin_unlock(&sdev->stat_lock);
726733
if (__ratelimit(&_rs))
727734
scrub_print_warning("i/o error", sblock_to_check);
735+
btrfs_dev_stat_inc_and_print(sdev->dev,
736+
BTRFS_DEV_STAT_READ_ERRS);
728737
} else if (sblock_bad->checksum_error) {
729738
spin_lock(&sdev->stat_lock);
730739
sdev->stat.csum_errors++;
731740
spin_unlock(&sdev->stat_lock);
732741
if (__ratelimit(&_rs))
733742
scrub_print_warning("checksum error", sblock_to_check);
743+
btrfs_dev_stat_inc_and_print(sdev->dev,
744+
BTRFS_DEV_STAT_CORRUPTION_ERRS);
734745
} else if (sblock_bad->header_error) {
735746
spin_lock(&sdev->stat_lock);
736747
sdev->stat.verify_errors++;
737748
spin_unlock(&sdev->stat_lock);
738749
if (__ratelimit(&_rs))
739750
scrub_print_warning("checksum/header error",
740751
sblock_to_check);
752+
if (sblock_bad->generation_error)
753+
btrfs_dev_stat_inc_and_print(sdev->dev,
754+
BTRFS_DEV_STAT_GENERATION_ERRS);
755+
else
756+
btrfs_dev_stat_inc_and_print(sdev->dev,
757+
BTRFS_DEV_STAT_CORRUPTION_ERRS);
741758
}
742759

743760
if (sdev->readonly)
@@ -998,8 +1015,8 @@ static int scrub_setup_recheck_block(struct scrub_dev *sdev,
9981015
page = sblock->pagev + page_index;
9991016
page->logical = logical;
10001017
page->physical = bbio->stripes[mirror_index].physical;
1001-
/* for missing devices, bdev is NULL */
1002-
page->bdev = bbio->stripes[mirror_index].dev->bdev;
1018+
/* for missing devices, dev->bdev is NULL */
1019+
page->dev = bbio->stripes[mirror_index].dev;
10031020
page->mirror_num = mirror_index + 1;
10041021
page->page = alloc_page(GFP_NOFS);
10051022
if (!page->page) {
@@ -1043,7 +1060,7 @@ static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
10431060
struct scrub_page *page = sblock->pagev + page_num;
10441061
DECLARE_COMPLETION_ONSTACK(complete);
10451062

1046-
if (page->bdev == NULL) {
1063+
if (page->dev->bdev == NULL) {
10471064
page->io_error = 1;
10481065
sblock->no_io_error_seen = 0;
10491066
continue;
@@ -1053,7 +1070,7 @@ static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
10531070
bio = bio_alloc(GFP_NOFS, 1);
10541071
if (!bio)
10551072
return -EIO;
1056-
bio->bi_bdev = page->bdev;
1073+
bio->bi_bdev = page->dev->bdev;
10571074
bio->bi_sector = page->physical >> 9;
10581075
bio->bi_end_io = scrub_complete_bio_end_io;
10591076
bio->bi_private = &complete;
@@ -1102,11 +1119,14 @@ static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
11021119
h = (struct btrfs_header *)mapped_buffer;
11031120

11041121
if (sblock->pagev[0].logical != le64_to_cpu(h->bytenr) ||
1105-
generation != le64_to_cpu(h->generation) ||
11061122
memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
11071123
memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1108-
BTRFS_UUID_SIZE))
1124+
BTRFS_UUID_SIZE)) {
11091125
sblock->header_error = 1;
1126+
} else if (generation != le64_to_cpu(h->generation)) {
1127+
sblock->header_error = 1;
1128+
sblock->generation_error = 1;
1129+
}
11101130
csum = h->csum;
11111131
} else {
11121132
if (!have_csum)
@@ -1182,7 +1202,7 @@ static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
11821202
bio = bio_alloc(GFP_NOFS, 1);
11831203
if (!bio)
11841204
return -EIO;
1185-
bio->bi_bdev = page_bad->bdev;
1205+
bio->bi_bdev = page_bad->dev->bdev;
11861206
bio->bi_sector = page_bad->physical >> 9;
11871207
bio->bi_end_io = scrub_complete_bio_end_io;
11881208
bio->bi_private = &complete;
@@ -1196,6 +1216,12 @@ static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
11961216

11971217
/* this will also unplug the queue */
11981218
wait_for_completion(&complete);
1219+
if (!bio_flagged(bio, BIO_UPTODATE)) {
1220+
btrfs_dev_stat_inc_and_print(page_bad->dev,
1221+
BTRFS_DEV_STAT_WRITE_ERRS);
1222+
bio_put(bio);
1223+
return -EIO;
1224+
}
11991225
bio_put(bio);
12001226
}
12011227

@@ -1352,7 +1378,8 @@ static int scrub_checksum_super(struct scrub_block *sblock)
13521378
u64 mapped_size;
13531379
void *p;
13541380
u32 crc = ~(u32)0;
1355-
int fail = 0;
1381+
int fail_gen = 0;
1382+
int fail_cor = 0;
13561383
u64 len;
13571384
int index;
13581385

@@ -1363,13 +1390,13 @@ static int scrub_checksum_super(struct scrub_block *sblock)
13631390
memcpy(on_disk_csum, s->csum, sdev->csum_size);
13641391

13651392
if (sblock->pagev[0].logical != le64_to_cpu(s->bytenr))
1366-
++fail;
1393+
++fail_cor;
13671394

13681395
if (sblock->pagev[0].generation != le64_to_cpu(s->generation))
1369-
++fail;
1396+
++fail_gen;
13701397

13711398
if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1372-
++fail;
1399+
++fail_cor;
13731400

13741401
len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
13751402
mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
@@ -1394,9 +1421,9 @@ static int scrub_checksum_super(struct scrub_block *sblock)
13941421

13951422
btrfs_csum_final(crc, calculated_csum);
13961423
if (memcmp(calculated_csum, on_disk_csum, sdev->csum_size))
1397-
++fail;
1424+
++fail_cor;
13981425

1399-
if (fail) {
1426+
if (fail_cor + fail_gen) {
14001427
/*
14011428
* if we find an error in a super block, we just report it.
14021429
* They will get written with the next transaction commit
@@ -1405,9 +1432,15 @@ static int scrub_checksum_super(struct scrub_block *sblock)
14051432
spin_lock(&sdev->stat_lock);
14061433
++sdev->stat.super_errors;
14071434
spin_unlock(&sdev->stat_lock);
1435+
if (fail_cor)
1436+
btrfs_dev_stat_inc_and_print(sdev->dev,
1437+
BTRFS_DEV_STAT_CORRUPTION_ERRS);
1438+
else
1439+
btrfs_dev_stat_inc_and_print(sdev->dev,
1440+
BTRFS_DEV_STAT_GENERATION_ERRS);
14081441
}
14091442

1410-
return fail;
1443+
return fail_cor + fail_gen;
14111444
}
14121445

14131446
static void scrub_block_get(struct scrub_block *sblock)
@@ -1551,7 +1584,7 @@ static int scrub_pages(struct scrub_dev *sdev, u64 logical, u64 len,
15511584
return -ENOMEM;
15521585
}
15531586
spage->sblock = sblock;
1554-
spage->bdev = sdev->dev->bdev;
1587+
spage->dev = sdev->dev;
15551588
spage->flags = flags;
15561589
spage->generation = gen;
15571590
spage->logical = logical;

0 commit comments

Comments
 (0)