From 30033bdaea57fbc3056344976d851e369fbebb46 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Wed, 20 Jan 2021 10:53:27 +0100 Subject: [PATCH] block_util: Add new traits for handling disk files asynchronously Both DiskFile and AsyncIo traits are introduced to allow all kind of files (RAW, QCOW, VHD) to be able to handle asynchronous access to the underlying file. Signed-off-by: Sebastien Boeuf --- Cargo.lock | 1 + block_util/Cargo.toml | 1 + block_util/src/async_io.rs | 56 ++++++++++++++++++++++++++++++++++++++ block_util/src/lib.rs | 2 ++ 4 files changed, 60 insertions(+) create mode 100644 block_util/src/async_io.rs diff --git a/Cargo.lock b/Cargo.lock index b0f2f6348..a544e9eff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -162,6 +162,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "thiserror", "virtio-bindings", "vm-memory", "vm-virtio", diff --git a/block_util/Cargo.toml b/block_util/Cargo.toml index 4e137a970..232113545 100644 --- a/block_util/Cargo.toml +++ b/block_util/Cargo.toml @@ -15,6 +15,7 @@ log = "0.4.13" serde = ">=1.0.27" serde_derive = ">=1.0.27" serde_json = ">=1.0.9" +thiserror = "1.0" virtio-bindings = { version = "0.1", features = ["virtio-v5_0_0"]} vm-memory = { version = "0.4.0", features = ["backend-mmap", "backend-atomic"] } vm-virtio = { path = "../vm-virtio" } diff --git a/block_util/src/async_io.rs b/block_util/src/async_io.rs new file mode 100644 index 000000000..25e1880dc --- /dev/null +++ b/block_util/src/async_io.rs @@ -0,0 +1,56 @@ +// Copyright © 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +use thiserror::Error; +use vmm_sys_util::eventfd::EventFd; + +#[derive(Error, Debug)] +pub enum DiskFileError { + /// Failed getting disk file size. + #[error("Failed getting disk file size: {0}")] + Size(#[source] std::io::Error), + /// Failed creating a new AsyncIo. + #[error("Failed creating a new AsyncIo: {0}")] + NewAsyncIo(#[source] std::io::Error), +} + +pub type DiskFileResult = std::result::Result; + +pub trait DiskFile: Send + Sync { + fn size(&mut self) -> DiskFileResult; + fn new_async_io(&self, ring_depth: u32) -> DiskFileResult>; +} + +#[derive(Error, Debug)] +pub enum AsyncIoError { + /// Failed vectored reading from file. + #[error("Failed vectored reading from file: {0}")] + ReadVectored(#[source] std::io::Error), + /// Failed vectored writing to file. + #[error("Failed vectored writing to file: {0}")] + WriteVectored(#[source] std::io::Error), + /// Failed synchronizing file. + #[error("Failed synchronizing file: {0}")] + Fsync(#[source] std::io::Error), +} + +pub type AsyncIoResult = std::result::Result; + +pub trait AsyncIo: Send + Sync { + fn notifier(&self) -> &EventFd; + fn read_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()>; + fn write_vectored( + &mut self, + offset: libc::off_t, + iovecs: Vec, + user_data: u64, + ) -> AsyncIoResult<()>; + fn fsync(&mut self, user_data: Option) -> AsyncIoResult<()>; + fn complete(&mut self) -> Vec<(u64, i32)>; +} diff --git a/block_util/src/lib.rs b/block_util/src/lib.rs index 5514e1a72..71b89a994 100644 --- a/block_util/src/lib.rs +++ b/block_util/src/lib.rs @@ -13,6 +13,8 @@ extern crate log; #[macro_use] extern crate serde_derive; +pub mod async_io; + #[cfg(feature = "io_uring")] use io_uring::Probe; use io_uring::{opcode, squeue, IoUring};