generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / tests / btrfs / 177
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2018 Facebook.  All Rights Reserved.
4 #
5 # FS QA Test 177
6 #
7 # Test relocation (balance and resize) with an active swap file.
8 #
9 . ./common/preamble
10 _begin_fstest auto quick swap balance
11
12 . ./common/filter
13 . ./common/btrfs
14
15 # Modify as appropriate.
16 _supported_fs btrfs
17 _require_scratch_swapfile
18
19 # Eliminate the differences between the old and new output formats
20 # Old format:
21 #       Resize 'SCRATCH_MNT' of '1073741824'
22 # New format:
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()
27 {
28         local _field
29         local _val
30         local _suffix
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
37                 _suffix="B"
38         fi
39         _val=`echo "$_val$_suffix" | _filter_size_to_bytes`
40         echo "Resized to $_val"
41 }
42
43
44
45 swapfile="$SCRATCH_MNT/swap"
46
47 _require_scratch_size $((3 * 1024 * 1024)) #kB
48
49 # First, create a 1GB filesystem.
50 fssize=$((1024 * 1024 * 1024))
51 _scratch_mkfs_sized $fssize >> $seqres.full 2>&1
52 _scratch_mount
53
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
58
59 # Now fill it up.
60 dd if=/dev/zero of="$SCRATCH_MNT/refill" bs=4096 >> $seqres.full 2>&1
61
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
64 # filesystem.
65 $BTRFS_UTIL_PROG filesystem resize $((3 * fssize)) "$SCRATCH_MNT" | convert_resize_output
66 _format_swapfile "$swapfile" $((32 * 1024 * 1024))
67 swapon "$swapfile"
68
69 # Free up the first 1GB of the filesystem.
70 rm -f "$SCRATCH_MNT/fill"
71 rm -f "$SCRATCH_MNT/refill"
72
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
76
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"
79
80 swapoff "$swapfile"
81
82 # It should work again after swapoff.
83 $BTRFS_UTIL_PROG filesystem resize $fssize "$SCRATCH_MNT" | convert_resize_output
84
85 status=0
86 exit