virtio-devices: block: Derive discard alignment from topology

Set discard_sector_alignment from the logical block size
reported by the backend topology instead of hardcoding it
to 1 sector. This gives the guest accurate alignment hints
so it can avoid sub block discards that the filesystem
might silently ignore.

For example, on a 4K block filesystem the alignment is now
8 sectors (4096/512) instead of 1.

For image formats with their own allocation units (QCOW2
clusters, VHD/VHDX block sizes), the ideal alignment would
be derived from the format cluster/block size. This is
left for a followup that surfaces allocation granularity
through DiskTopology.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski
2026-03-17 17:49:09 +01:00
committed by Bo Chen
parent 9655eaddd5
commit 0ee0441f2b

View File

@@ -833,7 +833,7 @@ impl Block {
if avail_features & (1u64 << VIRTIO_BLK_F_DISCARD) != 0 {
config.max_discard_sectors = u32::MAX;
config.max_discard_seg = 1;
config.discard_sector_alignment = 1;
config.discard_sector_alignment = (logical_block_size / SECTOR_SIZE) as u32;
}
if num_queues > 1 {