diff mbox series

[1/1] scsi: storvsc: Handle BlockSize change in Hyper-V VHD/VHDX file

Message ID 1677516514-86060-1-git-send-email-mikelley@microsoft.com (mailing list archive)
State Accepted
Commit 11d9874c4204a785f43d899a1ab12f9dc8d9de3e
Headers show
Series [1/1] scsi: storvsc: Handle BlockSize change in Hyper-V VHD/VHDX file | expand

Commit Message

Michael Kelley (LINUX) Feb. 27, 2023, 4:48 p.m. UTC
Hyper-V uses a VHD or VHDX file on the host as the underlying storage for
a virtual disk.  The VHD/VHDX file format is a sparse format where real
disk space on the host is assigned in chunks that the VHD/VHDX file
format calls the BlockSize.  This BlockSize is not to be confused with
the 512-byte (or 4096-byte) sector size of the underlying storage device.
The default block size for a new VHD/VHDX file is 32 Mbytes.  When a
guest VM touches any disk space within a 32 Mbyte chunk of the VHD/VHDX
file, Hyper-V allocates 32 Mbytes of real disk space for that section of
the VHD/VHDX. Similiarly, if a discard operation is done that covers an
entire 32 Mbyte chunk, Hyper-V will free the real disk space for that
portion of the VHD/VHDX.  This BlockSize is surfaced in Linux as the
"discard_granularity" in /sys/block/sd<x>/queue, which makes sense.

Hyper-V also has differencing disks that can overlay a VHD/VHDX file to
capture changes to the VHD/VHDX while preserving the original VHD/VHDX.
One example of this differencing functionality is for VM snapshots.
When a snapshot is created, a differencing disk is created.  If the
snapshot is rolled back, Hyper-V can just delete the differencing disk,
and the VM will see the original disk contents at the time the snapshot
was taken. Differencing disks are used in other scenarios as well.

The BlockSize for a differencing disk defaults to 2 Mbytes, not 32 Mbytes.
The smaller default is used because changes to differencing disks are
typically scattered all over, and Hyper-V doesn't want to allocate 32
Mbytes of real disk space for a stray write here or there.  The smaller
BlockSize provides more efficient use of real disk space.

When a differencing disk is added to a VHD/VHDX, Hyper-V reports
UNIT_ATTENTION with a sense code indicating "Operating parameters have
changed", because the value of discard_granularity should be changed to
2 Mbytes. When the differencing disk is removed, discard_granularity
should be changed back to 32 Mbytes.  However, current code simply
reports a message from scsi_report_sense() and the value of
/sys/block/sd<x>/queue/discard_granularity is not updated. The message
isn't very actionable by a sysadmin.

Fix this by having the storvsc driver check for the sense code indicating
that the underly VHD/VHDX block size has changed, and do a rescan of the
device to pick up the new discard_granularity.  With this change the
entire transition to/from differencing disks is handled automatically
and transparently, with no confusing messages being output.

Signed-off-by: Michael Kelley <mikelley@microsoft.com>
---
 drivers/scsi/storvsc_drv.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
diff mbox series

Patch

diff --git a/drivers/scsi/storvsc_drv.c b/drivers/scsi/storvsc_drv.c
index d7a84c0..e79002d 100644
--- a/drivers/scsi/storvsc_drv.c
+++ b/drivers/scsi/storvsc_drv.c
@@ -988,6 +988,22 @@  static void storvsc_handle_error(struct vmscsi_request *vm_srb,
 			}
 
 			/*
+			 * Check for "Operating parameters have changed"
+			 * due to Hyper-V changing the VHD/VHDX BlockSize
+			 * when adding/removing a differencing disk. This
+			 * causes discard_granularity to change, so do a
+			 * rescan to pick up the new granularity. We don't
+			 * want scsi_report_sense() to output a message
+			 * that a sysadmin wouldn't know what to do with.
+			 */
+			if ((asc == 0x3f) && (ascq != 0x03) &&
+					(ascq != 0x0e)) {
+				process_err_fn = storvsc_device_scan;
+				set_host_byte(scmnd, DID_REQUEUE);
+				goto do_work;
+			}
+
+			/*
 			 * Otherwise, let upper layer deal with the
 			 * error when sense message is present
 			 */