xref: /openbmc/qemu/tests/qemu-iotests/179 (revision 4c4465ff)
1#!/usr/bin/env bash
2#
3# Test case for write zeroes with unmap
4#
5# Copyright (C) 2017 Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21# creator
22owner=eblake@redhat.com
23
24seq="$(basename $0)"
25echo "QA output created by $seq"
26
27status=1	# failure is the default!
28
29_cleanup()
30{
31	_cleanup_test_img
32    rm -f "$TEST_DIR/blkdebug.conf"
33}
34trap "_cleanup; exit \$status" 0 1 2 3 15
35
36# get standard environment, filters and checks
37. ./common.rc
38. ./common.filter
39
40_supported_fmt qcow2
41_supported_proto file fuse
42_supported_os Linux
43
44# v2 images can't mark clusters as zero
45_unsupported_imgopts compat=0.10
46
47echo
48echo '=== Testing write zeroes with unmap ==='
49echo
50
51TEST_IMG="$TEST_IMG.base" _make_test_img 64M
52_make_test_img -b "$TEST_IMG.base" -F $IMGFMT
53
54# Offsets chosen at or near 2M boundaries so test works at all cluster sizes
55# 8k and larger (smaller clusters fail due to non-contiguous allocations)
56
57# Aligned writes to unallocated cluster should not allocate mapping, but must
58# mark cluster as zero, whether or not unmap was requested
59$QEMU_IO -c "write -z -u 2M 2M" "$TEST_IMG.base" | _filter_qemu_io
60$QEMU_IO -c "write -z 6M 2M" "$TEST_IMG.base" | _filter_qemu_io
61$QEMU_IO -c "map" "$TEST_IMG.base" | _filter_qemu_io
62$QEMU_IMG map --output=json "$TEST_IMG.base" | _filter_qemu_img_map
63
64# Unaligned writes need not allocate mapping if the cluster already reads
65# as zero, but must mark cluster as zero, whether or not unmap was requested
66$QEMU_IO -c "write -z -u 10485761 2097150" "$TEST_IMG.base" | _filter_qemu_io
67$QEMU_IO -c "write -z 14680065 2097150" "$TEST_IMG.base" | _filter_qemu_io
68$QEMU_IO -c "map" "$TEST_IMG.base" | _filter_qemu_io
69$QEMU_IMG map --output=json "$TEST_IMG.base" | _filter_qemu_img_map
70
71# Requesting unmap of normal data must deallocate; omitting unmap should
72# preserve the mapping
73$QEMU_IO -c "write 18M 14M" "$TEST_IMG.base" | _filter_qemu_io
74$QEMU_IO -c "write -z -u 20M 2M" "$TEST_IMG.base" | _filter_qemu_io
75$QEMU_IO -c "write -z 24M 6M" "$TEST_IMG.base" | _filter_qemu_io
76$QEMU_IO -c "map" "$TEST_IMG.base" | _filter_qemu_io
77$QEMU_IMG map --output=json "$TEST_IMG.base" | _filter_qemu_img_map
78
79# Likewise when writing on already-mapped zero data
80$QEMU_IO -c "write -z -u 26M 2M" "$TEST_IMG.base" | _filter_qemu_io
81$QEMU_IO -c "write -z 28M 2M" "$TEST_IMG.base" | _filter_qemu_io
82$QEMU_IO -c "map" "$TEST_IMG.base" | _filter_qemu_io
83$QEMU_IMG map --output=json "$TEST_IMG.base" | _filter_qemu_img_map
84
85# Writing on unmapped zeroes does not allocate
86$QEMU_IO -c "write -z 32M 8M" "$TEST_IMG.base" | _filter_qemu_io
87$QEMU_IO -c "write -z -u 34M 2M" "$TEST_IMG.base" | _filter_qemu_io
88$QEMU_IO -c "write -z 36M 2M" "$TEST_IMG.base" | _filter_qemu_io
89$QEMU_IO -c "map" "$TEST_IMG.base" | _filter_qemu_io
90$QEMU_IMG map --output=json "$TEST_IMG.base" | _filter_qemu_img_map
91
92# Writing zero overrides a backing file, regardless of backing cluster type
93$QEMU_IO -c "write -z 40M 8M" "$TEST_IMG.base" | _filter_qemu_io
94$QEMU_IO -c "write 48M 8M" "$TEST_IMG.base" | _filter_qemu_io
95$QEMU_IO -c "write -z -u 42M 2M" "$TEST_IMG" | _filter_qemu_io
96$QEMU_IO -c "write -z 44M 2M" "$TEST_IMG" | _filter_qemu_io
97$QEMU_IO -c "write -z -u 50M 2M" "$TEST_IMG" | _filter_qemu_io
98$QEMU_IO -c "write -z 52M 2M" "$TEST_IMG" | _filter_qemu_io
99$QEMU_IO -c "write -z -u 58M 2M" "$TEST_IMG" | _filter_qemu_io
100$QEMU_IO -c "write -z 60M 2M" "$TEST_IMG" | _filter_qemu_io
101$QEMU_IO -c "map" "$TEST_IMG" | _filter_qemu_io
102$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map
103
104# Final check that mappings are correct and images are still sane
105TEST_IMG="$TEST_IMG.base" _check_test_img
106_check_test_img
107
108echo
109echo '=== Testing cache optimization ==='
110echo
111
112BLKDBG_TEST_IMG="blkdebug:$TEST_DIR/blkdebug.conf:$TEST_IMG.base"
113
114cat > "$TEST_DIR/blkdebug.conf" <<EOF
115[inject-error]
116event = "l2_update"
117errno = "5"
118immediately = "on"
119once = "off"
120EOF
121
122# None of the following writes should trigger an L2 update, because the
123# cluster already reads as zero, and we don't have to change allocation
124$QEMU_IO -c "w -z -u 20M 2M" "$BLKDBG_TEST_IMG" | _filter_qemu_io
125$QEMU_IO -c "w -z 20M 2M" "$BLKDBG_TEST_IMG" | _filter_qemu_io
126$QEMU_IO -c "w -z 28M 2M" "$BLKDBG_TEST_IMG" | _filter_qemu_io
127
128# success, all done
129echo '*** done'
130status=0
131