Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] OVFS: Modify error handling #5312

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions integrations/virtiofs/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,18 @@ use std::ptr;
use vm_memory::bitmap::BitmapSlice;
use vm_memory::VolatileSlice;

use crate::error::*;

/// ReadWriteAtVolatile is a trait that allows reading and writing from a slice of VolatileSlice.
pub trait ReadWriteAtVolatile<B: BitmapSlice> {
fn read_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> Result<usize>;
fn write_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> Result<usize>;
fn read_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> usize;
fn write_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> usize;
}

impl<B: BitmapSlice, T: ReadWriteAtVolatile<B> + ?Sized> ReadWriteAtVolatile<B> for &T {
fn read_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> Result<usize> {
fn read_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> usize {
(**self).read_vectored_at_volatile(bufs)
}

fn write_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> Result<usize> {
fn write_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> usize {
(**self).write_vectored_at_volatile(bufs)
}
}
Expand All @@ -58,7 +56,7 @@ impl BufferWrapper {
}

impl<B: BitmapSlice> ReadWriteAtVolatile<B> for BufferWrapper {
fn read_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> Result<usize> {
fn read_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> usize {
let slice_guards: Vec<_> = bufs.iter().map(|s| s.ptr_guard_mut()).collect();
let iovecs: Vec<_> = slice_guards
.iter()
Expand All @@ -68,7 +66,7 @@ impl<B: BitmapSlice> ReadWriteAtVolatile<B> for BufferWrapper {
})
.collect();
if iovecs.is_empty() {
return Ok(0);
return 0;
}
let data = self.buffer.borrow().to_vec();
let mut result = 0;
Expand All @@ -83,10 +81,10 @@ impl<B: BitmapSlice> ReadWriteAtVolatile<B> for BufferWrapper {
bufs[index].bitmap().mark_dirty(0, num);
result += num;
}
Ok(result)
result
}

fn write_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> Result<usize> {
fn write_vectored_at_volatile(&self, bufs: &[&VolatileSlice<B>]) -> usize {
let slice_guards: Vec<_> = bufs.iter().map(|s| s.ptr_guard()).collect();
let iovecs: Vec<_> = slice_guards
.iter()
Expand All @@ -96,7 +94,7 @@ impl<B: BitmapSlice> ReadWriteAtVolatile<B> for BufferWrapper {
})
.collect();
if iovecs.is_empty() {
return Ok(0);
return 0;
}
let len = iovecs.iter().map(|iov| iov.iov_len).sum();
let mut data = vec![0; len];
Expand All @@ -112,6 +110,6 @@ impl<B: BitmapSlice> ReadWriteAtVolatile<B> for BufferWrapper {
offset += iov.iov_len;
}
*self.buffer.borrow_mut() = opendal::Buffer::from(data);
Ok(len)
len
}
}
64 changes: 32 additions & 32 deletions integrations/virtiofs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@
// specific language governing permissions and limitations
// under the License.

use std::ffi::CStr;
use std::io;

use anyhow::Error as AnyError;
use opendal::ErrorKind;
use snafu::prelude::Snafu;

/// Error is a error struct returned by all ovfs functions.
#[derive(Debug, Snafu)]
#[non_exhaustive]
pub enum Error {
#[snafu(display("Vhost user fs error: {}, source: {:?}", message, source))]
VhostUserFsError {
message: String,
#[snafu(display("IO error: {:?}", source))]
IOError {
#[snafu(source(false))]
source: Option<AnyError>,
source: io::Error,
},
#[snafu(display("Unexpected error: {}, source: {:?}", message, source))]
Unexpected {
Expand All @@ -41,33 +40,41 @@ pub enum Error {

impl From<libc::c_int> for Error {
fn from(errno: libc::c_int) -> Error {
let err_str = unsafe { libc::strerror(errno) };
let message = if err_str.is_null() {
format!("errno: {}", errno)
} else {
let c_str = unsafe { CStr::from_ptr(err_str) };
c_str.to_string_lossy().into_owned()
};
Error::VhostUserFsError {
message,
source: None,
Error::IOError {
source: io::Error::from_raw_os_error(errno),
}
}
}

impl From<opendal::Error> for Error {
fn from(error: opendal::Error) -> Error {
match error.kind() {
ErrorKind::Unsupported => libc::ENOTSUP.into(),
ErrorKind::IsADirectory => libc::EISDIR.into(),
ErrorKind::NotFound => libc::ENOENT.into(),
ErrorKind::PermissionDenied => libc::EPERM.into(),
ErrorKind::AlreadyExists => libc::EEXIST.into(),
ErrorKind::NotADirectory => libc::ENOTDIR.into(),
ErrorKind::RangeNotSatisfied => libc::ERANGE.into(),
ErrorKind::RateLimited => libc::EAGAIN.into(),
_ => libc::EIO.into(),
}
}
}

impl From<Error> for libc::c_int {
fn from(error: Error) -> libc::c_int {
match error {
Error::IOError { source } => source.raw_os_error().unwrap_or(libc::EIO),
Error::Unexpected { .. } => libc::EIO,
}
}
}

impl From<Error> for io::Error {
fn from(error: Error) -> io::Error {
match error {
Error::VhostUserFsError { message, source } => {
let message = format!("Vhost user fs error: {}", message);
match source {
Some(source) => io::Error::new(
io::ErrorKind::Other,
format!("{}, source: {:?}", message, source),
),
None => io::Error::new(io::ErrorKind::Other, message),
}
}
Error::IOError { source } => source,
Error::Unexpected { message, source } => {
let message = format!("Unexpected error: {}", message);
match source {
Expand All @@ -85,13 +92,6 @@ impl From<Error> for io::Error {
/// Result is a result wrapper in ovfs.
pub type Result<T, E = Error> = std::result::Result<T, E>;

pub fn new_vhost_user_fs_error(message: &str, source: Option<AnyError>) -> Error {
Error::VhostUserFsError {
message: message.to_string(),
source,
}
}

pub fn new_unexpected_error(message: &str, source: Option<AnyError>) -> Error {
Error::Unexpected {
message: message.to_string(),
Expand Down
Loading
Loading