xfstests: include test subdirectory support
[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
30 seq=`basename $0`
31 echo "QA output created by $seq"
32
33 here=`pwd`
34 tmp=/tmp/$$
35 status=1        # failure is the default!
36
37 _cleanup()
38 {
39     rm -f $tmp.*
40 }
41
42 trap "_cleanup ; exit \$status" 0 1 2 3 15
43
44 # get standard environment, filters and checks
45 . ./common.rc
46 . ./common.filter
47
48 # real QA test starts here
49 _supported_fs xfs
50 _supported_os Linux
51 _require_scratch
52
53 rm -f $seq.full
54
55 [ "$XFS_FSR_PROG" = "" ] && _notrun "xfs_fsr not found"
56
57 # create freespace holes of 1-3 blocks in length
58 #
59 # This is done to ensure that defragmented files have roughly 1/3 the
60 # number of extents they started with. This will ensure we get
61 # transistions from btree format (say 15 extents) to extent format
62 # (say 5 extents) and lots of variations around that dependent on the
63 # number of attributes in the files being defragmented.
64 #
65 # We have to make sure there are enough free inodes for the test to
66 # pass without needing to allocate new clusters during the test.
67 # With such fragemented free space, that will fail.
68 #
69 fragment_freespace()
70 {
71         _file="$SCRATCH_MNT/not_free"
72         _dir="$SCRATCH_MNT/saved"
73
74         # allocate inode space
75         mkdir -p $_dir
76         for i in `seq 0 1 1000`; do
77                 touch $_file.$i
78         done
79         for i in `seq 0 63 1000`; do
80                 mv $_file.$i $_dir
81         done
82         for i in `seq 0 1 1000`; do
83                 rm -f $_file.$i
84         done
85
86         $XFS_IO_PROG -fs -c "resvsp 0 40000k" $_file > /dev/null 2>&1
87
88         for i in `seq 0 8 40000`; do
89                 $XFS_IO_PROG -f -c "unresvsp ${i}k 4k" $_file \
90                                         > /dev/null 2>&1
91         done
92         for i in `seq 0 28 40000`; do
93                 $XFS_IO_PROG -f -c "unresvsp ${i}k 4k" $_file \
94                                         > /dev/null 2>&1
95         done
96         sync
97
98         # and now use up all the remaining extents larger than 3 blocks
99         $XFS_IO_PROG -fs -c "resvsp 0 4m" $_file.large > /dev/null 2>&1
100 }
101
102 create_attrs()
103 {
104         for foo in `seq 0 1 $1`; do
105                 $SETFATTR_PROG -n user.$foo -v 0xbabe $2
106         done
107 }
108
109 create_data()
110 {
111         size=`expr \( $1 + 1 \) \* 4096`
112         $XFS_IO_PROG -f -c "truncate $size" $2 > /dev/null 2>&1
113         for foo in `seq $1 -1 0`; do
114                 let offset=$foo*4096
115                 $XFS_IO_PROG -f -c "resvsp $offset 4096" $2 > /dev/null 2>&1
116         done
117 }
118
119 # create the designated file with a certain number of attributes and a certain
120 # number of data extents. Reverse order synchronous data writes are used to
121 # create fragmented files, though with the way the filesystem freespace is
122 # fragmented, this is probably not necessary. Create the attributes first so
123 # that they cause the initial fork offset pressure to move it about.
124 #
125 create_target_attr_first()
126 {
127         nattrs=$1
128         file_blocks=$2
129         target=$3
130
131         rm -f $target
132         touch $target
133         create_attrs $nattrs $target
134         create_data $file_blocks $target
135 }
136
137 # Same as create_target_attr_first, but this time put the attributes on after
138 # the data extents have been created. This puts different pressure on the
139 # inode fork offset, so should exercise the kernel code differently and give us
140 # a different pattern of fork offsets to work with compared to creating the
141 # attrs first.
142 #
143 create_target_attr_last()
144 {
145         nattrs=$1
146         file_blocks=$2
147         target=$3
148
149         rm -f $target
150         touch $target
151         create_data $file_blocks $target
152         create_attrs $nattrs $target
153 }
154
155 rm -f $seq.full
156
157 # use a small filesystem so we can control freespace easily
158 _scratch_mkfs_sized $((50 * 1024 * 1024)) >> $seq.full 2>&1
159 _scratch_mount
160 fragment_freespace
161
162 # unmount and remount to reset all allocator indexes
163 umount $SCRATCH_MNT
164 _scratch_mount
165
166 # create a range of source files, then fsr them to a known size
167 #
168 # This assumes 256 byte inodes.
169 #
170 # n = number of target fragments for xfs_fsr
171 #       - only a guideline, but forces multiple fragments via sync writes
172 #       - start at 4 as that typically covers all extent format situations
173 #       - end at 12 as that is beyond the maximum that canbe fit in extent
174 #         format
175 # i = number of 2 byte attributes on the file
176 #       - it takes 6 attributes to change the fork offset from the start value
177 #         of 120 bytes to 112 bytes, so we start at 5.
178 #       - 15 is enough to push to btree format, so we stop there.
179 # j = number of data extents on the file
180 #       - start in extent format, but we also want btree format as well, so
181 #         start at 5 so that the number of attributes determines the starting
182 #         format.
183 #       - need enough extents that if they are all 3 blocks in length the final
184 #         format will be dependent on the number of attributes on the inode. 20
185 #         initial single block extents gives us 6-8 extents after defrag which
186 #         puts us right on the threshold of what the extent format can hold.
187
188 targ=$SCRATCH_MNT/fsr_test_file.$$
189 for n in `seq 4 1 12`; do
190         echo "*** n == $n ***" >> $seq.full
191         for i in `seq 5 1 15`; do
192                 for j in `seq 5 1 20`; do
193                         create_target_attr_first $i $j $targ.$i.$j >> $seq.full 2>&1
194                 done
195                 xfs_bmap -vp $targ.$i.* >> $seq.full 2>&1
196                 FSRXFSTEST=true xfs_fsr -d -v -C $n $targ.$i.* >> $seq.full 2>&1
197                 xfs_bmap -vp $targ.$i.* >> $seq.full 2>&1
198                 for j in `seq 5 1 20`; do
199                         create_target_attr_last $i $j $targ.$i.$j >> $seq.full 2>&1
200                 done
201                 xfs_bmap -vp $targ.$i.* >> $seq.full 2>&1
202                 FSRXFSTEST=true xfs_fsr -d -v -C $n $targ.$i.* >> $seq.full 2>&1
203                 xfs_bmap -vp $targ.$i.* >> $seq.full 2>&1
204         done
205 done
206
207 umount $SCRATCH_MNT
208 echo "--- silence is golden ---"
209 status=0 ; exit