Skip to content

Commit 03018e5

Browse files
fdmananakdave
authored andcommitted
btrfs: fix swap file activation failure due to extents that used to be shared
When activating a swap file, to determine if an extent is shared we use can_nocow_extent(), which ends up at btrfs_cross_ref_exist(). That helper is meant to be quick because it's used in the NOCOW write path, when flushing delalloc and when doing a direct IO write, however it does return some false positives, meaning it may indicate that an extent is shared even if it's no longer the case. For the write path this is fine, we just do a unnecessary COW operation instead of doing a more rigorous check which would be too heavy (calling btrfs_is_data_extent_shared()). However when activating a swap file, the false positives simply result in a failure, which is confusing for users/applications. One particular case where this happens is when a data extent only has 1 reference but that reference is not inlined in the extent item located in the extent tree - this happens when we create more than 33 references for an extent and then delete those 33 references plus every other non-inline reference except one. The function check_committed_ref() assumes that if the size of an extent item doesn't match the size of struct btrfs_extent_item plus the size of an inline reference (plus an owner reference in case simple quotas are enabled), then the extent is shared - that is not the case however, we can have a single reference but it's not inlined - the reason we do this is to be fast and avoid inspecting non-inline references which may be located in another leaf of the extent tree, slowing down write paths. The following test script reproduces the bug: $ cat test.sh #!/bin/bash DEV=/dev/sdi MNT=/mnt/sdi NUM_CLONES=50 umount $DEV &> /dev/null run_test() { local sync_after_add_reflinks=$1 local sync_after_remove_reflinks=$2 mkfs.btrfs -f $DEV > /dev/null #mkfs.xfs -f $DEV > /dev/null mount $DEV $MNT touch $MNT/foo chmod 0600 $MNT/foo # On btrfs the file must be NOCOW. chattr +C $MNT/foo &> /dev/null xfs_io -s -c "pwrite -b 1M 0 1M" $MNT/foo mkswap $MNT/foo for ((i = 1; i <= $NUM_CLONES; i++)); do touch $MNT/foo_clone_$i chmod 0600 $MNT/foo_clone_$i # On btrfs the file must be NOCOW. chattr +C $MNT/foo_clone_$i &> /dev/null cp --reflink=always $MNT/foo $MNT/foo_clone_$i done if [ $sync_after_add_reflinks -ne 0 ]; then # Flush delayed refs and commit current transaction. sync -f $MNT fi # Remove the original file and all clones except the last. rm -f $MNT/foo for ((i = 1; i < $NUM_CLONES; i++)); do rm -f $MNT/foo_clone_$i done if [ $sync_after_remove_reflinks -ne 0 ]; then # Flush delayed refs and commit current transaction. sync -f $MNT fi # Now use the last clone as a swap file. It should work since # its extent are not shared anymore. swapon $MNT/foo_clone_${NUM_CLONES} swapoff $MNT/foo_clone_${NUM_CLONES} umount $MNT } echo -e "\nTest without sync after creating and removing clones" run_test 0 0 echo -e "\nTest with sync after creating clones" run_test 1 0 echo -e "\nTest with sync after removing clones" run_test 0 1 echo -e "\nTest with sync after creating and removing clones" run_test 1 1 Running the test: $ ./test.sh Test without sync after creating and removing clones wrote 1048576/1048576 bytes at offset 0 1 MiB, 1 ops; 0.0017 sec (556.793 MiB/sec and 556.7929 ops/sec) Setting up swapspace version 1, size = 1020 KiB (1044480 bytes) no label, UUID=a6b9c29e-5ef4-4689-a8ac-bc199c750f02 swapon: /mnt/sdi/foo_clone_50: swapon failed: Invalid argument swapoff: /mnt/sdi/foo_clone_50: swapoff failed: Invalid argument Test with sync after creating clones wrote 1048576/1048576 bytes at offset 0 1 MiB, 1 ops; 0.0036 sec (271.739 MiB/sec and 271.7391 ops/sec) Setting up swapspace version 1, size = 1020 KiB (1044480 bytes) no label, UUID=5e9008d6-1f7a-4948-a1b4-3f30aba20a33 swapon: /mnt/sdi/foo_clone_50: swapon failed: Invalid argument swapoff: /mnt/sdi/foo_clone_50: swapoff failed: Invalid argument Test with sync after removing clones wrote 1048576/1048576 bytes at offset 0 1 MiB, 1 ops; 0.0103 sec (96.665 MiB/sec and 96.6651 ops/sec) Setting up swapspace version 1, size = 1020 KiB (1044480 bytes) no label, UUID=916c2740-fa9f-4385-9f06-29c3f89e4764 Test with sync after creating and removing clones wrote 1048576/1048576 bytes at offset 0 1 MiB, 1 ops; 0.0031 sec (314.268 MiB/sec and 314.2678 ops/sec) Setting up swapspace version 1, size = 1020 KiB (1044480 bytes) no label, UUID=06aab1dd-4d90-49c0-bd9f-3a8db4e2f912 swapon: /mnt/sdi/foo_clone_50: swapon failed: Invalid argument swapoff: /mnt/sdi/foo_clone_50: swapoff failed: Invalid argument Fix this by reworking btrfs_swap_activate() to instead of using extent maps and checking for shared extents with can_nocow_extent(), iterate over the inode's file extent items and use the accurate btrfs_is_data_extent_shared(). CC: stable@vger.kernel.org # 5.4+ Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 0525064 commit 03018e5

File tree

1 file changed

+69
-27
lines changed

1 file changed

+69
-27
lines changed

fs/btrfs/inode.c

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9799,15 +9799,16 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
97999799
struct btrfs_fs_info *fs_info = root->fs_info;
98009800
struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
98019801
struct extent_state *cached_state = NULL;
9802-
struct extent_map *em = NULL;
98039802
struct btrfs_chunk_map *map = NULL;
98049803
struct btrfs_device *device = NULL;
98059804
struct btrfs_swap_info bsi = {
98069805
.lowest_ppage = (sector_t)-1ULL,
98079806
};
9807+
struct btrfs_backref_share_check_ctx *backref_ctx = NULL;
9808+
struct btrfs_path *path = NULL;
98089809
int ret = 0;
98099810
u64 isize;
9810-
u64 start;
9811+
u64 prev_extent_end = 0;
98119812

98129813
/*
98139814
* Acquire the inode's mmap lock to prevent races with memory mapped
@@ -9846,6 +9847,13 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
98469847
goto out_unlock_mmap;
98479848
}
98489849

9850+
path = btrfs_alloc_path();
9851+
backref_ctx = btrfs_alloc_backref_share_check_ctx();
9852+
if (!path || !backref_ctx) {
9853+
ret = -ENOMEM;
9854+
goto out_unlock_mmap;
9855+
}
9856+
98499857
/*
98509858
* Balance or device remove/replace/resize can move stuff around from
98519859
* under us. The exclop protection makes sure they aren't running/won't
@@ -9904,24 +9912,39 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
99049912
isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
99059913

99069914
lock_extent(io_tree, 0, isize - 1, &cached_state);
9907-
start = 0;
9908-
while (start < isize) {
9909-
u64 logical_block_start, physical_block_start;
9915+
while (prev_extent_end < isize) {
9916+
struct btrfs_key key;
9917+
struct extent_buffer *leaf;
9918+
struct btrfs_file_extent_item *ei;
99109919
struct btrfs_block_group *bg;
9911-
u64 len = isize - start;
9920+
u64 logical_block_start;
9921+
u64 physical_block_start;
9922+
u64 extent_gen;
9923+
u64 disk_bytenr;
9924+
u64 len;
99129925

9913-
em = btrfs_get_extent(BTRFS_I(inode), NULL, start, len);
9914-
if (IS_ERR(em)) {
9915-
ret = PTR_ERR(em);
9926+
key.objectid = btrfs_ino(BTRFS_I(inode));
9927+
key.type = BTRFS_EXTENT_DATA_KEY;
9928+
key.offset = prev_extent_end;
9929+
9930+
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
9931+
if (ret < 0)
99169932
goto out;
9917-
}
99189933

9919-
if (em->disk_bytenr == EXTENT_MAP_HOLE) {
9934+
/*
9935+
* If key not found it means we have an implicit hole (NO_HOLES
9936+
* is enabled).
9937+
*/
9938+
if (ret > 0) {
99209939
btrfs_warn(fs_info, "swapfile must not have holes");
99219940
ret = -EINVAL;
99229941
goto out;
99239942
}
9924-
if (em->disk_bytenr == EXTENT_MAP_INLINE) {
9943+
9944+
leaf = path->nodes[0];
9945+
ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
9946+
9947+
if (btrfs_file_extent_type(leaf, ei) == BTRFS_FILE_EXTENT_INLINE) {
99259948
/*
99269949
* It's unlikely we'll ever actually find ourselves
99279950
* here, as a file small enough to fit inline won't be
@@ -9933,23 +9956,45 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
99339956
ret = -EINVAL;
99349957
goto out;
99359958
}
9936-
if (extent_map_is_compressed(em)) {
9959+
9960+
if (btrfs_file_extent_compression(leaf, ei) != BTRFS_COMPRESS_NONE) {
99379961
btrfs_warn(fs_info, "swapfile must not be compressed");
99389962
ret = -EINVAL;
99399963
goto out;
99409964
}
99419965

9942-
logical_block_start = extent_map_block_start(em) + (start - em->start);
9943-
len = min(len, em->len - (start - em->start));
9944-
free_extent_map(em);
9945-
em = NULL;
9966+
disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei);
9967+
if (disk_bytenr == 0) {
9968+
btrfs_warn(fs_info, "swapfile must not have holes");
9969+
ret = -EINVAL;
9970+
goto out;
9971+
}
9972+
9973+
logical_block_start = disk_bytenr + btrfs_file_extent_offset(leaf, ei);
9974+
extent_gen = btrfs_file_extent_generation(leaf, ei);
9975+
prev_extent_end = btrfs_file_extent_end(path);
9976+
9977+
if (prev_extent_end > isize)
9978+
len = isize - key.offset;
9979+
else
9980+
len = btrfs_file_extent_num_bytes(leaf, ei);
99469981

9947-
ret = can_nocow_extent(inode, start, &len, NULL, false, true);
9982+
backref_ctx->curr_leaf_bytenr = leaf->start;
9983+
9984+
/*
9985+
* Don't need the path anymore, release to avoid deadlocks when
9986+
* calling btrfs_is_data_extent_shared() because when joining a
9987+
* transaction it can block waiting for the current one's commit
9988+
* which in turn may be trying to lock the same leaf to flush
9989+
* delayed items for example.
9990+
*/
9991+
btrfs_release_path(path);
9992+
9993+
ret = btrfs_is_data_extent_shared(BTRFS_I(inode), disk_bytenr,
9994+
extent_gen, backref_ctx);
99489995
if (ret < 0) {
99499996
goto out;
9950-
} else if (ret) {
9951-
ret = 0;
9952-
} else {
9997+
} else if (ret > 0) {
99539998
btrfs_warn(fs_info,
99549999
"swapfile must not be copy-on-write");
995510000
ret = -EINVAL;
@@ -9984,7 +10029,6 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
998410029

998510030
physical_block_start = (map->stripes[0].physical +
998610031
(logical_block_start - map->start));
9987-
len = min(len, map->chunk_len - (logical_block_start - map->start));
998810032
btrfs_free_chunk_map(map);
998910033
map = NULL;
999010034

@@ -10025,20 +10069,16 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
1002510069
if (ret)
1002610070
goto out;
1002710071
}
10028-
bsi.start = start;
10072+
bsi.start = key.offset;
1002910073
bsi.block_start = physical_block_start;
1003010074
bsi.block_len = len;
1003110075
}
10032-
10033-
start += len;
1003410076
}
1003510077

1003610078
if (bsi.block_len)
1003710079
ret = btrfs_add_swap_extent(sis, &bsi);
1003810080

1003910081
out:
10040-
if (!IS_ERR_OR_NULL(em))
10041-
free_extent_map(em);
1004210082
if (!IS_ERR_OR_NULL(map))
1004310083
btrfs_free_chunk_map(map);
1004410084

@@ -10053,6 +10093,8 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
1005310093

1005410094
out_unlock_mmap:
1005510095
up_write(&BTRFS_I(inode)->i_mmap_lock);
10096+
btrfs_free_backref_share_ctx(backref_ctx);
10097+
btrfs_free_path(path);
1005610098
if (ret)
1005710099
return ret;
1005810100

0 commit comments

Comments
 (0)