fstests: fix broken _require_scratch usage
[xfstests-dev.git] / tests / xfs / 194
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2008 Eric Sandeen.  All Rights Reserved.
4 #
5 # FS QA Test No. 194
6 #
7 # Test mapping around/over holes for sub-page blocks
8 #
9 seq=`basename $0`
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
12
13 here=`pwd`
14 tmp=/tmp/$$
15 status=1        # failure is the default!
16 trap "_cleanup; exit \$status" 0 1 2 3 15
17
18 _cleanup()
19 {
20     cd /
21     rm -f $tmp.*
22 }
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27
28 # only xfs supported due to use of xfs_bmap
29 _supported_fs xfs
30 _supported_os Linux
31
32 # real QA test starts here
33 rm -f $seqres.full
34
35 _require_scratch
36 _scratch_mkfs_xfs >/dev/null 2>&1
37
38 # For this test we use block size = 1/8 page size
39 pgsize=`$here/src/feature -s`
40 blksize=`expr $pgsize / 8`
41 secsize=`_min_dio_alignment $SCRATCH_DEV`
42
43 if [ $secsize -gt $blksize ];then
44         _notrun "sector size($secsize) too large for platform page size($pgsize)"
45 fi
46
47 # Filter out file mountpoint and physical location info
48 # Input:
49 #  EXT: FILE-OFFSET      BLOCK-RANGE      AG AG-OFFSET        TOTAL
50 #  0: [0..63]:         160..223          0 (160..223)          64
51 #  1: [64..127]:       hole                                    64
52 # Output:
53 #  SCRATCH_MNT/testfile4:       TYPE
54 #  EXT: TYPE    TOTAL
55 #  0:   blocks  1
56 #  1:   hole    1
57
58 _filter_bmap()
59 {
60     tee -a $seqres.full | \
61     sed "s#$SCRATCH_MNT#SCRATCH_MNT#g" | \
62     awk \
63         '$3 ~ /hole/     { print $1 "\t" $3 "\t" ($4 * 512) / blksize; next }
64          $1 ~ /^[0-9]/   { print $1 "\tblocks\t" ($6 * 512) / blksize; next }
65          $1 ~ /^SCRATCH/ { print $1; next }
66                          { print $1 "\tTYPE\t" $6 }' blksize=$blksize
67 }
68
69 # Filter out offsets, which vary by blocksize
70 _filter_od()
71 {
72     tee -a $seqres.full | \
73     sed -e "s/^[0-9A-Fa-f ]\{7,8\}//"
74 }
75
76 unset MKFS_OPTIONS
77 unset XFS_MKFS_OPTIONS
78
79 # we need 512 byte block size, so crc's are turned off
80 _scratch_mkfs_xfs -m crc=0 -b size=$blksize >/dev/null 2>&1
81 _scratch_mount
82
83 # 512b block / 4k page example:
84 #
85 #1) Write 1k of data (buffered):
86 #
87 # |1111|1111|
88 #
89 # 2) ftruncate back to 256 bytes:
90 #
91 # |1100|
92 #
93 # 3) ftruncate out to 4k: ("H" means hole (expected))
94 #
95 # |1100|HHHH|HHHH|HHHH|HHHH|HHHH|HHHH|HHHH|
96 #
97 # So we should have 1 block of data/0, 7 blocks of holes.
98 #
99 # 4) check what's there with a direct IO read
100 #
101 # In fact what I get is 1 block of data/0, 1 block of 0's, and 7 blocks of
102 # garbage:
103 #
104 # |1100|0000|GGGG|GGGG|GGGG|GGGG|GGGG|GGGG|
105 #
106 # The garbage is in fact stale data from the disk.
107 #
108 # Check that we don't get stale data and that the hole is a hole:
109
110 echo "== Test 1 =="
111 # Write, truncate in, truncate out
112 xfs_io \
113 -c "pwrite -S 0x11 -b `expr $pgsize / 2`  0 `expr $pgsize / 2`" \
114 -c "truncate `expr $blksize / 2`" \
115 -c "truncate $pgsize" \
116 -t -f $SCRATCH_MNT/testfile1 >> $seqres.full
117
118 # directio read of entire file
119 xfs_io \
120 -c "pread 0 $pgsize" \
121 -d $SCRATCH_MNT/testfile1 >> $seqres.full
122
123 xfs_bmap -v $SCRATCH_MNT/testfile1 | _filter_bmap
124 od -x $SCRATCH_MNT/testfile1 | _filter_od
125
126 # Similar but write another block to create block/hole/block/hole
127
128 echo "== Test 2 =="
129 # Write, truncate in, truncate out, write to middle
130 xfs_io \
131 -c "pwrite -S 0x11 -b `expr $pgsize / 2`  0 `expr $pgsize / 2`" \
132 -c "truncate `expr $blksize / 2`" \
133 -c "truncate $pgsize" \
134 -c "pwrite -S 0x22 -b $blksize `expr $blksize \* 4` $blksize" \
135 -t -f $SCRATCH_MNT/testfile2 >> $seqres.full
136
137 # directio read of entire file
138 xfs_io \
139 -c "pread 0 $pgsize" \
140 -d $SCRATCH_MNT/testfile2 >> $seqres.full
141
142 xfs_bmap -v $SCRATCH_MNT/testfile2 | _filter_bmap
143 od -x $SCRATCH_MNT/testfile2 | _filter_od
144
145 # 512 byte block / 4k page example:
146
147 # direct write 1 page (8 blocks) of "0x11" to 0x1000
148 # map read 1 block, 512 (0x200) at 0
149 # truncate to half a block, 256 (0x100)
150 # truncate to block+1, 513 (0x201)
151 # direct write "0x22" for 1 block at offset 2048 (0x800)
152
153 # |1111|1111|1111|1111|1111|1111|1111|1111|     Write 1's
154 # |MRMR|1111|1111|1111|1111|1111|1111|1111|     mapread
155 # |11--|                                        truncate down
156 # |1100|0---|                                   truncate up, block+1
157 # |    |    |HHHH|HHHH|2222|                    Write 2's (extending)
158
159 #           |uptodate?|
160 # |1100|0000|1111|1111|2222|----|----|----|     <- potential badness
161
162 # We're looking for this badness due to mapping over a hole:
163 # Exposes stale data from 0x400 (1024) through 0x800 (2048)
164
165 # 00000000  11 11 11 11 11 11 11 11  11 11 11 11 11 11 11 11  |................|
166 # *
167 # 00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
168 # *
169 # 00000400  11 11 11 11 11 11 11 11  11 11 11 11 11 11 11 11  |................| <- BAD
170 # *
171 # 00000800  22 22 22 22 22 22 22 22  22 22 22 22 22 22 22 22  |""""""""""""""""|
172 # *
173 # 00000a00
174
175 # We *should* get:
176 # |1100|HHHH|HHHH|HHHH|2222|----|----|----|
177
178 echo "== Test 3 =="
179 xfs_io \
180 -c "pwrite -S 0x11 -b $pgsize 0 $pgsize" \
181 -c "mmap -r 0 $blksize" -c "mread 0 $blksize" -c "munmap" \
182 -c "truncate `expr $blksize / 2`" \
183 -c "truncate `expr $blksize + 1`" \
184 -c "pwrite -S 0x22 -b $blksize `expr $pgsize / 2` $blksize" \
185 -t -d -f $SCRATCH_MNT/testfile3 >> $seqres.full
186
187 xfs_bmap -v $SCRATCH_MNT/testfile3 | _filter_bmap
188 od -x $SCRATCH_MNT/testfile3 | _filter_od
189
190 # Now try the same thing but write a sector in the middle of that hole
191 # If things go badly stale data will be exposed either side.
192 # This is most interesting for block size > 512 (page size > 4096)
193
194 # We *should* get:
195 # |1100|HHHH|33HH|HHHH|2222|----|----|----|
196
197 echo "== Test 4 =="
198 xfs_io \
199 -c "pwrite -S 0x11 -b $pgsize 0 $pgsize" \
200 -c "mmap -r 0 $blksize" -c "mread 0 $blksize" -c "munmap" \
201 -c "truncate `expr $blksize / 2`" \
202 -c "truncate `expr $blksize + 1`" \
203 -c "pwrite -S 0x22 -b $blksize `expr $pgsize / 2` $blksize" \
204 -c "pwrite -S 0x33 -b $secsize `expr $blksize \* 2` $secsize" \
205 -t -d -f $SCRATCH_MNT/testfile4 >> $seqres.full
206
207 xfs_bmap -v $SCRATCH_MNT/testfile4 | _filter_bmap
208 od -x $SCRATCH_MNT/testfile4 | _filter_od
209
210 # success, all done
211 status=0
212 exit