From 4596c502fb38c6fb6a33e506c17bf9af0f2a57ac Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Sat, 4 Jul 2026 09:29:26 +0200 Subject: [PATCH] vhost_user_block: Query the disk size instead of seeking The disk size in sectors was computed by seeking the AlignedFile to the end. Use query_device_size instead, which also handles block devices, and drop the now unused Seek and SeekFrom imports. Signed-off-by: Anatol Belski --- vhost_user_block/src/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/vhost_user_block/src/lib.rs b/vhost_user_block/src/lib.rs index f78e39726..eb120a296 100644 --- a/vhost_user_block/src/lib.rs +++ b/vhost_user_block/src/lib.rs @@ -9,7 +9,6 @@ // SPDX-License-Identifier: (Apache-2.0 AND BSD-3-Clause) use std::fs::{File, OpenOptions}; -use std::io::{Seek, SeekFrom}; use std::ops::Deref; use std::os::unix::fs::OpenOptionsExt; use std::os::unix::io::{FromRawFd, IntoRawFd}; @@ -19,7 +18,9 @@ use std::sync::{Arc, Mutex, RwLock, RwLockWriteGuard}; use std::time::Instant; use std::{convert, io, process, result}; -use block::{AlignedFile, Request, RequestType, VirtioBlockConfig, build_serial}; +use block::{ + AlignedFile, Request, RequestType, VirtioBlockConfig, build_serial, query_device_size, +}; use libc::EFD_NONBLOCK; use log::{debug, error, info, warn}; use option_parser::{OptionParser, OptionParserError, Toggle}; @@ -234,7 +235,7 @@ impl VhostUserBlkBackend { let serial = build_serial(&PathBuf::from(&image_path)); let image = Arc::new(Mutex::new(raw_img)); - let nsectors = (image.lock().unwrap().seek(SeekFrom::End(0)).unwrap()) / SECTOR_SIZE; + let nsectors = query_device_size(image.lock().unwrap().file()).unwrap().0 / SECTOR_SIZE; let config = VirtioBlockConfig { capacity: nsectors, blk_size: BLK_SIZE,