mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
block: Use RawDisk in factory, remove old wrappers
Update open_raw to construct RawDisk instead of choosing between RawFileDisk, RawFileDiskSync and RawFileDiskAio. The backend decision is now made inside RawDisk::create_async_io. Remove the DiskFile wrapper structs from raw_sync.rs, raw_async.rs and raw_async_aio.rs. Only the AsyncIo worker structs remain in those files. Reduce their module visibility to pub(crate). Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
@@ -3,91 +3,13 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::fs::File;
|
||||
use std::os::unix::io::{AsRawFd, RawFd};
|
||||
use std::os::unix::io::RawFd;
|
||||
|
||||
use libc::{FALLOC_FL_KEEP_SIZE, FALLOC_FL_PUNCH_HOLE, FALLOC_FL_ZERO_RANGE};
|
||||
use log::warn;
|
||||
use vmm_sys_util::eventfd::EventFd;
|
||||
|
||||
use crate::async_io::{AsyncIo, AsyncIoError, AsyncIoResult, BorrowedDiskFd, DiskFileError};
|
||||
use crate::error::{BlockError, BlockErrorKind, BlockResult};
|
||||
use crate::{DiskTopology, SECTOR_SIZE, disk_file, probe_sparse_support, query_device_size};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RawFileDiskSync {
|
||||
file: File,
|
||||
}
|
||||
|
||||
impl RawFileDiskSync {
|
||||
pub fn new(file: File) -> Self {
|
||||
RawFileDiskSync { file }
|
||||
}
|
||||
}
|
||||
|
||||
impl disk_file::DiskSize for RawFileDiskSync {
|
||||
fn logical_size(&self) -> BlockResult<u64> {
|
||||
query_device_size(&self.file)
|
||||
.map(|(logical_size, _)| logical_size)
|
||||
.map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::Size(e)))
|
||||
}
|
||||
}
|
||||
|
||||
impl disk_file::PhysicalSize for RawFileDiskSync {
|
||||
fn physical_size(&self) -> BlockResult<u64> {
|
||||
query_device_size(&self.file)
|
||||
.map(|(_, physical_size)| physical_size)
|
||||
.map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::Size(e)))
|
||||
}
|
||||
}
|
||||
|
||||
impl disk_file::DiskFd for RawFileDiskSync {
|
||||
fn fd(&self) -> BorrowedDiskFd<'_> {
|
||||
BorrowedDiskFd::new(self.file.as_raw_fd())
|
||||
}
|
||||
}
|
||||
|
||||
impl disk_file::Geometry for RawFileDiskSync {
|
||||
fn topology(&self) -> DiskTopology {
|
||||
DiskTopology::probe(&self.file).unwrap_or_else(|_| {
|
||||
warn!("Unable to get device topology. Using default topology");
|
||||
DiskTopology::default()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl disk_file::SparseCapable for RawFileDiskSync {
|
||||
fn supports_sparse_operations(&self) -> bool {
|
||||
probe_sparse_support(&self.file)
|
||||
}
|
||||
}
|
||||
|
||||
impl disk_file::Resizable for RawFileDiskSync {
|
||||
fn resize(&mut self, size: u64) -> BlockResult<()> {
|
||||
self.file
|
||||
.set_len(size)
|
||||
.map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::ResizeError(e)))
|
||||
}
|
||||
}
|
||||
|
||||
impl disk_file::DiskFile for RawFileDiskSync {}
|
||||
|
||||
impl disk_file::AsyncDiskFile for RawFileDiskSync {
|
||||
fn try_clone(&self) -> BlockResult<Box<dyn disk_file::AsyncDiskFile>> {
|
||||
let file = self
|
||||
.file
|
||||
.try_clone()
|
||||
.map_err(|e| BlockError::new(BlockErrorKind::Io, DiskFileError::Clone(e)))?;
|
||||
Ok(Box::new(RawFileDiskSync { file }))
|
||||
}
|
||||
|
||||
fn create_async_io(&self, _ring_depth: u32) -> BlockResult<Box<dyn AsyncIo>> {
|
||||
let mut raw = RawFileSync::new(self.file.as_raw_fd());
|
||||
raw.alignment =
|
||||
DiskTopology::probe(&self.file).map_or(SECTOR_SIZE, |t| t.logical_block_size);
|
||||
Ok(Box::new(raw) as Box<dyn AsyncIo>)
|
||||
}
|
||||
}
|
||||
use crate::SECTOR_SIZE;
|
||||
use crate::async_io::{AsyncIo, AsyncIoError, AsyncIoResult};
|
||||
|
||||
pub struct RawFileSync {
|
||||
fd: RawFd,
|
||||
|
||||
Reference in New Issue
Block a user