Skip to content
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
4 changes: 4 additions & 0 deletions resources/seccomp/aarch64-unknown-linux-musl.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
"syscall": "lseek",
"comment": "Used by the block device"
},
{
"syscall": "fallocate",
"comment": "Used by the block device for discard (hole punching)"
},
{
"syscall": "mremap",
"comment": "Used for re-allocating large memory regions, for example vectors"
Expand Down
4 changes: 4 additions & 0 deletions resources/seccomp/x86_64-unknown-linux-musl.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
"syscall": "lseek",
"comment": "Used by the block device"
},
{
"syscall": "fallocate",
"comment": "Used by the block device for discard (hole punching)"
},
{
"syscall": "mremap",
"comment": "Used for re-allocating large memory regions, for example vectors"
Expand Down
6 changes: 4 additions & 2 deletions src/vmm/src/devices/virtio/block/virtio/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::devices::virtio::block::CacheType;
use crate::devices::virtio::block::virtio::metrics::{BlockDeviceMetrics, BlockMetricsPerDevice};
use crate::devices::virtio::device::{ActiveState, DeviceState, VirtioDevice};
use crate::devices::virtio::generated::virtio_blk::{
VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_RO, VIRTIO_BLK_ID_BYTES,
VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_RO, VIRTIO_BLK_ID_BYTES,
};
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
use crate::devices::virtio::generated::virtio_ids::VIRTIO_ID_BLOCK;
Expand Down Expand Up @@ -298,7 +298,9 @@ impl VirtioBlock {
.map_err(VirtioBlockError::RateLimiter)?
.unwrap_or_default();

let mut avail_features = (1u64 << VIRTIO_F_VERSION_1) | (1u64 << VIRTIO_RING_F_EVENT_IDX);
let mut avail_features = (1u64 << VIRTIO_F_VERSION_1)
| (1u64 << VIRTIO_RING_F_EVENT_IDX)
| (1u64 << VIRTIO_BLK_F_DISCARD);

if config.cache_type == CacheType::Writeback {
avail_features |= 1u64 << VIRTIO_BLK_F_FLUSH;
Expand Down
17 changes: 16 additions & 1 deletion src/vmm/src/devices/virtio/block/virtio/io/async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ impl AsyncFileEngine {
Ok(())
}

#[cfg(test)]
pub fn file(&self) -> &File {
&self.file
}
Expand Down Expand Up @@ -230,6 +229,22 @@ impl AsyncFileEngine {
Ok(())
}

pub fn push_discard(
&mut self,
offset: u64,
len: u32,
req: PendingRequest,
) -> Result<(), RequestError<AsyncIoError>> {
let wrapped_user_data = WrappedRequest::new(req);

self.ring
.push(Operation::fallocate(0, len, offset, wrapped_user_data))
.map_err(|(io_uring_error, data)| RequestError {
req: data.req,
error: AsyncIoError::IoUring(io_uring_error),
})
}

fn do_pop(&mut self) -> Result<Option<Cqe<WrappedRequest>>, AsyncIoError> {
self.ring.pop().map_err(AsyncIoError::IoUring)
}
Expand Down
29 changes: 28 additions & 1 deletion src/vmm/src/devices/virtio/block/virtio/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ pub mod sync_io;

use std::fmt::Debug;
use std::fs::File;
use std::os::unix::io::AsRawFd;

use libc::{FALLOC_FL_KEEP_SIZE, FALLOC_FL_PUNCH_HOLE, c_int, off64_t};

pub use self::async_io::{AsyncFileEngine, AsyncIoError};
pub use self::sync_io::{SyncFileEngine, SyncIoError};
use crate::device_manager::mmio::MMIO_LEN;
use crate::devices::virtio::block::virtio::PendingRequest;
use crate::devices::virtio::block::virtio::device::FileEngineType;
use crate::vstate::memory::{GuestAddress, GuestMemoryMmap};
Expand Down Expand Up @@ -75,7 +79,6 @@ impl FileEngine {
Ok(())
}

#[cfg(test)]
pub fn file(&self) -> &File {
match self {
FileEngine::Async(engine) => engine.file(),
Expand Down Expand Up @@ -172,6 +175,30 @@ impl FileEngine {
FileEngine::Sync(engine) => engine.flush().map_err(BlockIoError::Sync),
}
}

pub fn discard(
&mut self,
offset: u64,
count: u32,
req: PendingRequest,
) -> Result<FileEngineOk, RequestError<BlockIoError>> {
match self {
FileEngine::Async(engine) => match engine.push_discard(offset, count, req) {
Ok(_) => Ok(FileEngineOk::Submitted),
Err(err) => Err(RequestError {
req: err.req,
error: BlockIoError::Async(err.error),
}),
},
FileEngine::Sync(engine) => match engine.discard(offset, count) {
Ok(count) => Ok(FileEngineOk::Executed(RequestOk { req, count })),
Err(err) => Err(RequestError {
req,
error: BlockIoError::Sync(err),
}),
},
}
}
}

#[cfg(test)]
Expand Down
51 changes: 50 additions & 1 deletion src/vmm/src/devices/virtio/block/virtio/io/sync_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

use std::fs::File;
use std::io::{Seek, SeekFrom, Write};
use std::os::unix::io::AsRawFd;

use libc::{FALLOC_FL_KEEP_SIZE, FALLOC_FL_PUNCH_HOLE, c_int, off64_t};
use vm_memory::{GuestMemoryError, ReadVolatile, WriteVolatile};

use crate::vstate::memory::{GuestAddress, GuestMemory, GuestMemoryMmap};
Expand All @@ -18,6 +20,8 @@ pub enum SyncIoError {
SyncAll(std::io::Error),
/// Transfer: {0}
Transfer(GuestMemoryError),
/// Discard: {0}
Discard(std::io::Error),
}

#[derive(Debug)]
Expand All @@ -33,7 +37,6 @@ impl SyncFileEngine {
SyncFileEngine { file }
}

#[cfg(test)]
pub fn file(&self) -> &File {
&self.file
}
Expand Down Expand Up @@ -81,4 +84,50 @@ impl SyncFileEngine {
// Sync data out to physical media on host.
self.file.sync_all().map_err(SyncIoError::SyncAll)
}

pub fn discard(&mut self, offset: u64, len: u32) -> Result<u32, SyncIoError> {
// Do checked conversion to avoid possible wrap/cast issues on 64-bit systems.
let off_i64 = i64::try_from(offset).map_err(|_| {
SyncIoError::Discard(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"offset overflow",
))
})?;

let len_i64: i64 = len.into();

// SAFETY: calling libc::fallocate is safe here because:
// - `self.file.as_raw_fd()` is a valid file descriptor owned by this struct,
// - `off_i64` and `len_i64` are validated copies of the incoming unsigned values converted
// to the C `off64_t` type, and
// - the syscall is properly checked for an error return value.
unsafe {
let ret = libc::fallocate(
self.file.as_raw_fd(),
libc::FALLOC_FL_PUNCH_HOLE | libc::FALLOC_FL_KEEP_SIZE,
off_i64,
len_i64,
);
if ret != 0 {
return Err(SyncIoError::Discard(std::io::Error::last_os_error()));
}
}
Ok(len)
}

pub fn fallocate(
fd: c_int,
mode: i32,
offset: off64_t,
len: off64_t,
) -> Result<(), std::io::Error> {
// SAFETY: calling libc::fallocate is safe because we're passing plain C-compatible
// integer types (fd, mode, offset, len) and we check the integer return value.
let ret: i32 = unsafe { libc::fallocate(fd, mode, offset, len) };
if ret == 0 {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
}
}
3 changes: 3 additions & 0 deletions src/vmm/src/devices/virtio/block/virtio/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ pub struct BlockDeviceMetrics {
pub invalid_reqs_count: SharedIncMetric,
/// Number of flushes operation triggered on this block device.
pub flush_count: SharedIncMetric,
/// Number of discard operation triggered on this block device.
pub discard_count: SharedIncMetric,
/// Number of events triggered on the queue of this block device.
pub queue_event_count: SharedIncMetric,
/// Number of events ratelimiter-related.
Expand Down Expand Up @@ -210,6 +212,7 @@ impl BlockDeviceMetrics {
self.invalid_reqs_count
.add(other.invalid_reqs_count.fetch_diff());
self.flush_count.add(other.flush_count.fetch_diff());
self.discard_count.add(other.discard_count.fetch_diff());
self.queue_event_count
.add(other.queue_event_count.fetch_diff());
self.rate_limiter_event_count
Expand Down
8 changes: 8 additions & 0 deletions src/vmm/src/devices/virtio/block/virtio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ pub enum VirtioBlockError {
RateLimiter(std::io::Error),
/// Persistence error: {0}
Persist(crate::devices::virtio::persist::PersistError),
/// Sector overflow in discard segment
SectorOverflow,
/// Discard segment exceeds disk size
BeyondDiskSize,
/// Invalid flags in discard segment
InvalidDiscardFlags,
/// Invalid discard request (e.g., empty segments)
InvalidDiscardRequest,
}
Loading