ceba9ff67abfc0319efd89d06b996a20b591c37a
[xfstests-dev.git] / tests / xfs / 509
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014 Red Hat, Inc.  All Rights Reserved.
4 # Copyright (c) 2019 Oracle, Inc.  All Rights Reserved.
5 #
6 # FS QA Test No. 509
7 #
8 # Use the xfs_io bulkstat utility to verify bulkstat finds all inodes in a
9 # filesystem.  Test under various inode counts, inobt record layouts and
10 # bulkstat batch sizes.  Test v1 and v5 ioctls explicitly, as well as the
11 # ioctl version autodetection code in libfrog.
12 #
13 seq=`basename $0`
14 seqres=$RESULT_DIR/$seq
15 echo "QA output created by $seq"
16
17 here=`pwd`
18 tmp=/tmp/$$
19 status=1        # failure is the default!
20 trap "_cleanup; exit \$status" 0 1 2 3 15
21
22 _cleanup()
23 {
24         cd /
25         rm -f $tmp.*
26 }
27
28 bstat_versions()
29 {
30         echo "default"
31         echo "v1 -v1"
32         if [ -n "$has_v5" ]; then
33                 echo "v5 -v5"
34         else
35                 echo "v5"
36         fi
37 }
38
39 # Print the number of inodes counted by bulkstat
40 bstat_count()
41 {
42         local batchsize="$1"
43         local tag="$2"
44
45         bstat_versions | while read v_tag v_flag; do
46                 echo "$tag($v_tag): passing \"$v_flag\" to bulkstat" >> $seqres.full
47                 echo -n "bulkstat $tag($v_tag): "
48                 $XFS_IO_PROG -c "bulkstat -n $batchsize $v_flag" $SCRATCH_MNT | grep ino | wc -l
49         done
50 }
51
52 # Print the number of inodes counted by per-ag bulkstat
53 bstat_perag_count()
54 {
55         local batchsize="$1"
56         local tag="$2"
57
58         local agcount=$(_xfs_mount_agcount $SCRATCH_MNT)
59
60         bstat_versions | while read v_tag v_flag; do
61                 echo -n "bulkstat $tag($v_tag): "
62                 seq 0 $((agcount - 1)) | while read ag; do
63                         $XFS_IO_PROG -c "bulkstat -a $ag -n $batchsize $v_flag" $SCRATCH_MNT
64                 done | grep ino | wc -l
65         done
66 }
67
68 # Sum the number of allocated inodes in each AG in a fs.
69 inumbers_ag()
70 {
71         local agcount="$1"
72         local batchsize="$2"
73         local mount="$3"
74         local v_flag="$4"
75
76         seq 0 $((agcount - 1)) | while read ag; do
77                 $XFS_IO_PROG -c "inumbers -a $ag -n $batchsize $v_flag" $mount
78         done | grep alloccount | awk '{x += $3} END { print(x) }'
79 }
80
81 # Sum the number of allocated inodes in the whole fs all at once.
82 inumbers_fs()
83 {
84         local dir="$1"
85         local v_flag="$2"
86
87         $XFS_IO_PROG -c "inumbers $v_flag" "$dir" | grep alloccount | \
88                 awk '{x += $3} END { print(x) }'
89 }
90
91 # Print the number of inodes counted by inumbers
92 inumbers_count()
93 {
94         local expect="$1"
95
96         # There probably aren't more than 10 hidden inodes, right?
97         local tolerance=10
98
99         # Force all background unlinked inode cleanup to run so that we don't
100         # race changes to the inode btree with our inumbers query.
101         _scratch_cycle_mount
102
103         bstat_versions | while read v_tag v_flag; do
104                 echo -n "inumbers all($v_tag): "
105                 nr=$(inumbers_fs $SCRATCH_MNT $v_flag)
106                 _within_tolerance "inumbers" $nr $expect $tolerance -v
107
108                 local agcount=$(_xfs_mount_agcount $SCRATCH_MNT)
109                 for batchsize in 71 2 1; do
110                         echo -n "inumbers $batchsize($v_tag): "
111                         nr=$(inumbers_ag $agcount $batchsize $SCRATCH_MNT $v_flag)
112                         _within_tolerance "inumbers" $nr $expect $tolerance -v
113                 done
114         done
115 }
116
117 # Compare the src/bstat output against the xfs_io bstat output.
118 # This compares the actual inode numbers output by one tool against another,
119 # so we can't easily put the output in the golden output.
120 bstat_compare()
121 {
122         bstat_versions | while read v_tag v_flag; do
123                 diff -u <(./src/bstat $SCRATCH_MNT | grep ino | awk '{print $2}') \
124                         <($XFS_IO_PROG -c "bulkstat $v_flag" $SCRATCH_MNT | grep ino | awk '{print $3}')
125         done
126 }
127
128 # Print bulkstat counts using varied batch sizes
129 bstat_test()
130 {
131         expect=`find $SCRATCH_MNT -print | wc -l`
132         echo
133         echo "expect $expect"
134
135         for sz in 4096 71 32 1; do
136                 bstat_count $sz "$sz all"
137                 bstat_perag_count $sz "$sz perag"
138                 bstat_compare
139                 inumbers_count $expect
140         done
141 }
142
143 # Get standard environment, filters and checks
144 . ./common/rc
145 . ./common/filter
146
147 _require_scratch
148 _require_xfs_io_command bulkstat
149 _require_xfs_io_command bulkstat_single
150 _require_xfs_io_command inumbers
151
152 # Real QA test starts here
153
154 _supported_fs xfs
155 _supported_os Linux
156
157 rm -f $seqres.full
158
159 DIRCOUNT=8
160 INOCOUNT=$((2048 / DIRCOUNT))
161
162 _scratch_mkfs "-d agcount=$DIRCOUNT" >> $seqres.full 2>&1 || _fail "mkfs failed"
163 _scratch_mount
164
165 # Figure out if we have v5 bulkstat/inumbers ioctls.
166 has_v5=
167 bs_root_out="$($XFS_IO_PROG -c 'bulkstat_single root' $SCRATCH_MNT 2>>$seqres.full)"
168 test -n "$bs_root_out" && has_v5=1
169
170 echo "this will be 1 if we have v5 bulkstat: $has_v5" >> $seqres.full
171
172 # If v5 bulkstat is present, query the root inode and compare it to the stat
173 # output of $SCRATCH_MNT to make sure it gave us the correct number
174 if [ -n "$has_v5" ]; then
175         bs_root=$(echo "$bs_root_out" | grep ino | awk '{print $3}')
176         stat_root=$(stat -c '%i' $SCRATCH_MNT)
177         if [ "$stat_root" -ne "$bs_root" ]; then
178                 echo "stat says root is $stat_root but bulkstat says $bs_root"
179         fi
180 fi
181
182 # Create a set of directories and fill each with a fixed number of files
183 for dir in $(seq 1 $DIRCOUNT); do
184         mkdir -p $SCRATCH_MNT/$dir
185         for i in $(seq 1 $INOCOUNT); do
186                 touch $SCRATCH_MNT/$dir/$i
187         done
188 done
189 bstat_test
190
191 # Remove every other file from each dir
192 for dir in $(seq 1 $DIRCOUNT); do
193         for i in $(seq 2 2 $INOCOUNT); do
194                 rm -f $SCRATCH_MNT/$dir/$i
195         done
196 done
197 bstat_test
198
199 # Remove the entire second half of files
200 for dir in $(seq 1 $DIRCOUNT); do
201         for i in $(seq $((INOCOUNT / 2)) $INOCOUNT); do
202                 rm -f $SCRATCH_MNT/$dir/$i
203         done
204 done
205 bstat_test
206
207 # Remove all regular files
208 for dir in $(seq 1 $DIRCOUNT); do
209         rm -f $SCRATCH_MNT/$dir/*
210 done
211 bstat_test
212
213 # Success, all done
214 status=0
215 exit