label: don't round writes past the logical block size
PATCH 1/1 label: don't round writes past the logical block size
When LVM updates its metadata, it writes back 128KiB pieces of the disk that it read earlier, cut short at the end of the metadata area and rounded up to whole blocks, as direct I/O requires.
LVM rounds up to 4096 on devices with 1024- or 2048-byte logical blocks and 4096-byte physical blocks, such as dm-crypt with 2048-byte sectors on a 512e disk. If LV data starts at an offset that is not a multiple of 4096, for example with pvcreate --dataalignmentoffset, the write also covers up to 3KiB of LV data with what LVM read earlier, undoing any write made there since.
Round up to the logical block size instead. That is all direct I/O needs, and the added bytes cannot belong to an active LV: an LV there would start at an offset that is not a multiple of the logical block size, which device-mapper refuses to map. Devices with 512- or 4096-byte logical blocks are not affected.
---
Split out of the review of "label: fix writes when the logical block size is not 512 or 4096", where this case came up:
https://lore.kernel.org/lvm-devel/[email protected]/
A v2 of that patch will follow on top of this one.
Tested on main 77da16212 with Linux 7.3 x86_64: the new test passes, and fails without the fix. allow-mixed-block-sizes, large-physical-sector-size, mda-rollback, metadata-balance, pe-align, pvcreate-metadata0, pvcreate-operation and topology-support still pass. Debian's lvm2 2.03.31 and Ubuntu's 2.03.11 lose the write too.
WHATS_NEW | 1 + lib/label/label.c | 14 ++++-- test/shell/metadata-write-rounding.sh | 66 +++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 test/shell/metadata-write-rounding.sh
WHATS_NEW +1 -0
@@ -1,5 +1,6 @@ Version 2.03.45 - ================== + Fix metadata writes overwriting LV data with 1K or 2K logical block size. Skip kernel VDO formatting in test mode for pool creation and conversion. Round up and size extended RAID integrity metadata with image journal. Fit percentage-sized RAID integrity LVs with their metadata.
lib/label/label.c +11 -3
@@ -2078,9 +2078,17 @@ void dev_set_last_byte(struct device *dev, uint64_t offset) physical_block_size, logical_block_size); bs =512 ; }else if ((physical_block_size ==4096 ) || (logical_block_size ==4096 )) { -log_debug ("Set last byte mixed block sizes physical %u logical %u using 4096" , - physical_block_size, logical_block_size); - bs =4096 ; +/* +* Use the logical block size, not 4096. With 1024 or 2048 +* byte logical blocks, the first PE can start at an offset +* that is not a multiple of 4096, and rounding up to 4096 +* would overwrite its start with data read earlier. +* device-mapper maps PEs only at multiples of the logical +* block size, so rounding up to that never reaches a PE in use. +*/ + bs = logical_block_size ? logical_block_size :4096 ; +log_debug ("Set last byte mixed block sizes physical %u logical %u using %u" , + physical_block_size, logical_block_size, bs); }else {log_debug ("Set last byte mixed block sizes physical %u logical %u using 512" , physical_block_size, logical_block_size);
test/shell/metadata-write-rounding.sh new file +66 -0
66 lines not on this page · raw message
base-commit: 77da16212876fa7694fb266575b37a88c6e0108c
Signature · 1 line
2.34.1