3 # Copyright (C) 2018 Red Hat Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of Red Hat nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 sock1
=$
(mktemp
-u /tmp
/nbdkit-test-sock.XXXXXX
)
39 sock2
=$
(mktemp
-u /tmp
/nbdkit-test-sock.XXXXXX
)
40 files
="blocksize1.img blocksize1.log $sock1 blocksize1.pid
41 blocksize2.img blocksize2.log $sock2 blocksize2.pid"
44 # Prep images, and check that qemu-io understands the actions we plan on doing.
45 truncate
-s 10M blocksize1.img
46 if ! qemu-io
-f raw
-c 'r 0 1' -c 'w -z 1000 2000' \
47 -c 'w -P 0 1M 2M' -c 'discard 3M 4M' blocksize1.img
; then
48 echo "$0: missing or broken qemu-io"
52 truncate
-s 10M blocksize2.img
54 # For easier debugging, dump the final log files before removing them
58 echo "Log 1 file contents:"
59 cat blocksize1.log ||
:
60 echo "Log 2 file contents:"
61 cat blocksize2.log ||
:
66 # Run two parallel nbdkit; to compare the logs and see what changes.
67 start_nbdkit
-P blocksize1.pid
-U $sock1 \
68 --filter=log
file logfile
=blocksize1.log blocksize1.img
69 start_nbdkit
-P blocksize2.pid
-U $sock2 --filter=blocksize \
70 --filter=log
file logfile
=blocksize2.log blocksize2.img \
71 minblock
=1024 maxdata
=512k maxlen
=1M
73 # Test behavior on short accesses.
74 qemu-io
-f raw
-c 'r 1 1' -c 'w 10001 1' -c 'w -z 20001 1' \
75 -c 'discard 30001 1' "nbd+unix://?socket=$sock1"
76 qemu-io
-f raw
-c 'r 1 1' -c 'w 10001 1' -c 'w -z 20001 1' \
77 -c 'discard 30001 1' "nbd+unix://?socket=$sock2"
79 # Read should round up (qemu-io may round to 512, but we must round to 1024
80 grep 'connection=1 Read .* count=0x\(1\|200\) ' blocksize1.log ||
81 { echo "qemu-io can't pass 1-byte reads"; exit 77; }
82 grep 'connection=1 Read .* offset=0x0 count=0x400 ' blocksize2.log
83 # Write should become read-modify-write
84 grep 'connection=1 Write .* count=0x\(1\|200\) ' blocksize1.log ||
85 { echo "qemu-io can't pass 1-byte writes"; exit 77; }
86 grep 'connection=1 Read .* offset=0x2400 count=0x400 ' blocksize2.log
87 grep 'connection=1 Write .* offset=0x2400 count=0x400 ' blocksize2.log
88 # Zero should become read-modify-write
89 if grep 'connection=1 Zero' blocksize2.log
; then
90 echo "filter should have converted short zero to write"
93 grep 'connection=1 Read .* offset=0x4c00 count=0x400 ' blocksize2.log
94 grep 'connection=1 Write .* offset=0x4c00 count=0x400 ' blocksize2.log
95 # Trim should be discarded
96 if grep 'connection=1 Trim' blocksize2.log
; then
97 echo "filter should have dropped too-small trim"
101 # Test behavior on overlarge accesses.
102 qemu-io
-f raw
-c 'w -P 11 1048575 4094305' -c 'w -z 1050000 1100000' \
103 -c 'r -P 0 1050000 1100000' -c 'r -P 11 3000000 1048577' \
104 -c 'discard 7340031 2097153' "nbd+unix://?socket=$sock1"
105 qemu-io
-f raw
-c 'w -P 11 1048575 4094305' -c 'w -z 1050000 1100000' \
106 -c 'r -P 0 1050000 1100000' -c 'r -P 11 3000000 1048577' \
107 -c 'discard 7340031 2097153' "nbd+unix://?socket=$sock2"
109 # Reads and writes should have been split.
110 test "$(grep -c '\(Read\|Write\) .*count=0x80000 ' blocksize2.log)" -ge 10
111 test "$(grep -c '\(Read\|Write\) .*count=0x[0-9a-f]\{6\} ' blocksize2.log)" = 0
112 # Zero and trim should be split, but at different boundary
113 grep 'Zero .*count=0x100000 ' blocksize2.log
114 test "$(grep -c 'connection=2 Zero' blocksize2.log)" = 2
115 if grep Trim blocksize1.log
; then
116 test "$(grep -c 'connection=2 Trim .*count=0x100000 ' blocksize2.log)" = 2
119 # Final sanity checks.
120 if grep 'offset=0x[0-9a-f]*\([1235679abdef]00\|[0-9a-f]\(.[^0]\|[^0].\)\) ' \
122 echo "filter didn't align offset to 1024";
125 if grep 'count=0x[0-9a-f]*\([1235679abdef]00\|[0-9a-f]\(.[^0]\|[^0].\)\) ' \
127 echo"filter didn't align count to 512";
130 diff -u blocksize1.img blocksize2.img