Hi Amritanshu,
I explored further on the FSCTL_REQUEST_FILTER_OPLOCK/FILE_RESERVE_OPFILTER suggestion given by you and I don’t really see how it is going to help address my use case?
I wrote a sample application below (at end of this post) in user space that uses CreateFile/DeviceIoControl sequence to acquire a filter oplock on the file. It works exactly as expected. I run this application with a particular file and then if I try to modify the same file from other application say notepad.exe, the save action blocks. My WaitForSingleObject unblocks and it waits to press a key (getchar). After pressing a key my application “Gets out of the way” by closing the handle and then notepad.exe unblocks and proceeds with the save operation.
NOTE: My application succeeds to acquire the filter op-lock only if there is no handle open to the file. If I run filetest.exe and keep the file open in any sharing mode/desired access, my application cannot get the filter oplock and fails with ERROR_OPLOCK_NOT_GRANTED error.
This probably explains the behavior that I am getting in my minifilter too. When, I try to re-open the file PostOpCreate with FSCTL_REQUEST_FILTER_OPLOCK/FILE_RESERVE_OPFILTER, if fails with STATUS_OPLOCK_NOT_GRANTED. This is same as my user space application getting ERROR_OPLOCK_NOT_GRANTED.
This makes me go back to my question that: Should I reopen the file at all? I have asked this question at http://www.osronline.com/showthread.cfm?link=247683. But, haven’t really heard from anybody yet.
main(int argc, char **argv)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
DWORD bytesReturned;
HANDLE hEvent = NULL;
OVERLAPPED ov = {0};
DWORD lastError;
if (argc != 2) {
printf(“Usage: %s filename\n”, argv[0]);
goto exit;
}
hFile = CreateFile(argv[1],
FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (INVALID_HANDLE_VALUE == hFile) {
printf(“Unable to open file, lasterr = %d\n”, lastError = GetLastError());
goto exit;
}
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (NULL == hEvent) {
printf(“Unable to create event object, lasterr = %d\n”, lastError = GetLastError());
goto exit;
}
ov.hEvent = hEvent;
if (FALSE == DeviceIoControl(hFile,
FSCTL_REQUEST_FILTER_OPLOCK,
NULL,
0,
NULL,
0,
&bytesReturned,
&ov)) {
lastError = GetLastError();
if (ERROR_IO_PENDING != lastError) {
printf(“DeviceIoControl failed, lasterr = %d\n”, lastError);
goto exit;
}
}
printf(“Waiting for event to get signaled”);
WaitForSingleObject(hEvent, INFINITE);
printf(“Event signaled”);
getchar();
exit:
if (INVALID_HANDLE_VALUE != hFile) {
CloseHandle(hFile);
}
if (NULL != hEvent) {
CloseHandle(hEvent);
}
return 0;
}
Thanks.
-Prasad