xfstests: randholes: use posix_memalign()
[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 [ "$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 fragment_freespace()
66 {
67         _file="$SCRATCH_MNT/not_free"
68
69         for i in `seq 0 1 10000`; do
70                 echo foo > $_file.$i
71         done
72         sync
73
74         for i in `seq 0 2 10000`; do
75                 rm -f $_file.$i
76         done
77         for i in `seq 0 7 10000`; do
78                 rm -f $_file.$i
79         done
80         sync
81
82         # and now use up all the remaining extents larger than 3 blocks
83         dd if=/dev/zero of=$_file.large bs=4k count=1024 > /dev/null 2>&1
84         sync
85 }
86
87 create_attrs()
88 {
89         for foo in `seq 0 1 $1`; do
90                 $SETFATTR_PROG -n user.$foo -v 0xbabe $2
91         done
92 }
93
94 create_data()
95 {
96         for foo in `seq $1 -1 0`; do
97                 let offset=$foo*4096
98                 $XFS_IO_PROG -f -c "pwrite $offset 4096" -c "fsync" $2 > /dev/null 2>&1
99         done
100         xfs_bmap -vp $2
101 }
102
103 # create the designated file with a certain number of attributes and a certain
104 # number of data extents. Reverse order synchronous data writes are used to
105 # create fragmented files, though with the way the filesystem freespace is
106 # fragmented, this is probably not necessary. Create the attributes first so
107 # that they cause the initial fork offset pressure to move it about.
108 #
109 create_target_attr_first()
110 {
111         nattrs=$1
112         file_blocks=$2
113         target=$3
114
115         rm -f $target
116         echo > $target
117         create_attrs $nattrs $target
118         create_data $file_blocks $target
119 }
120
121 # Same as create_target_attr_first, but this time put the attributes on after
122 # the data extents have been created. This puts different pressure on the
123 # inode fork offset, so should exercise the kernel code differently and give us
124 # a different pattern of fork offsets to work with compared to creating the
125 # attrs first.
126 #
127 create_target_attr_last()
128 {
129         nattrs=$1
130         file_blocks=$2
131         target=$3
132
133         rm -f $target
134         echo > $target
135         create_data $file_blocks $target
136         create_attrs $nattrs $target
137 }
138
139 rm -f $seq.full
140
141 # use a small filesystem so we can control freespace easily
142 _scratch_mkfs_sized $((50 * 1024 * 1024)) >> $seq.full 2>&1
143 _scratch_mount
144 fragment_freespace
145
146 # unmount and remount to reset all allocator indexes
147 umount $SCRATCH_MNT
148 _scratch_mount
149
150 # create a range of source files, then fsr them to a known size
151 #
152 # This assumes 256 byte inodes.
153 #
154 # n = number of target fragments for xfs_fsr
155 #       - only a guideline, but forces multiple fragments via sync writes
156 #       - start at 4 as that typically covers all extent format situations
157 #       - end at 12 as that is beyond the maximum that canbe fit in extent
158 #         format
159 # i = number of 2 byte attributes on the file
160 #       - it takes 6 attributes to change the fork offset from the start value
161 #         of 120 bytes to 112 bytes, so we start at 5.
162 #       - 15 is enough to push to btree format, so we stop there.
163 # j = number of data extents on the file
164 #       - start in extent format, but we also want btree format as well, so
165 #         start at 5 so that the number of attributes determines the starting
166 #         format.
167 #       - need enough extents that if they are all 3 blocks in length the final
168 #         format will be dependent on the number of attributes on the inode. 20
169 #         initial single block extents gives us 6-8 extents after defrag which
170 #         puts us right on the threshold of what the extent format can hold.
171
172 targ=$SCRATCH_MNT/fsr_test_file.$$
173 for n in `seq 4 1 12`; do
174         echo "*** n == $n ***" >> $seq.full
175         for i in `seq 5 1 15`; do
176                 for j in `seq 5 1 20`; do
177                         create_target_attr_first $i $j $targ.$i.$j >> $seq.full 2>&1
178                 done
179                 FSRXFSTEST=true xfs_fsr -d -v -C $n $targ.$i.* >> $seq.full 2>&1
180                 xfs_bmap -vp $targ.$i.* >> $seq.full 2>&1
181                 for j in `seq 5 1 20`; do
182                         create_target_attr_last $i $j $targ.$i.$j >> $seq.full 2>&1
183                 done
184                 FSRXFSTEST=true xfs_fsr -d -v -C $n $targ.$i.* >> $seq.full 2>&1
185                 xfs_bmap -vp $targ.$i.* >> $seq.full 2>&1
186         done
187 done
188
189 umount $SCRATCH_MNT
190 echo "--- silence is golden ---"
191 status=0 ; exit