Skip to content

Commit d9ba850

Browse files
adam900710kdave
authored andcommitted
btrfs: fix beyond-EOF write handling
[BUG] For the following write sequence with 64K page size and 4K fs block size, it will lead to file extent items to be inserted without any data checksum: mkfs.btrfs -s 4k -f $dev > /dev/null mount $dev $mnt xfs_io -f -c "pwrite 0 16k" -c "pwrite 32k 4k" -c pwrite "60k 64K" \ -c "truncate 16k" $mnt/foobar umount $mnt This will result the following 2 file extent items to be inserted (extra trace point added to insert_ordered_extent_file_extent()): btrfs_finish_one_ordered: root=5 ino=257 file_off=61440 num_bytes=4096 csum_bytes=0 btrfs_finish_one_ordered: root=5 ino=257 file_off=0 num_bytes=16384 csum_bytes=16384 Note for file offset 60K, we're inserting a file extent without any data checksum. Also note that range [32K, 36K) didn't reach insert_ordered_extent_file_extent(), which is the correct behavior as that OE is fully truncated, should not result any file extent. Although file extent at 60K will be later dropped by btrfs_truncate(), if the transaction got committed after file extent inserted but before the file extent dropping, we will have a small window where we have a file extent beyond EOF and without any data checksum. That will cause "btrfs check" to report error. [CAUSE] The sequence happens like this: - Buffered write dirtied the page cache and updated isize Now the inode size is 64K, with the following page cache layout: 0 16K 32K 48K 64K |/////////////| |//| |//| - Truncate the inode to 16K Which will trigger writeback through: btrfs_setsize() |- truncate_setsize() | Now the inode size is set to 16K | |- btrfs_truncate() |- btrfs_wait_ordered_range() for [16K, u64(-1)] |- btrfs_fdatawrite_range() for [16K, u64(-1)} |- extent_writepage() for folio 0 |- writepage_delalloc() | Generated OE for [0, 16K), [32K, 36K] and [60K, 64K) | |- extent_writepage_io() Then inside extent_writepage_io(), the dirty fs blocks are handled differently: - Submit write for range [0, 16K) As they are still inside the inode size (16K). - Mark OE [32K, 36K) as truncated Since we only call btrfs_lookup_first_ordered_range() once, which returned the first OE after file offset 16K. - Mark all OEs inside range [16K, 64K) as finished Which will mark OE ranges [32K, 36K) and [60K, 64K) as finished. For OE [32K, 36K) since it's already marked as truncated, and its truncated length is 0, no file extent will be inserted. For OE [60K, 64K) it has never been submitted thus has no data checksum, and we insert the file extent as usual. This is the root cause of file extent at 60K to be inserted without any data checksum. - Clear dirty flags for range [16K, 64K) It is the function btrfs_folio_clear_dirty() which searches and clears any dirty blocks inside that range. [FIX] The bug itself was introduced a long time ago, way before subpage and large folio support. At that time, fs block size must match page size, thus the range [cur, end) is just one fs block. But later with subpage and large folios, the same range [cur, end) can have multiple blocks and ordered extents. Later commit 18de34d ("btrfs: truncate ordered extent when skipping writeback past i_size") was fixing a bug related to subpage/large folios, but it's still utilizing the old range [cur, end), meaning only the first OE will be marked as truncated. The proper fix here is to make EOF handling block-by-block, not trying to handle the whole range to @EnD. By this we always locate and truncate the OE for every dirty block. CC: stable@vger.kernel.org # 5.15+ Reviewed-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 0764470 commit d9ba850

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

fs/btrfs/extent_io.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,7 @@ static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode,
17411741
struct btrfs_ordered_extent *ordered;
17421742

17431743
ordered = btrfs_lookup_first_ordered_range(inode, cur,
1744-
folio_end - cur);
1744+
fs_info->sectorsize);
17451745
/*
17461746
* We have just run delalloc before getting here, so
17471747
* there must be an ordered extent.
@@ -1755,7 +1755,7 @@ static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode,
17551755
btrfs_put_ordered_extent(ordered);
17561756

17571757
btrfs_mark_ordered_io_finished(inode, folio, cur,
1758-
end - cur, true);
1758+
fs_info->sectorsize, true);
17591759
/*
17601760
* This range is beyond i_size, thus we don't need to
17611761
* bother writing back.
@@ -1764,8 +1764,8 @@ static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode,
17641764
* writeback the sectors with subpage dirty bits,
17651765
* causing writeback without ordered extent.
17661766
*/
1767-
btrfs_folio_clear_dirty(fs_info, folio, cur, end - cur);
1768-
break;
1767+
btrfs_folio_clear_dirty(fs_info, folio, cur, fs_info->sectorsize);
1768+
continue;
17691769
}
17701770
ret = submit_one_sector(inode, folio, cur, bio_ctrl, i_size);
17711771
if (unlikely(ret < 0)) {

0 commit comments

Comments
 (0)