Skip to contentSkip to conversation

blkio: fix writes when the logical block size is not 512 or 4096

PATCH New Itai Handler 1 participant 1 message Nov 4

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 blkio: fix writes when the logical block size is not 512 or 4096

Since lvm2 2.03.06, pvcreate fails on devices where neither the logical nor the physical block size is 512 or 4096 bytes, such as a loop device with 2048-byte sectors:

  # 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 rounds its metadata writes up to whole blocks, as direct I/O requires, but on such devices it rounds up to 512 bytes. Above, it writes 4608 bytes, 2.25 blocks of 2048 bytes, and the kernel rejects the write.

Move the rounding into blkio, which does the direct I/O, and round up to the logical block size of the device being written, read with BLKSSZGET. As explained in 669e2f2b6ccf ("label: don't round writes past the logical block size"), the added bytes cannot belong to an active LV. On devices with a logical or physical block size of 512 or 4096, that is almost all disks, LVM already rounds up to the logical block size, so they are not affected.

Such devices are uncommon but easy to create: loop devices with --sector-size 1024 or 2048, and dm-crypt with 1024- or 2048-byte sectors on disks with 512-byte physical sectors.

With this change, LVM can also use logical blocks larger than 4KiB, where the kernel supports them. Loop devices accept them on recent kernels, and dm-crypt accepts sectors up to the page size since target version 1.30.0, added by Linux commit 1acc65c30f85 ("dm-crypt: allow encryption sector size up to PAGE_SIZE"). For example, on a kernel configured with 64KiB pages, a dm-crypt device can have 64KiB sectors; without this change, the kernel rejects LVM's writes to it and pvcreate fails.

Fixes 7f347698e3d0 ("Fix rounding writes up to sector size")
Assisted-by LLM

---
v2:
- Round up in blkio, as Zdenek suggested: blkio reads the logical block size of the device it writes (BLKSSZGET), and dev_set_last_byte() only passes where the write must end.
- Rebased on main, which has since fixed the 1K/2K logical, 4K physical case separately (669e2f2b6).
- metadata-write-rounding.sh also covers 2048-byte logical blocks with 8192-byte physical blocks.
- logical-block-size.sh removes its LV and loop device if it fails.

v1: https://lore.kernel.org/lvm-devel/[email protected]/

Tested on main 7ea850a14 in QEMU, with Linux 7.3 on x86_64, where loop accepts sector sizes up to 64KiB, and with dm-crypt 1.30.0 (Linux 7.4 merge window, 65ba8f34ec88) on arm64 with 64KiB pages:
- logical-block-size passes with 1KiB to 64KiB loop devices, and metadata-write-rounding with all three cases on x86_64. Without the blkio change both fail, and pvcreate fails as shown above, with 64KiB pages at offset 65536.
- The unit tests, allow-mixed-block-sizes, large-physical-sector-size, mda-rollback, metadata-balance, pe-align, pvcreate-metadata0, pvcreate-operation and topology-support still pass on x86_64. On arm64, pe-align fails on main too, as it expects 4KiB pages, and so do pvcreate-metadata0 and topology-support, which need dm-snapshot, missing from that kernel.
- On dm-crypt with sector sizes from 1KiB up to the page size, pvcreate with two metadata areas, vgcreate, lvcreate, lvextend and reactivation work. Debian's lvm2 2.03.31 fails pvcreate on each, and main fails it with 64KiB sectors.

With pages larger than 4KiB, metadata-write-rounding fails in pvcreate, on main too. "test: skip metadata-write-rounding with pages larger than 4KiB", sent just before this patch, skips it. This patch applies with or without it.

 WHATS_NEW                             |  1 +
 lib/blkio/blkio.c                     | 46 ++++++++++++-----
 lib/blkio/blkio.h                     |  6 ++-
 lib/label/label.c                     | 37 +-------------
 test/shell/logical-block-size.sh      | 73 +++++++++++++++++++++++++++
 test/shell/metadata-write-rounding.sh | 15 +++---
 test/unit/blkio_t.c                   |  2 +-
 7 files changed, 124 insertions(+), 56 deletions(-)
 create mode 100644 test/shell/logical-block-size.sh
WHATS_NEW +1 -0
index fff0fcfde..65a3d57ec 100644
@@ -1,5 +1,6 @@
 Version 2.03.45 - 
 ==================
+  Fix failing writes on devices whose block sizes are neither 512 nor 4096.
   Fix vgcfgbackup segfault for a VG name ending in .tmp.
   Fix pvscan --allpvs crash when a PV has no device.
   Fix a stack overflow checking config definition paths with long names.
lib/blkio/blkio.c +34 -12

102 lines not on this page · raw message

lib/blkio/blkio.h +5 -1
index 7ca2a1e54..b7227443a 100644
@@ -80,7 +80,11 @@ bool blkio_zero_bytes(struct blkio *bio, int di, uint64_t start, size_t len);
 bool blkio_set_bytes(struct blkio *bio, int di, uint64_t start, size_t len, uint8_t val);
 bool blkio_invalidate_bytes(struct blkio *bio, int di, uint64_t start, size_t len);
 
-void blkio_set_last_byte(struct blkio *bio, int di, uint64_t offset, size_t sector_size);
+/*
+ * Writes to di stop at offset, rounded up to the logical block size of
+ * the device.
+ */
+void blkio_set_last_byte(struct blkio *bio, int di, uint64_t offset);
 void blkio_unset_last_byte(struct blkio *bio, int di);
 
 /*----------------------------------------------------------------*/
lib/label/label.c +1 -36
index 68698bf4d..e07c8a000 100644
@@ -2060,42 +2060,7 @@ fail:
 
 void dev_set_last_byte(struct device *dev, uint64_t offset)
 {
-	unsigned int physical_block_size = 0;
-	unsigned int logical_block_size = 0;
-	unsigned int bs;
-
-	if (!dev_get_direct_block_sizes(dev, &physical_block_size, &logical_block_size)) {
-		stack;
-		return; /* FIXME: error path ? */
-	}
-
-	if ((physical_block_size == 512) && (logical_block_size == 512))
-		bs = 512;
-	else if ((physical_block_size == 4096) && (logical_block_size == 4096))
-		bs = 4096;
-	else if ((physical_block_size == 512) || (logical_block_size == 512)) {
-		log_debug("Set last byte mixed block sizes physical %u logical %u using 512",
-			  physical_block_size, logical_block_size);
-		bs = 512;
-	} else if ((physical_block_size == 4096) || (logical_block_size == 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);
-		bs = 512;
-	}
-
-	blkio_set_last_byte(scan_blkio, dev->blkio_di, offset, bs);
+	blkio_set_last_byte(scan_blkio, dev->blkio_di, offset);
 }
 
 void dev_unset_last_byte(struct device *dev)
test/shell/logical-block-size.sh new file +73 -0

73 lines not on this page · raw message

test/shell/metadata-write-rounding.sh +9 -6
index 7bc2da2eb..d22cc3abb 100644
@@ -12,8 +12,9 @@
 
 # lvm writes a metadata update back from data it read earlier, up to the
 # end of the metadata area, rounded up to whole blocks for direct I/O.
-# With 1024- or 2048-byte logical and 4096-byte physical blocks, the
-# first PE can start at an offset that is not a multiple of 4096.  Check
+# With logical blocks smaller than physical ones, such as 1024- or
+# 2048-byte logical and 4096-byte physical blocks, the first PE can start
+# at an offset that is not a multiple of the physical block size.  Check
 # that a metadata update does not undo a write to an LV on that PE, made
 # while the command waits for confirmation.
 
@@ -22,11 +23,13 @@
 
 mkfifo fifo
 
-for bs in 1024 2048 ; do
-	aux prepare_scsi_debug_dev 64 sector_size=$bs \
-		physblk_exp=$(( bs == 1024 ? 2 : 1 )) || skip
+# Logical block size and physical block exponent: 1024/4096, 2048/4096
+# and 2048/8192.
+for sizes in "1024 2" "2048 1" "2048 2" ; do
+	read -r bs exp <<< "$sizes"
+	aux prepare_scsi_debug_dev 64 sector_size=$bs physblk_exp=$exp || skip
 	check sysfs "$(< SCSI_DEBUG_DEV)" queue/logical_block_size $bs
-	check sysfs "$(< SCSI_DEBUG_DEV)" queue/physical_block_size 4096
+	check sysfs "$(< SCSI_DEBUG_DEV)" queue/physical_block_size $(( bs << exp ))
 	aux prepare_pvs 1 60
 	get_devs
 
test/unit/blkio_t.c +1 -1
index afd9e3ae0..245ce8e2b 100644
@@ -1071,7 +1071,7 @@ static void test_rejected_write_preserves_pending(void *context)
 
 	T_ASSERT(blkio_get(f->bio, 0, 1, GF_ZERO, &b));
 	blkio_put(b);
-	blkio_set_last_byte(f->bio, 0, 512, 512);
+	blkio_set_last_byte(f->bio, 0, 512);
 	T_ASSERT(!blkio_flush(f->bio));
 
 	/* Removing the limit must let the same dirty block be retried. */

base-commit: 7ea850a1419522cc0c8096d718551015465322d2

Signature · 1 line
2.34.1