"sparse" backed block devices and TRIM/UNMAP

My driver is duplicating a block device from another source (say, VHD or block based backup file). If I detect that the source has a chunk of data that is all zeroes, how can I write that out in such a way that it will be zeroed on the target, but also not expand the target if it’s backed by a sparse format (eg VHD)?

So for example sectors 134217728 to 135266303 (for a total of 1MB of data) are all 0. After the operation is complete, I want to be sure that anything that reads that data gets a zero, but at the same time I don’t want my VHD(X) to grow by 1MB - I want it to stay sparse.

I think UNMAP will do this. My current plan is to detect support for UNMAP, and if present, send the UNMAP command (within the constraints of the reported support), and if it is unsupported or the UNMAP fails, then just write the zeroes. From a read of the SCSI spec, UNMAP should either wipe the sectors, or report failure. Can anyone confirm or deny this interpretation?

Thanks

James

>I think UNMAP will do this.

Thin-provisioned LUNs by Storage Spaces surely do support UNMAP properly.

Don’t know about Dynamic VHD files with VHDMP.SYS. Probably an experiment is needed.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com