MCPcopy Create free account
hub / github.com/cloud-hypervisor/cloud-hypervisor / preallocate_disk

Function preallocate_disk

block/src/lib.rs:425–453  ·  view source on GitHub ↗

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.

(file: &File, path: P)

Source from the content-addressed store, hash-verified

423/// more likely to place them contiguously than allocating on demand during
424/// random writes.
425pub fn preallocate_disk<P: AsRef<Path>>(file: &File, path: P) {
426 let size = match file.metadata() {
427 Ok(m) => m.len(),
428 Err(e) => {
429 warn!("Failed to get metadata for {:?}: {}", path.as_ref(), e);
430 return;
431 }
432 };
433
434 if size == 0 {
435 return;
436 }
437
438 // SAFETY: FFI call with valid file descriptor and size
439 let ret = unsafe { libc::fallocate(file.as_raw_fd(), 0, 0, size as libc::off_t) };
440
441 if ret != 0 {
442 warn!(
443 "Failed to preallocate disk space for {:?}: {}",
444 path.as_ref(),
445 io::Error::last_os_error()
446 );
447 } else {
448 debug!(
449 "Preallocated {size} bytes for disk image {:?}",
450 path.as_ref()
451 );
452 }
453}
454
455pub trait AsyncAdaptor {
456 fn read_vectored_sync(

Callers 1

open_rawFunction · 0.85

Calls 3

metadataMethod · 0.80
lenMethod · 0.45
as_raw_fdMethod · 0.45

Tested by

no test coverage detected