2018-10-01 14:46:20 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# Corrupt a filesystem that is using freespace tree and then ensure that
|
|
|
|
# btrfs check is able to repair it. This tests correct detection/repair of
|
|
|
|
# both a FREE_SPACE_EXTENT based FST and a FREE_SPACE_BITMAP based FST.
|
|
|
|
|
2020-08-28 16:21:30 +00:00
|
|
|
# Note: this needs a patched kernel to exercise extents and bitmaps
|
|
|
|
# ff51bf02d107 ("btrfs: block-group: fix free-space bitmap threshold")
|
|
|
|
|
2018-10-01 14:46:20 +00:00
|
|
|
source "$TEST_TOP/common"
|
|
|
|
|
|
|
|
setup_root_helper
|
|
|
|
prepare_test_dev 256M
|
|
|
|
|
|
|
|
check_prereq btrfs
|
|
|
|
check_prereq mkfs.btrfs
|
|
|
|
check_global_prereq grep
|
|
|
|
check_global_prereq tail
|
|
|
|
check_global_prereq head
|
|
|
|
check_global_prereq cut
|
|
|
|
|
|
|
|
# wrapper for btrfs-corrupt-item
|
|
|
|
# $1: Type of item we want to corrupt - extent or bitmap
|
|
|
|
corrupt_fst_item()
|
|
|
|
{
|
|
|
|
local type
|
|
|
|
local objectid
|
|
|
|
local offset
|
|
|
|
type="$1"
|
|
|
|
|
|
|
|
if [[ $type == "bitmap" ]]; then
|
|
|
|
type=200
|
|
|
|
objectid=$("$TOP/btrfs" inspect-internal dump-tree -t 10 "$TEST_DEV" | \
|
|
|
|
grep -o "[[:digit:]]* FREE_SPACE_BITMAP [[:digit:]]*" | \
|
|
|
|
cut -d' ' -f1 | tail -2 | head -1)
|
|
|
|
offset=$("$TOP/btrfs" inspect-internal dump-tree -t 10 "$TEST_DEV" | \
|
|
|
|
grep -o "[[:digit:]]* FREE_SPACE_BITMAP [[:digit:]]*" | \
|
|
|
|
cut -d' ' -f3 | tail -2 | head -1)
|
2020-08-28 16:21:30 +00:00
|
|
|
if [ -z "$objectid" -o -z "$offset" ]; then
|
|
|
|
_log_skipped "No bitmap to corrupt found, needs kernel patch"
|
|
|
|
return 1
|
|
|
|
fi
|
2019-07-24 13:53:18 +00:00
|
|
|
_log "Corrupting $objectid,FREE_SPACE_BITMAP,$offset"
|
2018-10-01 14:46:20 +00:00
|
|
|
elif [[ $type == "extent" ]]; then
|
|
|
|
type=199
|
|
|
|
objectid=$("$TOP/btrfs" inspect-internal dump-tree -t 10 "$TEST_DEV" | \
|
|
|
|
grep -o "[[:digit:]]* FREE_SPACE_EXTENT [[:digit:]]*" | \
|
|
|
|
cut -d' ' -f1 | tail -2 | head -1)
|
|
|
|
offset=$("$TOP/btrfs" inspect-internal dump-tree -t 10 "$TEST_DEV" | \
|
|
|
|
grep -o "[[:digit:]]* FREE_SPACE_EXTENT [[:digit:]]*" | \
|
|
|
|
cut -d' ' -f3 | tail -2 | head -1)
|
2020-08-28 16:21:30 +00:00
|
|
|
if [ -z "$objectid" -o -z "$offset" ]; then
|
|
|
|
_log_skipped "No extent to corrupt found, needs kernel patch"
|
|
|
|
return 1
|
|
|
|
fi
|
2019-07-24 13:53:18 +00:00
|
|
|
_log "Corrupting $objectid,FREE_SPACE_EXTENT,$offset"
|
2018-10-01 14:46:20 +00:00
|
|
|
else
|
|
|
|
_fail "Unknown item type for corruption"
|
|
|
|
fi
|
|
|
|
|
2020-01-13 21:49:14 +00:00
|
|
|
run_check "$INTERNAL_BIN/btrfs-corrupt-block" -r 10 -K "$objectid,$type,$offset" \
|
2018-10-01 14:46:20 +00:00
|
|
|
-f offset "$TEST_DEV"
|
|
|
|
}
|
|
|
|
|
2018-10-23 14:57:48 +00:00
|
|
|
if ! [ -f "/sys/fs/btrfs/features/free_space_tree" ]; then
|
|
|
|
_not_run "kernel does not support free-space-tree feature"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2019-06-05 17:09:38 +00:00
|
|
|
run_check_mkfs_test_dev -n 4k
|
2018-10-01 14:46:20 +00:00
|
|
|
run_check_mount_test_dev -oclear_cache,space_cache=v2
|
|
|
|
|
|
|
|
# create files which will populate the FST
|
2018-12-18 14:10:39 +00:00
|
|
|
for i in {0..9}; do
|
|
|
|
for j in {1..300}; do
|
|
|
|
run_check $SUDO_HELPER fallocate -l 4k "$TEST_MNT/file.$j$i" &
|
|
|
|
done
|
|
|
|
wait
|
2018-10-01 14:46:20 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
run_check_umount_test_dev
|
|
|
|
|
|
|
|
# now corrupt one of the bitmap items
|
2020-08-28 16:21:30 +00:00
|
|
|
if corrupt_fst_item "bitmap"; then
|
|
|
|
check_image "$TEST_DEV"
|
|
|
|
fi
|
2018-10-01 14:46:20 +00:00
|
|
|
|
|
|
|
# change the freespace such that we now have at least one free_space_extent
|
|
|
|
# object
|
|
|
|
run_check_mount_test_dev
|
|
|
|
rm -rf "$TEST_MNT/file.*"
|
|
|
|
run_check $SUDO_HELPER fallocate -l 50m "$TEST_MNT/file"
|
|
|
|
run_check_umount_test_dev
|
|
|
|
|
|
|
|
# now corrupt an extent
|
2020-08-28 16:21:30 +00:00
|
|
|
if corrupt_fst_item "extent"; then
|
|
|
|
check_image "$TEST_DEV"
|
|
|
|
fi
|