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