From bbedead5fc767b50f38af384fc8c5b2ccc4a74c9 Mon Sep 17 00:00:00 2001 From: Fiona Ebner Date: Fri, 4 Apr 2025 15:31:38 +0200 Subject: [PATCH] common: add deallocate helper function For punching holes via fallocate. This will be useful for the external backup provider API to discard parts of the source. The 'file-handle' mechanism there uses a fuse mount, which does not implement the BLKDISCARD ioctl, but does implement fallocate. Signed-off-by: Fiona Ebner Tested-by: Wolfgang Bumiller Reviewed-by: Wolfgang Bumiller Link: https://lore.proxmox.com/20250404133204.239783-4-f.ebner@proxmox.com --- src/PVE/Storage/Common.pm | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/PVE/Storage/Common.pm b/src/PVE/Storage/Common.pm index 1f81234..bd9c951 100644 --- a/src/PVE/Storage/Common.pm +++ b/src/PVE/Storage/Common.pm @@ -4,6 +4,12 @@ use strict; use warnings; use PVE::JSONSchema; +use PVE::Syscall; + +use constant { + FALLOC_FL_KEEP_SIZE => 0x01, # see linux/falloc.h + FALLOC_FL_PUNCH_HOLE => 0x02, # see linux/falloc.h +}; =pod @@ -78,4 +84,27 @@ sub align_size_up : prototype($$) { return $aligned_size; } +=pod + +=head3 deallocate + + deallocate($file_handle, $offset, $length) + +Deallocates the range with C<$length> many bytes starting from offset C<$offset> +for the file associated to the file handle C<$file_handle>. Dies on failure. + +=cut + +sub deallocate : prototype($$$) { + my ($file_handle, $offset, $length) = @_; + + my $mode = FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE; + $offset = int($offset); + $length = int($length); + + if (syscall(PVE::Syscall::fallocate, fileno($file_handle), $mode, $offset, $length) != 0) { + die "fallocate: punch hole failed (offset: $offset, length: $length) - $!\n"; + } +} + 1;