Skip to content

Commit 4ee3fad

Browse files
fdmananakdave
authored andcommitted
Btrfs: fix fsync after hole punching when using no-holes feature
When we have the no-holes mode enabled and fsync a file after punching a hole in it, we can end up not logging the whole hole range in the log tree. This happens if the file has extent items that span more than one leaf and we punch a hole that covers a range that starts in a leaf but does not go beyond the offset of the first extent in the next leaf. Example: $ mkfs.btrfs -f -O no-holes -n 65536 /dev/sdb $ mount /dev/sdb /mnt $ for ((i = 0; i <= 831; i++)); do offset=$((i * 2 * 256 * 1024)) xfs_io -f -c "pwrite -S 0xab -b 256K $offset 256K" \ /mnt/foobar >/dev/null done $ sync # We now have 2 leafs in our filesystem fs tree, the first leaf has an # item corresponding the extent at file offset 216530944 and the second # leaf has a first item corresponding to the extent at offset 217055232. # Now we punch a hole that partially covers the range of the extent at # offset 216530944 but does go beyond the offset 217055232. $ xfs_io -c "fpunch $((216530944 + 128 * 1024 - 4000)) 256K" /mnt/foobar $ xfs_io -c "fsync" /mnt/foobar <power fail> # mount to replay the log $ mount /dev/sdb /mnt # Before this patch, only the subrange [216658016, 216662016[ (length of # 4000 bytes) was logged, leaving an incorrect file layout after log # replay. Fix this by checking if there is a hole between the last extent item that we processed and the first extent item in the next leaf, and if there is one, log an explicit hole extent item. Fixes: 16e7549 ("Btrfs: incompatible format change to remove hole extents") Signed-off-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent a1840b5 commit 4ee3fad

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

fs/btrfs/tree-log.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4002,6 +4002,36 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
40024002
break;
40034003
*last_extent = extent_end;
40044004
}
4005+
4006+
/*
4007+
* Check if there is a hole between the last extent found in our leaf
4008+
* and the first extent in the next leaf. If there is one, we need to
4009+
* log an explicit hole so that at replay time we can punch the hole.
4010+
*/
4011+
if (ret == 0 &&
4012+
key.objectid == btrfs_ino(inode) &&
4013+
key.type == BTRFS_EXTENT_DATA_KEY &&
4014+
i == btrfs_header_nritems(src_path->nodes[0])) {
4015+
ret = btrfs_next_leaf(inode->root, src_path);
4016+
need_find_last_extent = true;
4017+
if (ret > 0) {
4018+
ret = 0;
4019+
} else if (ret == 0) {
4020+
btrfs_item_key_to_cpu(src_path->nodes[0], &key,
4021+
src_path->slots[0]);
4022+
if (key.objectid == btrfs_ino(inode) &&
4023+
key.type == BTRFS_EXTENT_DATA_KEY &&
4024+
*last_extent < key.offset) {
4025+
const u64 len = key.offset - *last_extent;
4026+
4027+
ret = btrfs_insert_file_extent(trans, log,
4028+
btrfs_ino(inode),
4029+
*last_extent, 0,
4030+
0, len, 0, len,
4031+
0, 0, 0);
4032+
}
4033+
}
4034+
}
40054035
/*
40064036
* Need to let the callers know we dropped the path so they should
40074037
* re-search.

0 commit comments

Comments
 (0)