label: fix writes when the logical block size is not 512 or 4096
PATCH 1/1 label: fix writes when the logical block size is not 512 or 4096
Since lvm2 2.03.06, LVM cannot be used on a device whose logical block size, the smallest unit it can read or write, is neither 512 nor 4096 bytes. pvcreate, which prepares a device for LVM, fails:
# truncate -s 64M disk.img
# losetup --sector-size 2048 --show -f disk.img
/dev/loop8
# pvcreate /dev/loop8
WARNING: threads io engine write device offset 0 len 4608: Invalid argument.
Error writing device /dev/loop8 at 4096 length 512.
Failed to write mda header to /dev/loop8.
Failed to write physical volume "/dev/loop8".LVM writes with direct I/O, bypassing the kernel's cache, and such writes must cover whole logical blocks. LVM keeps the data it reads in memory, in 128KiB pieces. To save a change, it writes a piece back from its start up to the end of the area being updated, rounded up to a whole block. But for this rounding it only knows block sizes of 512 and 4096 bytes, and uses 512 for any other size. So above, to update the 512-byte metadata header at offset 4096, LVM writes the first 4608 bytes, 2.25 blocks of 2048 bytes, and the kernel rejects the write.
Fix this by rounding up to the logical block size of the device when it is neither 512 nor 4096. Writes then cover a little more of the device, but the extra bytes are rewritten with the data already there, just as on devices with 4096-byte blocks. Devices with 512- or 4096-byte blocks, which are almost all disks, are not affected.
Devices with other block sizes are uncommon, but easy to create: loop devices with --sector-size 1024 or 2048, and dm-crypt (disk encryption) devices with 1024- or 2048-byte sectors. On kernels configured with pages larger than 4KiB, both can also use 8KiB up to the page size. For dm-crypt this needs target version 1.30.0, added by Linux commit 1acc65c30f85 ("dm-crypt: allow encryption sector size up to PAGE_SIZE"). With blocks that large, the fix also lets lvcreate clear the first 4KiB of a new logical volume, which inherits the block size of the device it is on.
---
Tested on main a2decfaf4:
- Linux 6.8 x86_64 host: the new test passes with 1KiB and 2KiB loop devices and fails without the fix (larger sizes are skipped, loop accepts at most the page size there). allow-mixed-block-sizes, large-physical-sector-size and topology-support still pass. pvcreate with two metadata areas, vgcreate, lvcreate, lvextend and reactivation work on dm-crypt with sector_size 2048.
- QEMU, Linux 7.4 merge window kernel (65ba8f34ec88), arm64 with 64KiB
pages: the new test passes with all six sizes, 1KiB to 64KiB, and fails without the fix. The same sequence works on dm-crypt with these sector sizes; a new LV on reused extents has its first 4KiB zeroed and the rest of its first logical block unchanged. Debian's lvm2 2.03.31 fails pvcreate on each of these devices.
WHATS_NEW | 1 + lib/label/label.c | 11 ++++-- test/shell/logical-block-size.sh | 65 ++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 test/shell/logical-block-size.sh
WHATS_NEW +1 -0
@@ -1,5 +1,6 @@ Version 2.03.45 - ================== + Fix writes to devices with logical block size other than 512 or 4096. Explain RAID extend failures when images share a PV or PVs differ. Version 2.03.44 - 27th October 2026
lib/label/label.c +8 -3
@@ -2082,9 +2082,14 @@ void dev_set_last_byte(struct device *dev, uint64_t offset) physical_block_size, logical_block_size); bs =4096 ; }else { -log_debug ("Set last byte mixed block sizes physical %u logical %u using 512" , - physical_block_size, logical_block_size); - bs =512 ; +/* +* Neither size is 512 or 4096, e.g. a loop device or dm-crypt +* with 2048-byte sectors. Direct I/O must still cover whole +* logical blocks. +*/ + bs = logical_block_size ? logical_block_size :512 ; +log_debug ("Set last byte block sizes physical %u logical %u using %u" , + physical_block_size, logical_block_size, bs); }blkio_set_last_byte (scan_blkio, dev->blkio_di, offset, bs);
test/shell/logical-block-size.sh new file +65 -0
65 lines not on this page · raw message
base-commit: a2decfaf4ce6f132137da61ba9132578b840e333
Signature · 1 line
2.34.1
Dne 29. 10. 26 v 13:53 Itai Handler napsal(a):
Quoted 4 lines
Since lvm2 2.03.06, LVM cannot be used on a device whose logical block size, the smallest unit it can read or write, is neither 512 nor 4096 bytes. pvcreate, which prepares a device for LVM, fails:
Quoted 8 lines
# truncate -s 64M disk.img
# losetup --sector-size 2048 --show -f disk.img
/dev/loop8
# pvcreate /dev/loop8
WARNING: threads io engine write device offset 0 len 4608: Invalid argument.
Error writing device /dev/loop8 at 4096 length 512.
Failed to write mda header to /dev/loop8.
Failed to write physical volume "/dev/loop8".Hi
Hmm - this thread io engine is surely a new logic we are putting in.
I'd like to know more details what is the scenario we are trying to solve - normally we expect devices to really have 512 or 4K writable blocks.
We have some set of patches to improve support on systems with 64K pagesize.
But I'm puzzled from where do we get 1K or 2K block sizes ??
Quoted 5 lines
Fix this by rounding up to the logical block size of the device when it is neither 512 nor 4096. Writes then cover a little more of the device, but the extra bytes are rewritten with the data already there, just as on devices with 4096-byte blocks. Devices with 512- or 4096-byte blocks, which are almost all disks, are not affected.
I think we want to fully hide this in blkio - label layer should be above.
So I will think more about this.
Quoted 2 lines
Devices with other block sizes are uncommon, but easy to create: loop
Can you tell why do you actually need to use this blocksize - IMHO it must bring some inefficiency ??
Regards
Zdenek
Hi Zdenek,
Quoted 1 line · Zdenek Kabelac
Hmm - this thread io engine is surely a new logic we are putting in.
This is not related to the threads engine; the warning just names the engine in use. On main, pvcreate fails the same way with io_engine set to sync or libaio, and also with releases from before the threads engine, such as Ubuntu's 2.03.11 and Debian's 2.03.31. Built from the release tags, 2.03.05 works on 1024- and 2048-byte loop devices (pvcreate, vgcreate, lvcreate, vgremove), and 2.03.06 fails on the 2048-byte one from the patch:
# pvcreate /dev/loop8
Error writing device /dev/loop8 at 4096 length 512.
bcache_invalidate: block (3, 0) still dirty
Failed to write mda header to /dev/loop8 fd -1
Failed to write physical volume "/dev/loop8".The cause is 7f347698e3d0 ("Fix rounding writes up to sector size"), which went into 2.03.06. Before it, writes were rounded up to the physical block size, which is never smaller than the logical one. Since then only 512 and 4096 are used, and when neither block size is one of them, it falls back to 512.
Quoted 8 lines
I'd like to know more details what is the scenario we are trying to solve - normally we expect devices to really have 512 or 4K writable blocks.
We have some set of patches to improve support on systems with 64K pagesize.
But I'm puzzled from where do we get 1K or 2K block sizes ??
I ran into this while testing LVM on dm-crypt with sectors larger than 4KiB. A dm-crypt device uses its encryption sector size as its logical block size. dm-crypt has accepted sector sizes from 512 to 4096 since Linux 4.12, and my kernel change 1acc65c30f85 ("dm-crypt:
allow encryption sector size up to PAGE_SIZE", target version 1.30.0) raises the limit to the page size, so on kernels configured with 64KiB pages it can also be 8KiB to 64KiB. pvcreate fails on all of these, and while looking into why, I found that 1K and 2K fail the same way on any kernel.
1K and 2K block sizes are not common, but they are valid today:
- cryptsetup has offered --sector-size for LUKS2 and plain devices since 2.0.0, and accepts 1024 and 2048. For example, pvcreate fails on a LUKS2 device made with "cryptsetup luksFormat --sector-size 2048", and LVM on LUKS is a common setup.
- s390 DASDs can be formatted with 1024- or 2048-byte blocks (dasdfmt -b), although 4096 is the recommended size.
- loop devices with --sector-size, which is the easiest way to reproduce it.
The 64K page size changes already in main, such as 3e91a5265 ("striped: allow stripe size smaller than page size"), were in the tree I tested, and do not change this. If you have more that are not in main yet, I'd be glad to test them on my arm64 VM with 64KiB pages.
Quoted 5 lines
Quoted 2 lines
Fix this by rounding up to the logical block size of the device when it is neither 512 nor 4096. [...]
I think we want to fully hide this in blkio - label layer should be above.
Agreed, blkio does the direct I/O, so it should know the alignment it needs. For example, it could get the logical block size of each fd it is given (BLKSSZGET) and round shortened writes up to that, so the label layer passes only the offset. For devices with 512- or 4096-byte logical blocks this rounds exactly as today, including those with a huge physical block size, as in large-physical-sector-size.sh.
Quoted 1 line
So I will think more about this.
Sure. I can send a v2 along those lines, keeping the test, or test your version if you prefer to write it.
Quoted 4 lines
Quoted 1 line
Devices with other block sizes are uncommon, but easy to create: loop
Can you tell why do you actually need to use this blocksize - IMHO it must bring some inefficiency ??
I agree that blocks smaller than the disk's sector, such as 2KiB on an SSD with 4KiB sectors, are inefficient, since a 2KiB write makes the disk read and rewrite a whole 4KiB sector. I don't need those myself; I mention them only because they hit the same bug.
What I want is the opposite: larger blocks, from 8KiB up to 64KiB, for dm-crypt on kernels configured with 64KiB pages. These are whole multiples of the disk's sector, so they do not have that problem.
Whichever size is used, the cost to LVM is small. Only LVM's own writes, such as metadata updates, are rounded up to whole blocks, which adds less than one block to each, and they happen only when LVM changes something. LV data does not go through this code, and the default 1MiB pe_start keeps extents aligned for any of these sizes.
Thanks,
Itai
Quoted 5 lines · Itai Handler
Whichever size is used, the cost to LVM is small. Only LVM's own writes, such as metadata updates, are rounded up to whole blocks, which adds less than one block to each, and they happen only when LVM changes something. LV data does not go through this code, and the default 1MiB
pe_startkeeps extents aligned for any of these sizes.
This last line touches on the main concern I have with the rouding up. dev_set_last_byte was added by aecf542126640faa17c240afbb1ea61f11355c39 which addressed a very subtle data corruption bug that was introduced by the bcache (now blkio) layer. In order to safely round up, we need to be certain that no VGs exist in which that rounded up space could be used by LV extents. In the past (less so today), the pe_start value could be highly variable due to several config settings or command line options. We'll need to prove that current and past versions of lvm would not have placed pe_start within the rounded-up space.
Dave
Hi David,
Quoted 9 lines · David Teigland
This last line touches on the main concern I have with the rouding up.
dev_set_last_bytewas added byaecf542126640faa17c240afbb1ea61f11355c39which addressed a very subtle data corruption bug that was introduced by the bcache (now blkio) layer. In order to safely round up, we need to be certain that no VGs exist in which that rounded up space could be used by LV extents. In the past (less so today), thepe_startvalue could be highly variable due to several config settings or command line options. We'll need to prove that current and past versions of lvm would not have placedpe_startwithin the rounded-up space.
Right, my last line only covered the default pe_start. But I think the kernel makes rounding up safe for any pe_start, whatever version or option chose it, as long as lvm rounds up to the logical block size and no further.
Rounding adds the bytes from the end of the area being written (label, mda header or metadata area) up to the next multiple of the logical block size. An extent can't overlap these areas, so if it holds any of the added bytes, it starts among them, between two multiples of the logical block size. device-mapper refuses to map such an extent (device_area_is_invalid() in dm-table.c, since 2.6.31), so no active LV can write those bytes, and writing back what lvm read there changes nothing.
For example, with this patch, on a loop device with 2048-byte blocks:
# pvcreate --dataalignmentoffset 1k /dev/loop0
# vgcreate vg /dev/loop0
# lvcreate -l1 -n lv vg
device-mapper: reload ioctl on (252:0) failed: Invalid argument
Failed to activate new LV vg/lv.and the kernel logs "start=2050 not aligned to h/w logical block size 2048 of loop0". pe_start is 1049600 bytes (sector 2050), so metadata writes that end there are rounded up 1KiB into the first extent, but no LV can use that extent.
However, main doesn't always stop at the logical block size: with 1K or 2K logical and 4K physical blocks, such as dm-crypt with 2048-byte sectors on a 512e disk, it rounds up to 4096. If pe_start is a multiple of the logical block size but not of 4096, the first extent can be active, and a metadata update puts up to 3KiB of old data back at its start. I reproduced this lost write with scsi_debug on main, and with Ubuntu's 2.03.11 and Debian's 2.03.31. It needs an unusual pe_start, so I doubt anyone has hit it.
So I'm sending a fix for that first, as a separate patch on top of
main: "label: don't round writes past the logical block size". Then I'll send v2 of this patch on top of it, with the rounding moved into blkio as Zdenek suggested, using the logical block size of each fd (BLKSSZGET). Compared with main plus that fix, v2 would only change devices where lvm can't write today.
Thanks,
Itai