xref: /cloud-hypervisor/block/src/fixed_vhd_sync.rs (revision 19d36c765fdf00be749d95b3e61028bc302d6d73)
1 // Copyright © 2021 Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 
5 use std::fs::File;
6 use std::os::unix::io::{AsRawFd, RawFd};
7 
8 use vmm_sys_util::eventfd::EventFd;
9 
10 use crate::async_io::{
11     AsyncIo, AsyncIoError, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult,
12 };
13 use crate::fixed_vhd::FixedVhd;
14 use crate::raw_sync::RawFileSync;
15 use crate::BlockBackend;
16 
17 pub struct FixedVhdDiskSync(FixedVhd);
18 
19 impl FixedVhdDiskSync {
20     pub fn new(file: File) -> std::io::Result<Self> {
21         Ok(Self(FixedVhd::new(file)?))
22     }
23 }
24 
25 impl DiskFile for FixedVhdDiskSync {
26     fn size(&mut self) -> DiskFileResult<u64> {
27         Ok(self.0.size().unwrap())
28     }
29 
30     fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>> {
31         Ok(Box::new(
32             FixedVhdSync::new(self.0.as_raw_fd(), self.0.size().unwrap())
33                 .map_err(DiskFileError::NewAsyncIo)?,
34         ) as Box<dyn AsyncIo>)
35     }
36 }
37 
38 pub struct FixedVhdSync {
39     raw_file_sync: RawFileSync,
40     size: u64,
41 }
42 
43 impl FixedVhdSync {
44     pub fn new(fd: RawFd, size: u64) -> std::io::Result<Self> {
45         Ok(FixedVhdSync {
46             raw_file_sync: RawFileSync::new(fd),
47             size,
48         })
49     }
50 }
51 
52 impl AsyncIo for FixedVhdSync {
53     fn notifier(&self) -> &EventFd {
54         self.raw_file_sync.notifier()
55     }
56 
57     fn read_vectored(
58         &mut self,
59         offset: libc::off_t,
60         iovecs: &[libc::iovec],
61         user_data: u64,
62     ) -> AsyncIoResult<()> {
63         if offset as u64 >= self.size {
64             return Err(AsyncIoError::ReadVectored(std::io::Error::new(
65                 std::io::ErrorKind::InvalidData,
66                 format!(
67                     "Invalid offset {}, can't be larger than file size {}",
68                     offset, self.size
69                 ),
70             )));
71         }
72 
73         self.raw_file_sync.read_vectored(offset, iovecs, user_data)
74     }
75 
76     fn write_vectored(
77         &mut self,
78         offset: libc::off_t,
79         iovecs: &[libc::iovec],
80         user_data: u64,
81     ) -> AsyncIoResult<()> {
82         if offset as u64 >= self.size {
83             return Err(AsyncIoError::WriteVectored(std::io::Error::new(
84                 std::io::ErrorKind::InvalidData,
85                 format!(
86                     "Invalid offset {}, can't be larger than file size {}",
87                     offset, self.size
88                 ),
89             )));
90         }
91 
92         self.raw_file_sync.write_vectored(offset, iovecs, user_data)
93     }
94 
95     fn fsync(&mut self, user_data: Option<u64>) -> AsyncIoResult<()> {
96         self.raw_file_sync.fsync(user_data)
97     }
98 
99     fn next_completed_request(&mut self) -> Option<(u64, i32)> {
100         self.raw_file_sync.next_completed_request()
101     }
102 }
103