windowswinapistoragevirtualizationscsi

How to determine what bloks are allocated for the disk device?


A lot of modern storage types use Thin provisioning to allocate blocks. I need to get Block allocation map for the disk device. There is FSCTL_GET_VOLUME_BITMAP to get volume bitmap, but it is file-system specific and I need an approach that is not FS specific. Starting in Windows 8 Windows is sending "TRIM and Unmap" hints to storage media to track allocated blocks.

UNMAP is the SCSI command by which an application or the system can communicate to the storage stack and the disk that a certain sector or range of sectors are currently not in use, including sectors that were previously in use by files that were later deleted.

So this should be possible. Unfortunately, I was unable to find Disk Management Control Code or Disk Management Function to get it. Maybe someone know know the way to get it?


Solution

  • Like gubblebozer made a hint - GET LBA STATUS command introduced in SBC-3 is the way to retrieve the low-level mappings from the device itself. From Thin Provisioning

    The application can call the IOCTL DSM allocation routine to send the SCSI 
    command and retrieve the mapped or unmapped state of each slab in a particular 
    range. If the LBA provisioning status returned does not describe the entire 
    allocation range, the application sends another SCSI command to retrieve the 
    provisioning status of the remaining LBA range.
    

    Looks like this can be done with the help of IOCTL_STORAGE_MANAGE_DATA_SET_ATTRIBUTES then DEVICE_DATA_SET_LB_PROVISIONING_STATE structure will contain a bitmap of slab allocations.