nbd patches for 2022-01-28

- Hanna Reitz: regression fix for block status caching
 - Philippe Mathieu-Daude: documentation formatting
 - Nir Soffer: dead code removal
 -----BEGIN PGP SIGNATURE-----
 
 iQEyBAABCAAdFiEEccLMIrHEYCkn0vOqp6FrSiUnQ2oFAmH0dSoACgkQp6FrSiUn
 Q2oCswf43/8/xbv/lhxSiYBTFIQVS08P3zhLHXeIOrVpgjmUXsuSdPIK81dwLmcO
 nxvwUNiUJYrxouWYxLWb9MQlJnFk8nu1zxHxH+P9YnrbFz4A7Sn8JrKLmSdvXax/
 yds6ESkGDW4KR59WCZyCCug5jkc3AwvtlVXE3ErMbHKGp0P2Cd7YbmclunYIRZlW
 pwRgiCDViZkOe64kV2fVOntW03GlOeAIXYelL0Svc0K+HUDJvgZUq4VZCobfJOIz
 3j95tCmRoZa9Jqy4UpBOn9BLCdaL2pQ4J4o2SiboEYxoiIleP4lvWKpISOs1oJz4
 fBLO1xOuoED0jrgREx5bBIqSjtSO
 =B+1h
 -----END PGP SIGNATURE-----

Merge remote-tracking branch 'remotes/ericb/tags/pull-nbd-2022-01-28' into staging

nbd patches for 2022-01-28

- Hanna Reitz: regression fix for block status caching
- Philippe Mathieu-Daude: documentation formatting
- Nir Soffer: dead code removal

# gpg: Signature made Fri 28 Jan 2022 22:58:50 GMT
# gpg:                using RSA key 71C2CC22B1C4602927D2F3AAA7A16B4A2527436A
# gpg: Good signature from "Eric Blake <eblake@redhat.com>" [full]
# gpg:                 aka "Eric Blake (Free Software Programmer) <ebb9@byu.net>" [full]
# gpg:                 aka "[jpeg image of size 6874]" [full]
# Primary key fingerprint: 71C2 CC22 B1C4 6029 27D2  F3AA A7A1 6B4A 2527 436A

* remotes/ericb/tags/pull-nbd-2022-01-28:
  iotests/block-status-cache: New test
  block/io: Update BSC only if want_zero is true
  qapi/block: Cosmetic change in BlockExportType schema
  nbd/server.c: Remove unused field

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
staging
Peter Maydell 2022-01-30 10:00:28 +00:00
commit bfc3db5cf6
5 changed files with 151 additions and 3 deletions

View File

@ -2497,8 +2497,12 @@ static int coroutine_fn bdrv_co_block_status(BlockDriverState *bs,
* non-protocol nodes, and then it is never used. However, filling
* the cache requires an RCU update, so double check here to avoid
* such an update if possible.
*
* Check want_zero, because we only want to update the cache when we
* have accurate information about what is zero and what is data.
*/
if (ret == (BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID) &&
if (want_zero &&
ret == (BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID) &&
QLIST_EMPTY(&bs->children))
{
/*

View File

@ -77,7 +77,6 @@ static int system_errno_to_nbd_errno(int err)
typedef struct NBDRequestData NBDRequestData;
struct NBDRequestData {
QSIMPLEQ_ENTRY(NBDRequestData) entry;
NBDClient *client;
uint8_t *data;
bool complete;

View File

@ -278,7 +278,8 @@
##
{ 'enum': 'BlockExportType',
'data': [ 'nbd',
{ 'name': 'vhost-user-blk', 'if': 'CONFIG_VHOST_USER_BLK_SERVER' },
{ 'name': 'vhost-user-blk',
'if': 'CONFIG_VHOST_USER_BLK_SERVER' },
{ 'name': 'fuse', 'if': 'CONFIG_FUSE' } ] }
##

View File

@ -0,0 +1,139 @@
#!/usr/bin/env python3
# group: rw quick
#
# Test cases for the block-status cache.
#
# Copyright (C) 2022 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/>.
#
import os
import signal
import iotests
from iotests import qemu_img_create, qemu_img_pipe, qemu_nbd
image_size = 1 * 1024 * 1024
test_img = os.path.join(iotests.test_dir, 'test.img')
nbd_pidfile = os.path.join(iotests.test_dir, 'nbd.pid')
nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock')
class TestBscWithNbd(iotests.QMPTestCase):
def setUp(self) -> None:
"""Just create an empty image with a read-only NBD server on it"""
assert qemu_img_create('-f', iotests.imgfmt, test_img,
str(image_size)) == 0
# Pass --allocation-depth to enable the qemu:allocation-depth context,
# which we are going to query to provoke a block-status inquiry with
# want_zero=false.
assert qemu_nbd(f'--socket={nbd_sock}',
f'--format={iotests.imgfmt}',
'--persistent',
'--allocation-depth',
'--read-only',
f'--pid-file={nbd_pidfile}',
test_img) \
== 0
def tearDown(self) -> None:
with open(nbd_pidfile, encoding='utf-8') as f:
pid = int(f.read())
os.kill(pid, signal.SIGTERM)
os.remove(nbd_pidfile)
os.remove(test_img)
def test_with_zero_bug(self) -> None:
"""
Verify that the block-status cache is not corrupted by a
want_zero=false call.
We can provoke a want_zero=false call with `qemu-img map` over NBD with
x-dirty-bitmap=qemu:allocation-depth, so we first run a normal `map`
(which results in want_zero=true), then using said
qemu:allocation-depth context, and finally another normal `map` to
verify that the cache has not been corrupted.
"""
nbd_img_opts = f'driver=nbd,server.type=unix,server.path={nbd_sock}'
nbd_img_opts_alloc_depth = nbd_img_opts + \
',x-dirty-bitmap=qemu:allocation-depth'
# Normal map, results in want_zero=true.
# This will probably detect an allocated data sector first (qemu likes
# to allocate the first sector to facilitate alignment probing), and
# then the rest to be zero. The BSC will thus contain (if anything)
# one range covering the first sector.
map_pre = qemu_img_pipe('map', '--output=json', '--image-opts',
nbd_img_opts)
# qemu:allocation-depth maps for want_zero=false.
# want_zero=false should (with the file driver, which the server is
# using) report everything as data. While this is sufficient for
# want_zero=false, this is nothing that should end up in the
# block-status cache.
# Due to a bug, this information did end up in the cache, though, and
# this would lead to wrong information being returned on subsequent
# want_zero=true calls.
#
# We need to run this map twice: On the first call, we probably still
# have the first sector in the cache, and so this will be served from
# the cache; and only the subsequent range will be queried from the
# block driver. This subsequent range will then be entered into the
# cache.
# If we did a want_zero=true call at this point, we would thus get
# correct information: The first sector is not covered by the cache, so
# we would get fresh block-status information from the driver, which
# would return a data range, and this would then go into the cache,
# evicting the wrong range from the want_zero=false call before.
#
# Therefore, we need a second want_zero=false map to reproduce:
# Since the first sector is not in the cache, the query for its status
# will go to the driver, which will return a result that reports the
# whole image to be a single data area. This result will then go into
# the cache, and so the cache will then report the whole image to
# contain data.
#
# Note that once the cache reports the whole image to contain data, any
# subsequent map operation will be served from the cache, and so we can
# never loop too many times here.
for _ in range(2):
# (Ignore the result, this is just to contaminate the cache)
qemu_img_pipe('map', '--output=json', '--image-opts',
nbd_img_opts_alloc_depth)
# Now let's see whether the cache reports everything as data, or
# whether we get correct information (i.e. the same as we got on our
# first attempt).
map_post = qemu_img_pipe('map', '--output=json', '--image-opts',
nbd_img_opts)
if map_pre != map_post:
print('ERROR: Map information differs before and after querying ' +
'qemu:allocation-depth')
print('Before:')
print(map_pre)
print('After:')
print(map_post)
self.fail("Map information differs")
if __name__ == '__main__':
# The block-status cache only works on the protocol layer, so to test it,
# we can only use the raw format
iotests.main(supported_fmts=['raw'],
supported_protocols=['file'])

View File

@ -0,0 +1,5 @@
.
----------------------------------------------------------------------
Ran 1 tests
OK