xfs: Check for extent overflow when trivally adding a new extent
[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 <($here/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
156 rm -f $seqres.full
157
158 DIRCOUNT=8
159 INOCOUNT=$((2048 / DIRCOUNT))
160
161 _scratch_mkfs "-d agcount=$DIRCOUNT" >> $seqres.full 2>&1 || _fail "mkfs failed"
162 _scratch_mount
163
164 # Figure out if we have v5 bulkstat/inumbers ioctls.
165 has_v5=
166 bs_root_out="$($XFS_IO_PROG -c 'bulkstat_single root' $SCRATCH_MNT 2>>$seqres.full)"
167 test -n "$bs_root_out" && has_v5=1
168
169 echo "this will be 1 if we have v5 bulkstat: $has_v5" >> $seqres.full
170
171 # If v5 bulkstat is present, query the root inode and compare it to the stat
172 # output of $SCRATCH_MNT to make sure it gave us the correct number
173 if [ -n "$has_v5" ]; then
174         bs_root=$(echo "$bs_root_out" | grep ino | awk '{print $3}')
175         stat_root=$(stat -c '%i' $SCRATCH_MNT)
176         if [ "$stat_root" -ne "$bs_root" ]; then
177                 echo "stat says root is $stat_root but bulkstat says $bs_root"
178         fi
179 fi
180
181 # Create a set of directories and fill each with a fixed number of files
182 for dir in $(seq 1 $DIRCOUNT); do
183         mkdir -p $SCRATCH_MNT/$dir
184         for i in $(seq 1 $INOCOUNT); do
185                 touch $SCRATCH_MNT/$dir/$i
186         done
187 done
188 bstat_test
189
190 # Remove every other file from each dir
191 for dir in $(seq 1 $DIRCOUNT); do
192         for i in $(seq 2 2 $INOCOUNT); do
193                 rm -f $SCRATCH_MNT/$dir/$i
194         done
195 done
196 bstat_test
197
198 # Remove the entire second half of files
199 for dir in $(seq 1 $DIRCOUNT); do
200         for i in $(seq $((INOCOUNT / 2)) $INOCOUNT); do
201                 rm -f $SCRATCH_MNT/$dir/$i
202         done
203 done
204 bstat_test
205
206 # Remove all regular files
207 for dir in $(seq 1 $DIRCOUNT); do
208         rm -f $SCRATCH_MNT/$dir/*
209 done
210 bstat_test
211
212 # Success, all done
213 status=0
214 exit