From 334b900fcdb31e85e01804e2b84e2e94af5214fa Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Mon, 20 Apr 2026 10:11:10 +0100 Subject: [PATCH] vhost_user_block: Correctly report number of used bytes The number of bytes written into descriptors should be reported for `add_used()`. Here it is either just the status byte or also the size of serial ID for the block device. Signed-off-by: Rob Bradford --- vhost_user_block/src/lib.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/vhost_user_block/src/lib.rs b/vhost_user_block/src/lib.rs index 9b0e429ee..25c10fa50 100644 --- a/vhost_user_block/src/lib.rs +++ b/vhost_user_block/src/lib.rs @@ -20,7 +20,7 @@ use std::time::Instant; use std::{convert, io, process, result}; use block::qcow::{self, ImageType, QcowFile}; -use block::{Request, VirtioBlockConfig, build_serial}; +use block::{Request, RequestType, VirtioBlockConfig, build_serial}; use libc::EFD_NONBLOCK; use log::{debug, error, info, warn}; use option_parser::{OptionParser, OptionParserError, Toggle}; @@ -128,36 +128,33 @@ impl VhostUserBlkThread { .pop_descriptor_chain(self.mem.memory()) { debug!("got an element in the queue"); - let len; - match Request::parse(&mut desc_chain, None) { + let len = match Request::parse(&mut desc_chain, None) { Ok(mut request) => { debug!("element is a valid request"); request.set_writeback(self.writeback.load(Ordering::Acquire)); - let status = match request.execute( + let (status, len) = match request.execute( &mut self.disk_image.lock().unwrap().deref_mut(), self.disk_nsectors, desc_chain.memory(), &self.serial, ) { - Ok(l) => { - len = l; - VIRTIO_BLK_S_OK as u8 - } - Err(e) => { - len = 1; - e.status() + Ok(_) if request.request_type == RequestType::GetDeviceId => { + (VIRTIO_BLK_S_OK as u8, self.serial.len() as u32 + 1) } + Ok(l) => (VIRTIO_BLK_S_OK as u8, l + 1), + Err(e) => (e.status(), 1), }; desc_chain .memory() .write_obj(status, request.status_addr) .unwrap(); + len } Err(err) => { error!("failed to parse available descriptor chain: {err:?}"); - len = 0; + 0 } - } + }; vring .get_queue_mut()