generic: test swapfile creation, activation, and deactivation
[xfstests-dev.git] / tests / generic / 496
1 #! /bin/bash
2 # FS QA Test No. 496
3 #
4 # Test various swapfile activation oddities on filesystems that support
5 # fallocated swapfiles.
6 #
7 #-----------------------------------------------------------------------
8 # Copyright (c) 2018 Oracle.  All Rights Reserved.
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation.
13 #
14 # This program is distributed in the hope that it would be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write the Free Software Foundation,
21 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22 #
23 #-----------------------------------------------------------------------
24
25 seq=`basename $0`
26 seqres=$RESULT_DIR/$seq
27 echo "QA output created by $seq"
28
29 here=`pwd`
30 tmp=/tmp/$$
31 status=1        # failure is the default!
32 trap "_cleanup; exit \$status" 0 1 2 3 15
33
34 _cleanup()
35 {
36         cd /
37         swapoff $swapfile 2> /dev/null
38         rm -f $tmp.*
39 }
40
41 # get standard environment, filters and checks
42 . ./common/rc
43 . ./common/filter
44
45 # remove previous $seqres.full before test
46 rm -f $seqres.full
47
48 # real QA test starts here
49 _supported_fs generic
50 _supported_os Linux
51 _require_scratch_swapfile
52 _require_test_program mkswap
53 _require_test_program swapon
54 _require_xfs_io_command "falloc"
55
56 rm -f $seqres.full
57 _scratch_mkfs >>$seqres.full 2>&1
58 _scratch_mount >>$seqres.full 2>&1
59
60 swapfile=$SCRATCH_MNT/swap
61 len=$((2 * 1048576))
62 page_size=$(get_page_size)
63
64 swapfile_cycle() {
65         local swapfile="$1"
66
67         # Swap files must be nocow on Btrfs.
68         $CHATTR_PROG +C $swapfile >> $seqres.full 2>&1
69         "$here/src/mkswap" $swapfile >> $seqres.full
70         "$here/src/swapon" $swapfile 2>&1 | _filter_scratch
71         swapoff $swapfile 2>> $seeqres.full
72         rm -f $swapfile
73 }
74
75 # Create a fallocated swap file
76 echo "fallocate swap" | tee -a $seqres.full
77 $XFS_IO_PROG -f -c "falloc 0 $len" $swapfile >> $seqres.full
78 $CHATTR_PROG +C $swapfile >> $seqres.full 2>&1
79 "$here/src/mkswap" $swapfile
80 "$here/src/swapon" $swapfile >> $seqres.full 2>&1 || \
81         _notrun "fallocated swap not supported here"
82 swapoff $swapfile
83
84 # Create a fallocated swap file and touch every other $PAGE_SIZE to create
85 # a mess of written/unwritten extent records
86 echo "mixed swap" | tee -a $seqres.full
87 $XFS_IO_PROG -f -c "falloc 0 $len" $swapfile >> $seqres.full
88 seq $page_size $((page_size * 2)) $len | while read offset; do
89         _pwrite_byte 0x58 $offset 1 $swapfile >> $seqres.full
90 done
91 swapfile_cycle $swapfile
92
93 status=0
94 exit