2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2008 Eric Sandeen. All Rights Reserved.
7 # Test mapping around/over holes for sub-page blocks
12 # Override the default cleanup function.
16 # Unmount the V4 filesystem we forcibly created to run this test so that
17 # the post-test wrapup checks won't try to remount the filesystem with
18 # different MOUNT_OPTIONS (specifically, the ones that get screened out by
19 # _force_xfsv4_mount_options) and fail.
24 # Import common functions.
27 # only xfs supported due to use of xfs_bmap
30 # real QA test starts here
33 _scratch_mkfs_xfs >/dev/null 2>&1
35 # For this test we use block size = 1/8 page size
36 pgsize=`$here/src/feature -s`
37 blksize=`expr $pgsize / 8`
38 secsize=`_min_dio_alignment $SCRATCH_DEV`
40 if [ $secsize -gt $blksize ];then
41 _notrun "sector size($secsize) too large for platform page size($pgsize)"
44 # Filter out file mountpoint and physical location info
46 # EXT: FILE-OFFSET BLOCK-RANGE AG AG-OFFSET TOTAL
47 # 0: [0..63]: 160..223 0 (160..223) 64
48 # 1: [64..127]: hole 64
50 # SCRATCH_MNT/testfile4: TYPE
57 tee -a $seqres.full | \
58 sed "s#$SCRATCH_MNT#SCRATCH_MNT#g" | \
60 '$3 ~ /hole/ { print $1 "\t" $3 "\t" ($4 * 512) / blksize; next }
61 $1 ~ /^[0-9]/ { print $1 "\tblocks\t" ($6 * 512) / blksize; next }
62 $1 ~ /^SCRATCH/ { print $1; next }
63 { print $1 "\tTYPE\t" $6 }' blksize=$blksize
66 # Filter out offsets, which vary by blocksize
69 tee -a $seqres.full | \
70 sed -e "s/^[0-9A-Fa-f ]\{7,8\}//"
74 unset XFS_MKFS_OPTIONS
76 # we need 512 byte block size, so crc's are turned off
77 _scratch_mkfs_xfs -m crc=0 -b size=$blksize >/dev/null 2>&1
79 test "$(_get_block_size $SCRATCH_MNT)" = $blksize || \
80 _notrun "Could not get $blksize-byte blocks"
82 # 512b block / 4k page example:
84 #1) Write 1k of data (buffered):
88 # 2) ftruncate back to 256 bytes:
92 # 3) ftruncate out to 4k: ("H" means hole (expected))
94 # |1100|HHHH|HHHH|HHHH|HHHH|HHHH|HHHH|HHHH|
96 # So we should have 1 block of data/0, 7 blocks of holes.
98 # 4) check what's there with a direct IO read
100 # In fact what I get is 1 block of data/0, 1 block of 0's, and 7 blocks of
103 # |1100|0000|GGGG|GGGG|GGGG|GGGG|GGGG|GGGG|
105 # The garbage is in fact stale data from the disk.
107 # Check that we don't get stale data and that the hole is a hole:
110 # Write, truncate in, truncate out
112 -c "pwrite -S 0x11 -b `expr $pgsize / 2` 0 `expr $pgsize / 2`" \
113 -c "truncate `expr $blksize / 2`" \
114 -c "truncate $pgsize" \
115 -t -f $SCRATCH_MNT/testfile1 >> $seqres.full
117 # directio read of entire file
119 -c "pread 0 $pgsize" \
120 -d $SCRATCH_MNT/testfile1 >> $seqres.full
122 xfs_bmap -v $SCRATCH_MNT/testfile1 | _filter_bmap
123 od -x $SCRATCH_MNT/testfile1 | _filter_od
125 # Similar but write another block to create block/hole/block/hole
128 # Write, truncate in, truncate out, write to middle
130 -c "pwrite -S 0x11 -b `expr $pgsize / 2` 0 `expr $pgsize / 2`" \
131 -c "truncate `expr $blksize / 2`" \
132 -c "truncate $pgsize" \
133 -c "pwrite -S 0x22 -b $blksize `expr $blksize \* 4` $blksize" \
134 -t -f $SCRATCH_MNT/testfile2 >> $seqres.full
136 # directio read of entire file
138 -c "pread 0 $pgsize" \
139 -d $SCRATCH_MNT/testfile2 >> $seqres.full
141 xfs_bmap -v $SCRATCH_MNT/testfile2 | _filter_bmap
142 od -x $SCRATCH_MNT/testfile2 | _filter_od
144 # 512 byte block / 4k page example:
146 # direct write 1 page (8 blocks) of "0x11" to 0x1000
147 # map read 1 block, 512 (0x200) at 0
148 # truncate to half a block, 256 (0x100)
149 # truncate to block+1, 513 (0x201)
150 # direct write "0x22" for 1 block at offset 2048 (0x800)
152 # |1111|1111|1111|1111|1111|1111|1111|1111| Write 1's
153 # |MRMR|1111|1111|1111|1111|1111|1111|1111| mapread
154 # |11--| truncate down
155 # |1100|0---| truncate up, block+1
156 # | | |HHHH|HHHH|2222| Write 2's (extending)
159 # |1100|0000|1111|1111|2222|----|----|----| <- potential badness
161 # We're looking for this badness due to mapping over a hole:
162 # Exposes stale data from 0x400 (1024) through 0x800 (2048)
164 # 00000000 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 |................|
166 # 00000100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
168 # 00000400 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 |................| <- BAD
170 # 00000800 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 |""""""""""""""""|
175 # |1100|HHHH|HHHH|HHHH|2222|----|----|----|
179 -c "pwrite -S 0x11 -b $pgsize 0 $pgsize" \
180 -c "mmap -r 0 $blksize" -c "mread 0 $blksize" -c "munmap" \
181 -c "truncate `expr $blksize / 2`" \
182 -c "truncate `expr $blksize + 1`" \
183 -c "pwrite -S 0x22 -b $blksize `expr $pgsize / 2` $blksize" \
184 -t -d -f $SCRATCH_MNT/testfile3 >> $seqres.full
186 xfs_bmap -v $SCRATCH_MNT/testfile3 | _filter_bmap
187 od -x $SCRATCH_MNT/testfile3 | _filter_od
189 # Now try the same thing but write a sector in the middle of that hole
190 # If things go badly stale data will be exposed either side.
191 # This is most interesting for block size > 512 (page size > 4096)
194 # |1100|HHHH|33HH|HHHH|2222|----|----|----|
198 -c "pwrite -S 0x11 -b $pgsize 0 $pgsize" \
199 -c "mmap -r 0 $blksize" -c "mread 0 $blksize" -c "munmap" \
200 -c "truncate `expr $blksize / 2`" \
201 -c "truncate `expr $blksize + 1`" \
202 -c "pwrite -S 0x22 -b $blksize `expr $pgsize / 2` $blksize" \
203 -c "pwrite -S 0x33 -b $secsize `expr $blksize \* 2` $secsize" \
204 -t -d -f $SCRATCH_MNT/testfile4 >> $seqres.full
206 xfs_bmap -v $SCRATCH_MNT/testfile4 | _filter_bmap
207 od -x $SCRATCH_MNT/testfile4 | _filter_od