mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
No remaining consumers after switching to DiskBackend::Next. Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
158 lines
4.6 KiB
Rust
158 lines
4.6 KiB
Rust
// Copyright © 2021 Intel Corporation
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
use std::fs::File;
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
use crate::async_io::{AsyncIo, AsyncIoError, AsyncIoResult, BorrowedDiskFd, DiskFileError};
|
|
use crate::error::{BlockError, BlockErrorKind, BlockResult, ErrorOp};
|
|
use crate::fixed_vhd::FixedVhd;
|
|
use crate::raw_sync::RawFileSync;
|
|
use crate::{BlockBackend, disk_file};
|
|
|
|
#[derive(Debug)]
|
|
pub struct FixedVhdDiskSync(FixedVhd);
|
|
|
|
impl FixedVhdDiskSync {
|
|
pub fn new(file: File) -> BlockResult<Self> {
|
|
Ok(Self(
|
|
FixedVhd::new(file).map_err(|e| BlockError::from(e).with_op(ErrorOp::Open))?,
|
|
))
|
|
}
|
|
}
|
|
|
|
impl disk_file::DiskSize for FixedVhdDiskSync {
|
|
fn logical_size(&self) -> BlockResult<u64> {
|
|
Ok(self.0.logical_size().unwrap())
|
|
}
|
|
}
|
|
|
|
impl disk_file::PhysicalSize for FixedVhdDiskSync {
|
|
fn physical_size(&self) -> BlockResult<u64> {
|
|
self.0.physical_size().map_err(|e| match e {
|
|
crate::Error::GetFileMetadata(io) => {
|
|
BlockError::new(BlockErrorKind::Io, crate::Error::GetFileMetadata(io))
|
|
}
|
|
_ => BlockError::new(BlockErrorKind::Io, e),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl disk_file::DiskFd for FixedVhdDiskSync {
|
|
fn fd(&self) -> BorrowedDiskFd<'_> {
|
|
BorrowedDiskFd::new(self.0.as_raw_fd())
|
|
}
|
|
}
|
|
|
|
impl disk_file::Geometry for FixedVhdDiskSync {}
|
|
|
|
impl disk_file::SparseCapable for FixedVhdDiskSync {}
|
|
|
|
impl disk_file::Resizable for FixedVhdDiskSync {
|
|
fn resize(&mut self, _size: u64) -> BlockResult<()> {
|
|
Err(BlockError::new(
|
|
BlockErrorKind::UnsupportedFeature,
|
|
DiskFileError::ResizeError(std::io::Error::other("resize not supported for fixed VHD")),
|
|
)
|
|
.with_op(ErrorOp::Resize))
|
|
}
|
|
}
|
|
|
|
impl disk_file::DiskFile for FixedVhdDiskSync {}
|
|
|
|
impl disk_file::AsyncDiskFile for FixedVhdDiskSync {
|
|
fn try_clone(&self) -> BlockResult<Box<dyn disk_file::AsyncDiskFile>> {
|
|
Ok(Box::new(FixedVhdDiskSync(self.0.clone())))
|
|
}
|
|
|
|
fn new_async_io(&self, _ring_depth: u32) -> BlockResult<Box<dyn AsyncIo>> {
|
|
Ok(Box::new(
|
|
FixedVhdSync::new(self.0.as_raw_fd(), self.0.logical_size().unwrap()).map_err(|e| {
|
|
BlockError::new(BlockErrorKind::Io, DiskFileError::NewAsyncIo(e))
|
|
.with_op(ErrorOp::Open)
|
|
})?,
|
|
))
|
|
}
|
|
}
|
|
|
|
pub struct FixedVhdSync {
|
|
raw_file_sync: RawFileSync,
|
|
size: u64,
|
|
}
|
|
|
|
impl FixedVhdSync {
|
|
pub fn new(fd: RawFd, size: u64) -> std::io::Result<Self> {
|
|
Ok(FixedVhdSync {
|
|
raw_file_sync: RawFileSync::new(fd),
|
|
size,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl AsyncIo for FixedVhdSync {
|
|
fn notifier(&self) -> &EventFd {
|
|
self.raw_file_sync.notifier()
|
|
}
|
|
|
|
fn read_vectored(
|
|
&mut self,
|
|
offset: libc::off_t,
|
|
iovecs: &[libc::iovec],
|
|
user_data: u64,
|
|
) -> AsyncIoResult<()> {
|
|
if offset as u64 >= self.size {
|
|
return Err(AsyncIoError::ReadVectored(std::io::Error::new(
|
|
std::io::ErrorKind::InvalidData,
|
|
format!(
|
|
"Invalid offset {}, can't be larger than file size {}",
|
|
offset, self.size
|
|
),
|
|
)));
|
|
}
|
|
|
|
self.raw_file_sync.read_vectored(offset, iovecs, user_data)
|
|
}
|
|
|
|
fn write_vectored(
|
|
&mut self,
|
|
offset: libc::off_t,
|
|
iovecs: &[libc::iovec],
|
|
user_data: u64,
|
|
) -> AsyncIoResult<()> {
|
|
if offset as u64 >= self.size {
|
|
return Err(AsyncIoError::WriteVectored(std::io::Error::new(
|
|
std::io::ErrorKind::InvalidData,
|
|
format!(
|
|
"Invalid offset {}, can't be larger than file size {}",
|
|
offset, self.size
|
|
),
|
|
)));
|
|
}
|
|
|
|
self.raw_file_sync.write_vectored(offset, iovecs, user_data)
|
|
}
|
|
|
|
fn fsync(&mut self, user_data: Option<u64>) -> AsyncIoResult<()> {
|
|
self.raw_file_sync.fsync(user_data)
|
|
}
|
|
|
|
fn next_completed_request(&mut self) -> Option<(u64, i32)> {
|
|
self.raw_file_sync.next_completed_request()
|
|
}
|
|
|
|
fn punch_hole(&mut self, _offset: u64, _length: u64, _user_data: u64) -> AsyncIoResult<()> {
|
|
Err(AsyncIoError::PunchHole(std::io::Error::other(
|
|
"punch_hole not supported for fixed VHD",
|
|
)))
|
|
}
|
|
|
|
fn write_zeroes(&mut self, _offset: u64, _length: u64, _user_data: u64) -> AsyncIoResult<()> {
|
|
Err(AsyncIoError::WriteZeroes(std::io::Error::other(
|
|
"write_zeroes not supported for fixed VHD",
|
|
)))
|
|
}
|
|
}
|