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 <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski
2026-07-04 09:29:26 +02:00
committed by Rob Bradford
parent 15b59fdd9f
commit 4596c502fb

View File

@@ -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,