2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2018 Facebook. All Rights Reserved.
7 # Test relocation (balance and resize) with an active swap file.
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
15 status=1 # failure is the default!
16 trap "_cleanup; exit \$status" 0 1 2 3 15
30 # Modify as appropriate.
33 _require_scratch_swapfile
35 swapfile="$SCRATCH_MNT/swap"
37 _require_scratch_size $((3 * 1024 * 1024)) #kB
39 # First, create a 1GB filesystem.
40 fssize=$((1024 * 1024 * 1024))
41 _scratch_mkfs_sized $fssize >> $seqres.full 2>&1
44 # Create a small file and run balance so we shall deal with the chunk
45 # size as allocated by the kernel, mkfs allocated chunks are smaller.
46 dd if=/dev/zero of="$SCRATCH_MNT/fill" bs=4096 count=1 >> $seqres.full 2>&1
47 _run_btrfs_balance_start "$SCRATCH_MNT"
50 dd if=/dev/zero of="$SCRATCH_MNT/refill" bs=4096 >> $seqres.full 2>&1
52 # Now add more space and create a swap file. We know that the first $fssize
53 # of the filesystem was used, so the swap file must be in the new part of the
55 $BTRFS_UTIL_PROG filesystem resize $((3 * fssize)) "$SCRATCH_MNT" | \
57 _format_swapfile "$swapfile" $((32 * 1024 * 1024))
60 # Free up the first 1GB of the filesystem.
61 rm -f "$SCRATCH_MNT/fill"
62 rm -f "$SCRATCH_MNT/refill"
64 # Get rid of empty block groups and also make sure that balance skips block
65 # groups containing active swap files.
66 _run_btrfs_balance_start "$SCRATCH_MNT"
68 # Try to shrink away the area occupied by the swap file, which should fail.
69 $BTRFS_UTIL_PROG filesystem resize 1G "$SCRATCH_MNT" 2>&1 | grep -o "Text file busy"
73 # It should work again after swapoff.
74 $BTRFS_UTIL_PROG filesystem resize $fssize "$SCRATCH_MNT" | _filter_scratch