// Copyright © 2021 Intel Corporation // // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause use crate::async_io::{AsyncIo, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult}; use crate::{fsync_sync, read_vectored_sync, write_vectored_sync}; use qcow::{QcowFile, RawFile, Result as QcowResult}; use std::fs::File; use std::io::{Seek, SeekFrom}; use std::sync::{Arc, Mutex}; use vmm_sys_util::eventfd::EventFd; pub struct QcowDiskSync { qcow_file: QcowFile, semaphore: Arc>, } impl QcowDiskSync { pub fn new(file: File, direct_io: bool) -> QcowResult { Ok(QcowDiskSync { qcow_file: QcowFile::from(RawFile::new(file, direct_io))?, semaphore: Arc::new(Mutex::new(())), }) } } impl DiskFile for QcowDiskSync { fn size(&mut self) -> DiskFileResult { // Take the semaphore to ensure other threads are not interacting with // the underlying file. let _lock = self.semaphore.lock().unwrap(); Ok(self .qcow_file .seek(SeekFrom::End(0)) .map_err(DiskFileError::Size)? as u64) } fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult> { Ok(Box::new(QcowSync::new( self.qcow_file.clone(), self.semaphore.clone(), )) as Box) } } pub struct QcowSync { qcow_file: QcowFile, eventfd: EventFd, completion_list: Vec<(u64, i32)>, semaphore: Arc>, } impl QcowSync { pub fn new(qcow_file: QcowFile, semaphore: Arc>) -> Self { QcowSync { qcow_file, eventfd: EventFd::new(libc::EFD_NONBLOCK) .expect("Failed creating EventFd for QcowSync"), completion_list: Vec::new(), semaphore, } } } impl AsyncIo for QcowSync { fn notifier(&self) -> &EventFd { &self.eventfd } fn read_vectored( &mut self, offset: libc::off_t, iovecs: Vec, user_data: u64, ) -> AsyncIoResult<()> { read_vectored_sync( offset, iovecs, user_data, &mut self.qcow_file, &self.eventfd, &mut self.completion_list, &mut self.semaphore, ) } fn write_vectored( &mut self, offset: libc::off_t, iovecs: Vec, user_data: u64, ) -> AsyncIoResult<()> { write_vectored_sync( offset, iovecs, user_data, &mut self.qcow_file, &self.eventfd, &mut self.completion_list, &mut self.semaphore, ) } fn fsync(&mut self, user_data: Option) -> AsyncIoResult<()> { fsync_sync( user_data, &mut self.qcow_file, &self.eventfd, &mut self.completion_list, &mut self.semaphore, ) } fn complete(&mut self) -> Vec<(u64, i32)> { self.completion_list.drain(..).collect() } }