Re: [PATCH 2/2] scsi: sd: Fix data corruption in WRITE SAME(10) discards
From: Chris Down
Date: Sat Nov 28 2026 - 04:27:06 EST
Hi Christoph,
Christoph Hellwig writes:
>Nah, that's not really how things work. See my reply to patch 1
>for the suggested proper fix.
Sure, I'll do the same for writesame_10 and writesame_zero in v2.
Even with that, a discard can still reach sd_setup_write_same10_cmnd()
with a count that doesn't fit, though.
One case is a discard that is already queued when the mode changes. sd
sets provisioning_mode before it freezes the queue to commit the new
limits, so that request is still sized for the old mode. For that one
I'll set the mode with the queue frozen instead, as
nvme_update_ns_info_block() does for its own state.
The other is that __bio_split_discard() doesn't split a discard at all
when max_discard_sectors is zero or below the discard granularity. With
scsi_debug, 512-byte blocks and an unmap granularity of 0x10000 blocks,
a 64 MiB discard in writesame_10 mode goes out as a single WRITE
SAME(10) with a count of zero. sd can avoid that one by disabling
discard when the granularity is above its limit, as drbd does. But
writing 0 to discard_max_bytes while a large discard is running also
sends the rest of the bio to sd unsplit, and I don't see how sd can
prevent that with its limits.
An LBA that doesn't fit can still get there as well. bio_check_eod()
only checks a bio against the partition it was submitted to. If a disk
shrinks to 2^32 blocks or fewer while a partition on it still ends
above that, a discard to the end of that partition is sent as WRITE
SAME(10) with its LBA truncated. sd_setup_read_write_cmnd() fails reads
and writes there with "access beyond end of device", but discards and
write zeroes have no such check.
Would you rather I fix those in the block layer, or have sd check the
LBA and count before it builds the command?
Chris