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 <anbelski@linux.microsoft.com>
This commit is contained in:
parent
0a287793df
commit
49a30cbbaf
2 changed files with 46 additions and 1 deletions
|
|
@ -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<P: AsRef<Path>>(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,
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue