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