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