1 // Copyright © 2021 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause 4 5 use thiserror::Error; 6 use vmm_sys_util::eventfd::EventFd; 7 8 use crate::DiskTopology; 9 10 #[derive(Error, Debug)] 11 pub enum DiskFileError { 12 /// Failed getting disk file size. 13 #[error("Failed getting disk file size: {0}")] 14 Size(#[source] std::io::Error), 15 /// Failed creating a new AsyncIo. 16 #[error("Failed creating a new AsyncIo: {0}")] 17 NewAsyncIo(#[source] std::io::Error), 18 } 19 20 pub type DiskFileResult<T> = std::result::Result<T, DiskFileError>; 21 22 pub trait DiskFile: Send { 23 fn size(&mut self) -> DiskFileResult<u64>; 24 fn new_async_io(&self, ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>>; 25 fn topology(&mut self) -> DiskTopology { 26 DiskTopology::default() 27 } 28 } 29 30 #[derive(Error, Debug)] 31 pub enum AsyncIoError { 32 /// Failed vectored reading from file. 33 #[error("Failed vectored reading from file: {0}")] 34 ReadVectored(#[source] std::io::Error), 35 /// Failed vectored writing to file. 36 #[error("Failed vectored writing to file: {0}")] 37 WriteVectored(#[source] std::io::Error), 38 /// Failed synchronizing file. 39 #[error("Failed synchronizing file: {0}")] 40 Fsync(#[source] std::io::Error), 41 } 42 43 pub type AsyncIoResult<T> = std::result::Result<T, AsyncIoError>; 44 45 pub trait AsyncIo: Send { 46 fn notifier(&self) -> &EventFd; 47 fn read_vectored( 48 &mut self, 49 offset: libc::off_t, 50 iovecs: &[libc::iovec], 51 user_data: u64, 52 ) -> AsyncIoResult<()>; 53 fn write_vectored( 54 &mut self, 55 offset: libc::off_t, 56 iovecs: &[libc::iovec], 57 user_data: u64, 58 ) -> AsyncIoResult<()>; 59 fn fsync(&mut self, user_data: Option<u64>) -> AsyncIoResult<()>; 60 fn next_completed_request(&mut self) -> Option<(u64, i32)>; 61 } 62