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