From cd59d5364f5b32ac136e4a91b8534f2410059cde Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Sun, 4 Mar 2018 18:48:16 +0100 Subject: Windows: Implement TRIM support for non-system SSD partitions/drives and add driver option to enable it (TRIM is disabled by default for non-system SSD partitions/drives) --- src/Driver/Ntvol.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/Driver/Ntvol.c') diff --git a/src/Driver/Ntvol.c b/src/Driver/Ntvol.c index 81549ba1..c8552ab3 100644 --- a/src/Driver/Ntvol.c +++ b/src/Driver/Ntvol.c @@ -68,6 +68,10 @@ NTSTATUS TCOpenVolume (PDEVICE_OBJECT DeviceObject, Extension->HostMaximumPhysicalPages = 17; Extension->HostAlignmentMask = 0; + /* default values for non-SSD drives */ + Extension->IncursSeekPenalty = TRUE; + Extension->TrimEnabled = FALSE; + RtlInitUnicodeString (&FullFileName, pwszMountVolume); InitializeObjectAttributes (&oaFileAttributes, &FullFileName, OBJ_CASE_INSENSITIVE | (forceAccessCheck ? OBJ_FORCE_ACCESS_CHECK : 0) | OBJ_KERNEL_HANDLE, NULL, NULL); KeInitializeEvent (&Extension->keVolumeEvent, NotificationEvent, FALSE); @@ -152,6 +156,8 @@ NTSTATUS TCOpenVolume (PDEVICE_OBJECT DeviceObject, { STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR alignmentDesc = {0}; STORAGE_ADAPTER_DESCRIPTOR adapterDesc = {0}; + DEVICE_SEEK_PENALTY_DESCRIPTOR penaltyDesc = {0}; + DEVICE_TRIM_DESCRIPTOR trimDesc = {0}; storagePropertyQuery.PropertyId = StorageAccessAlignmentProperty; storagePropertyQuery.QueryType = PropertyStandardQuery; @@ -178,6 +184,28 @@ NTSTATUS TCOpenVolume (PDEVICE_OBJECT DeviceObject, Extension->HostMaximumPhysicalPages = adapterDesc.MaximumPhysicalPages; Extension->HostAlignmentMask = adapterDesc.AlignmentMask; } + + storagePropertyQuery.PropertyId = StorageDeviceSeekPenaltyProperty; + penaltyDesc.Version = sizeof (DEVICE_SEEK_PENALTY_DESCRIPTOR); + penaltyDesc.Size = sizeof (DEVICE_SEEK_PENALTY_DESCRIPTOR); + + if (NT_SUCCESS (TCSendHostDeviceIoControlRequestEx (DeviceObject, Extension, IOCTL_STORAGE_QUERY_PROPERTY, + (char*) &storagePropertyQuery, sizeof(storagePropertyQuery), + (char *) &penaltyDesc, sizeof (penaltyDesc)))) + { + Extension->IncursSeekPenalty = penaltyDesc.IncursSeekPenalty; + } + + storagePropertyQuery.PropertyId = StorageDeviceTrimProperty; + trimDesc.Version = sizeof (DEVICE_TRIM_DESCRIPTOR); + trimDesc.Size = sizeof (DEVICE_TRIM_DESCRIPTOR); + + if (NT_SUCCESS (TCSendHostDeviceIoControlRequestEx (DeviceObject, Extension, IOCTL_STORAGE_QUERY_PROPERTY, + (char*) &storagePropertyQuery, sizeof(storagePropertyQuery), + (char *) &trimDesc, sizeof (trimDesc)))) + { + Extension->TrimEnabled = trimDesc.TrimEnabled; + } } // Drive geometry is used only when IOCTL_DISK_GET_PARTITION_INFO fails -- cgit v1.2.3