block: enable to get a raw FD of each block device's DiskFile

This is a prerequisite for the next steps.

Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
On-behalf-of: SAP philipp.schuster@sap.com
This commit is contained in:
Philipp Schuster
2025-05-09 14:11:01 +02:00
committed by Rob Bradford
parent a23d4b7cf2
commit a647d7863c
12 changed files with 67 additions and 0 deletions

View File

@@ -2,6 +2,8 @@
//
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
use std::os::fd::RawFd;
use thiserror::Error;
use vmm_sys_util::eventfd::EventFd;
@@ -25,6 +27,12 @@ pub trait DiskFile: Send {
fn topology(&mut self) -> DiskTopology {
DiskTopology::default()
}
/// Returns the file descriptor of the underlying disk image file.
// Impl Note:
// This must be `RawFd` instead of `BorrowedFd` or `&File`, as some
// implementations wrap the file in an `Arc<Mutex<T>>`, which makes it
// impossible to return a reference.
fn fd(&mut self) -> RawFd;
}
#[derive(Error, Debug)]

View File

@@ -33,6 +33,10 @@ impl DiskFile for FixedVhdDiskAsync {
.map_err(DiskFileError::NewAsyncIo)?,
) as Box<dyn AsyncIo>)
}
fn fd(&mut self) -> RawFd {
self.0.as_raw_fd()
}
}
pub struct FixedVhdAsync {

View File

@@ -33,6 +33,10 @@ impl DiskFile for FixedVhdDiskSync {
.map_err(DiskFileError::NewAsyncIo)?,
) as Box<dyn AsyncIo>)
}
fn fd(&mut self) -> RawFd {
self.0.as_raw_fd()
}
}
pub struct FixedVhdSync {

View File

@@ -13,6 +13,7 @@ use std::cmp::{max, min};
use std::fs::OpenOptions;
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::mem::size_of;
use std::os::fd::{AsRawFd, RawFd};
use std::str;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
@@ -1516,6 +1517,12 @@ impl QcowFile {
}
}
impl AsRawFd for QcowFile {
fn as_raw_fd(&self) -> RawFd {
self.raw_file.as_raw_fd()
}
}
impl Drop for QcowFile {
fn drop(&mut self) {
let _ = self.sync_caches();

View File

@@ -6,6 +6,7 @@
use std::io::{self, BufWriter, Seek, SeekFrom, Write};
use std::mem::size_of;
use std::os::fd::{AsRawFd, RawFd};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use vmm_sys_util::write_zeroes::WriteZeroes;
@@ -159,3 +160,9 @@ impl Clone for QcowRawFile {
}
}
}
impl AsRawFd for QcowRawFile {
fn as_raw_fd(&self) -> RawFd {
self.file.as_raw_fd()
}
}

View File

@@ -369,3 +369,9 @@ impl Clone for RawFile {
}
}
}
impl AsRawFd for RawFile {
fn as_raw_fd(&self) -> RawFd {
self.file.as_raw_fd()
}
}

View File

@@ -5,6 +5,7 @@
use std::collections::VecDeque;
use std::fs::File;
use std::io::{Seek, SeekFrom};
use std::os::fd::{AsRawFd, RawFd};
use std::sync::{Arc, Mutex, MutexGuard};
use vmm_sys_util::eventfd::EventFd;
@@ -35,6 +36,11 @@ impl DiskFile for QcowDiskSync {
fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>> {
Ok(Box::new(QcowSync::new(self.qcow_file.clone())) as Box<dyn AsyncIo>)
}
fn fd(&mut self) -> RawFd {
let lock = self.qcow_file.lock().unwrap();
lock.as_raw_fd()
}
}
pub struct QcowSync {

View File

@@ -46,6 +46,10 @@ impl DiskFile for RawFileDisk {
DiskTopology::default()
}
}
fn fd(&mut self) -> RawFd {
self.file.as_raw_fd()
}
}
pub struct RawFileAsync {

View File

@@ -49,6 +49,10 @@ impl DiskFile for RawFileDiskAio {
DiskTopology::default()
}
}
fn fd(&mut self) -> RawFd {
self.file.as_raw_fd()
}
}
pub struct RawFileAsyncAio {

View File

@@ -43,6 +43,10 @@ impl DiskFile for RawFileDiskSync {
DiskTopology::default()
}
}
fn fd(&mut self) -> RawFd {
self.file.as_raw_fd()
}
}
pub struct RawFileSync {

View File

@@ -5,6 +5,7 @@
use std::collections::btree_map::BTreeMap;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use std::os::fd::{AsRawFd, RawFd};
use byteorder::{BigEndian, ByteOrder};
use remain::sorted;
@@ -222,6 +223,12 @@ impl Clone for Vhdx {
}
}
impl AsRawFd for Vhdx {
fn as_raw_fd(&self) -> RawFd {
self.file.as_raw_fd()
}
}
pub(crate) fn uuid_from_guid(buf: &[u8]) -> Uuid {
// The first 3 fields of UUID are stored in Big Endian format, and
// the last 8 bytes are stored as byte array. Therefore, we read the

View File

@@ -4,6 +4,7 @@
use std::collections::VecDeque;
use std::fs::File;
use std::os::fd::{AsRawFd, RawFd};
use std::sync::{Arc, Mutex, MutexGuard};
use vmm_sys_util::eventfd::EventFd;
@@ -35,6 +36,11 @@ impl DiskFile for VhdxDiskSync {
as Box<dyn AsyncIo>,
)
}
fn fd(&mut self) -> RawFd {
let lock = self.vhdx_file.lock().unwrap();
lock.as_raw_fd()
}
}
pub struct VhdxSync {