From fab55c3d14fe76fc05425aca0db5ee4ef10c748c Mon Sep 17 00:00:00 2001 From: dahall Date: Mon, 11 Apr 2022 15:42:08 -0600 Subject: [PATCH] VirtDisk: Added CREATE_VIRTUAL_DISK_PARAMETERS.ctor, RAW_SCSI_VIRTUAL_DISK_PARAMETERS.ctor and enhanced OPEN_VIRTUAL_DISK_PARAMETERS.ctor for platform adherance. --- PInvoke/VirtDisk/VirtDisk.cs | 83 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 11 deletions(-) diff --git a/PInvoke/VirtDisk/VirtDisk.cs b/PInvoke/VirtDisk/VirtDisk.cs index fb723f84..d2ce9ec8 100644 --- a/PInvoke/VirtDisk/VirtDisk.cs +++ b/PInvoke/VirtDisk/VirtDisk.cs @@ -889,7 +889,10 @@ namespace Vanara.PInvoke /// VIRTUAL_STORAGE_TYPE_DEVICE_VHDX = 3, - /// + /// + /// VHD Set files (.vhds file) are a new shared Virtual Disk model for guest clusters in Windows Server 2016. VHD Set files + /// support online resizing of shared virtual disks, support Hyper-V Replica, and can be included in application-consistent checkpoints. + /// VIRTUAL_STORAGE_TYPE_DEVICE_VHDSET = 4 } @@ -1809,6 +1812,30 @@ namespace Vanara.PInvoke Version1.SectorSizeInBytes = logicalSectorSize; } + /// Initializes a CREATE_VIRTUAL_DISK_PARAMETERS with a maximum size. + /// + /// Optional path to a parent virtual disk object. Associates the new virtual disk with an existing virtual disk. If this + /// parameter is not NULL, SourcePath must be NULL. + /// + /// + /// Fully qualified path to pre-populate the new virtual disk object with block data from an existing disk. This path may refer + /// to a virtual disk or a physical disk. If this parameter is not NULL, SourcePath must be NULL. + /// + public CREATE_VIRTUAL_DISK_PARAMETERS(IntPtr pParentPath, IntPtr pSourcePath) : this() + { + Version = Environment.OSVersion.Version < new Version(6, 2) ? CREATE_VIRTUAL_DISK_VERSION.CREATE_VIRTUAL_DISK_VERSION_1 : CREATE_VIRTUAL_DISK_VERSION.CREATE_VIRTUAL_DISK_VERSION_2; + if (Version == CREATE_VIRTUAL_DISK_VERSION.CREATE_VIRTUAL_DISK_VERSION_1) + { + Version1.ParentPath = pParentPath; + Version1.SourcePath = pSourcePath; + } + else + { + Version2.ParentPath = pParentPath; + Version2.SourcePath = pSourcePath; + } + } + /// /// Contains virtual hard disk (VHD) creation parameters, providing control over, and information about, the newly created /// virtual disk. @@ -2377,6 +2404,38 @@ namespace Vanara.PInvoke /// Caller-supplied CDB data. (The CDB structure is declared in scsi.h.) public IntPtr Cdb; } + + /// Initializes a new instance of the struct. + /// + /// If , indicates the SCSI command will read data from the DataBuffer. If , + /// indicates data may be written. + /// + /// + /// Caller-supplied SRB_FLAGS-prefixed bit flag specifying the requested operation. Flags are defined in srb.h. + /// + /// The SCSI data buffer. + /// A buffer to receive SCSI sense info after completion of the command. + /// Caller-supplied CDB data. (The CDB structure is declared in scsi.h.) + /// + /// If , indicates the operation will be transported to the virtual disk using the RSVD protocol. + /// + public RAW_SCSI_VIRTUAL_DISK_PARAMETERS(bool read, byte srbFlags, SafeAllocatedMemoryHandleBase scsiData, + SafeAllocatedMemoryHandleBase senseInfo, SafeAllocatedMemoryHandleBase cbdData, bool useRSVD = false) + { + Version = RAW_SCSI_VIRTUAL_DISK_VERSION.RAW_SCSI_VIRTUAL_DISK_VERSION_1; + Version1 = new() + { + RSVDHandle = useRSVD, + DataIn = read, + CdbLength = (byte)(uint)cbdData.Size, + SenseInfoLength = (byte)(uint)senseInfo.Size, + SrbFlags = srbFlags, + DataTransferLength = scsiData.Size, + DataBuffer = scsiData, + SenseInfo = senseInfo, + Cdb = cbdData + }; + } } /// Contains raw SCSI virtual disk response parameters. @@ -2401,7 +2460,7 @@ namespace Vanara.PInvoke /// A SRB_STATUS-prefixed status value (defined in srb.h). public byte ScsiStatus; - /// Length, in bytes, of the sense buffer. + /// A SRB_STATUS-prefixed status value (defined in srb.h). public byte SenseInfoLength; /// Length, in bytes, of the buffer to be transferred. @@ -2832,25 +2891,27 @@ namespace Vanara.PInvoke /// /// Initializes a new instance of the struct setting Version to OPEN_VIRTUAL_DISK_VERSION_2. - /// - /// Windows 7 and Windows Server 2008 R2: This constructor is not supported until Windows 8 and Windows Server 2012. - /// + /// Only supported on Windows 8 and later. /// /// If TRUE, indicates the file backing store is to be opened as read-only. /// If TRUE, indicates the handle is only to be used to get information on the virtual disk. /// Resiliency GUID to specify when opening files. - public OPEN_VIRTUAL_DISK_PARAMETERS(bool readOnly, bool getInfoOnly = false, Guid resiliencyGuid = default) + /// The snapshot identifier. Only supported on Windows 10 and later. + public OPEN_VIRTUAL_DISK_PARAMETERS(bool readOnly, bool getInfoOnly = false, Guid resiliencyGuid = default, Guid snapshotId = default) { - if (Environment.OSVersion.Version < new Version(6, 2)) - throw new InvalidOperationException(); - Version = OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_2; + if (!PInvokeClient.Windows8.IsPlatformSupported()) + throw new NotSupportedException(); + if (snapshotId != Guid.Empty && !PInvokeClient.Windows10.IsPlatformSupported()) + throw new NotSupportedException("Snapshots are only supported on Windows 10 and later."); + Version = PInvokeClient.Windows10.IsPlatformSupported() ? OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_3 : OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_2; Version2.GetInfoOnly = getInfoOnly; Version2.ReadOnly = readOnly; Version2.ResiliencyGuid = resiliencyGuid; + Version3.SnapshotId = snapshotId; } - /// Gets the default value for this structure. This is currently the only valid value for . - public static OPEN_VIRTUAL_DISK_PARAMETERS DefaultV2 => new(false); + /// Gets the default value for this structure based on current OS version. + public static OPEN_VIRTUAL_DISK_PARAMETERS Default => PInvokeClient.Windows8.IsPlatformSupported() ? new(false) : new(0); /// public override string ToString()