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