FltWriteFile with byte range locking

An app locks a byte range with key 1 and then writes to the file. In
Pre-Write, I try to write ourselves to that file using FltWriteFile with cached-write.

But as FltWriteFile does not take key of byte range lock, our FltWrite fails
with STATUS_FILE_LOCK_CONFLICT.

So I created callback data, set params appropriately
(set Iopb->Parameters.Write.Key = 1) and called it. But it also fails
with same error.

Debugging it, the callback data does the key is 1.But,the key in IRP is 0.So is that a bug with FltMgr?the FltMgr don’t copy the key to IRP?
There is FltWriteFileEx which takes key but available starting from windows 8.

the following is my implention for FltWriteFile with the Key:
NTSTATUS
MyWriteFile(
IN PFLT_INSTANCE Instance,
IN PFILE_OBJECT FileObject,
IN ULONG Key,
IN LARGE_INTEGER ByteOffset,
IN ULONG Length,
OUT PVOID Buffer,
IN FLT_IO_OPERATION_FLAGS Flags,
OUT PULONG BytesWritten
)
{
PFLT_CALLBACK_DATA data;
NTSTATUS status;

status = FltAllocateCallbackData( Instance, FileObject, &data );
if (!NT_SUCCESS( status )) return status;
data->Iopb->MajorFunction = IRP_MJ_WRITE;
data->Iopb->MinorFunction = 0;
data->Iopb->Parameters.Write.Length = Length;
data->Iopb->Parameters.Write.Key = Key;
data->Iopb->Parameters.Write.ByteOffset = ByteOffset;
data->Iopb->Parameters.Write.WriteBuffer = Buffer;
data->Iopb->Parameters.Write.MdlAddress = NULL;
data->Iopb->IrpFlags = Flags|IRP_SYNCHRONOUS_API;
FltPerformSynchronousIo( data );
status = data->IoStatus.Status;
*BytesWritten= data->IoStatus.Information;
FltFreeCallbackData( data );
return status;
}

> IN FLT_IO_OPERATION_FLAGS Flags,

data->Iopb->IrpFlags = Flags|IRP_SYNCHRONOUS_API;

Without being able to provide input to you question I’ll observe that that
isn’t write.

FLT_IO_OPERATION_FLAGS != Irp->Flags : you will need to map them
yourself.

oh,my fault.
So,what’s the correct IRP flags for a synchronous cached write?

in filespy,I can observed the IrpFlags is
IRP_WRITE_OPERATION|IRP_DEFER_IO_COMPLETION.
But in WDK,no IRP_WRITE_OPERATION and IRP_DEFER_IO_COMPLETION for FLT_IO_PARAMETER_BLOCK.IrpFlags.
In WDK,there are the following:
IRP_BUFFERED_IO The operation is a buffered I/O operation.
IRP_CLOSE_OPERATION The operation is a cleanup or close operation.
IRP_DEALLOCATE_BUFFER The I/O Manager will free the buffer during the completion phase for the IRP.
IRP_INPUT_OPERATION The operation is an input operation.
IRP_NOCACHE The operation is a noncached I/O operation.
IRP_PAGING_IO The operation is a paging I/O operation.
IRP_SYNCHRONOUS_API The I/O operation is synchronous.
IRP_SYNCHRONOUS_PAGING_IO The operation is a synchronous paging I/O operation.

I think I should use:IRP_WRITE_OPERATION|IRP_BUFFERED_IO|IRP_SYNCHRONOUS_API ??

I use:
data->Iopb->IrpFlags = IRP_BUFFERED_IO|IRP_WRITE_OPERATION|IRP_SYNCHRONOUS_API;

the return is still STATUS_FILE_LOCK_CONFLICT.

Only Paging I/O ignores byte range data locks.

Tony
OSR