1#!/usr/bin/env bash
2#
3# Test concurrent cluster allocations
4#
5# Copyright (C) 2012 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=kwolf@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}
33trap "_cleanup; exit \$status" 0 1 2 3 15
34
35# get standard environment, filters and checks
36. ./common.rc
37. ./common.filter
38
39_supported_fmt qcow2
40_supported_proto file
41
42CLUSTER_SIZE=64k
43size=128M
44
45echo
46echo "== creating backing file for COW tests =="
47
48_make_test_img $size
49
50backing_io()
51{
52    local offset=$1
53    local sectors=$2
54    local op=$3
55    local pattern=0
56    local cur_sec=0
57
58    for ((i=0;i<=$((sectors - 1));i++)); do
59        cur_sec=$((offset / 65536 + i))
60        pattern=$(( ( (cur_sec % 128) + (cur_sec / 128)) % 128 ))
61
62        echo "$op -P $pattern $((cur_sec * 64))k 64k"
63    done
64}
65
66backing_io 0 32 write | $QEMU_IO "$TEST_IMG" | _filter_qemu_io
67
68mv "$TEST_IMG" "$TEST_IMG.base"
69
70_make_test_img -b "$TEST_IMG.base" 6G
71
72echo
73echo "== Some concurrent requests touching the same cluster =="
74
75overlay_io()
76{
77# Allocate middle of cluster 1, then write to somewhere before and after it
78cat  <<EOF
79break write_aio A
80aio_write -P 10 0x18000 0x2000
81wait_break A
82
83aio_write -P 11 0x12000 0x2000
84aio_write -P 12 0x1c000 0x2000
85
86resume A
87aio_flush
88EOF
89
90# Sequential write case: Alloc middle of cluster 2, then write overlapping
91# to next cluster
92cat  <<EOF
93break write_aio A
94aio_write -P 20 0x28000 0x2000
95wait_break A
96aio_write -P 21 0x2a000 0x10000
97resume A
98aio_flush
99EOF
100
101# The same with a gap between both requests
102cat  <<EOF
103break write_aio A
104aio_write -P 40 0x48000 0x2000
105wait_break A
106aio_write -P 41 0x4c000 0x10000
107resume A
108aio_flush
109EOF
110
111# Sequential write, but the next cluster is already allocated
112cat  <<EOF
113write -P 70 0x76000 0x8000
114aio_flush
115break write_aio A
116aio_write -P 60 0x66000 0x2000
117wait_break A
118aio_write -P 61 0x6a000 0xe000
119resume A
120aio_flush
121EOF
122
123# Sequential write, but the next cluster is already allocated
124# and phyiscally in the right position
125cat  <<EOF
126write -P 89 0x80000 0x1000
127write -P 90 0x96000 0x8000
128aio_flush
129discard 0x80000 0x10000
130aio_flush
131break write_aio A
132aio_write -P 80 0x86000 0x2000
133wait_break A
134aio_write -P 81 0x8a000 0xe000
135resume A
136aio_flush
137EOF
138
139# Sequential write, and the next cluster is compressed
140cat  <<EOF
141write    -P 109 0xa0000 0x1000
142write -c -P 110 0xb0000 0x10000
143aio_flush
144discard 0xa0000 0x10000
145aio_flush
146break write_aio A
147aio_write -P 100 0xa6000 0x2000
148wait_break A
149aio_write -P 101 0xaa000 0xe000
150resume A
151aio_flush
152EOF
153
154# Reverse sequential write
155cat  <<EOF
156break write_aio A
157aio_write -P 121 0xdc000 0x2000
158wait_break A
159aio_write -P 120 0xc4000 0x18000
160resume A
161aio_flush
162EOF
163
164# Reverse sequential write with a gap
165cat  <<EOF
166break write_aio A
167aio_write -P 141 0xfc000 0x2000
168wait_break A
169aio_write -P 140 0xe4000 0x14000
170resume A
171aio_flush
172EOF
173
174# Allocate an area in the middle and then overwrite with a larger request
175cat  <<EOF
176break write_aio A
177aio_write -P 161 0x10c000 0x8000
178wait_break A
179aio_write -P 160 0x104000 0x18000
180resume A
181aio_flush
182EOF
183}
184
185overlay_io | $QEMU_IO blkdebug::"$TEST_IMG" | _filter_qemu_io |\
186	sed -e 's/bytes at offset [0-9]*/bytes at offset XXX/g'
187
188echo
189echo "== Verify image content =="
190
191verify_io()
192{
193    if ($QEMU_IMG info -U -f "$IMGFMT" "$TEST_IMG" | grep "compat: 0.10" > /dev/null); then
194        # For v2 images, discarded clusters are read from the backing file
195        # Keep the variable empty so that the backing file value can be used as
196        # the default below
197        discarded=
198    else
199        # Discarded clusters are zeroed for v3 or later
200        discarded=0
201    fi
202
203    echo read -P 0 0 0x10000
204
205    echo read -P 1  0x10000 0x2000
206    echo read -P 11 0x12000 0x2000
207    echo read -P 1  0x14000 0x4000
208    echo read -P 10 0x18000 0x2000
209    echo read -P 1  0x1a000 0x2000
210    echo read -P 12 0x1c000 0x2000
211    echo read -P 1  0x1e000 0x2000
212
213    echo read -P 2  0x20000 0x8000
214    echo read -P 20 0x28000 0x2000
215    echo read -P 21 0x2a000 0x10000
216    echo read -P 3  0x3a000 0x6000
217
218    echo read -P 4  0x40000 0x8000
219    echo read -P 40 0x48000 0x2000
220    echo read -P 4  0x4a000 0x2000
221    echo read -P 41 0x4c000 0x10000
222    echo read -P 5  0x5c000 0x4000
223
224    echo read -P 6  0x60000 0x6000
225    echo read -P 60 0x66000 0x2000
226    echo read -P 6  0x68000 0x2000
227    echo read -P 61 0x6a000 0xe000
228    echo read -P 70 0x78000 0x6000
229    echo read -P 7  0x7e000 0x2000
230
231    echo read -P ${discarded:-8} 0x80000 0x6000
232    echo read -P 80 0x86000 0x2000
233    echo read -P ${discarded:-8} 0x88000 0x2000
234    echo read -P 81 0x8a000 0xe000
235    echo read -P 90 0x98000 0x6000
236    echo read -P 9  0x9e000 0x2000
237
238    echo read -P ${discarded:-10} 0xa0000 0x6000
239    echo read -P 100 0xa6000 0x2000
240    echo read -P ${discarded:-10} 0xa8000 0x2000
241    echo read -P 101 0xaa000 0xe000
242    echo read -P 110 0xb8000 0x8000
243
244    echo read -P 12  0xc0000 0x4000
245    echo read -P 120 0xc4000 0x18000
246    echo read -P 121 0xdc000 0x2000
247    echo read -P 13  0xde000 0x2000
248
249    echo read -P 14  0xe0000 0x4000
250    echo read -P 140 0xe4000 0x14000
251    echo read -P 15  0xf8000 0x4000
252    echo read -P 141 0xfc000 0x2000
253    echo read -P 15  0xfe000 0x2000
254
255    echo read -P 16  0x100000 0x4000
256    echo read -P 160 0x104000 0x8000
257    # Undefined content for 0x10c000 0x8000
258    echo read -P 160 0x114000 0x8000
259    echo read -P 17  0x11c000 0x4000
260}
261
262verify_io | $QEMU_IO "$TEST_IMG" | _filter_qemu_io
263
264_check_test_img
265
266# success, all done
267echo "*** done"
268rm -f $seq.full
269status=0
270