3 # Copyright (C) 2018-2019 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
35 # Run nbdkit with various can_* callbacks defined and with or without
36 # the -r flag, and check that nbdkit constructs the export flags
37 # controlling READ_ONLY, ROTATIONAL, SEND_TRIM, etc. as expected.
39 # We use the shell plugin because it gives maximum control over the
40 # can_* callbacks (at least, max without having to write a C plugin).
45 requires qemu-nbd
--version
47 # This test uses the ‘qemu-nbd --list’ option added in qemu 4.0.
48 if ! qemu-nbd
--help |
grep -sq -- --list; then
49 echo "$0: skipping because qemu-nbd does not support the --list option"
55 cleanup_fn
rm -f $files
58 # See also common/protocol/protocol.h
59 HAS_FLAGS
=$
(( 1 << 0 ))
60 READ_ONLY=$(( 1 << 1 ))
61 SEND_FLUSH=$(( 1 << 2 ))
62 SEND_FUA=$(( 1 << 3 ))
63 ROTATIONAL=$(( 1 << 4 ))
64 SEND_TRIM=$(( 1 << 5 ))
65 SEND_WRITE_ZEROES=$(( 1 << 6 ))
67 CAN_MULTI_CONN=$(( 1 << 8 ))
68 SEND_RESIZE=$(( 1 << 9 ))
69 SEND_CACHE=$(( 1 << 10 ))
73 nbdkit -v -U - "$@" sh - --run 'qemu-nbd --list -k $unixsocket' |
74 grep -E "flags: 0x
" | grep -Eoi '0x[a-f0-9]+' > eflags.out
75 echo -n eflags=; cat eflags.out
77 # Convert hex flags to decimal and assign it to $eflags.
78 eflags=$(printf "%d
" $(cat eflags.out))
83 echo "error
: $@
(actual flags were
$eflags)"
87 #----------------------------------------------------------------------
97 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
98 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF
"
100 #----------------------------------------------------------------------
111 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
112 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF
"
114 #----------------------------------------------------------------------
117 # NBD_FLAG_SEND_WRITE_ZEROES is always set on writable connections
118 # even if can_zero returns false, because nbdkit reckons it can
119 # emulate zeroing using pwrite.
129 [ $eflags -eq $(( HAS_FLAGS|SEND_WRITE_ZEROES|SEND_DF )) ] ||
130 fail "expected HAS_FLAGS|SEND_WRITE_ZEROES|SEND_DF
"
132 #----------------------------------------------------------------------
136 # The -r flag overrides the plugin so this behaves as if can_write is
147 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
148 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF
"
150 #----------------------------------------------------------------------
155 # If writing is not possible then trim and zero are always disabled.
167 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
168 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF
"
170 #----------------------------------------------------------------------
176 # This is a formality, but check it's the same as above.
188 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
189 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF
"
191 #----------------------------------------------------------------------
204 [ $eflags -eq $(( HAS_FLAGS|SEND_TRIM|SEND_WRITE_ZEROES|SEND_DF )) ] ||
205 fail "expected HAS_FLAGS|SEND_TRIM|SEND_WRITE_ZEROES|SEND_DF
"
207 #----------------------------------------------------------------------
215 is_rotational) exit 0 ;;
220 [ $eflags -eq $(( HAS_FLAGS|ROTATIONAL|SEND_WRITE_ZEROES|SEND_DF )) ] ||
221 fail "expected HAS_FLAGS|ROTATIONAL|SEND_WRITE_ZEROES|SEND_DF
"
223 #----------------------------------------------------------------------
232 is_rotational) exit 0 ;;
237 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|ROTATIONAL|SEND_DF )) ] ||
238 fail "expected HAS_FLAGS|READ_ONLY|ROTATIONAL|SEND_DF
"
240 #----------------------------------------------------------------------
248 can_fua) echo "native
" ;;
253 [ $eflags -eq $(( HAS_FLAGS|SEND_FUA|SEND_WRITE_ZEROES|SEND_DF )) ] ||
254 fail "expected HAS_FLAGS|SEND_FUA|SEND_WRITE_ZEROES|SEND_DF
"
256 #----------------------------------------------------------------------
261 # Setting read-only should ignore can_fua.
267 can_fua) echo "native
" ;;
272 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
273 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF
"
275 #----------------------------------------------------------------------
277 # can_multi_conn=true
282 can_multi_conn) exit 0 ;;
287 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF|CAN_MULTI_CONN )) ] ||
288 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF|CAN_MULTI_CONN
"
290 #----------------------------------------------------------------------
297 can_cache) echo "emulate
" ;;
302 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF|SEND_CACHE )) ] ||
303 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF|SEND_CACHE
"