From 39e5776712f706d522e2999ef183d63134fcb33a Mon Sep 17 00:00:00 2001 From: Wesley Hershberger Date: Fri, 10 May 2024 16:30:24 -0500 Subject: [PATCH] lxd/storage/drivers/zfs: Round to zfs.blocksize or 16KiB Fixes #13420 Signed-off-by: Wesley Hershberger --- lxd/storage/drivers/driver_zfs.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lxd/storage/drivers/driver_zfs.go b/lxd/storage/drivers/driver_zfs.go index 5af19b7f107f..462e3865ff74 100644 --- a/lxd/storage/drivers/driver_zfs.go +++ b/lxd/storage/drivers/driver_zfs.go @@ -743,3 +743,28 @@ func (d *zfs) patchDropBlockVolumeFilesystemExtension() error { return nil } + +// roundVolumeBlockSizeBytes returns sizeBytes rounded up to the next multiple +// of `vol`'s "zfs.blocksize". +func (d *zfs) roundVolumeBlockSizeBytes(vol Volume, sizeBytes int64) int64 { + minBlockSize, err := units.ParseByteSizeString(vol.ExpandedConfig("zfs.blocksize")) + + // minBlockSize will be 0 if zfs.blocksize="" + if minBlockSize <= 0 || err != nil { + // 16KiB is the default volblocksize + minBlockSize = 16 * 1024 + } + + if sizeBytes < minBlockSize { + sizeBytes = minBlockSize + } + + roundedSizeBytes := int64(sizeBytes/minBlockSize) * minBlockSize + + // Ensure the rounded size is at least the size specified in sizeBytes. + if roundedSizeBytes < sizeBytes { + roundedSizeBytes += minBlockSize + } + + return roundedSizeBytes +}