Commit c36047d7 authored by Yan Zheng's avatar Yan Zheng Committed by Chris Mason

Btrfs: Fix race in btrfs_mark_extent_written

When extent needs to be split, btrfs_mark_extent_written truncates the extent
first, then inserts a new extent and increases the reference count.

The race happens if someone else deletes the old extent before the new extent
is inserted. The fix here is increase the reference count in advance. This race
is similar to the race in btrfs_drop_extents that was recently fixed.
Signed-off-by: default avatarYan Zheng <zheng.yan@oracle.com>
parent 2b82032c
...@@ -746,6 +746,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, ...@@ -746,6 +746,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
u64 other_end; u64 other_end;
u64 split = start; u64 split = start;
u64 locked_end = end; u64 locked_end = end;
u64 orig_parent;
int extent_type; int extent_type;
int split_end = 1; int split_end = 1;
int ret; int ret;
...@@ -890,6 +891,12 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, ...@@ -890,6 +891,12 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
} }
btrfs_mark_buffer_dirty(leaf); btrfs_mark_buffer_dirty(leaf);
orig_parent = leaf->start;
ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes,
orig_parent, root->root_key.objectid,
trans->transid, inode->i_ino);
BUG_ON(ret);
btrfs_release_path(root, path); btrfs_release_path(root, path);
key.offset = start; key.offset = start;
...@@ -910,10 +917,13 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, ...@@ -910,10 +917,13 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
btrfs_set_file_extent_encryption(leaf, fi, 0); btrfs_set_file_extent_encryption(leaf, fi, 0);
btrfs_set_file_extent_other_encoding(leaf, fi, 0); btrfs_set_file_extent_other_encoding(leaf, fi, 0);
ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, if (orig_parent != leaf->start) {
leaf->start, root->root_key.objectid, ret = btrfs_update_extent_ref(trans, root, bytenr,
trans->transid, inode->i_ino); orig_parent, leaf->start,
BUG_ON(ret); root->root_key.objectid,
trans->transid, inode->i_ino);
BUG_ON(ret);
}
done: done:
btrfs_mark_buffer_dirty(leaf); btrfs_mark_buffer_dirty(leaf);
btrfs_release_path(root, path); btrfs_release_path(root, path);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment