block: raw_sync: Use query_device_size() for size queries

Use query_device_size() instead of seek(End(0)) and metadata().len()
to correctly handle block device and regular file handles.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski
2026-03-25 23:21:18 +01:00
committed by Rob Bradford
parent f14e2d2b40
commit 290f57a6e3
+7 -9
View File
@@ -4,7 +4,6 @@
use std::collections::VecDeque; use std::collections::VecDeque;
use std::fs::File; use std::fs::File;
use std::io::{Seek, SeekFrom};
use std::os::unix::io::{AsRawFd, RawFd}; use std::os::unix::io::{AsRawFd, RawFd};
use libc::{FALLOC_FL_KEEP_SIZE, FALLOC_FL_PUNCH_HOLE, FALLOC_FL_ZERO_RANGE}; use libc::{FALLOC_FL_KEEP_SIZE, FALLOC_FL_PUNCH_HOLE, FALLOC_FL_ZERO_RANGE};
@@ -14,7 +13,7 @@ use vmm_sys_util::eventfd::EventFd;
use crate::async_io::{ use crate::async_io::{
AsyncIo, AsyncIoError, AsyncIoResult, BorrowedDiskFd, DiskFile, DiskFileError, DiskFileResult, AsyncIo, AsyncIoError, AsyncIoResult, BorrowedDiskFd, DiskFile, DiskFileError, DiskFileResult,
}; };
use crate::{DiskTopology, SECTOR_SIZE, probe_sparse_support}; use crate::{DiskTopology, SECTOR_SIZE, probe_sparse_support, query_device_size};
pub struct RawFileDiskSync { pub struct RawFileDiskSync {
file: File, file: File,
@@ -28,16 +27,15 @@ impl RawFileDiskSync {
impl DiskFile for RawFileDiskSync { impl DiskFile for RawFileDiskSync {
fn logical_size(&mut self) -> DiskFileResult<u64> { fn logical_size(&mut self) -> DiskFileResult<u64> {
self.file Ok(query_device_size(&self.file)
.seek(SeekFrom::End(0)) .map_err(DiskFileError::Size)?
.map_err(DiskFileError::Size) .0)
} }
fn physical_size(&mut self) -> DiskFileResult<u64> { fn physical_size(&mut self) -> DiskFileResult<u64> {
self.file Ok(query_device_size(&self.file)
.metadata() .map_err(DiskFileError::Size)?
.map(|m| m.len()) .1)
.map_err(DiskFileError::Size)
} }
fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>> { fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>> {