From a647d7863ca5059f451a325ceba70c1db567cfff Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Fri, 9 May 2025 14:11:01 +0200 Subject: [PATCH] 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 On-behalf-of: SAP philipp.schuster@sap.com --- block/src/async_io.rs | 8 ++++++++ block/src/fixed_vhd_async.rs | 4 ++++ block/src/fixed_vhd_sync.rs | 4 ++++ block/src/qcow/mod.rs | 7 +++++++ block/src/qcow/qcow_raw_file.rs | 7 +++++++ block/src/qcow/raw_file.rs | 6 ++++++ block/src/qcow_sync.rs | 6 ++++++ block/src/raw_async.rs | 4 ++++ block/src/raw_async_aio.rs | 4 ++++ block/src/raw_sync.rs | 4 ++++ block/src/vhdx/mod.rs | 7 +++++++ block/src/vhdx_sync.rs | 6 ++++++ 12 files changed, 67 insertions(+) diff --git a/block/src/async_io.rs b/block/src/async_io.rs index 9242bf038..07448d4d1 100644 --- a/block/src/async_io.rs +++ b/block/src/async_io.rs @@ -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>`, which makes it + // impossible to return a reference. + fn fd(&mut self) -> RawFd; } #[derive(Error, Debug)] diff --git a/block/src/fixed_vhd_async.rs b/block/src/fixed_vhd_async.rs index d3d897789..9ba72984e 100644 --- a/block/src/fixed_vhd_async.rs +++ b/block/src/fixed_vhd_async.rs @@ -33,6 +33,10 @@ impl DiskFile for FixedVhdDiskAsync { .map_err(DiskFileError::NewAsyncIo)?, ) as Box) } + + fn fd(&mut self) -> RawFd { + self.0.as_raw_fd() + } } pub struct FixedVhdAsync { diff --git a/block/src/fixed_vhd_sync.rs b/block/src/fixed_vhd_sync.rs index 089e6bfe7..1120aa37b 100644 --- a/block/src/fixed_vhd_sync.rs +++ b/block/src/fixed_vhd_sync.rs @@ -33,6 +33,10 @@ impl DiskFile for FixedVhdDiskSync { .map_err(DiskFileError::NewAsyncIo)?, ) as Box) } + + fn fd(&mut self) -> RawFd { + self.0.as_raw_fd() + } } pub struct FixedVhdSync { diff --git a/block/src/qcow/mod.rs b/block/src/qcow/mod.rs index 2a1b893f8..a62729380 100644 --- a/block/src/qcow/mod.rs +++ b/block/src/qcow/mod.rs @@ -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(); diff --git a/block/src/qcow/qcow_raw_file.rs b/block/src/qcow/qcow_raw_file.rs index ba985c1a2..764b02da6 100644 --- a/block/src/qcow/qcow_raw_file.rs +++ b/block/src/qcow/qcow_raw_file.rs @@ -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() + } +} diff --git a/block/src/qcow/raw_file.rs b/block/src/qcow/raw_file.rs index e42a2cd5b..cb9637601 100644 --- a/block/src/qcow/raw_file.rs +++ b/block/src/qcow/raw_file.rs @@ -369,3 +369,9 @@ impl Clone for RawFile { } } } + +impl AsRawFd for RawFile { + fn as_raw_fd(&self) -> RawFd { + self.file.as_raw_fd() + } +} diff --git a/block/src/qcow_sync.rs b/block/src/qcow_sync.rs index 9cde2a7df..7c086cd7d 100644 --- a/block/src/qcow_sync.rs +++ b/block/src/qcow_sync.rs @@ -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> { Ok(Box::new(QcowSync::new(self.qcow_file.clone())) as Box) } + + fn fd(&mut self) -> RawFd { + let lock = self.qcow_file.lock().unwrap(); + lock.as_raw_fd() + } } pub struct QcowSync { diff --git a/block/src/raw_async.rs b/block/src/raw_async.rs index 55b7355a6..7fd6d80d1 100644 --- a/block/src/raw_async.rs +++ b/block/src/raw_async.rs @@ -46,6 +46,10 @@ impl DiskFile for RawFileDisk { DiskTopology::default() } } + + fn fd(&mut self) -> RawFd { + self.file.as_raw_fd() + } } pub struct RawFileAsync { diff --git a/block/src/raw_async_aio.rs b/block/src/raw_async_aio.rs index 5a5345751..c9d94f963 100644 --- a/block/src/raw_async_aio.rs +++ b/block/src/raw_async_aio.rs @@ -49,6 +49,10 @@ impl DiskFile for RawFileDiskAio { DiskTopology::default() } } + + fn fd(&mut self) -> RawFd { + self.file.as_raw_fd() + } } pub struct RawFileAsyncAio { diff --git a/block/src/raw_sync.rs b/block/src/raw_sync.rs index 20f8aa075..ea716a0e4 100644 --- a/block/src/raw_sync.rs +++ b/block/src/raw_sync.rs @@ -43,6 +43,10 @@ impl DiskFile for RawFileDiskSync { DiskTopology::default() } } + + fn fd(&mut self) -> RawFd { + self.file.as_raw_fd() + } } pub struct RawFileSync { diff --git a/block/src/vhdx/mod.rs b/block/src/vhdx/mod.rs index c8dbd4055..2ec266c62 100644 --- a/block/src/vhdx/mod.rs +++ b/block/src/vhdx/mod.rs @@ -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 diff --git a/block/src/vhdx_sync.rs b/block/src/vhdx_sync.rs index b6736a990..a5b62ee0b 100644 --- a/block/src/vhdx_sync.rs +++ b/block/src/vhdx_sync.rs @@ -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, ) } + + fn fd(&mut self) -> RawFd { + let lock = self.vhdx_file.lock().unwrap(); + lock.as_raw_fd() + } } pub struct VhdxSync {