common/xfs: refactor commands to select a particular xfs backing device
[xfstests-dev.git] / tests / xfs / 004
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2000-2002 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 004
6 #
7 # exercise xfs_db bug #789674 and other freesp functionality
8 #
9 seq=`basename $0`
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
12
13 here=`pwd`
14 tmp=/tmp/$$
15 status=0
16
17 _cleanup()
18 {
19         _scratch_unmount
20         rm -f $tmp.*
21         exit $status
22 }
23 trap "_cleanup" 0 1 2 3 15
24
25 _populate_scratch()
26 {
27         echo "=== mkfs output ===" >>$seqres.full
28         _scratch_mkfs_xfs | tee -a $seqres.full | _filter_mkfs 2>$tmp.mkfs
29         . $tmp.mkfs
30         _scratch_mount
31         # This test looks at specific behaviors of the xfs_db freesp command,
32         # which reports on the contents of the free space btrees for the data
33         # device.  Don't let anything get created on the realtime volume.
34         _xfs_force_bdev data $SCRATCH_MNT
35         dd if=/dev/zero of=$SCRATCH_MNT/foo count=200 bs=4096 >/dev/null 2>&1 &
36         dd if=/dev/zero of=$SCRATCH_MNT/goo count=400 bs=4096 >/dev/null 2>&1 &
37         dd if=/dev/zero of=$SCRATCH_MNT/moo count=800 bs=4096 >/dev/null 2>&1 &
38         wait
39         _scratch_unmount                        # flush everything
40         _scratch_mount                          # and then remount
41 }
42
43 # get standard environment, filters and checks
44 . ./common/rc
45 . ./common/filter
46
47 # real QA test starts here
48 _supported_fs xfs
49
50 _require_scratch
51 _require_no_large_scratch_dev
52
53 rm -f $seqres.full
54
55 _populate_scratch
56
57 eval `$DF_PROG -P --block-size=512 $SCRATCH_MNT 2>&1 \
58         | tail -1 | $AWK_PROG '{ printf "blocks=%u used=%u avail=%u\n", $3, $4, $5 }'`
59 echo "df gave: blocks=$blocks used=$used avail=$avail" >>$seqres.full
60 echo "blocksize from mkfs is '$dbsize'" >>$seqres.full
61
62 _scratch_xfs_db -r -c "freesp -s"  >$tmp.xfs_db
63 echo "xfs_db for $SCRATCH_DEV" >>$seqres.full
64 cat $tmp.xfs_db >>$seqres.full
65
66 eval `$XFS_IO_PROG -x -c resblks $SCRATCH_MNT 2>&1 \
67         | $AWK_PROG '/available/ { printf "resblks=%u\n", $5 }'`
68 echo "resblks gave: resblks=$resblks" >>$seqres.full
69
70 # check the 'blocks' field from freesp command is OK
71 perl -ne '
72         BEGIN   { $avail ='$avail' * 512 + ('$resblks' * '$dbsize');
73                   $answer="(no xfs_db free blocks line?)" }
74         /free blocks (\d+)$/    || next;
75         $freesp = $1 * '$dbsize';
76         if ($freesp >= $avail) {
77                 $answer = "yes";
78         } else {
79                 $answer = "no ($freesp < $avail)";
80         }
81         END     { print "$answer\n" }
82         ' <$tmp.xfs_db >$tmp.ans
83 ans="`cat $tmp.ans`"
84 echo "Checking blocks column same as df: $ans"
85 if [ "$ans" != yes ]
86 then
87         echo "Error: $SCRATCH_DEV: freesp mismatch: $ans"
88         echo "xfs_db output ..."
89         cat $tmp.xfs_db
90         status=1
91 fi
92
93 # check the 'pct' field from freesp command is good
94 perl -ne '
95             BEGIN       { $percent = 0; }
96             /free/      && next;        # skip over free extent size number
97             if (/\s+(\d+\.\d+)$/) {
98                 $percent += $1;
99             }
100             END { $percent += 0.5; print int($percent), "\n" }  # round up
101 ' <$tmp.xfs_db >$tmp.ans
102 ans="`cat $tmp.ans`"
103 echo "Checking percent column yields 100: $ans"
104 if [ "$ans" != 100 ]
105 then
106         echo "Error: $SCRATCH_DEV: pct mismatch: $ans (expected 100)"
107         echo "xfs_db output ..."
108         cat $tmp.xfs_db
109         status=1
110 fi
111
112 exit