Getting error when using FSCTL_FILE_LEVEL_TRIM

Hi all,

Why my trim command failed?

hUDisk = CreateFile(L"\\.\C:\test.txt", //size of test.txt is 24MB
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,OPEN_EXISTING,0,NULL
);
if (hUDisk ==INVALID_HANDLE_VALUE)
{
printf(“Getting disk fail\n”);
return 0;
}

ZeroMemory(&FLTRWB, sizeof(FILE_LEVEL_TRIM_RANGE));
ZeroMemory(&FLTOWB, sizeof(FILE_LEVEL_TRIM_OUTPUT));

FLTRWB.Length = 4096;//In bytes
FLTRWB.Offset = 0;//In bytes

iRet = DeviceIoControl(hUDisk,
FSCTL_FILE_LEVEL_TRIM,
&FLTRWB,
sizeof(FILE_LEVEL_TRIM_RANGE),
&FLTOWB,
sizeof(FILE_LEVEL_TRIM_OUTPUT),
&bytesReturn,
&overlap);

if (0 == iRet)
{
printf(“error code = 0x%x\n”,GetLastError());
CloseHandle(hUDisk);
return 0;
}

error code is 0x57
means ERROR_INVALID_PARAMETER

On msdn website, they say it could be caused by “The file to trim is either compressed or encrypted, the input or output buffer length is invalid, or, no trim ranges are specified.”

However,my file to trim is FILE_ATTRIBUTE_ARCHIVE,and input buffer and output buffer is filled,and trim range is specified.

Thanks.

Hi all,

The input buffer structure is FILE_LEVEL_TRIM instead of FILE_LEVEL_TRIM_RANGE.
Following is Modified program

hUDisk = CreateFile(L"\\.\C:\test.txt", //size of test.txt is 24MB
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,OPEN_EXISTING,0,NULL
);
if (hUDisk ==INVALID_HANDLE_VALUE)
{
printf(“Getting disk fail\n”);
return 0;
}
ZeroMemory(&FLT,sizeof(FILE_LEVEL_TRIM));
ZeroMemory(&FLTO, sizeof(FILE_LEVEL_TRIM_OUTPUT));

FLT.Key =0;
FLT.NumRanges = 1;
FLT.Ranges[0].Length = 4096;
FLT.Ranges[0].Offset = 4096;

lengthI = sizeof(FILE_LEVEL_TRIM) + sizeof(FILE_LEVEL_TRIM_RANGE);
lengtho = sizeof(FILE_LEVEL_TRIM_OUTPUT);
iRet = DeviceIoControl(hUDisk,
FSCTL_FILE_LEVEL_TRIM,
&FLT,
lengthI,
&FLTO,
lengtho,
&bytesReturn,
NULL);

However, I get the other error “(0x32)ERROR_NOT_SUPPORTED”.
What’s the problem?

Thanks.

xxxxx@gmail.com wrote:

Why my trim command failed?

error code is 0x57
means ERROR_INVALID_PARAMETER

On msdn website, they say it could be caused by “The file to trim is either compressed or encrypted, the input or output buffer length is invalid, or, no trim ranges are specified.”

MSDN also says “to perform this operation, call FltFsControlFile or
ZwFsControlFile with the following parameters.” You are calling
DeviceIoControl.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

http://msdn.microsoft.com/en-us/library/windows/desktop/hh447306(v=vs.85).aspx

Gives the user mode access using DeviceIoControl. However, as has been
explained to the OP over on the windows forum, this command will only
work on disks that support it (SSD) using port drivers that know what
to do with it (AHCI).

Mark Roddy

On Mon, May 14, 2012 at 1:53 PM, Tim Roberts wrote:
> xxxxx@gmail.com wrote:
>> Why my trim command failed?
>> …
>> error code is 0x57
>> means ERROR_INVALID_PARAMETER
>>
>> On msdn website, they say it could be caused by “The file to trim is either compressed or encrypted, the input or output buffer length is invalid, or, no trim ranges are specified.”
>
> MSDN also says “to perform this operation, call FltFsControlFile or
> ZwFsControlFile with the following parameters.” ?You are calling
> DeviceIoControl.
>
> –
> Tim Roberts, xxxxx@probo.com
> Providenza & Boekelheide, Inc.
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Apologize for jumping in.

Is an ACHI driver really needed in order to pass the trim command?
The MSDN link does not say this.

Thanks,
– pa

On 14-May-2012 21:49, Mark Roddy wrote:

http://msdn.microsoft.com/en-us/library/windows/desktop/hh447306(v=vs.85).aspx

Gives the user mode access using DeviceIoControl. However, as has been
explained to the OP over on the windows forum, this command will only
work on disks that support it (SSD) using port drivers that know what
to do with it (AHCI).

Mark Roddy

It is in Win7. I have no idea about win8. Neither scsiport nor
storport know/knew what to do with it.

Mark Roddy

On Mon, May 14, 2012 at 3:06 PM, Pavel A wrote:
> Apologize for jumping in.
>
> Is an ACHI driver really needed in order to pass the trim command?
> The MSDN link does not say this.
>
> Thanks,
> – pa
>
>
> On 14-May-2012 21:49, Mark Roddy wrote:
>>
>>
>> http://msdn.microsoft.com/en-us/library/windows/desktop/hh447306(v=vs.85).aspx
>>
>> Gives the user mode access using DeviceIoControl. However, as has been
>> explained to the OP over on the windows forum, this command will only
>> work on disks that support it (SSD) using port drivers that know what
>> to do with it (AHCI).
>>
>>
>> Mark Roddy
>>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer