]> git.hungrycats.org Git - linux/commitdiff
btrfs: Avoid truncate tailing page if fallocate range doesn't exceed inode size
authorQu Wenruo <quwenruo@cn.fujitsu.com>
Wed, 14 Oct 2015 07:26:13 +0000 (15:26 +0800)
committerZygo Blaxell <zblaxell@serenity.furryterror.org>
Wed, 28 Oct 2015 01:04:54 +0000 (21:04 -0400)
Current code will always truncate tailing page if its alloc_start is
smaller than inode size.

For example, the file extent layout is like:
0 4K 8K 16K 32K
|<-----Extent A---------------->|
|<--Inode size: 18K---------->|

But if calling fallocate even for range [0,4K), it will cause btrfs to
re-truncate the range [16,32K), causing COW and a new extent.

0 4K 8K 16K 32K
|///////| <- Fallocate call range
|<-----Extent A-------->|<--B-->|

The cause is quite easy, just a careless btrfs_truncate_inode() in a
else branch without extra judgment.
Fix it by add judgment on whether the fallocate range is beyond isize.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 0f6925fa2907df58496cabc33fa4677c635e2223)
(cherry picked from commit c5d7d0646f446622731e27f3557bef7d1b63afb5)

fs/btrfs/file.c

index c8f49f59814c9da5e2d10f2695d3e9b0527a480f..64a72ee913f4598835722a17344209953f88962c 100644 (file)
@@ -2588,7 +2588,7 @@ static long btrfs_fallocate(struct file *file, int mode,
                                        alloc_start);
                if (ret)
                        goto out;
-       } else {
+       } else if (offset + len > inode->i_size) {
                /*
                 * If we are fallocating from the end of the file onward we
                 * need to zero out the end of the page if i_size lands in the