summaryrefslogtreecommitdiffstats
path: root/tests/qemu-iotests/154
blob: 24e29ae2ffe3e12d2c67c1da6284fe25f48e93e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/env bash
# group: rw auto backing quick
#
# qcow2 specific bdrv_pwrite_zeroes tests with backing files (complements 034)
#
# Copyright (C) 2016-2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# creator
owner=kwolf@redhat.com

seq=`basename $0`
echo "QA output created by $seq"

status=1	# failure is the default!

_cleanup()
{
	_cleanup_test_img
}
trap "_cleanup; exit \$status" 0 1 2 3 15

# get standard environment, filters and checks
. ./common.rc
. ./common.filter

_supported_fmt qcow2
_supported_proto file fuse
_supported_os Linux

CLUSTER_SIZE=4k
size=$((128 * 1024 * 1024))

# This test requires zero clusters, added in v3 images
_unsupported_imgopts compat=0.10

echo
echo == backing file contains zeros ==

CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT

# Make sure that the whole cluster is allocated even for partial write_zeroes
# when the backing file contains zeros

# X = non-zero data sector in backing file
# - = sector unallocated in whole backing chain
# 0 = sector touched by write_zeroes request

# 1. Tail unaligned:    00 00 -- --
# 2. Head unaligned:    -- -- 00 00
# 3. Both unaligned:    -- 00 00 --
# 4. Both, 2 clusters:  -- -- -- 00 | 00 -- -- --

$QEMU_IO -c "write -z 0 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "write -z 10k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "write -z 17k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "write -z 27k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == backing file contains non-zero data before write_zeroes ==

CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT

# Single cluster; non-zero data at the cluster start
# ... | XX -- 00 -- | ...
$QEMU_IO -c "write -P 0x11 32k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 34k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 32k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 33k 3k" "$TEST_IMG" | _filter_qemu_io

# Single cluster; non-zero data exists, but not at the cluster start
# ... | -- XX 00 -- | ...
$QEMU_IO -c "write -P 0x11 65k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 66k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 65k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 64k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 66k 2k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == backing file contains non-zero data after write_zeroes ==

CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT

# Single cluster; non-zero data directly after request
# ... | -- 00 XX -- | ...
$QEMU_IO -c "write -P 0x11 34k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 33k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 32k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 34k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 35k 1k" "$TEST_IMG" | _filter_qemu_io

# Single cluster; non-zero data exists, but not directly after request
# ... | -- 00 -- XX | ...
$QEMU_IO -c "write -P 0x11 43k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 41k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 43k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 40k 3k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == write_zeroes covers non-zero data ==

CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT

# non-zero data at front of request
# Backing file: -- XX -- --
# Active layer: -- 00 00 --

$QEMU_IO -c "write -P 0x11 5k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 5k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 4k 4k" "$TEST_IMG" | _filter_qemu_io

# non-zero data at end of request
# Backing file: -- -- XX --
# Active layer: -- 00 00 --

$QEMU_IO -c "write -P 0x11 14k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 13k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 12k 4k" "$TEST_IMG" | _filter_qemu_io

# non-zero data matches size of request
# Backing file: -- XX XX --
# Active layer: -- 00 00 --

$QEMU_IO -c "write -P 0x11 21k 2k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 21k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 20k 4k" "$TEST_IMG" | _filter_qemu_io

# non-zero data smaller than request
# Backing file: -- -X X- --
# Active layer: -- 00 00 --

$QEMU_IO -c "write -P 0x11 30208 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 29k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 28k 4k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == spanning two clusters, non-zero before request ==

CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT

# Two clusters; non-zero data before request:
# 1. At cluster start:          32k: XX -- -- 00 | 00 -- -- --
# 2. Between unallocated space: 48k: -- XX -- 00 | 00 -- -- --
# 3. Directly before request:   64k: -- -- XX 00 | 00 -- -- --

$QEMU_IO -c "write -P 0x11 32k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 35k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 32k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 33k 7k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IO -c "write -P 0x11 49k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 51k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 48k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 49k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 50k 6k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IO -c "write -P 0x11 66k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 67k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 64k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 66k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 67k 5k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == spanning two clusters, non-zero after request ==

CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT

# Two clusters; non-zero data after request:
# 1. Directly after request:    32k: -- -- -- 00 | 00 XX -- --
# 2. Between unallocated space: 48k: -- -- -- 00 | 00 -- XX --
# 3. At cluster end:            64k: -- -- -- 00 | 00 -- -- XX

$QEMU_IO -c "write -P 0x11 37k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 35k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 32k 5k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 37k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 38k 2k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IO -c "write -P 0x11 54k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 51k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 48k 6k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 54k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 55k 1k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IO -c "write -P 0x11 71k 1k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 67k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 64k 7k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 71k 1k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == spanning two clusters, partially overwriting backing file ==

CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT

# Backing file: -- -- XX XX | XX XX -- --
# Active layer: -- -- XX 00 | 00 XX -- --

$QEMU_IO -c "write -P 0x11 2k 4k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 3k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 0k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 2k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 3k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 5k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 6k 2k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == spanning multiple clusters, non-zero in first cluster ==

CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT

# Backing file: 64k: XX XX -- -- | -- -- -- -- | -- -- -- --
# Active layer: 64k: XX XX 00 00 | 00 00 00 00 | 00 -- -- --

$QEMU_IO -c "write -P 0x11 64k 2k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 66k 7k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 64k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 66k 10k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == spanning multiple clusters, non-zero in intermediate cluster ==

CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT

# Backing file: 64k: -- -- -- -- | -- XX XX -- | -- -- -- --
# Active layer: 64k: -- -- 00 00 | 00 00 00 00 | 00 -- -- --

$QEMU_IO -c "write -P 0x11 69k 2k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 66k 7k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 64k 12k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == spanning multiple clusters, non-zero in final cluster ==

CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT

# Backing file: 64k: -- -- -- -- | -- -- -- -- | -- -- XX XX
# Active layer: 64k: -- -- 00 00 | 00 00 00 00 | 00 -- XX XX

$QEMU_IO -c "write -P 0x11 74k 2k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 66k 7k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 64k 10k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 74k 2k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == spanning multiple clusters, partially overwriting backing file ==

CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT

# Backing file: 64k: -- XX XX XX | XX XX XX XX | XX XX XX --
# Active layer: 64k: -- XX 00 00 | 00 00 00 00 | 00 XX XX --

$QEMU_IO -c "write -P 0x11 65k 10k" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z 66k 7k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 64k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 65k 1k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 66k 7k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0x11 73k 2k" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 75k 1k" "$TEST_IMG" | _filter_qemu_io

$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == unaligned image tail cluster, no allocation needed ==

# With no backing file, write to all or part of unallocated partial cluster
# will mark the cluster as zero, but does not allocate.
# Re-create the image each time to get back to unallocated clusters.

# Write at the front: sector-wise, the request is: 128m... | 00 -- -- --
_make_test_img $((size + 2048))
$QEMU_IO -c "write -z $size 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Write at the back: sector-wise, the request is: 128m... | -- -- -- 00
_make_test_img $((size + 2048))
$QEMU_IO -c "write -z $((size + 1536)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Write at middle: sector-wise, the request is: 128m... | -- 00 00 --
_make_test_img $((size + 2048))
$QEMU_IO -c "write -z $((size + 512)) 1024" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Write entire cluster: sector-wise, the request is: 128m... | 00 00 00 00
_make_test_img $((size + 2048))
$QEMU_IO -c "write -z $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Repeat with backing file holding unallocated cluster.
CLUSTER_SIZE=2048 TEST_IMG="$TEST_IMG.base" _make_test_img $((size + 1024))

# Write at the front: sector-wise, the request is:
# backing: 128m... | -- --
# active:  128m... | 00 -- -- --
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $((size + 2048))
$QEMU_IO -c "write -z $size 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Write at the back: sector-wise, the request is:
# backing: 128m... | -- --
# active:  128m... | -- -- -- 00
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $((size + 2048))
$QEMU_IO -c "write -z $((size + 1536)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Write at middle: sector-wise, the request is:
# backing: 128m... | -- --
# active:  128m... | -- 00 00 --
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $((size + 2048))
$QEMU_IO -c "write -z $((size + 512)) 1024" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Write entire cluster: sector-wise, the request is:
# backing: 128m... | -- --
# active:  128m... | 00 00 00 00
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $((size + 2048))
$QEMU_IO -c "write -z $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Repeat with backing file holding zero'd cluster
$QEMU_IO -c "write -z $size 512" "$TEST_IMG.base" | _filter_qemu_io

# Write at the front: sector-wise, the request is:
# backing: 128m... | 00 00
# active:  128m... | 00 -- -- --
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $((size + 2048))
$QEMU_IO -c "write -z $size 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Write at the back: sector-wise, the request is:
# backing: 128m... | 00 00
# active:  128m... | -- -- -- 00
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $((size + 2048))
$QEMU_IO -c "write -z $((size + 1536)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Write at middle: sector-wise, the request is:
# backing: 128m... | 00 00
# active:  128m... | -- 00 00 --
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $((size + 2048))
$QEMU_IO -c "write -z $((size + 512)) 1024" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Write entire cluster: sector-wise, the request is:
# backing: 128m... | 00 00
# active:  128m... | 00 00 00 00
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $((size + 2048))
$QEMU_IO -c "write -z $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 2048" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# A preallocated cluster maintains its allocation, whether it stays as
# data due to a partial write:
# Convert 128m... | XX XX => ... | XX 00
_make_test_img $((size + 1024))
$QEMU_IO -c "write -P 1 $((size)) 1024" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "write -z $((size + 512)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 1 $((size)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 $((size + 512)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 1024" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# or because it is the entire cluster and can use the zero flag:
# Convert 128m... | XX XX => ... | 00 00
$QEMU_IO -c "write -z $((size)) 1024" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "alloc $size 1024" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 $size 1024" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

echo
echo == unaligned image tail cluster, allocation required ==

# Write beyond backing file must COW
# Backing file: 128m... | XX --
# Active layer: 128m... | -- -- 00 --
CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $((size + 1024))
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $((size + 2048))
$QEMU_IO -c "write -P 1 $((size)) 512" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z $((size + 1024)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 1 $((size)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 $((size + 512)) 1536" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# Writes at boundaries of (partial) cluster must not lose mid-cluster data
# Backing file: 128m: ... | -- XX
# Active layer: 128m: ... | 00 -- -- 00
CLUSTER_SIZE=512 TEST_IMG="$TEST_IMG.base" _make_test_img $((size + 1024))
_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $((size + 2048))
$QEMU_IO -c "write -P 1 $((size + 512)) 512" "$TEST_IMG.base" | _filter_qemu_io
$QEMU_IO -c "write -z $((size)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 $((size)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 1 $((size + 512)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 $((size + 1024)) 1024" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "write -z $((size + 1536)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 $((size)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 1 $((size + 512)) 512" "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c "read -P 0 $((size + 1024)) 1024" "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=json "$TEST_IMG" | _filter_qemu_img_map

# success, all done
echo "*** done"
rm -f $seq.full
status=0