xfstests: add _require_freeze and minor cleanups
[xfstests-dev.git] / 227
1 #! /bin/bash
2 # FS QA Test No. 227
3 #
4 # xfs_fsr QA tests
5 # run xfs_fsr over the test filesystem to give it a wide and varied set of
6 # inodes to try to defragment. This is effectively a crash/assert failure
7 # test looking for corruption induced by the kernel inadequately checking
8 # the indoes to be swapped. It also is good for validating fsr's attribute fork
9 # generation code.
10 #
11 #-----------------------------------------------------------------------
12 # Copyright (c) 2010 Dave Chinner.  All Rights Reserved.
13 #
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License as
16 # published by the Free Software Foundation.
17 #
18 # This program is distributed in the hope that it would be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write the Free Software Foundation,
25 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26 #
27 #-----------------------------------------------------------------------
28 #
29 # creator
30 owner=david@fromorbit.com
31
32 seq=`basename $0`
33 echo "QA output created by $seq"
34
35 here=`pwd`
36 tmp=/tmp/$$
37 status=1        # failure is the default!
38
39 _cleanup()
40 {
41     rm -f $tmp.*
42 }
43
44 trap "_cleanup ; exit \$status" 0 1 2 3 15
45
46 # get standard environment, filters and checks
47 . ./common.rc
48 . ./common.filter
49
50 # real QA test starts here
51 _supported_fs xfs
52 _supported_os Linux
53 _require_scratch
54
55 rm -f $seq.full
56
57 [ "$XFS_FSR_PROG" = "" ] && _notrun "xfs_fsr not found"
58
59 # create freespace holes of 1-3 blocks in length
60 #
61 # This is done to ensure that defragmented files have roughly 1/3 the
62 # number of extents they started with. This will ensure we get
63 # transistions from btree format (say 15 extents) to extent format
64 # (say 5 extents) and lots of variations around that dependent on the
65 # number of attributes in the files being defragmented.
66 #
67 # We have to make sure there are enough free inodes for the test to
68 # pass without needing to allocate new clusters during the test.
69 # With such fragemented free space, that will fail.
70 #
71 fragment_freespace()
72 {
73         _file="$SCRATCH_MNT/not_free"
74         _dir="$SCRATCH_MNT/saved"
75
76         # allocate inode space
77         mkdir -p $_dir
78         for i in `seq 0 1 1000`; do
79                 touch $_file.$i
80         done
81         for i in `seq 0 63 1000`; do
82                 mv $_file.$i $_dir
83         done
84         for i in `seq 0 1 1000`; do
85                 rm -f $_file.$i
86         done
87
88         $XFS_IO_PROG -fs -c "resvsp 0 40000k" $_file > /dev/null 2>&1
89
90         for i in `seq 0 8 40000`; do
91                 $XFS_IO_PROG -f -c "unresvsp ${i}k 4k" $_file \
92                                         > /dev/null 2>&1
93         done
94         for i in `seq 0 28 40000`; do
95                 $XFS_IO_PROG -f -c "unresvsp ${i}k 4k" $_file \
96                                         > /dev/null 2>&1
97         done
98         sync
99
100         # and now use up all the remaining extents larger than 3 blocks
101         $XFS_IO_PROG -fs -c "resvsp 0 4m" $_file.large > /dev/null 2>&1
102 }
103
104 create_attrs()
105 {
106         for foo in `seq 0 1 $1`; do
107                 $SETFATTR_PROG -n user.$foo -v 0xbabe $2
108         done
109 }
110
111 create_data()
112 {
113         size=`expr \( $1 + 1 \) \* 4096`
114         $XFS_IO_PROG -f -c "truncate $size" $2 > /dev/null 2>&1
115         for foo in `seq $1 -1 0`; do
116                 let offset=$foo*4096
117                 $XFS_IO_PROG -f -c "resvsp $offset 4096" $2 > /dev/null 2>&1
118         done
119 }
120
121 # create the designated file with a certain number of attributes and a certain
122 # number of data extents. Reverse order synchronous data writes are used to
123 # create fragmented files, though with the way the filesystem freespace is
124 # fragmented, this is probably not necessary. Create the attributes first so
125 # that they cause the initial fork offset pressure to move it about.
126 #
127 create_target_attr_first()
128 {
129         nattrs=$1
130         file_blocks=$2
131         target=$3
132
133         rm -f $target
134         touch $target
135         create_attrs $nattrs $target
136         create_data $file_blocks $target
137 }
138
139 # Same as create_target_attr_first, but this time put the attributes on after
140 # the data extents have been created. This puts different pressure on the
141 # inode fork offset, so should exercise the kernel code differently and give us
142 # a different pattern of fork offsets to work with compared to creating the
143 # attrs first.
144 #
145 create_target_attr_last()
146 {
147         nattrs=$1
148         file_blocks=$2
149         target=$3
150
151         rm -f $target
152         touch $target
153         create_data $file_blocks $target
154         create_attrs $nattrs $target
155 }
156
157 rm -f $seq.full
158
159 # use a small filesystem so we can control freespace easily
160 _scratch_mkfs_sized $((50 * 1024 * 1024)) >> $seq.full 2>&1
161 _scratch_mount
162 fragment_freespace
163
164 # unmount and remount to reset all allocator indexes
165 umount $SCRATCH_MNT
166 _scratch_mount
167
168 # create a range of source files, then fsr them to a known size
169 #
170 # This assumes 256 byte inodes.
171 #
172 # n = number of target fragments for xfs_fsr
173 #       - only a guideline, but forces multiple fragments via sync writes
174 #       - start at 4 as that typically covers all extent format situations
175 #       - end at 12 as that is beyond the maximum that canbe fit in extent
176 #         format
177 # i = number of 2 byte attributes on the file
178 #       - it takes 6 attributes to change the fork offset from the start value
179 #         of 120 bytes to 112 bytes, so we start at 5.
180 #       - 15 is enough to push to btree format, so we stop there.
181 # j = number of data extents on the file
182 #       - start in extent format, but we also want btree format as well, so
183 #         start at 5 so that the number of attributes determines the starting
184 #         format.
185 #       - need enough extents that if they are all 3 blocks in length the final
186 #         format will be dependent on the number of attributes on the inode. 20
187 #         initial single block extents gives us 6-8 extents after defrag which
188 #         puts us right on the threshold of what the extent format can hold.
189
190 targ=$SCRATCH_MNT/fsr_test_file.$$
191 for n in `seq 4 1 12`; do
192         echo "*** n == $n ***" >> $seq.full
193         for i in `seq 5 1 15`; do
194                 for j in `seq 5 1 20`; do
195                         create_target_attr_first $i $j $targ.$i.$j >> $seq.full 2>&1
196                 done
197                 xfs_bmap -vp $targ.$i.* >> $seq.full 2>&1
198                 FSRXFSTEST=true xfs_fsr -d -v -C $n $targ.$i.* >> $seq.full 2>&1
199                 xfs_bmap -vp $targ.$i.* >> $seq.full 2>&1
200                 for j in `seq 5 1 20`; do
201                         create_target_attr_last $i $j $targ.$i.$j >> $seq.full 2>&1
202                 done
203                 xfs_bmap -vp $targ.$i.* >> $seq.full 2>&1
204                 FSRXFSTEST=true xfs_fsr -d -v -C $n $targ.$i.* >> $seq.full 2>&1
205                 xfs_bmap -vp $targ.$i.* >> $seq.full 2>&1
206         done
207 done
208
209 umount $SCRATCH_MNT
210 echo "--- silence is golden ---"
211 status=0 ; exit