Skip to content

Commit 6fe189d

Browse files
adam900710kdave
authored andcommitted
btrfs: handle aligned EOF truncation correctly for subpage cases
[BUG] For the following fsx -e 1 run, the btrfs still fails the run on 64K page size with 4K fs block size: READ BAD DATA: offset = 0x26b3a, size = 0xfafa, fname = /mnt/btrfs/junk OFFSET GOOD BAD RANGE 0x26b3a 0x0000 0x15b4 0x0 operation# (mod 256) for the bad data may be 21 [...] LOG DUMP (28 total operations): 1( 1 mod 256): SKIPPED (no operation) 2( 2 mod 256): SKIPPED (no operation) 3( 3 mod 256): SKIPPED (no operation) 4( 4 mod 256): SKIPPED (no operation) 5( 5 mod 256): WRITE 0x1ea90 thru 0x285e0 (0x9b51 bytes) HOLE 6( 6 mod 256): ZERO 0x1b1a8 thru 0x20bd4 (0x5a2d bytes) 7( 7 mod 256): FALLOC 0x22b1a thru 0x272fa (0x47e0 bytes) INTERIOR 8( 8 mod 256): WRITE 0x741d thru 0x13522 (0xc106 bytes) 9( 9 mod 256): MAPWRITE 0x73ee thru 0xdeeb (0x6afe bytes) 10( 10 mod 256): FALLOC 0xb719 thru 0xb994 (0x27b bytes) INTERIOR 11( 11 mod 256): COPY 0x15ed8 thru 0x18be1 (0x2d0a bytes) to 0x25f6e thru 0x28c77 12( 12 mod 256): ZERO 0x1615e thru 0x1770e (0x15b1 bytes) 13( 13 mod 256): SKIPPED (no operation) 14( 14 mod 256): DEDUPE 0x20000 thru 0x27fff (0x8000 bytes) to 0x1000 thru 0x8fff 15( 15 mod 256): SKIPPED (no operation) 16( 16 mod 256): CLONE 0xa000 thru 0xffff (0x6000 bytes) to 0x36000 thru 0x3bfff 17( 17 mod 256): ZERO 0x14adc thru 0x1b78a (0x6caf bytes) 18( 18 mod 256): TRUNCATE DOWN from 0x3c000 to 0x1e2e3 ******WWWW 19( 19 mod 256): CLONE 0x4000 thru 0x11fff (0xe000 bytes) to 0x16000 thru 0x23fff 20( 20 mod 256): FALLOC 0x311e1 thru 0x3681b (0x563a bytes) PAST_EOF 21( 21 mod 256): FALLOC 0x351c5 thru 0x40000 (0xae3b bytes) EXTENDING 22( 22 mod 256): WRITE 0x920 thru 0x7e51 (0x7532 bytes) 23( 23 mod 256): COPY 0x2b58 thru 0xc508 (0x99b1 bytes) to 0x117b1 thru 0x1b161 24( 24 mod 256): TRUNCATE DOWN from 0x40000 to 0x3c9a5 25( 25 mod 256): SKIPPED (no operation) 26( 26 mod 256): MAPWRITE 0x25020 thru 0x26b06 (0x1ae7 bytes) 27( 27 mod 256): SKIPPED (no operation) 28( 28 mod 256): READ 0x26b3a thru 0x36633 (0xfafa bytes) ***RRRR*** [CAUSE] The involved operations are: fallocating to largest ever: 0x40000 21 pollute_eof 0x24000 thru 0x2ffff (0xc000 bytes) 21 falloc from 0x351c5 to 0x40000 (0xae3b bytes) 28 read 0x26b3a thru 0x36633 (0xfafa bytes) At operation torvalds#21 a pollute_eof is done, by memory mappaed write into range [0x24000, 0x2ffff). At this stage, the inode size is 0x24000, which is block aligned. Then fallocate happens, and since it's expanding the inode, it will call btrfs_truncate_block() to truncate any unaligned range. But since the inode size is already block aligned, btrfs_truncate_block() does nothing and exit. However remember the folio at 0x20000 has some range polluted already, although they will not be written back to disk, it still affects the page cache, resulting the later operation torvalds#28 to read out the polluted value. [FIX] Instead of early exit from btrfs_truncate_block() if the range is already block aligned, do extra filio zeroing if the fs block size is smaller than the page size and we're truncating beyond EOF. This is to address exactly the above case where memory mapped write can still leave some garbage beyond EOF. Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent d5a1047 commit 6fe189d

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

fs/btrfs/inode.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4778,6 +4778,52 @@ static bool is_inside_block(u64 bytenr, u64 blockstart, u32 blocksize)
47784778
return false;
47794779
}
47804780

4781+
static int truncate_block_zero_beyond_eof(struct btrfs_inode *inode, u64 start)
4782+
{
4783+
const pgoff_t index = start >> PAGE_SHIFT;
4784+
struct address_space *mapping = inode->vfs_inode.i_mapping;
4785+
struct folio *folio;
4786+
u64 zero_start;
4787+
u64 zero_end;
4788+
int ret = 0;
4789+
4790+
again:
4791+
folio = filemap_lock_folio(mapping, index);
4792+
/* No folio present. */
4793+
if (IS_ERR(folio))
4794+
return 0;
4795+
4796+
if (!folio_test_uptodate(folio)) {
4797+
ret = btrfs_read_folio(NULL, folio);
4798+
folio_lock(folio);
4799+
if (folio->mapping != mapping) {
4800+
folio_unlock(folio);
4801+
folio_put(folio);
4802+
goto again;
4803+
}
4804+
if (!folio_test_uptodate(folio)) {
4805+
ret = -EIO;
4806+
goto out_unlock;
4807+
}
4808+
}
4809+
folio_wait_writeback(folio);
4810+
4811+
/*
4812+
* We do not need to lock extents nor wait for OE, as it's already
4813+
* beyond EOF.
4814+
*/
4815+
4816+
zero_start = max_t(u64, folio_pos(folio), start);
4817+
zero_end = folio_pos(folio) + folio_size(folio) - 1;
4818+
folio_zero_range(folio, zero_start - folio_pos(folio),
4819+
zero_end - zero_start + 1);
4820+
4821+
out_unlock:
4822+
folio_unlock(folio);
4823+
folio_put(folio);
4824+
return ret;
4825+
}
4826+
47814827
/*
47824828
* Handle the truncation of a fs block.
47834829
*
@@ -4824,8 +4870,15 @@ int btrfs_truncate_block(struct btrfs_inode *inode, u64 from, u64 start, u64 end
48244870
from, start, end);
48254871

48264872
/* The range is aligned at both ends. */
4827-
if (IS_ALIGNED(start, blocksize) && IS_ALIGNED(end + 1, blocksize))
4873+
if (IS_ALIGNED(start, blocksize) && IS_ALIGNED(end + 1, blocksize)) {
4874+
/*
4875+
* For block size < page size case, we may have polluted blocks
4876+
* beyond EOF. So we also need to zero them out.
4877+
*/
4878+
if (end == (u64)-1 && blocksize < PAGE_SIZE)
4879+
ret = truncate_block_zero_beyond_eof(inode, start);
48284880
goto out;
4881+
}
48294882

48304883
/*
48314884
* @from may not be inside the head nor tail block. In that case

0 commit comments

Comments
 (0)