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 _begin_fstest auto quick swap balance
15 # Modify as appropriate.
17 _require_scratch_swapfile
19 # Eliminate the differences between the old and new output formats
21 # Resize 'SCRATCH_MNT' of '1073741824'
23 # Resize device id 1 (SCRATCH_DEV) from 3.00GiB to 1.00GiB
24 # Convert both outputs to:
25 # Resized to 1073741824
26 convert_resize_output()
31 _field=`$AWK_PROG '{print $NF}' | tr -d "'"`
32 # remove trailing zeroes
33 _val=`echo $_field | $AWK_PROG '{print $1 * 1}'`
34 # get the first unit char, for example return G in case we have GiB
35 _suffix=`echo $_field | grep -o "[GMB]"`
36 if [ -z "$_suffix" ]; then
39 _val=`echo "$_val$_suffix" | _filter_size_to_bytes`
40 echo "Resized to $_val"
45 swapfile="$SCRATCH_MNT/swap"
47 _require_scratch_size $((3 * 1024 * 1024)) #kB
49 # First, create a 1GB filesystem.
50 fssize=$((1024 * 1024 * 1024))
51 _scratch_mkfs_sized $fssize >> $seqres.full 2>&1
54 # Create a small file and run balance so we shall deal with the chunk
55 # size as allocated by the kernel, mkfs allocated chunks are smaller.
56 dd if=/dev/zero of="$SCRATCH_MNT/fill" bs=4096 count=1 >> $seqres.full 2>&1
57 _run_btrfs_balance_start "$SCRATCH_MNT" >>$seqres.full
60 dd if=/dev/zero of="$SCRATCH_MNT/refill" bs=4096 >> $seqres.full 2>&1
62 # Now add more space and create a swap file. We know that the first $fssize
63 # of the filesystem was used, so the swap file must be in the new part of the
65 $BTRFS_UTIL_PROG filesystem resize $((3 * fssize)) "$SCRATCH_MNT" | convert_resize_output
66 _format_swapfile "$swapfile" $((32 * 1024 * 1024))
69 # Free up the first 1GB of the filesystem.
70 rm -f "$SCRATCH_MNT/fill"
71 rm -f "$SCRATCH_MNT/refill"
73 # Get rid of empty block groups and also make sure that balance skips block
74 # groups containing active swap files.
75 _run_btrfs_balance_start "$SCRATCH_MNT" >>$seqres.full
77 # Try to shrink away the area occupied by the swap file, which should fail.
78 $BTRFS_UTIL_PROG filesystem resize 1G "$SCRATCH_MNT" 2>&1 | grep -o "Text file busy"
82 # It should work again after swapoff.
83 $BTRFS_UTIL_PROG filesystem resize $fssize "$SCRATCH_MNT" | convert_resize_output