From 49a30cbbaf7e4dc8e5a1de3e99dad560c9d90d2d Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Fri, 30 Jan 2026 18:57:33 +0100 Subject: [PATCH] block: raw: Implement disk preallocation for sparse=false When sparse=false is configured, preallocate the entire raw disk file at startup using fallocate(). This provides space reservation and reduces fragmentation. Only applies to raw disks. QCOW2/VHD/VHDX formats manage their own allocation. Signed-off-by: Anatol Belski --- block/src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ vmm/src/device_manager.rs | 11 ++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/block/src/lib.rs b/block/src/lib.rs index f6c990ce7..f718f3f82 100644 --- a/block/src/lib.rs +++ b/block/src/lib.rs @@ -787,6 +787,42 @@ fn probe_block_device_sparse_support(fd: libc::c_int) -> bool { supported } +/// Preallocate disk space for a disk image file. +/// +/// Uses `fallocate()` to allocate all disk space upfront, ensuring storage +/// availability and reducing fragmentation. Allocating all blocks upfront is +/// more likely to place them contiguously than allocating on demand during +/// random writes. +pub fn preallocate_disk>(file: &File, path: P) { + let size = match file.metadata() { + Ok(m) => m.len(), + Err(e) => { + warn!("Failed to get metadata for {:?}: {}", path.as_ref(), e); + return; + } + }; + + if size == 0 { + return; + } + + // SAFETY: FFI call with valid file descriptor and size + let ret = unsafe { libc::fallocate(file.as_raw_fd(), 0, 0, size as libc::off_t) }; + + if ret != 0 { + warn!( + "Failed to preallocate disk space for {:?}: {}", + path.as_ref(), + io::Error::last_os_error() + ); + } else { + debug!( + "Preallocated {size} bytes for disk image {:?}", + path.as_ref() + ); + } +} + pub trait AsyncAdaptor { fn read_vectored_sync( &mut self, diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index e476cc7a9..842f529c8 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -39,7 +39,8 @@ use block::raw_async_aio::RawFileDiskAio; use block::raw_sync::RawFileDiskSync; use block::vhdx_sync::VhdxDiskSync; use block::{ - ImageType, block_aio_is_supported, block_io_uring_is_supported, detect_image_type, qcow, vhdx, + ImageType, block_aio_is_supported, block_io_uring_is_supported, detect_image_type, + preallocate_disk, qcow, vhdx, }; #[cfg(feature = "io_uring")] use block::{fixed_vhd_async::FixedVhdDiskAsync, raw_async::RawFileDisk}; @@ -2699,6 +2700,14 @@ impl DeviceManager { } } ImageType::Raw => { + // For non-sparse RAW disks, preallocate disk space + if !disk_cfg.readonly + && !disk_cfg.sparse + && let Some(path) = &disk_cfg.path + { + preallocate_disk(&file, path); + } + // Use asynchronous backend relying on io_uring if the // syscalls are supported. if cfg!(feature = "io_uring")