Btrfs: fix unexpected result when dio reading corrupted blocks
PATCH 1/1 Btrfs: fix unexpected result when dio reading corrupted blocks
commit 4246a0b63bd8 ("block: add a bi_error field to struct bio") changed the logic of how dio read endio reports errors.
For single stripe dio read, %bio->bi_status reflects the error before verifying checksum, and now we're updating it when data block matches with its checksum, while in the mismatching case, %bio->bi_status is not updated to relfect that.
When some blocks in a file have been corrupted on disk, reading such a file ends up with
1) checksum errros are reported in kernel log
2) read(2) returns successfully with some content being 0x01.
In order to fix it, we need to report its checksum mismatch error to the upper layer (dio layer in this case) as well.
--- fs/btrfs/inode.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
fs/btrfs/inode.c +2 -5
@@ -8267,11 +8267,8 @@ static void btrfs_endio_direct_read(struct bio *bio)struct btrfs_io_bio *io_bio =btrfs_io_bio (bio);blk_status_t err = bio->bi_status; -if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) { +if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) err =btrfs_subio_endio_read (inode, io_bio, err); -if (!err) - bio->bi_status =0 ; - }unlock_extent (&BTRFS_I (inode)->io_tree, dip->logical_offset, dip->logical_offset + dip->bytes -1 );
@@ -8279,7 +8276,7 @@ static void btrfs_endio_direct_read(struct bio *bio)kfree (dip); - dio_bio->bi_status = bio->bi_status; + dio_bio->bi_status = err;dio_end_io (dio_bio);if (io_bio->end_io)
Signature · 1 line
1.8.3.1
Quoted 8 lines · Liu Bo
commit
4246a0b63bd8("block: add abi_errorfield to struct bio") changed the logic of how dio read endio reports errors.For single stripe dio read, %bio->bi_status reflects the error before verifying checksum, and now we're updating it when data block matches with its checksum, while in the mismatching case, %bio->bi_status is not updated to relfect that.
Quoted 8 lines
When some blocks in a file have been corrupted on disk, reading such a file ends up with
1) checksum errros are reported in kernel log
2) read(2) returns successfully with some content being0x01.In order to fix it, we need to report its checksum mismatch error to the upper layer (dio layer in this case) as well.
I tested it, and now it works: even using O_DIRECT -EIO is returned if the file is corrupted.
ghigo@venice:~/btrfs/crash-o-direct/t$ ls -li total 16384 257 -rw-r--r-- 1 root root 16777216 Sep 15 20:51 abcd ghigo@venice:~/btrfs/crash-o-direct/t$ date Sat Sep 16 13:56:26 CEST 2017
ghigo@venice:~/btrfs/crash-o-direct/t$ cat abcd cat: abcd: Input/output error ghigo@venice:~/btrfs/crash-o-direct/t$ dmesg -T | tail -1 [Sat Sep 16 13:56:29 2017] BTRFS warning (device sdd5): csum failed root 5 ino 257 off 0 csum 0x98f94189 expected csum 0x0ab6be80 mirror 1
ghigo@venice:~/btrfs/crash-o-direct/t$ dd if=abcd iflag=direct dd: error reading 'abcd': Input/output error 0+0 records in 0+0 records out 0 bytes copied, 0.000404156 s, 0.0 kB/s ghigo@venice:~/btrfs/crash-o-direct/t$ dmesg -T | tail -1 [Sat Sep 16 13:56:41 2017] BTRFS warning (device sdd5): csum failed root 5 ino 257 off 0 csum 0x98f94189 expected csum 0x0ab6be80 mirror 1
Quoted 35 lines
Signed-off-by Liu Bo <[email protected]>Reported-by Goffredo Baroncelli <[email protected]>cc Goffredo Baroncelli <[email protected]>--- fs/btrfs/inode.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)fs/btrfs/inode.c +2
-5@@ -8267,11 +8267,8 @@ static void btrfs_endio_direct_read(struct bio *bio)struct btrfs_io_bio *io_bio =btrfs_io_bio (bio);blk_status_t err = bio->bi_status; -if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) { +if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) err =btrfs_subio_endio_read (inode, io_bio, err); -if (!err) - bio->bi_status =0 ; - }unlock_extent (&BTRFS_I (inode)->io_tree, dip->logical_offset, dip->logical_offset + dip->bytes -1 );@@ -8279,7 +8276,7 @@ static void btrfs_endio_direct_read(struct bio *bio)kfree (dip); - dio_bio->bi_status = bio->bi_status; + dio_bio->bi_status = err;dio_end_io (dio_bio);if (io_bio->end_io)
Signature · 2 lines
gpg @keyserver.linux.it: Goffredo Baroncelli <kreijackATinwind.it> Key fingerprint BBF5 1610 0B64 DAC6 5F7D 17B2 0EDA 9B37 8B82 E0B5
Quoted 31 lines · Goffredo Baroncelli
Quoted 8 lines · Liu Bo
commit
4246a0b63bd8("block: add abi_errorfield to struct bio") changed the logic of how dio read endio reports errors.For single stripe dio read, %bio->bi_status reflects the error before verifying checksum, and now we're updating it when data block matches with its checksum, while in the mismatching case, %bio->bi_status is not updated to relfect that.
Quoted 8 lines
When some blocks in a file have been corrupted on disk, reading such a file ends up with
1) checksum errros are reported in kernel log
2) read(2) returns successfully with some content being0x01.In order to fix it, we need to report its checksum mismatch error to the upper layer (dio layer in this case) as well.
I tested it, and now it works: even using
O_DIRECT-EIOis returned if the file is corrupted.ghigo@venice:~/btrfs/crash-o-direct/t$ ls -li total 16384 257 -rw-r--r-- 1 root root 16777216 Sep 15 20:51 abcd ghigo@venice:~/btrfs/crash-o-direct/t$ date Sat Sep 16 13:56:26 CEST 2017ghigo@venice:~/btrfs/crash-o-direct/t$ cat abcd cat: abcd: Input/output error ghigo@venice:~/btrfs/crash-o-direct/t$ dmesg -T | tail -1 [Sat Sep 16 13:56:29 2017] BTRFS warning (device sdd5): csum failed root 5 ino 257 off 0 csum 0x98f94189 expected csum 0x0ab6be80 mirror 1
Quoted 8 lines
ghigo@venice:~/btrfs/crash-o-direct/t$ dd if=abcd iflag=direct dd: error reading 'abcd': Input/output error 0+0 records in 0+0 records out 0 bytes copied, 0.000404156 s, 0.0 kB/s ghigo@venice:~/btrfs/crash-o-direct/t$ dmesg -T | tail -1 [Sat Sep 16 13:56:41 2017] BTRFS warning (device sdd5): csum failed root 5 ino 257 off 0 csum 0x98f94189 expected csum 0x0ab6be80 mirror 1
Thanks a lot, any chance I can get your 'Tested-by' tag?
Thanks,
-liubo
Quoted 43 lines
Quoted 38 lines
Signed-off-by Liu Bo <[email protected]>Reported-by Goffredo Baroncelli <[email protected]>cc Goffredo Baroncelli <[email protected]>--- fs/btrfs/inode.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)fs/btrfs/inode.c +2
-5@@ -8267,11 +8267,8 @@ static void btrfs_endio_direct_read(struct bio *bio)struct btrfs_io_bio *io_bio =btrfs_io_bio (bio);blk_status_t err = bio->bi_status; -if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) { +if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) err =btrfs_subio_endio_read (inode, io_bio, err); -if (!err) - bio->bi_status =0 ; - }unlock_extent (&BTRFS_I (inode)->io_tree, dip->logical_offset, dip->logical_offset + dip->bytes -1 );@@ -8279,7 +8276,7 @@ static void btrfs_endio_direct_read(struct bio *bio)kfree (dip); - dio_bio->bi_status = bio->bi_status; + dio_bio->bi_status = err;dio_end_io (dio_bio);if (io_bio->end_io)Signature · 2 lines
gpg @keyserver.linux.it: Goffredo Baroncelli <kreijackATinwind.it> Key fingerprint BBF5 1610 0B64 DAC6 5F7D 17B2 0EDA 9B37 8B82 E0B5
Hello, quick question for backporting..
Quoted 2 lines · Liu Bo
commit
4246a0b63bd8("block: add abi_errorfield to struct bio") changed the logic of how dio read endio reports errors.
[snip]
I've tried to merge this into my 4.9.x++ tree but have a question since the DIO APIs changed recently and itt's hard to tell what is a bug and what is a feature.. :-/
Quoted 5 lines
fs/btrfs/inode.c +0
-0@@ -8267,11 +8267,8 @@ static void btrfs_endio_direct_read(struct bio *bio)struct btrfs_io_bio *io_bio =btrfs_io_bio (bio);blk_status_t err = bio->bi_status;
fs/btrfs/inode.c static void btrfs_endio_direct_read(struct bio *bio) · line 8274
Quoted 10 lines
fs/btrfs/inode.c +1
-4-if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) { +if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) err =btrfs_subio_endio_read (inode, io_bio, err); -if (!err) - bio->bi_status =0 ; - }unlock_extent (&BTRFS_I (inode)->io_tree, dip->logical_offset, dip->logical_offset + dip->bytes -1 );
This hunk is fairly easy, just reverse bi_status to bi->error. However..
fs/btrfs/inode.c static void btrfs_endio_direct_read(struct bio *bio) · line 8280
Quoted 7 lines
fs/btrfs/inode.c +1
-1@@ -8279,7 +8276,7 @@ static void btrfs_endio_direct_read(struct bio *bio)kfree (dip); - dio_bio->bi_status = bio->bi_status; + dio_bio->bi_status = err;dio_end_io (dio_bio);
^^^^^^^^^^^^^^^^^^^
Same here, except that the call to dio_end_io used to take a second parameter (the error code, which has been moved into bi_status in 4.10+) and looked like this:
dio_end_io(dio_bio, bio->bi_error);
Given that "bio->bi_error" should have been "err" instead, I think err should also be passed to dio_end_io(), so that the whole hunk would look like:
..
- dio_bio->bi_error = bio->bi_error; - dio_end_io(dio_bio, bio->bi_error); + dio_bio->bi_error = err; + dio_end_io(dio_bio, err);
..
Would this be correct or did I misunderstand some subtle aspect about the DIO error handling?
Thanks :)
Holger
Quoted 12 lines · Holger Hoffstätte
Hello, quick question for backporting..
Quoted 2 lines · Liu Bo
commit
4246a0b63bd8("block: add abi_errorfield to struct bio") changed the logic of how dio read endio reports errors.[snip]
I've tried to merge this into my 4.9.x++ tree but have a question since the DIO APIs changed recently and itt's hard to tell what is a bug and what is a feature.. :-/
Good point, hopefully it's the last change on those API.
Quoted 44 lines
Quoted 5 lines
fs/btrfs/inode.c +0
-0@@ -8267,11 +8267,8 @@ static void btrfs_endio_direct_read(struct bio *bio)struct btrfs_io_bio *io_bio =btrfs_io_bio (bio);blk_status_t err = bio->bi_status;fs/btrfs/inode.c static void btrfs_endio_direct_read(struct bio *bio) · line 8274
Quoted 10 lines
fs/btrfs/inode.c +1
-4-if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) { +if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) err =btrfs_subio_endio_read (inode, io_bio, err); -if (!err) - bio->bi_status =0 ; - }unlock_extent (&BTRFS_I (inode)->io_tree, dip->logical_offset, dip->logical_offset + dip->bytes -1 );This hunk is fairly easy, just reverse
bi_statustobi->error. However..fs/btrfs/inode.c static void btrfs_endio_direct_read(struct bio *bio) · line 8280
Quoted 7 lines
fs/btrfs/inode.c +1
-1@@ -8279,7 +8276,7 @@ static void btrfs_endio_direct_read(struct bio *bio)kfree (dip); - dio_bio->bi_status = bio->bi_status; + dio_bio->bi_status = err;dio_end_io (dio_bio);^^^^^^^^^^^^^^^^^^^Same here, except that the call to
dio_end_ioused to take a second parameter (the error code, which has been moved intobi_statusin 4.10+) and looked like this:dio_end_io(dio_bio, bio->bi_error);Given that "
bio->bi_error" should have been "err" instead, I think err should also be passed todio_end_io(), so that the whole hunk would look like:..
(quoted diff) +2
-2- dio_bio->bi_error = bio->bi_error; - dio_end_io(dio_bio, bio->bi_error); + dio_bio->bi_error = err; + dio_end_io(dio_bio, err);
Quoted 2 lines
Would this be correct or did I misunderstand some subtle aspect about the DIO error handling?
Yes, the diff looks good to me.
Thanks,
-liubo
Quoted 14 lines · Liu Bo
commit
4246a0b63bd8("block: add abi_errorfield to struct bio") changed the logic of how dio read endio reports errors.For single stripe dio read, %bio->bi_status reflects the error before verifying checksum, and now we're updating it when data block matches with its checksum, while in the mismatching case, %bio->bi_status is not updated to relfect that.
When some blocks in a file have been corrupted on disk, reading such a file ends up with
1) checksum errros are reported in kernel log
2) read(2) returns successfully with some content being0x01.
Quoted 6 lines
In order to fix it, we need to report its checksum mismatch error to the upper layer (dio layer in this case) as well.
Signed-off-by Liu Bo <[email protected]>Reported-by Goffredo Baroncelli <[email protected]>cc Goffredo Baroncelli <[email protected]>
Quoted 31 lines
--- fs/btrfs/inode.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)fs/btrfs/inode.c +2
-5@@ -8267,11 +8267,8 @@ static void btrfs_endio_direct_read(struct bio *bio)struct btrfs_io_bio *io_bio =btrfs_io_bio (bio);blk_status_t err = bio->bi_status; -if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) { +if (dip->flags &BTRFS_DIO_ORIG_BIO_SUBMITTED ) err =btrfs_subio_endio_read (inode, io_bio, err); -if (!err) - bio->bi_status =0 ; - }unlock_extent (&BTRFS_I (inode)->io_tree, dip->logical_offset, dip->logical_offset + dip->bytes -1 );@@ -8279,7 +8276,7 @@ static void btrfs_endio_direct_read(struct bio *bio)kfree (dip); - dio_bio->bi_status = bio->bi_status; + dio_bio->bi_status = err;dio_end_io (dio_bio);if (io_bio->end_io)
Signature · 2 lines
gpg @keyserver.linux.it: Goffredo Baroncelli <kreijackATinwind.it> Key fingerprint BBF5 1610 0B64 DAC6 5F7D 17B2 0EDA 9B37 8B82 E0B5
Quoted 14 lines · Liu Bo
commit
4246a0b63bd8("block: add abi_errorfield to struct bio") changed the logic of how dio read endio reports errors.For single stripe dio read, %bio->bi_status reflects the error before verifying checksum, and now we're updating it when data block matches with its checksum, while in the mismatching case, %bio->bi_status is not updated to relfect that.
When some blocks in a file have been corrupted on disk, reading such a file ends up with
1) checksum errros are reported in kernel log
2) read(2) returns successfully with some content being0x01.
Quoted 6 lines
In order to fix it, we need to report its checksum mismatch error to the upper layer (dio layer in this case) as well.
Signed-off-by Liu Bo <[email protected]>Reported-by Goffredo Baroncelli <[email protected]>cc Goffredo Baroncelli <[email protected]>