block: Add sparse capability detection

Add functions to probe whether a file or block device actually
supports PUNCH_HOLE and ZERO_RANGE operations at runtime. The
probe is performed at file open time by testing the operations
at EOF with a zero-length range, which is a safe no-op.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski
2026-02-13 16:30:15 +01:00
committed by Rob Bradford
parent 7095605d84
commit d5dad48618
2 changed files with 110 additions and 1 deletions

View File

@@ -108,6 +108,10 @@ mod kvm {
pub const KVM_SET_NESTED_STATE: u64 = 1082175167;
}
// Block device ioctls for sparse support probing (not exported by libc)
const BLKDISCARD: u64 = 0x1277; // _IO(0x12, 119)
const BLKZEROOUT: u64 = 0x127f; // _IO(0x12, 127)
// MSHV IOCTL code. This is unstable until the kernel code has been declared stable.
#[cfg(feature = "mshv")]
use hypervisor::mshv::mshv_ioctls::*;
@@ -259,6 +263,8 @@ fn create_vmm_ioctl_seccomp_rule_common(
and![Cond::new(1, ArgLen::Dword, Eq, BLKPBSZGET as _)?],
and![Cond::new(1, ArgLen::Dword, Eq, BLKIOMIN as _)?],
and![Cond::new(1, ArgLen::Dword, Eq, BLKIOOPT as _)?],
and![Cond::new(1, ArgLen::Dword, Eq, BLKDISCARD as _)?],
and![Cond::new(1, ArgLen::Dword, Eq, BLKZEROOUT as _)?],
and![Cond::new(1, ArgLen::Dword, Eq, FIOCLEX as _)?],
and![Cond::new(1, ArgLen::Dword, Eq, FIONBIO as _)?],
and![Cond::new(1, ArgLen::Dword, Eq, SIOCGIFFLAGS)?],