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