2009-10-12 03:35:35 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: ntoskrnl/fsrtl/filelock.c
|
|
|
|
* PURPOSE: File Locking implementation for File System Drivers
|
|
|
|
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
|
|
|
#include <ntoskrnl.h>
|
2012-03-10 07:28:03 +00:00
|
|
|
#define NDEBUG
|
2009-10-12 03:35:35 +00:00
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/* GLOBALS *******************************************************************/
|
|
|
|
|
|
|
|
PAGED_LOOKASIDE_LIST FsRtlFileLockLookasideList;
|
|
|
|
|
2012-02-29 09:18:57 +00:00
|
|
|
/* Note: this aligns the two types of lock entry structs so we can access the
|
|
|
|
FILE_LOCK_INFO part in common. Add elements after Shared if new stuff is needed.
|
|
|
|
*/
|
|
|
|
typedef union _COMBINED_LOCK_ELEMENT
|
|
|
|
{
|
2012-07-23 18:53:50 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
LIST_ENTRY dummy;
|
|
|
|
FILE_SHARED_LOCK_ENTRY Shared;
|
|
|
|
};
|
|
|
|
FILE_EXCLUSIVE_LOCK_ENTRY Exclusive;
|
2012-02-29 09:18:57 +00:00
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
COMBINED_LOCK_ELEMENT, *PCOMBINED_LOCK_ELEMENT;
|
2012-02-29 09:18:57 +00:00
|
|
|
|
|
|
|
typedef struct _LOCK_INFORMATION
|
|
|
|
{
|
2012-03-10 07:28:03 +00:00
|
|
|
RTL_GENERIC_TABLE RangeTable;
|
|
|
|
IO_CSQ Csq;
|
|
|
|
KSPIN_LOCK CsqLock;
|
2012-03-05 09:49:06 +00:00
|
|
|
LIST_ENTRY CsqList;
|
2012-03-10 07:28:03 +00:00
|
|
|
PFILE_LOCK BelongsTo;
|
|
|
|
LIST_ENTRY SharedLocks;
|
2012-07-23 18:53:50 +00:00
|
|
|
ULONG Generation;
|
2012-02-29 09:18:57 +00:00
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
LOCK_INFORMATION, *PLOCK_INFORMATION;
|
|
|
|
|
|
|
|
typedef struct _LOCK_SHARED_RANGE
|
|
|
|
{
|
|
|
|
LIST_ENTRY Entry;
|
|
|
|
LARGE_INTEGER Start, End;
|
|
|
|
ULONG Key;
|
|
|
|
PVOID ProcessId;
|
|
|
|
}
|
|
|
|
LOCK_SHARED_RANGE, *PLOCK_SHARED_RANGE;
|
2012-02-29 09:18:57 +00:00
|
|
|
|
2012-12-09 15:11:09 +00:00
|
|
|
#define TAG_TABLE 'LTAB'
|
|
|
|
#define TAG_RANGE 'FSRA'
|
|
|
|
#define TAG_FLOCK 'FLCK'
|
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* PRIVATE FUNCTIONS *********************************************************/
|
|
|
|
|
2012-02-29 09:18:57 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlCompleteLockIrpReal(IN PCOMPLETE_LOCK_IRP_ROUTINE CompleteRoutine,
|
|
|
|
IN PVOID Context,
|
|
|
|
IN PIRP Irp,
|
|
|
|
IN NTSTATUS Status,
|
|
|
|
OUT PNTSTATUS NewStatus,
|
|
|
|
IN PFILE_OBJECT FileObject OPTIONAL);
|
|
|
|
|
|
|
|
/* Generic table methods */
|
|
|
|
|
|
|
|
static PVOID NTAPI LockAllocate(PRTL_GENERIC_TABLE Table, CLONG Bytes)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
PVOID Result;
|
2012-12-09 15:11:09 +00:00
|
|
|
Result = ExAllocatePoolWithTag(NonPagedPool, Bytes, TAG_TABLE);
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT("LockAllocate(%lu) => %p\n", Bytes, Result);
|
2012-07-23 18:50:16 +00:00
|
|
|
return Result;
|
2012-02-29 09:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static VOID NTAPI LockFree(PRTL_GENERIC_TABLE Table, PVOID Buffer)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
DPRINT("LockFree(%p)\n", Buffer);
|
2012-12-09 15:11:09 +00:00
|
|
|
ExFreePoolWithTag(Buffer, TAG_TABLE);
|
2012-02-29 09:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static RTL_GENERIC_COMPARE_RESULTS NTAPI LockCompare
|
|
|
|
(PRTL_GENERIC_TABLE Table, PVOID PtrA, PVOID PtrB)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
PCOMBINED_LOCK_ELEMENT A = PtrA, B = PtrB;
|
|
|
|
RTL_GENERIC_COMPARE_RESULTS Result;
|
2012-03-10 07:28:03 +00:00
|
|
|
#if 0
|
|
|
|
DPRINT("Starting to compare element %x to element %x\n", PtrA, PtrB);
|
|
|
|
#endif
|
|
|
|
/* Match if we overlap */
|
|
|
|
if (((A->Exclusive.FileLock.StartingByte.QuadPart <
|
|
|
|
B->Exclusive.FileLock.EndingByte.QuadPart) &&
|
|
|
|
(A->Exclusive.FileLock.StartingByte.QuadPart >=
|
|
|
|
B->Exclusive.FileLock.StartingByte.QuadPart)) ||
|
|
|
|
((B->Exclusive.FileLock.StartingByte.QuadPart <
|
|
|
|
A->Exclusive.FileLock.EndingByte.QuadPart) &&
|
|
|
|
(B->Exclusive.FileLock.StartingByte.QuadPart >=
|
|
|
|
A->Exclusive.FileLock.StartingByte.QuadPart)))
|
|
|
|
return GenericEqual;
|
|
|
|
/* Otherwise, key on the starting byte */
|
2012-07-23 18:50:16 +00:00
|
|
|
Result =
|
|
|
|
(A->Exclusive.FileLock.StartingByte.QuadPart <
|
|
|
|
B->Exclusive.FileLock.StartingByte.QuadPart) ? GenericLessThan :
|
|
|
|
(A->Exclusive.FileLock.StartingByte.QuadPart >
|
|
|
|
B->Exclusive.FileLock.StartingByte.QuadPart) ? GenericGreaterThan :
|
|
|
|
GenericEqual;
|
2012-03-10 07:28:03 +00:00
|
|
|
#if 0
|
2012-07-23 18:50:16 +00:00
|
|
|
DPRINT("Compare(%x:%x) %x-%x to %x-%x => %d\n",
|
|
|
|
A,B,
|
|
|
|
A->Exclusive.FileLock.StartingByte.LowPart,
|
|
|
|
A->Exclusive.FileLock.EndingByte.LowPart,
|
|
|
|
B->Exclusive.FileLock.StartingByte.LowPart,
|
|
|
|
B->Exclusive.FileLock.EndingByte.LowPart,
|
|
|
|
Result);
|
2012-03-10 07:28:03 +00:00
|
|
|
#endif
|
2012-07-23 18:50:16 +00:00
|
|
|
return Result;
|
2012-02-29 09:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CSQ methods */
|
|
|
|
|
|
|
|
static NTSTATUS NTAPI LockInsertIrpEx
|
|
|
|
(PIO_CSQ Csq,
|
|
|
|
PIRP Irp,
|
|
|
|
PVOID InsertContext)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
PLOCK_INFORMATION LockInfo = CONTAINING_RECORD(Csq, LOCK_INFORMATION, Csq);
|
|
|
|
InsertTailList(&LockInfo->CsqList, &Irp->Tail.Overlay.ListEntry);
|
|
|
|
return STATUS_SUCCESS;
|
2012-02-29 09:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static VOID NTAPI LockRemoveIrp(PIO_CSQ Csq, PIRP Irp)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
RemoveEntryList(&Irp->Tail.Overlay.ListEntry);
|
2012-02-29 09:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PIRP NTAPI LockPeekNextIrp(PIO_CSQ Csq, PIRP Irp, PVOID PeekContext)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
// Context will be a COMBINED_LOCK_ELEMENT. We're looking for a
|
|
|
|
// lock that can be acquired, now that the lock matching PeekContext
|
|
|
|
// has been removed.
|
|
|
|
COMBINED_LOCK_ELEMENT LockElement;
|
2012-03-05 09:49:06 +00:00
|
|
|
PCOMBINED_LOCK_ELEMENT WhereUnlock = PeekContext;
|
2012-07-23 18:50:16 +00:00
|
|
|
PLOCK_INFORMATION LockInfo = CONTAINING_RECORD(Csq, LOCK_INFORMATION, Csq);
|
2012-03-05 09:49:06 +00:00
|
|
|
PLIST_ENTRY Following;
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("PeekNextIrp(IRP %p, Context %p)\n", Irp, PeekContext);
|
2012-03-05 09:49:06 +00:00
|
|
|
if (!Irp)
|
|
|
|
{
|
2012-03-10 07:28:03 +00:00
|
|
|
Following = LockInfo->CsqList.Flink;
|
2012-03-05 09:49:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Following = Irp->Tail.Overlay.ListEntry.Flink;
|
2012-03-10 07:28:03 +00:00
|
|
|
|
|
|
|
DPRINT("ListEntry %p Head %p\n", Following, &LockInfo->CsqList);
|
2012-03-05 09:49:06 +00:00
|
|
|
for (;
|
|
|
|
Following != &LockInfo->CsqList;
|
|
|
|
Following = Following->Flink)
|
|
|
|
{
|
2012-03-10 07:28:03 +00:00
|
|
|
PIO_STACK_LOCATION IoStack;
|
2012-03-05 09:49:06 +00:00
|
|
|
BOOLEAN Matching;
|
2012-03-10 07:28:03 +00:00
|
|
|
Irp = CONTAINING_RECORD(Following, IRP, Tail.Overlay.ListEntry);
|
|
|
|
DPRINT("Irp %p\n", Irp);
|
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
2012-03-05 09:49:06 +00:00
|
|
|
LockElement.Exclusive.FileLock.StartingByte =
|
|
|
|
IoStack->Parameters.LockControl.ByteOffset;
|
|
|
|
LockElement.Exclusive.FileLock.EndingByte.QuadPart =
|
|
|
|
LockElement.Exclusive.FileLock.StartingByte.QuadPart +
|
|
|
|
IoStack->Parameters.LockControl.Length->QuadPart;
|
|
|
|
/* If a context was specified, it's a range to check to unlock */
|
|
|
|
if (WhereUnlock)
|
|
|
|
{
|
|
|
|
Matching = LockCompare
|
|
|
|
(&LockInfo->RangeTable, &LockElement, WhereUnlock) != GenericEqual;
|
|
|
|
}
|
|
|
|
/* Else get any completable IRP */
|
|
|
|
else
|
|
|
|
{
|
2012-03-10 07:28:03 +00:00
|
|
|
Matching = FALSE;
|
2012-03-05 09:49:06 +00:00
|
|
|
}
|
|
|
|
if (!Matching)
|
|
|
|
{
|
|
|
|
// This IRP is fine...
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("Returning the IRP %p\n", Irp);
|
2012-03-05 09:49:06 +00:00
|
|
|
return Irp;
|
|
|
|
}
|
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("Return NULL\n");
|
2012-03-05 09:49:06 +00:00
|
|
|
return NULL;
|
2012-02-29 09:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static VOID NTAPI
|
|
|
|
LockAcquireQueueLock(PIO_CSQ Csq, PKIRQL Irql)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
PLOCK_INFORMATION LockInfo = CONTAINING_RECORD(Csq, LOCK_INFORMATION, Csq);
|
|
|
|
KeAcquireSpinLock(&LockInfo->CsqLock, Irql);
|
2012-02-29 09:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static VOID NTAPI
|
|
|
|
LockReleaseQueueLock(PIO_CSQ Csq, KIRQL Irql)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
PLOCK_INFORMATION LockInfo = CONTAINING_RECORD(Csq, LOCK_INFORMATION, Csq);
|
|
|
|
KeReleaseSpinLock(&LockInfo->CsqLock, Irql);
|
2012-02-29 09:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static VOID NTAPI
|
|
|
|
LockCompleteCanceledIrp(PIO_CSQ Csq, PIRP Irp)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
PLOCK_INFORMATION LockInfo = CONTAINING_RECORD(Csq, LOCK_INFORMATION, Csq);
|
|
|
|
DPRINT("Complete cancelled IRP %p Status %x\n", Irp, STATUS_CANCELLED);
|
|
|
|
FsRtlCompleteLockIrpReal
|
|
|
|
(LockInfo->BelongsTo->CompleteLockIrpRoutine,
|
|
|
|
NULL,
|
|
|
|
Irp,
|
|
|
|
STATUS_CANCELLED,
|
|
|
|
&Status,
|
|
|
|
NULL);
|
2012-02-29 09:18:57 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlCompleteLockIrpReal(IN PCOMPLETE_LOCK_IRP_ROUTINE CompleteRoutine,
|
|
|
|
IN PVOID Context,
|
|
|
|
IN PIRP Irp,
|
|
|
|
IN NTSTATUS Status,
|
|
|
|
OUT PNTSTATUS NewStatus,
|
|
|
|
IN PFILE_OBJECT FileObject OPTIONAL)
|
|
|
|
{
|
|
|
|
/* Check if we have a complete routine */
|
2012-07-23 18:53:50 +00:00
|
|
|
Irp->IoStatus.Information = 0;
|
2009-10-12 03:35:35 +00:00
|
|
|
if (CompleteRoutine)
|
|
|
|
{
|
|
|
|
/* Check if we have a file object */
|
|
|
|
if (FileObject) FileObject->LastLock = NULL;
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* Set the I/O Status and do completion */
|
|
|
|
Irp->IoStatus.Status = Status;
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("Calling completion routine %p Status %x\n", Irp, Status);
|
2009-10-12 03:35:35 +00:00
|
|
|
*NewStatus = CompleteRoutine(Context, Irp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise do a normal I/O complete request */
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("Completing IRP %p Status %x\n", Irp, Status);
|
2009-10-12 03:35:35 +00:00
|
|
|
FsRtlCompleteRequest(Irp, Status);
|
|
|
|
*NewStatus = Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PUBLIC FUNCTIONS **********************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
PFILE_LOCK_INFO
|
|
|
|
NTAPI
|
|
|
|
FsRtlGetNextFileLock(IN PFILE_LOCK FileLock,
|
|
|
|
IN BOOLEAN Restart)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
PCOMBINED_LOCK_ELEMENT Entry;
|
|
|
|
if (!FileLock->LockInformation) return NULL;
|
|
|
|
Entry = RtlEnumerateGenericTable(FileLock->LockInformation, Restart);
|
|
|
|
if (!Entry) return NULL;
|
|
|
|
else return &Entry->Exclusive.FileLock;
|
2009-10-12 03:35:35 +00:00
|
|
|
}
|
|
|
|
|
2012-07-23 18:53:50 +00:00
|
|
|
VOID
|
2012-03-10 07:28:03 +00:00
|
|
|
NTAPI
|
2012-07-23 18:53:50 +00:00
|
|
|
FsRtlpExpandLockElement
|
|
|
|
(PCOMBINED_LOCK_ELEMENT ToExpand,
|
2012-03-10 07:28:03 +00:00
|
|
|
PCOMBINED_LOCK_ELEMENT Conflict)
|
|
|
|
{
|
2012-07-23 18:53:50 +00:00
|
|
|
if (ToExpand->Exclusive.FileLock.StartingByte.QuadPart >
|
2012-03-10 07:28:03 +00:00
|
|
|
Conflict->Exclusive.FileLock.StartingByte.QuadPart)
|
|
|
|
{
|
2012-07-23 18:53:50 +00:00
|
|
|
ToExpand->Exclusive.FileLock.StartingByte =
|
2012-03-10 07:28:03 +00:00
|
|
|
Conflict->Exclusive.FileLock.StartingByte;
|
|
|
|
}
|
2012-07-23 18:53:50 +00:00
|
|
|
if (ToExpand->Exclusive.FileLock.EndingByte.QuadPart <
|
2012-03-10 07:28:03 +00:00
|
|
|
Conflict->Exclusive.FileLock.EndingByte.QuadPart)
|
|
|
|
{
|
2012-07-23 18:53:50 +00:00
|
|
|
ToExpand->Exclusive.FileLock.EndingByte =
|
2012-03-10 07:28:03 +00:00
|
|
|
Conflict->Exclusive.FileLock.EndingByte;
|
|
|
|
}
|
2012-07-23 18:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function expands the conflicting range Conflict by removing and reinserting it,
|
|
|
|
then adds a shared range of the same size */
|
|
|
|
PCOMBINED_LOCK_ELEMENT
|
|
|
|
NTAPI
|
|
|
|
FsRtlpRebuildSharedLockRange
|
|
|
|
(PFILE_LOCK FileLock,
|
|
|
|
PLOCK_INFORMATION LockInfo,
|
|
|
|
PCOMBINED_LOCK_ELEMENT Conflict)
|
|
|
|
{
|
|
|
|
/* Starting at Conflict->StartingByte and going to Conflict->EndingByte
|
|
|
|
* capture and expand a shared range from the shared range list.
|
|
|
|
* Finish when we've incorporated all overlapping shared regions.
|
|
|
|
*/
|
|
|
|
BOOLEAN InsertedNew = FALSE, RemovedOld;
|
|
|
|
COMBINED_LOCK_ELEMENT NewElement = *Conflict;
|
|
|
|
PCOMBINED_LOCK_ELEMENT Entry;
|
|
|
|
while ((Entry = RtlLookupElementGenericTable
|
|
|
|
(FileLock->LockInformation, &NewElement)))
|
|
|
|
{
|
|
|
|
FsRtlpExpandLockElement(&NewElement, Entry);
|
|
|
|
RemovedOld = RtlDeleteElementGenericTable
|
|
|
|
(&LockInfo->RangeTable,
|
|
|
|
Entry);
|
|
|
|
ASSERT(RemovedOld);
|
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
Conflict = RtlInsertElementGenericTable
|
|
|
|
(&LockInfo->RangeTable,
|
2012-07-23 18:53:50 +00:00
|
|
|
&NewElement,
|
|
|
|
sizeof(NewElement),
|
2012-03-10 07:28:03 +00:00
|
|
|
&InsertedNew);
|
2012-07-23 18:53:50 +00:00
|
|
|
ASSERT(InsertedNew);
|
2012-03-10 07:28:03 +00:00
|
|
|
return Conflict;
|
|
|
|
}
|
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlPrivateLock(IN PFILE_LOCK FileLock,
|
|
|
|
IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN PLARGE_INTEGER Length,
|
|
|
|
IN PEPROCESS Process,
|
|
|
|
IN ULONG Key,
|
|
|
|
IN BOOLEAN FailImmediately,
|
|
|
|
IN BOOLEAN ExclusiveLock,
|
|
|
|
OUT PIO_STATUS_BLOCK IoStatus,
|
|
|
|
IN PIRP Irp OPTIONAL,
|
|
|
|
IN PVOID Context OPTIONAL,
|
|
|
|
IN BOOLEAN AlreadySynchronized)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
2012-07-23 18:50:16 +00:00
|
|
|
COMBINED_LOCK_ELEMENT ToInsert;
|
|
|
|
PCOMBINED_LOCK_ELEMENT Conflict;
|
|
|
|
PLOCK_INFORMATION LockInfo;
|
2012-07-23 18:53:50 +00:00
|
|
|
PLOCK_SHARED_RANGE NewSharedRange;
|
2012-07-23 18:50:16 +00:00
|
|
|
BOOLEAN InsertedNew;
|
2012-07-23 18:53:50 +00:00
|
|
|
ULARGE_INTEGER UnsignedStart;
|
|
|
|
ULARGE_INTEGER UnsignedEnd;
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT("FsRtlPrivateLock(%wZ, Offset %08x%08x (%d), Length %08x%08x (%d), Key %x, FailImmediately %u, Exclusive %u)\n",
|
2012-03-10 07:28:03 +00:00
|
|
|
&FileObject->FileName,
|
|
|
|
FileOffset->HighPart,
|
|
|
|
FileOffset->LowPart,
|
2012-07-23 18:53:50 +00:00
|
|
|
(int)FileOffset->QuadPart,
|
2012-03-10 07:28:03 +00:00
|
|
|
Length->HighPart,
|
|
|
|
Length->LowPart,
|
2012-07-23 18:53:50 +00:00
|
|
|
(int)Length->QuadPart,
|
2012-03-10 07:28:03 +00:00
|
|
|
Key,
|
|
|
|
FailImmediately,
|
|
|
|
ExclusiveLock);
|
|
|
|
|
2012-07-23 18:53:50 +00:00
|
|
|
UnsignedStart.QuadPart = FileOffset->QuadPart;
|
|
|
|
UnsignedEnd.QuadPart = FileOffset->QuadPart + Length->QuadPart;
|
|
|
|
|
|
|
|
if (UnsignedEnd.QuadPart < UnsignedStart.QuadPart)
|
2012-03-10 07:28:03 +00:00
|
|
|
{
|
|
|
|
DPRINT("File offset out of range\n");
|
|
|
|
IoStatus->Status = STATUS_INVALID_PARAMETER;
|
|
|
|
if (Irp)
|
|
|
|
{
|
|
|
|
DPRINT("Complete lock %p Status %x\n", Irp, IoStatus->Status);
|
|
|
|
FsRtlCompleteLockIrpReal
|
|
|
|
(FileLock->CompleteLockIrpRoutine,
|
|
|
|
Context,
|
|
|
|
Irp,
|
|
|
|
IoStatus->Status,
|
|
|
|
&Status,
|
|
|
|
FileObject);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* Initialize the lock, if necessary */
|
|
|
|
if (!FileLock->LockInformation)
|
|
|
|
{
|
2012-12-09 15:11:09 +00:00
|
|
|
LockInfo = ExAllocatePoolWithTag(NonPagedPool, sizeof(LOCK_INFORMATION), TAG_FLOCK);
|
[NTOSKRNL]
Coverity code defects fixes :
- Cache: CID 701441
- Config: CIDs 716570, 716669, 716760
- Dbgk: Kdbg: CIDs 716571, 515128/9, 500432
- Ex: CIDs 500156/7, 515122, 716200/67, 701301, 514669
- Fsrtl: Fstub: CIDs 701341/2, 701288, 716770, 701302, and CIDs 716576/7/8 + 514636 + 716805 thanks to Thomas Faber
- Io: CIDs 514576, 514643, 514672/3, 716203, 716269, 716581, 716591, 716713
- Ke: CIDs 515125, 716592
- Ps: CIDs 716603/4, 701422
- Ob: Po: CIDs 514671/680, 701419/420/421, 716763, 716601/2
All the details are given in the different bug reports.
CORE-6677 CORE-6679 CORE-6680 CORE-6683 CORE-6686 CORE-6692 CORE-6693 CORE-6694 CORE-6695 CORE-6696 #comment Committed in rev.57400 #resolve #close
svn path=/trunk/; revision=57400
2012-09-27 17:16:31 +00:00
|
|
|
if (!LockInfo)
|
|
|
|
{
|
2012-03-10 07:28:03 +00:00
|
|
|
IoStatus->Status = STATUS_NO_MEMORY;
|
2012-07-23 18:50:16 +00:00
|
|
|
return FALSE;
|
2012-03-10 07:28:03 +00:00
|
|
|
}
|
[NTOSKRNL]
Coverity code defects fixes :
- Cache: CID 701441
- Config: CIDs 716570, 716669, 716760
- Dbgk: Kdbg: CIDs 716571, 515128/9, 500432
- Ex: CIDs 500156/7, 515122, 716200/67, 701301, 514669
- Fsrtl: Fstub: CIDs 701341/2, 701288, 716770, 701302, and CIDs 716576/7/8 + 514636 + 716805 thanks to Thomas Faber
- Io: CIDs 514576, 514643, 514672/3, 716203, 716269, 716581, 716591, 716713
- Ke: CIDs 515125, 716592
- Ps: CIDs 716603/4, 701422
- Ob: Po: CIDs 514671/680, 701419/420/421, 716763, 716601/2
All the details are given in the different bug reports.
CORE-6677 CORE-6679 CORE-6680 CORE-6683 CORE-6686 CORE-6692 CORE-6693 CORE-6694 CORE-6695 CORE-6696 #comment Committed in rev.57400 #resolve #close
svn path=/trunk/; revision=57400
2012-09-27 17:16:31 +00:00
|
|
|
FileLock->LockInformation = LockInfo;
|
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
LockInfo->BelongsTo = FileLock;
|
2012-03-10 07:28:03 +00:00
|
|
|
InitializeListHead(&LockInfo->SharedLocks);
|
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
RtlInitializeGenericTable
|
|
|
|
(&LockInfo->RangeTable,
|
|
|
|
LockCompare,
|
|
|
|
LockAllocate,
|
|
|
|
LockFree,
|
|
|
|
NULL);
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
KeInitializeSpinLock(&LockInfo->CsqLock);
|
2012-03-05 09:49:06 +00:00
|
|
|
InitializeListHead(&LockInfo->CsqList);
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
IoCsqInitializeEx
|
|
|
|
(&LockInfo->Csq,
|
|
|
|
LockInsertIrpEx,
|
|
|
|
LockRemoveIrp,
|
|
|
|
LockPeekNextIrp,
|
|
|
|
LockAcquireQueueLock,
|
|
|
|
LockReleaseQueueLock,
|
|
|
|
LockCompleteCanceledIrp);
|
2009-10-12 03:35:35 +00:00
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
LockInfo = FileLock->LockInformation;
|
|
|
|
ToInsert.Exclusive.FileLock.FileObject = FileObject;
|
|
|
|
ToInsert.Exclusive.FileLock.StartingByte = *FileOffset;
|
|
|
|
ToInsert.Exclusive.FileLock.EndingByte.QuadPart = FileOffset->QuadPart + Length->QuadPart;
|
|
|
|
ToInsert.Exclusive.FileLock.ProcessId = Process->UniqueProcessId;
|
|
|
|
ToInsert.Exclusive.FileLock.Key = Key;
|
|
|
|
ToInsert.Exclusive.FileLock.ExclusiveLock = ExclusiveLock;
|
[NTOSKRNL]
Coverity code defects fixes :
- Cache: CID 701441
- Config: CIDs 716570, 716669, 716760
- Dbgk: Kdbg: CIDs 716571, 515128/9, 500432
- Ex: CIDs 500156/7, 515122, 716200/67, 701301, 514669
- Fsrtl: Fstub: CIDs 701341/2, 701288, 716770, 701302, and CIDs 716576/7/8 + 514636 + 716805 thanks to Thomas Faber
- Io: CIDs 514576, 514643, 514672/3, 716203, 716269, 716581, 716591, 716713
- Ke: CIDs 515125, 716592
- Ps: CIDs 716603/4, 701422
- Ob: Po: CIDs 514671/680, 701419/420/421, 716763, 716601/2
All the details are given in the different bug reports.
CORE-6677 CORE-6679 CORE-6680 CORE-6683 CORE-6686 CORE-6692 CORE-6693 CORE-6694 CORE-6695 CORE-6696 #comment Committed in rev.57400 #resolve #close
svn path=/trunk/; revision=57400
2012-09-27 17:16:31 +00:00
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
Conflict = RtlInsertElementGenericTable
|
|
|
|
(FileLock->LockInformation,
|
|
|
|
&ToInsert,
|
|
|
|
sizeof(ToInsert),
|
|
|
|
&InsertedNew);
|
[NTOSKRNL]
Coverity code defects fixes :
- Cache: CID 701441
- Config: CIDs 716570, 716669, 716760
- Dbgk: Kdbg: CIDs 716571, 515128/9, 500432
- Ex: CIDs 500156/7, 515122, 716200/67, 701301, 514669
- Fsrtl: Fstub: CIDs 701341/2, 701288, 716770, 701302, and CIDs 716576/7/8 + 514636 + 716805 thanks to Thomas Faber
- Io: CIDs 514576, 514643, 514672/3, 716203, 716269, 716581, 716591, 716713
- Ke: CIDs 515125, 716592
- Ps: CIDs 716603/4, 701422
- Ob: Po: CIDs 514671/680, 701419/420/421, 716763, 716601/2
All the details are given in the different bug reports.
CORE-6677 CORE-6679 CORE-6680 CORE-6683 CORE-6686 CORE-6692 CORE-6693 CORE-6694 CORE-6695 CORE-6696 #comment Committed in rev.57400 #resolve #close
svn path=/trunk/; revision=57400
2012-09-27 17:16:31 +00:00
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
if (Conflict && !InsertedNew)
|
|
|
|
{
|
|
|
|
if (Conflict->Exclusive.FileLock.ExclusiveLock || ExclusiveLock)
|
|
|
|
{
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT("Conflict %08x%08x:%08x%08x Exc %u (Want Exc %u)\n",
|
2012-07-23 18:53:50 +00:00
|
|
|
Conflict->Exclusive.FileLock.StartingByte.HighPart,
|
|
|
|
Conflict->Exclusive.FileLock.StartingByte.LowPart,
|
|
|
|
Conflict->Exclusive.FileLock.EndingByte.HighPart,
|
|
|
|
Conflict->Exclusive.FileLock.EndingByte.LowPart,
|
|
|
|
Conflict->Exclusive.FileLock.ExclusiveLock,
|
|
|
|
ExclusiveLock);
|
2012-07-23 18:50:16 +00:00
|
|
|
if (FailImmediately)
|
|
|
|
{
|
2012-07-23 18:53:50 +00:00
|
|
|
DPRINT("STATUS_FILE_LOCK_CONFLICT\n");
|
2012-07-23 18:50:16 +00:00
|
|
|
IoStatus->Status = STATUS_FILE_LOCK_CONFLICT;
|
|
|
|
if (Irp)
|
|
|
|
{
|
2012-07-23 18:53:50 +00:00
|
|
|
DPRINT("STATUS_FILE_LOCK_CONFLICT: Complete\n");
|
2012-07-23 18:50:16 +00:00
|
|
|
FsRtlCompleteLockIrpReal
|
|
|
|
(FileLock->CompleteLockIrpRoutine,
|
|
|
|
Context,
|
|
|
|
Irp,
|
|
|
|
IoStatus->Status,
|
|
|
|
&Status,
|
|
|
|
FileObject);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IoStatus->Status = STATUS_PENDING;
|
|
|
|
if (Irp)
|
|
|
|
{
|
2012-07-23 18:53:50 +00:00
|
|
|
Irp->IoStatus.Information = LockInfo->Generation;
|
2012-07-23 18:50:16 +00:00
|
|
|
IoMarkIrpPending(Irp);
|
|
|
|
IoCsqInsertIrpEx
|
|
|
|
(&LockInfo->Csq,
|
|
|
|
Irp,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-10 07:28:03 +00:00
|
|
|
ULONG i;
|
|
|
|
/* We know of at least one lock in range that's shared. We need to
|
|
|
|
* find out if any more exist and any are exclusive. */
|
|
|
|
for (i = 0; i < RtlNumberGenericTableElements(&LockInfo->RangeTable); i++)
|
|
|
|
{
|
|
|
|
Conflict = RtlGetElementGenericTable(&LockInfo->RangeTable, i);
|
[NTOSKRNL]
Coverity code defects fixes :
- Cache: CID 701441
- Config: CIDs 716570, 716669, 716760
- Dbgk: Kdbg: CIDs 716571, 515128/9, 500432
- Ex: CIDs 500156/7, 515122, 716200/67, 701301, 514669
- Fsrtl: Fstub: CIDs 701341/2, 701288, 716770, 701302, and CIDs 716576/7/8 + 514636 + 716805 thanks to Thomas Faber
- Io: CIDs 514576, 514643, 514672/3, 716203, 716269, 716581, 716591, 716713
- Ke: CIDs 515125, 716592
- Ps: CIDs 716603/4, 701422
- Ob: Po: CIDs 514671/680, 701419/420/421, 716763, 716601/2
All the details are given in the different bug reports.
CORE-6677 CORE-6679 CORE-6680 CORE-6683 CORE-6686 CORE-6692 CORE-6693 CORE-6694 CORE-6695 CORE-6696 #comment Committed in rev.57400 #resolve #close
svn path=/trunk/; revision=57400
2012-09-27 17:16:31 +00:00
|
|
|
|
2012-03-10 07:28:03 +00:00
|
|
|
/* The first argument will be inserted as a shared range */
|
[NTOSKRNL]
Coverity code defects fixes :
- Cache: CID 701441
- Config: CIDs 716570, 716669, 716760
- Dbgk: Kdbg: CIDs 716571, 515128/9, 500432
- Ex: CIDs 500156/7, 515122, 716200/67, 701301, 514669
- Fsrtl: Fstub: CIDs 701341/2, 701288, 716770, 701302, and CIDs 716576/7/8 + 514636 + 716805 thanks to Thomas Faber
- Io: CIDs 514576, 514643, 514672/3, 716203, 716269, 716581, 716591, 716713
- Ke: CIDs 515125, 716592
- Ps: CIDs 716603/4, 701422
- Ob: Po: CIDs 514671/680, 701419/420/421, 716763, 716601/2
All the details are given in the different bug reports.
CORE-6677 CORE-6679 CORE-6680 CORE-6683 CORE-6686 CORE-6692 CORE-6693 CORE-6694 CORE-6695 CORE-6696 #comment Committed in rev.57400 #resolve #close
svn path=/trunk/; revision=57400
2012-09-27 17:16:31 +00:00
|
|
|
if (Conflict && (LockCompare(&LockInfo->RangeTable, Conflict, &ToInsert) == GenericEqual))
|
2012-03-10 07:28:03 +00:00
|
|
|
{
|
|
|
|
if (Conflict->Exclusive.FileLock.ExclusiveLock)
|
|
|
|
{
|
|
|
|
/* Found an exclusive match */
|
|
|
|
if (FailImmediately)
|
|
|
|
{
|
|
|
|
IoStatus->Status = STATUS_FILE_LOCK_CONFLICT;
|
2012-07-23 18:53:50 +00:00
|
|
|
DPRINT("STATUS_FILE_LOCK_CONFLICT\n");
|
2012-03-10 07:28:03 +00:00
|
|
|
if (Irp)
|
|
|
|
{
|
2012-07-23 18:53:50 +00:00
|
|
|
DPRINT("STATUS_FILE_LOCK_CONFLICT: Complete\n");
|
2012-03-10 07:28:03 +00:00
|
|
|
FsRtlCompleteLockIrpReal
|
|
|
|
(FileLock->CompleteLockIrpRoutine,
|
|
|
|
Context,
|
|
|
|
Irp,
|
|
|
|
IoStatus->Status,
|
|
|
|
&Status,
|
|
|
|
FileObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IoStatus->Status = STATUS_PENDING;
|
|
|
|
if (Irp)
|
|
|
|
{
|
|
|
|
IoMarkIrpPending(Irp);
|
|
|
|
IoCsqInsertIrpEx
|
|
|
|
(&LockInfo->Csq,
|
|
|
|
Irp,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-12-04 12:09:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINT("Overlapping shared lock %wZ %08x%08x %08x%08x\n",
|
|
|
|
&FileObject->FileName,
|
|
|
|
Conflict->Exclusive.FileLock.StartingByte.HighPart,
|
|
|
|
Conflict->Exclusive.FileLock.StartingByte.LowPart,
|
|
|
|
Conflict->Exclusive.FileLock.EndingByte.HighPart,
|
|
|
|
Conflict->Exclusive.FileLock.EndingByte.LowPart);
|
|
|
|
Conflict = FsRtlpRebuildSharedLockRange(FileLock,
|
|
|
|
LockInfo,
|
|
|
|
&ToInsert);
|
|
|
|
if (!Conflict)
|
|
|
|
{
|
|
|
|
IoStatus->Status = STATUS_NO_MEMORY;
|
|
|
|
if (Irp)
|
|
|
|
{
|
|
|
|
FsRtlCompleteLockIrpReal
|
|
|
|
(FileLock->CompleteLockIrpRoutine,
|
|
|
|
Context,
|
|
|
|
Irp,
|
|
|
|
IoStatus->Status,
|
|
|
|
&Status,
|
|
|
|
FileObject);
|
2012-03-10 07:28:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We got here because there were only overlapping shared locks */
|
2012-07-23 18:53:50 +00:00
|
|
|
/* A shared lock is both a range *and* a list entry. Insert the
|
|
|
|
entry here. */
|
|
|
|
|
|
|
|
DPRINT("Adding shared lock %wZ\n", &FileObject->FileName);
|
|
|
|
NewSharedRange =
|
2012-12-09 15:11:09 +00:00
|
|
|
ExAllocatePoolWithTag(NonPagedPool, sizeof(*NewSharedRange), TAG_RANGE);
|
2012-07-23 18:53:50 +00:00
|
|
|
if (!NewSharedRange)
|
2012-07-23 18:50:16 +00:00
|
|
|
{
|
2012-03-10 07:28:03 +00:00
|
|
|
IoStatus->Status = STATUS_NO_MEMORY;
|
|
|
|
if (Irp)
|
|
|
|
{
|
|
|
|
FsRtlCompleteLockIrpReal
|
|
|
|
(FileLock->CompleteLockIrpRoutine,
|
|
|
|
Context,
|
|
|
|
Irp,
|
|
|
|
IoStatus->Status,
|
|
|
|
&Status,
|
|
|
|
FileObject);
|
|
|
|
}
|
|
|
|
return FALSE;
|
2012-07-23 18:50:16 +00:00
|
|
|
}
|
2012-07-23 18:53:50 +00:00
|
|
|
DPRINT("Adding shared lock %wZ\n", &FileObject->FileName);
|
2012-12-04 12:09:59 +00:00
|
|
|
NewSharedRange->Start = *FileOffset;
|
|
|
|
NewSharedRange->End.QuadPart = FileOffset->QuadPart + Length->QuadPart;
|
2012-07-23 18:53:50 +00:00
|
|
|
NewSharedRange->Key = Key;
|
|
|
|
NewSharedRange->ProcessId = ToInsert.Exclusive.FileLock.ProcessId;
|
|
|
|
InsertTailList(&LockInfo->SharedLocks, &NewSharedRange->Entry);
|
|
|
|
|
|
|
|
DPRINT("Acquired shared lock %wZ %08x%08x %08x%08x\n",
|
|
|
|
&FileObject->FileName,
|
|
|
|
Conflict->Exclusive.FileLock.StartingByte.HighPart,
|
|
|
|
Conflict->Exclusive.FileLock.StartingByte.LowPart,
|
|
|
|
Conflict->Exclusive.FileLock.EndingByte.HighPart,
|
|
|
|
Conflict->Exclusive.FileLock.EndingByte.LowPart);
|
2012-03-10 07:28:03 +00:00
|
|
|
IoStatus->Status = STATUS_SUCCESS;
|
|
|
|
if (Irp)
|
|
|
|
{
|
|
|
|
FsRtlCompleteLockIrpReal
|
|
|
|
(FileLock->CompleteLockIrpRoutine,
|
|
|
|
Context,
|
|
|
|
Irp,
|
|
|
|
IoStatus->Status,
|
|
|
|
&Status,
|
|
|
|
FileObject);
|
|
|
|
}
|
2012-07-23 18:50:16 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!Conflict)
|
|
|
|
{
|
|
|
|
/* Conflict here is (or would be) the newly inserted element, but we ran
|
|
|
|
* out of space probably. */
|
|
|
|
IoStatus->Status = STATUS_NO_MEMORY;
|
|
|
|
if (Irp)
|
|
|
|
{
|
|
|
|
FsRtlCompleteLockIrpReal
|
|
|
|
(FileLock->CompleteLockIrpRoutine,
|
|
|
|
Context,
|
|
|
|
Irp,
|
|
|
|
IoStatus->Status,
|
|
|
|
&Status,
|
|
|
|
FileObject);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT("Inserted new lock %wZ %08x%08x %08x%08x exclusive %u\n",
|
2012-03-10 07:28:03 +00:00
|
|
|
&FileObject->FileName,
|
|
|
|
Conflict->Exclusive.FileLock.StartingByte.HighPart,
|
|
|
|
Conflict->Exclusive.FileLock.StartingByte.LowPart,
|
2012-06-14 16:19:01 +00:00
|
|
|
Conflict->Exclusive.FileLock.EndingByte.HighPart,
|
|
|
|
Conflict->Exclusive.FileLock.EndingByte.LowPart,
|
2012-03-10 07:28:03 +00:00
|
|
|
Conflict->Exclusive.FileLock.ExclusiveLock);
|
|
|
|
if (!ExclusiveLock)
|
|
|
|
{
|
|
|
|
NewSharedRange =
|
2012-12-09 15:11:09 +00:00
|
|
|
ExAllocatePoolWithTag(NonPagedPool, sizeof(*NewSharedRange), TAG_RANGE);
|
2012-03-10 07:28:03 +00:00
|
|
|
if (!NewSharedRange)
|
|
|
|
{
|
|
|
|
IoStatus->Status = STATUS_NO_MEMORY;
|
|
|
|
if (Irp)
|
|
|
|
{
|
|
|
|
FsRtlCompleteLockIrpReal
|
|
|
|
(FileLock->CompleteLockIrpRoutine,
|
|
|
|
Context,
|
|
|
|
Irp,
|
|
|
|
IoStatus->Status,
|
|
|
|
&Status,
|
|
|
|
FileObject);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
DPRINT("Adding shared lock %wZ\n", &FileObject->FileName);
|
2012-12-04 12:09:59 +00:00
|
|
|
NewSharedRange->Start = *FileOffset;
|
|
|
|
NewSharedRange->End.QuadPart = FileOffset->QuadPart + Length->QuadPart;
|
2012-03-10 07:28:03 +00:00
|
|
|
NewSharedRange->Key = Key;
|
2012-12-04 12:09:59 +00:00
|
|
|
NewSharedRange->ProcessId = Process->UniqueProcessId;
|
2012-03-10 07:28:03 +00:00
|
|
|
InsertTailList(&LockInfo->SharedLocks, &NewSharedRange->Entry);
|
|
|
|
}
|
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
/* Assume all is cool, and lock is set */
|
|
|
|
IoStatus->Status = STATUS_SUCCESS;
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
if (Irp)
|
|
|
|
{
|
|
|
|
/* Complete the request */
|
|
|
|
FsRtlCompleteLockIrpReal(FileLock->CompleteLockIrpRoutine,
|
|
|
|
Context,
|
|
|
|
Irp,
|
|
|
|
IoStatus->Status,
|
|
|
|
&Status,
|
|
|
|
FileObject);
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
/* Update the status */
|
|
|
|
IoStatus->Status = Status;
|
|
|
|
}
|
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlCheckLockForReadAccess(IN PFILE_LOCK FileLock,
|
|
|
|
IN PIRP Irp)
|
|
|
|
{
|
2012-03-10 07:28:03 +00:00
|
|
|
BOOLEAN Result;
|
2012-07-23 18:50:16 +00:00
|
|
|
PIO_STACK_LOCATION IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
COMBINED_LOCK_ELEMENT ToFind;
|
|
|
|
PCOMBINED_LOCK_ELEMENT Found;
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("CheckLockForReadAccess(%wZ, Offset %08x%08x, Length %x)\n",
|
|
|
|
&IoStack->FileObject->FileName,
|
|
|
|
IoStack->Parameters.Read.ByteOffset.HighPart,
|
|
|
|
IoStack->Parameters.Read.ByteOffset.LowPart,
|
|
|
|
IoStack->Parameters.Read.Length);
|
2012-07-23 18:50:16 +00:00
|
|
|
if (!FileLock->LockInformation) {
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("CheckLockForReadAccess(%wZ) => TRUE\n", &IoStack->FileObject->FileName);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-07-23 18:50:16 +00:00
|
|
|
ToFind.Exclusive.FileLock.StartingByte = IoStack->Parameters.Read.ByteOffset;
|
|
|
|
ToFind.Exclusive.FileLock.EndingByte.QuadPart =
|
|
|
|
ToFind.Exclusive.FileLock.StartingByte.QuadPart +
|
|
|
|
IoStack->Parameters.Read.Length;
|
|
|
|
Found = RtlLookupElementGenericTable
|
|
|
|
(FileLock->LockInformation,
|
|
|
|
&ToFind);
|
|
|
|
if (!Found) {
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("CheckLockForReadAccess(%wZ) => TRUE\n", &IoStack->FileObject->FileName);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-07-23 18:50:16 +00:00
|
|
|
Result = !Found->Exclusive.FileLock.ExclusiveLock ||
|
|
|
|
IoStack->Parameters.Read.Key == Found->Exclusive.FileLock.Key;
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("CheckLockForReadAccess(%wZ) => %s\n", &IoStack->FileObject->FileName, Result ? "TRUE" : "FALSE");
|
|
|
|
return Result;
|
2009-10-12 03:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlCheckLockForWriteAccess(IN PFILE_LOCK FileLock,
|
|
|
|
IN PIRP Irp)
|
|
|
|
{
|
2012-03-10 07:28:03 +00:00
|
|
|
BOOLEAN Result;
|
2012-07-23 18:50:16 +00:00
|
|
|
PIO_STACK_LOCATION IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
COMBINED_LOCK_ELEMENT ToFind;
|
|
|
|
PCOMBINED_LOCK_ELEMENT Found;
|
|
|
|
PEPROCESS Process = Irp->Tail.Overlay.Thread->ThreadsProcess;
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("CheckLockForWriteAccess(%wZ, Offset %08x%08x, Length %x)\n",
|
|
|
|
&IoStack->FileObject->FileName,
|
|
|
|
IoStack->Parameters.Write.ByteOffset.HighPart,
|
|
|
|
IoStack->Parameters.Write.ByteOffset.LowPart,
|
|
|
|
IoStack->Parameters.Write.Length);
|
2012-07-23 18:50:16 +00:00
|
|
|
if (!FileLock->LockInformation) {
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("CheckLockForWriteAccess(%wZ) => TRUE\n", &IoStack->FileObject->FileName);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-07-23 18:50:16 +00:00
|
|
|
ToFind.Exclusive.FileLock.StartingByte = IoStack->Parameters.Write.ByteOffset;
|
|
|
|
ToFind.Exclusive.FileLock.EndingByte.QuadPart =
|
|
|
|
ToFind.Exclusive.FileLock.StartingByte.QuadPart +
|
|
|
|
IoStack->Parameters.Write.Length;
|
|
|
|
Found = RtlLookupElementGenericTable
|
|
|
|
(FileLock->LockInformation,
|
|
|
|
&ToFind);
|
|
|
|
if (!Found) {
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("CheckLockForWriteAccess(%wZ) => TRUE\n", &IoStack->FileObject->FileName);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-07-23 18:50:16 +00:00
|
|
|
Result = Process->UniqueProcessId == Found->Exclusive.FileLock.ProcessId;
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("CheckLockForWriteAccess(%wZ) => %s\n", &IoStack->FileObject->FileName, Result ? "TRUE" : "FALSE");
|
|
|
|
return Result;
|
2009-10-12 03:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlFastCheckLockForRead(IN PFILE_LOCK FileLock,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN PLARGE_INTEGER Length,
|
|
|
|
IN ULONG Key,
|
|
|
|
IN PFILE_OBJECT FileObject,
|
|
|
|
IN PVOID Process)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
PEPROCESS EProcess = Process;
|
|
|
|
COMBINED_LOCK_ELEMENT ToFind;
|
|
|
|
PCOMBINED_LOCK_ELEMENT Found;
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("FsRtlFastCheckLockForRead(%wZ, Offset %08x%08x, Length %08x%08x, Key %x)\n",
|
|
|
|
&FileObject->FileName,
|
|
|
|
FileOffset->HighPart,
|
|
|
|
FileOffset->LowPart,
|
|
|
|
Length->HighPart,
|
|
|
|
Length->LowPart,
|
|
|
|
Key);
|
2012-07-23 18:50:16 +00:00
|
|
|
ToFind.Exclusive.FileLock.StartingByte = *FileOffset;
|
|
|
|
ToFind.Exclusive.FileLock.EndingByte.QuadPart =
|
|
|
|
FileOffset->QuadPart + Length->QuadPart;
|
|
|
|
if (!FileLock->LockInformation) return TRUE;
|
|
|
|
Found = RtlLookupElementGenericTable
|
|
|
|
(FileLock->LockInformation,
|
|
|
|
&ToFind);
|
|
|
|
if (!Found || !Found->Exclusive.FileLock.ExclusiveLock) return TRUE;
|
|
|
|
return Found->Exclusive.FileLock.Key == Key &&
|
|
|
|
Found->Exclusive.FileLock.ProcessId == EProcess->UniqueProcessId;
|
2009-10-12 03:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
FsRtlFastCheckLockForWrite(IN PFILE_LOCK FileLock,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN PLARGE_INTEGER Length,
|
|
|
|
IN ULONG Key,
|
|
|
|
IN PFILE_OBJECT FileObject,
|
|
|
|
IN PVOID Process)
|
|
|
|
{
|
2012-03-10 07:28:03 +00:00
|
|
|
BOOLEAN Result;
|
2012-07-23 18:50:16 +00:00
|
|
|
PEPROCESS EProcess = Process;
|
|
|
|
COMBINED_LOCK_ELEMENT ToFind;
|
|
|
|
PCOMBINED_LOCK_ELEMENT Found;
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("FsRtlFastCheckLockForWrite(%wZ, Offset %08x%08x, Length %08x%08x, Key %x)\n",
|
|
|
|
&FileObject->FileName,
|
|
|
|
FileOffset->HighPart,
|
|
|
|
FileOffset->LowPart,
|
|
|
|
Length->HighPart,
|
|
|
|
Length->LowPart,
|
|
|
|
Key);
|
2012-07-23 18:50:16 +00:00
|
|
|
ToFind.Exclusive.FileLock.StartingByte = *FileOffset;
|
|
|
|
ToFind.Exclusive.FileLock.EndingByte.QuadPart =
|
|
|
|
FileOffset->QuadPart + Length->QuadPart;
|
|
|
|
if (!FileLock->LockInformation) {
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("CheckForWrite(%wZ) => TRUE\n", &FileObject->FileName);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-07-23 18:50:16 +00:00
|
|
|
Found = RtlLookupElementGenericTable
|
|
|
|
(FileLock->LockInformation,
|
|
|
|
&ToFind);
|
|
|
|
if (!Found) {
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("CheckForWrite(%wZ) => TRUE\n", &FileObject->FileName);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-07-23 18:50:16 +00:00
|
|
|
Result = Found->Exclusive.FileLock.Key == Key &&
|
|
|
|
Found->Exclusive.FileLock.ProcessId == EProcess->UniqueProcessId;
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("CheckForWrite(%wZ) => %s\n", &FileObject->FileName, Result ? "TRUE" : "FALSE");
|
|
|
|
return Result;
|
2009-10-12 03:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
FsRtlFastUnlockSingle(IN PFILE_LOCK FileLock,
|
|
|
|
IN PFILE_OBJECT FileObject,
|
|
|
|
IN PLARGE_INTEGER FileOffset,
|
|
|
|
IN PLARGE_INTEGER Length,
|
|
|
|
IN PEPROCESS Process,
|
|
|
|
IN ULONG Key,
|
|
|
|
IN PVOID Context OPTIONAL,
|
|
|
|
IN BOOLEAN AlreadySynchronized)
|
|
|
|
{
|
2012-03-10 07:28:03 +00:00
|
|
|
BOOLEAN FoundShared = FALSE;
|
|
|
|
PLIST_ENTRY SharedEntry;
|
|
|
|
PLOCK_SHARED_RANGE SharedRange = NULL;
|
2012-07-23 18:50:16 +00:00
|
|
|
COMBINED_LOCK_ELEMENT Find;
|
|
|
|
PCOMBINED_LOCK_ELEMENT Entry;
|
|
|
|
PIRP NextMatchingLockIrp;
|
|
|
|
PLOCK_INFORMATION InternalInfo = FileLock->LockInformation;
|
2012-07-23 18:53:50 +00:00
|
|
|
DPRINT("FsRtlFastUnlockSingle(%wZ, Offset %08x%08x (%d), Length %08x%08x (%d), Key %x)\n",
|
2012-03-10 07:28:03 +00:00
|
|
|
&FileObject->FileName,
|
|
|
|
FileOffset->HighPart,
|
|
|
|
FileOffset->LowPart,
|
2012-07-23 18:53:50 +00:00
|
|
|
(int)FileOffset->QuadPart,
|
2012-03-10 07:28:03 +00:00
|
|
|
Length->HighPart,
|
|
|
|
Length->LowPart,
|
2012-07-23 18:53:50 +00:00
|
|
|
(int)Length->QuadPart,
|
2012-03-10 07:28:03 +00:00
|
|
|
Key);
|
2012-07-23 18:50:16 +00:00
|
|
|
// The region to unlock must correspond exactly to a previously locked region
|
|
|
|
// -- msdn
|
|
|
|
// But Windows 2003 doesn't assert on it and simply ignores that parameter
|
|
|
|
// ASSERT(AlreadySynchronized);
|
|
|
|
Find.Exclusive.FileLock.StartingByte = *FileOffset;
|
|
|
|
Find.Exclusive.FileLock.EndingByte.QuadPart =
|
|
|
|
FileOffset->QuadPart + Length->QuadPart;
|
2013-02-02 18:49:53 +00:00
|
|
|
if (!InternalInfo) {
|
|
|
|
DPRINT("File not previously locked (ever)\n");
|
|
|
|
return STATUS_RANGE_NOT_LOCKED;
|
|
|
|
}
|
2012-07-23 18:50:16 +00:00
|
|
|
Entry = RtlLookupElementGenericTable(&InternalInfo->RangeTable, &Find);
|
|
|
|
if (!Entry) {
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("Range not locked %wZ\n", &FileObject->FileName);
|
|
|
|
return STATUS_RANGE_NOT_LOCKED;
|
|
|
|
}
|
2012-06-14 16:19:01 +00:00
|
|
|
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT("Found lock entry: Exclusive %u %08x%08x:%08x%08x %wZ\n",
|
2012-06-14 16:19:01 +00:00
|
|
|
Entry->Exclusive.FileLock.ExclusiveLock,
|
|
|
|
Entry->Exclusive.FileLock.StartingByte.HighPart,
|
|
|
|
Entry->Exclusive.FileLock.StartingByte.LowPart,
|
|
|
|
Entry->Exclusive.FileLock.EndingByte.HighPart,
|
|
|
|
Entry->Exclusive.FileLock.EndingByte.LowPart,
|
|
|
|
&FileObject->FileName);
|
2012-03-10 07:28:03 +00:00
|
|
|
|
|
|
|
if (Entry->Exclusive.FileLock.ExclusiveLock)
|
|
|
|
{
|
|
|
|
if (Entry->Exclusive.FileLock.Key != Key ||
|
|
|
|
Entry->Exclusive.FileLock.ProcessId != Process->UniqueProcessId ||
|
|
|
|
Entry->Exclusive.FileLock.StartingByte.QuadPart != FileOffset->QuadPart ||
|
|
|
|
Entry->Exclusive.FileLock.EndingByte.QuadPart !=
|
|
|
|
FileOffset->QuadPart + Length->QuadPart)
|
|
|
|
{
|
|
|
|
DPRINT("Range not locked %wZ\n", &FileObject->FileName);
|
|
|
|
return STATUS_RANGE_NOT_LOCKED;
|
|
|
|
}
|
|
|
|
RtlCopyMemory(&Find, Entry, sizeof(Find));
|
2012-06-14 16:19:01 +00:00
|
|
|
// Remove the old exclusive lock region
|
|
|
|
RtlDeleteElementGenericTable(&InternalInfo->RangeTable, Entry);
|
2012-03-10 07:28:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINT("Shared lock %wZ Start %08x%08x End %08x%08x\n",
|
|
|
|
&FileObject->FileName,
|
|
|
|
Entry->Exclusive.FileLock.StartingByte.HighPart,
|
|
|
|
Entry->Exclusive.FileLock.StartingByte.LowPart,
|
|
|
|
Entry->Exclusive.FileLock.EndingByte.HighPart,
|
|
|
|
Entry->Exclusive.FileLock.EndingByte.LowPart);
|
|
|
|
for (SharedEntry = InternalInfo->SharedLocks.Flink;
|
|
|
|
SharedEntry != &InternalInfo->SharedLocks;
|
|
|
|
SharedEntry = SharedEntry->Flink)
|
|
|
|
{
|
|
|
|
SharedRange = CONTAINING_RECORD(SharedEntry, LOCK_SHARED_RANGE, Entry);
|
|
|
|
if (SharedRange->Start.QuadPart == FileOffset->QuadPart &&
|
|
|
|
SharedRange->End.QuadPart == FileOffset->QuadPart + Length->QuadPart &&
|
|
|
|
SharedRange->Key == Key &&
|
|
|
|
SharedRange->ProcessId == Process->UniqueProcessId)
|
|
|
|
{
|
|
|
|
FoundShared = TRUE;
|
|
|
|
DPRINT("Found shared element to delete %wZ Start %08x%08x End %08x%08x Key %x\n",
|
|
|
|
&FileObject->FileName,
|
|
|
|
SharedRange->Start.HighPart,
|
|
|
|
SharedRange->Start.LowPart,
|
|
|
|
SharedRange->End.HighPart,
|
|
|
|
SharedRange->End.LowPart,
|
|
|
|
SharedRange->Key);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (FoundShared)
|
|
|
|
{
|
2012-12-04 12:09:59 +00:00
|
|
|
/* Remove the found range from the shared range lists */
|
2012-03-10 07:28:03 +00:00
|
|
|
RemoveEntryList(&SharedRange->Entry);
|
2012-12-09 15:11:09 +00:00
|
|
|
ExFreePoolWithTag(SharedRange, TAG_RANGE);
|
2012-07-23 18:53:50 +00:00
|
|
|
/* We need to rebuild the list of shared ranges. */
|
|
|
|
DPRINT("Removing the lock entry %wZ (%08x%08x:%08x%08x)\n",
|
|
|
|
&FileObject->FileName,
|
|
|
|
Entry->Exclusive.FileLock.StartingByte.HighPart,
|
|
|
|
Entry->Exclusive.FileLock.StartingByte.LowPart,
|
|
|
|
Entry->Exclusive.FileLock.EndingByte.HighPart,
|
|
|
|
Entry->Exclusive.FileLock.EndingByte.LowPart);
|
2012-12-04 12:09:59 +00:00
|
|
|
|
|
|
|
/* Remember what was in there and remove it from the table */
|
|
|
|
Find = *Entry;
|
|
|
|
RtlDeleteElementGenericTable(&InternalInfo->RangeTable, &Find);
|
2012-07-23 18:53:50 +00:00
|
|
|
/* Put shared locks back in place */
|
2012-12-04 12:09:59 +00:00
|
|
|
for (SharedEntry = InternalInfo->SharedLocks.Flink;
|
|
|
|
SharedEntry != &InternalInfo->SharedLocks;
|
|
|
|
SharedEntry = SharedEntry->Flink)
|
2012-07-23 18:53:50 +00:00
|
|
|
{
|
[NTOSKRNL]
Coverity code defects fixes :
- Cache: CID 701441
- Config: CIDs 716570, 716669, 716760
- Dbgk: Kdbg: CIDs 716571, 515128/9, 500432
- Ex: CIDs 500156/7, 515122, 716200/67, 701301, 514669
- Fsrtl: Fstub: CIDs 701341/2, 701288, 716770, 701302, and CIDs 716576/7/8 + 514636 + 716805 thanks to Thomas Faber
- Io: CIDs 514576, 514643, 514672/3, 716203, 716269, 716581, 716591, 716713
- Ke: CIDs 515125, 716592
- Ps: CIDs 716603/4, 701422
- Ob: Po: CIDs 514671/680, 701419/420/421, 716763, 716601/2
All the details are given in the different bug reports.
CORE-6677 CORE-6679 CORE-6680 CORE-6683 CORE-6686 CORE-6692 CORE-6693 CORE-6694 CORE-6695 CORE-6696 #comment Committed in rev.57400 #resolve #close
svn path=/trunk/; revision=57400
2012-09-27 17:16:31 +00:00
|
|
|
COMBINED_LOCK_ELEMENT LockElement;
|
2012-12-04 12:09:59 +00:00
|
|
|
SharedRange = CONTAINING_RECORD(SharedEntry, LOCK_SHARED_RANGE, Entry);
|
|
|
|
LockElement.Exclusive.FileLock.FileObject = FileObject;
|
|
|
|
LockElement.Exclusive.FileLock.StartingByte = SharedRange->Start;
|
|
|
|
LockElement.Exclusive.FileLock.EndingByte = SharedRange->End;
|
|
|
|
LockElement.Exclusive.FileLock.ProcessId = SharedRange->ProcessId;
|
|
|
|
LockElement.Exclusive.FileLock.Key = SharedRange->Key;
|
|
|
|
LockElement.Exclusive.FileLock.ExclusiveLock = FALSE;
|
|
|
|
|
|
|
|
if (LockCompare(&InternalInfo->RangeTable, &Find, &LockElement) != GenericEqual)
|
2012-07-23 18:53:50 +00:00
|
|
|
{
|
|
|
|
DPRINT("Skipping range %08x%08x:%08x%08x\n",
|
[NTOSKRNL]
Coverity code defects fixes :
- Cache: CID 701441
- Config: CIDs 716570, 716669, 716760
- Dbgk: Kdbg: CIDs 716571, 515128/9, 500432
- Ex: CIDs 500156/7, 515122, 716200/67, 701301, 514669
- Fsrtl: Fstub: CIDs 701341/2, 701288, 716770, 701302, and CIDs 716576/7/8 + 514636 + 716805 thanks to Thomas Faber
- Io: CIDs 514576, 514643, 514672/3, 716203, 716269, 716581, 716591, 716713
- Ke: CIDs 515125, 716592
- Ps: CIDs 716603/4, 701422
- Ob: Po: CIDs 514671/680, 701419/420/421, 716763, 716601/2
All the details are given in the different bug reports.
CORE-6677 CORE-6679 CORE-6680 CORE-6683 CORE-6686 CORE-6692 CORE-6693 CORE-6694 CORE-6695 CORE-6696 #comment Committed in rev.57400 #resolve #close
svn path=/trunk/; revision=57400
2012-09-27 17:16:31 +00:00
|
|
|
LockElement.Exclusive.FileLock.StartingByte.HighPart,
|
|
|
|
LockElement.Exclusive.FileLock.StartingByte.LowPart,
|
|
|
|
LockElement.Exclusive.FileLock.EndingByte.HighPart,
|
|
|
|
LockElement.Exclusive.FileLock.EndingByte.LowPart);
|
2012-07-23 18:53:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DPRINT("Re-creating range %08x%08x:%08x%08x\n",
|
[NTOSKRNL]
Coverity code defects fixes :
- Cache: CID 701441
- Config: CIDs 716570, 716669, 716760
- Dbgk: Kdbg: CIDs 716571, 515128/9, 500432
- Ex: CIDs 500156/7, 515122, 716200/67, 701301, 514669
- Fsrtl: Fstub: CIDs 701341/2, 701288, 716770, 701302, and CIDs 716576/7/8 + 514636 + 716805 thanks to Thomas Faber
- Io: CIDs 514576, 514643, 514672/3, 716203, 716269, 716581, 716591, 716713
- Ke: CIDs 515125, 716592
- Ps: CIDs 716603/4, 701422
- Ob: Po: CIDs 514671/680, 701419/420/421, 716763, 716601/2
All the details are given in the different bug reports.
CORE-6677 CORE-6679 CORE-6680 CORE-6683 CORE-6686 CORE-6692 CORE-6693 CORE-6694 CORE-6695 CORE-6696 #comment Committed in rev.57400 #resolve #close
svn path=/trunk/; revision=57400
2012-09-27 17:16:31 +00:00
|
|
|
LockElement.Exclusive.FileLock.StartingByte.HighPart,
|
|
|
|
LockElement.Exclusive.FileLock.StartingByte.LowPart,
|
|
|
|
LockElement.Exclusive.FileLock.EndingByte.HighPart,
|
|
|
|
LockElement.Exclusive.FileLock.EndingByte.LowPart);
|
2012-12-04 12:09:59 +00:00
|
|
|
FsRtlpRebuildSharedLockRange(FileLock, InternalInfo, &LockElement);
|
2012-07-23 18:53:50 +00:00
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return STATUS_RANGE_NOT_LOCKED;
|
|
|
|
}
|
|
|
|
}
|
2012-12-04 12:09:59 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2012-07-23 18:53:50 +00:00
|
|
|
DPRINT("Lock still has:\n");
|
|
|
|
for (SharedEntry = InternalInfo->SharedLocks.Flink;
|
|
|
|
SharedEntry != &InternalInfo->SharedLocks;
|
|
|
|
SharedEntry = SharedEntry->Flink)
|
|
|
|
{
|
|
|
|
SharedRange = CONTAINING_RECORD(SharedEntry, LOCK_SHARED_RANGE, Entry);
|
|
|
|
DPRINT("Shared element %wZ Offset %08x%08x Length %08x%08x Key %x\n",
|
|
|
|
&FileObject->FileName,
|
|
|
|
SharedRange->Start.HighPart,
|
|
|
|
SharedRange->Start.LowPart,
|
|
|
|
SharedRange->End.HighPart,
|
|
|
|
SharedRange->End.LowPart,
|
|
|
|
SharedRange->Key);
|
2012-03-10 07:28:03 +00:00
|
|
|
}
|
2012-12-04 12:09:59 +00:00
|
|
|
#endif
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
// this is definitely the thing we want
|
2012-07-23 18:53:50 +00:00
|
|
|
InternalInfo->Generation++;
|
|
|
|
while ((NextMatchingLockIrp = IoCsqRemoveNextIrp(&InternalInfo->Csq, &Find)))
|
2012-03-10 07:28:03 +00:00
|
|
|
{
|
2012-07-23 18:53:50 +00:00
|
|
|
if (NextMatchingLockIrp->IoStatus.Information == InternalInfo->Generation)
|
|
|
|
{
|
|
|
|
// We've already looked at this one, meaning that we looped.
|
|
|
|
// Put it back and exit.
|
|
|
|
IoCsqInsertIrpEx
|
|
|
|
(&InternalInfo->Csq,
|
|
|
|
NextMatchingLockIrp,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
break;
|
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
// Got a new lock irp... try to do the new lock operation
|
|
|
|
// Note that we pick an operation that would succeed at the time
|
|
|
|
// we looked, but can't guarantee that it won't just be re-queued
|
|
|
|
// because somebody else snatched part of the range in a new thread.
|
|
|
|
DPRINT("Locking another IRP %p for %p %wZ\n",
|
|
|
|
&FileObject->FileName, FileLock, NextMatchingLockIrp);
|
|
|
|
FsRtlProcessFileLock(InternalInfo->BelongsTo, NextMatchingLockIrp, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINT("Success %wZ\n", &FileObject->FileName);
|
2012-07-23 18:50:16 +00:00
|
|
|
return STATUS_SUCCESS;
|
2009-10-12 03:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
FsRtlFastUnlockAll(IN PFILE_LOCK FileLock,
|
|
|
|
IN PFILE_OBJECT FileObject,
|
|
|
|
IN PEPROCESS Process,
|
|
|
|
IN PVOID Context OPTIONAL)
|
|
|
|
{
|
2012-07-23 18:53:50 +00:00
|
|
|
PLIST_ENTRY ListEntry;
|
2012-07-23 18:50:16 +00:00
|
|
|
PCOMBINED_LOCK_ELEMENT Entry;
|
2012-07-23 18:53:50 +00:00
|
|
|
PLOCK_INFORMATION InternalInfo = FileLock->LockInformation;
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("FsRtlFastUnlockAll(%wZ)\n", &FileObject->FileName);
|
2012-07-23 18:50:16 +00:00
|
|
|
// XXX Synchronize somehow
|
|
|
|
if (!FileLock->LockInformation) {
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("Not locked %wZ\n", &FileObject->FileName);
|
|
|
|
return STATUS_RANGE_NOT_LOCKED; // no locks
|
|
|
|
}
|
2012-07-23 18:53:50 +00:00
|
|
|
for (ListEntry = InternalInfo->SharedLocks.Flink;
|
|
|
|
ListEntry != &InternalInfo->SharedLocks;)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER Length;
|
|
|
|
PLOCK_SHARED_RANGE Range = CONTAINING_RECORD(ListEntry, LOCK_SHARED_RANGE, Entry);
|
|
|
|
Length.QuadPart = Range->End.QuadPart - Range->Start.QuadPart;
|
|
|
|
ListEntry = ListEntry->Flink;
|
|
|
|
if (Range->ProcessId != Process->UniqueProcessId)
|
|
|
|
continue;
|
|
|
|
FsRtlFastUnlockSingle
|
|
|
|
(FileLock,
|
|
|
|
FileObject,
|
|
|
|
&Range->Start,
|
|
|
|
&Length,
|
|
|
|
Range->ProcessId,
|
|
|
|
Range->Key,
|
|
|
|
Context,
|
|
|
|
TRUE);
|
|
|
|
}
|
|
|
|
for (Entry = RtlEnumerateGenericTable(&InternalInfo->RangeTable, TRUE);
|
2012-03-10 07:28:03 +00:00
|
|
|
Entry;
|
2012-07-23 18:53:50 +00:00
|
|
|
Entry = RtlEnumerateGenericTable(&InternalInfo->RangeTable, FALSE))
|
2012-07-23 18:50:16 +00:00
|
|
|
{
|
|
|
|
LARGE_INTEGER Length;
|
|
|
|
// We'll take the first one to be the list head, and free the others first...
|
|
|
|
Length.QuadPart =
|
|
|
|
Entry->Exclusive.FileLock.EndingByte.QuadPart -
|
|
|
|
Entry->Exclusive.FileLock.StartingByte.QuadPart;
|
|
|
|
FsRtlFastUnlockSingle
|
|
|
|
(FileLock,
|
|
|
|
Entry->Exclusive.FileLock.FileObject,
|
|
|
|
&Entry->Exclusive.FileLock.StartingByte,
|
|
|
|
&Length,
|
|
|
|
Entry->Exclusive.FileLock.ProcessId,
|
|
|
|
Entry->Exclusive.FileLock.Key,
|
|
|
|
Context,
|
|
|
|
TRUE);
|
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("Done %wZ\n", &FileObject->FileName);
|
2012-02-29 09:18:57 +00:00
|
|
|
return STATUS_SUCCESS;
|
2009-10-12 03:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
FsRtlFastUnlockAllByKey(IN PFILE_LOCK FileLock,
|
|
|
|
IN PFILE_OBJECT FileObject,
|
|
|
|
IN PEPROCESS Process,
|
|
|
|
IN ULONG Key,
|
|
|
|
IN PVOID Context OPTIONAL)
|
|
|
|
{
|
2012-07-23 18:53:50 +00:00
|
|
|
PLIST_ENTRY ListEntry;
|
2012-07-23 18:50:16 +00:00
|
|
|
PCOMBINED_LOCK_ELEMENT Entry;
|
|
|
|
PLOCK_INFORMATION InternalInfo = FileLock->LockInformation;
|
2012-03-10 07:28:03 +00:00
|
|
|
|
|
|
|
DPRINT("FsRtlFastUnlockAllByKey(%wZ,Key %x)\n", &FileObject->FileName, Key);
|
|
|
|
|
2012-07-23 18:50:16 +00:00
|
|
|
// XXX Synchronize somehow
|
|
|
|
if (!FileLock->LockInformation) return STATUS_RANGE_NOT_LOCKED; // no locks
|
2012-07-23 18:53:50 +00:00
|
|
|
for (ListEntry = InternalInfo->SharedLocks.Flink;
|
|
|
|
ListEntry != &InternalInfo->SharedLocks;)
|
|
|
|
{
|
|
|
|
PLOCK_SHARED_RANGE Range = CONTAINING_RECORD(ListEntry, LOCK_SHARED_RANGE, Entry);
|
|
|
|
LARGE_INTEGER Length;
|
|
|
|
Length.QuadPart = Range->End.QuadPart - Range->Start.QuadPart;
|
|
|
|
ListEntry = ListEntry->Flink;
|
|
|
|
if (Range->ProcessId != Process->UniqueProcessId ||
|
|
|
|
Range->Key != Key)
|
|
|
|
continue;
|
|
|
|
FsRtlFastUnlockSingle
|
|
|
|
(FileLock,
|
|
|
|
FileObject,
|
|
|
|
&Range->Start,
|
|
|
|
&Length,
|
|
|
|
Range->ProcessId,
|
|
|
|
Range->Key,
|
|
|
|
Context,
|
|
|
|
TRUE);
|
|
|
|
}
|
2012-07-23 18:50:16 +00:00
|
|
|
for (Entry = RtlEnumerateGenericTable(&InternalInfo->RangeTable, TRUE);
|
2012-03-10 07:28:03 +00:00
|
|
|
Entry;
|
2012-07-23 18:50:16 +00:00
|
|
|
Entry = RtlEnumerateGenericTable(&InternalInfo->RangeTable, FALSE))
|
|
|
|
{
|
|
|
|
LARGE_INTEGER Length;
|
|
|
|
// We'll take the first one to be the list head, and free the others first...
|
|
|
|
Length.QuadPart =
|
|
|
|
Entry->Exclusive.FileLock.EndingByte.QuadPart -
|
|
|
|
Entry->Exclusive.FileLock.StartingByte.QuadPart;
|
|
|
|
if (Entry->Exclusive.FileLock.Key == Key &&
|
|
|
|
Entry->Exclusive.FileLock.ProcessId == Process->UniqueProcessId)
|
|
|
|
{
|
|
|
|
FsRtlFastUnlockSingle
|
|
|
|
(FileLock,
|
|
|
|
Entry->Exclusive.FileLock.FileObject,
|
|
|
|
&Entry->Exclusive.FileLock.StartingByte,
|
|
|
|
&Length,
|
|
|
|
Entry->Exclusive.FileLock.ProcessId,
|
|
|
|
Entry->Exclusive.FileLock.Key,
|
|
|
|
Context,
|
|
|
|
TRUE);
|
|
|
|
}
|
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2012-02-29 09:18:57 +00:00
|
|
|
return STATUS_SUCCESS;
|
2009-10-12 03:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
FsRtlProcessFileLock(IN PFILE_LOCK FileLock,
|
|
|
|
IN PIRP Irp,
|
|
|
|
IN PVOID Context OPTIONAL)
|
|
|
|
{
|
|
|
|
PIO_STACK_LOCATION IoStackLocation;
|
|
|
|
NTSTATUS Status;
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* Get the I/O Stack location */
|
|
|
|
IoStackLocation = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
ASSERT(IoStackLocation->MajorFunction == IRP_MJ_LOCK_CONTROL);
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* Clear the I/O status block and check what function this is */
|
|
|
|
IoStatusBlock.Information = 0;
|
2012-03-10 07:28:03 +00:00
|
|
|
|
|
|
|
DPRINT("FsRtlProcessFileLock(%wZ, MinorFunction %x)\n",
|
|
|
|
&IoStackLocation->FileObject->FileName,
|
|
|
|
IoStackLocation->MinorFunction);
|
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
switch(IoStackLocation->MinorFunction)
|
|
|
|
{
|
|
|
|
/* A lock */
|
2012-03-10 07:28:03 +00:00
|
|
|
case IRP_MN_LOCK:
|
|
|
|
|
|
|
|
/* Call the private lock routine */
|
|
|
|
FsRtlPrivateLock(FileLock,
|
|
|
|
IoStackLocation->FileObject,
|
|
|
|
&IoStackLocation->
|
|
|
|
Parameters.LockControl.ByteOffset,
|
|
|
|
IoStackLocation->Parameters.LockControl.Length,
|
|
|
|
IoGetRequestorProcess(Irp),
|
|
|
|
IoStackLocation->Parameters.LockControl.Key,
|
|
|
|
IoStackLocation->Flags & SL_FAIL_IMMEDIATELY,
|
|
|
|
IoStackLocation->Flags & SL_EXCLUSIVE_LOCK,
|
|
|
|
&IoStatusBlock,
|
|
|
|
Irp,
|
|
|
|
Context,
|
|
|
|
FALSE);
|
|
|
|
return IoStatusBlock.Status;
|
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* A single unlock */
|
2012-03-10 07:28:03 +00:00
|
|
|
case IRP_MN_UNLOCK_SINGLE:
|
|
|
|
|
|
|
|
/* Call fast unlock */
|
|
|
|
IoStatusBlock.Status =
|
|
|
|
FsRtlFastUnlockSingle(FileLock,
|
|
|
|
IoStackLocation->FileObject,
|
|
|
|
&IoStackLocation->Parameters.LockControl.
|
|
|
|
ByteOffset,
|
|
|
|
IoStackLocation->Parameters.LockControl.
|
|
|
|
Length,
|
|
|
|
IoGetRequestorProcess(Irp),
|
|
|
|
IoStackLocation->Parameters.LockControl.
|
|
|
|
Key,
|
|
|
|
Context,
|
|
|
|
FALSE);
|
|
|
|
break;
|
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* Total unlock */
|
2012-03-10 07:28:03 +00:00
|
|
|
case IRP_MN_UNLOCK_ALL:
|
|
|
|
|
|
|
|
/* Do a fast unlock */
|
|
|
|
IoStatusBlock.Status = FsRtlFastUnlockAll(FileLock,
|
|
|
|
IoStackLocation->
|
|
|
|
FileObject,
|
|
|
|
IoGetRequestorProcess(Irp),
|
|
|
|
Context);
|
|
|
|
break;
|
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* Unlock by key */
|
2012-03-10 07:28:03 +00:00
|
|
|
case IRP_MN_UNLOCK_ALL_BY_KEY:
|
|
|
|
|
|
|
|
/* Do it */
|
|
|
|
IoStatusBlock.Status =
|
|
|
|
FsRtlFastUnlockAllByKey(FileLock,
|
|
|
|
IoStackLocation->FileObject,
|
|
|
|
IoGetRequestorProcess(Irp),
|
|
|
|
IoStackLocation->Parameters.
|
|
|
|
LockControl.Key,
|
|
|
|
Context);
|
|
|
|
break;
|
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* Invalid request */
|
2012-03-10 07:28:03 +00:00
|
|
|
default:
|
|
|
|
|
|
|
|
/* Complete it */
|
|
|
|
FsRtlCompleteRequest(Irp, STATUS_INVALID_DEVICE_REQUEST);
|
|
|
|
IoStatusBlock.Status = STATUS_INVALID_DEVICE_REQUEST;
|
|
|
|
return STATUS_INVALID_DEVICE_REQUEST;
|
2009-10-12 03:35:35 +00:00
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* Return the status */
|
2012-03-10 07:28:03 +00:00
|
|
|
DPRINT("Lock IRP %p %x\n", Irp, IoStatusBlock.Status);
|
|
|
|
FsRtlCompleteLockIrpReal
|
|
|
|
(FileLock->CompleteLockIrpRoutine,
|
|
|
|
Context,
|
|
|
|
Irp,
|
|
|
|
IoStatusBlock.Status,
|
|
|
|
&Status,
|
|
|
|
NULL);
|
2009-10-12 03:35:35 +00:00
|
|
|
return IoStatusBlock.Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlInitializeFileLock (IN PFILE_LOCK FileLock,
|
|
|
|
IN PCOMPLETE_LOCK_IRP_ROUTINE CompleteLockIrpRoutine OPTIONAL,
|
|
|
|
IN PUNLOCK_ROUTINE UnlockRoutine OPTIONAL)
|
|
|
|
{
|
|
|
|
/* Setup the lock */
|
2012-03-05 09:49:06 +00:00
|
|
|
RtlZeroMemory(FileLock, sizeof(*FileLock));
|
2009-10-12 03:35:35 +00:00
|
|
|
FileLock->FastIoIsQuestionable = FALSE;
|
|
|
|
FileLock->CompleteLockIrpRoutine = CompleteLockIrpRoutine;
|
|
|
|
FileLock->UnlockRoutine = UnlockRoutine;
|
|
|
|
FileLock->LockInformation = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlUninitializeFileLock(IN PFILE_LOCK FileLock)
|
|
|
|
{
|
2012-07-23 18:50:16 +00:00
|
|
|
if (FileLock->LockInformation)
|
|
|
|
{
|
2012-03-05 09:49:06 +00:00
|
|
|
PIRP Irp;
|
|
|
|
PLOCK_INFORMATION InternalInfo = FileLock->LockInformation;
|
|
|
|
PCOMBINED_LOCK_ELEMENT Entry;
|
2012-03-10 07:28:03 +00:00
|
|
|
PLIST_ENTRY SharedEntry;
|
|
|
|
PLOCK_SHARED_RANGE SharedRange;
|
2012-03-05 09:49:06 +00:00
|
|
|
// MSDN: this completes any remaining lock IRPs
|
2012-03-10 07:28:03 +00:00
|
|
|
for (SharedEntry = InternalInfo->SharedLocks.Flink;
|
|
|
|
SharedEntry != &InternalInfo->SharedLocks;)
|
|
|
|
{
|
|
|
|
SharedRange = CONTAINING_RECORD(SharedEntry, LOCK_SHARED_RANGE, Entry);
|
|
|
|
SharedEntry = SharedEntry->Flink;
|
2012-12-04 12:09:59 +00:00
|
|
|
RemoveEntryList(&SharedRange->Entry);
|
2012-12-09 15:11:09 +00:00
|
|
|
ExFreePoolWithTag(SharedRange, TAG_RANGE);
|
2012-03-10 07:28:03 +00:00
|
|
|
}
|
2012-03-05 09:49:06 +00:00
|
|
|
while ((Entry = RtlGetElementGenericTable(&InternalInfo->RangeTable, 0)) != NULL)
|
|
|
|
{
|
|
|
|
RtlDeleteElementGenericTable(&InternalInfo->RangeTable, Entry);
|
|
|
|
}
|
|
|
|
while ((Irp = IoCsqRemoveNextIrp(&InternalInfo->Csq, NULL)) != NULL)
|
|
|
|
{
|
|
|
|
FsRtlProcessFileLock(FileLock, Irp, NULL);
|
|
|
|
}
|
2012-12-09 15:11:09 +00:00
|
|
|
ExFreePoolWithTag(InternalInfo, TAG_FLOCK);
|
2012-07-23 18:50:16 +00:00
|
|
|
FileLock->LockInformation = NULL;
|
|
|
|
}
|
2009-10-12 03:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
PFILE_LOCK
|
|
|
|
NTAPI
|
|
|
|
FsRtlAllocateFileLock(IN PCOMPLETE_LOCK_IRP_ROUTINE CompleteLockIrpRoutine OPTIONAL,
|
|
|
|
IN PUNLOCK_ROUTINE UnlockRoutine OPTIONAL)
|
|
|
|
{
|
|
|
|
PFILE_LOCK FileLock;
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* Try to allocate it */
|
|
|
|
FileLock = ExAllocateFromPagedLookasideList(&FsRtlFileLockLookasideList);
|
|
|
|
if (FileLock)
|
|
|
|
{
|
|
|
|
/* Initialize it */
|
|
|
|
FsRtlInitializeFileLock(FileLock,
|
|
|
|
CompleteLockIrpRoutine,
|
|
|
|
UnlockRoutine);
|
|
|
|
}
|
2012-03-10 07:28:03 +00:00
|
|
|
|
2009-10-12 03:35:35 +00:00
|
|
|
/* Return the lock */
|
|
|
|
return FileLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
FsRtlFreeFileLock(IN PFILE_LOCK FileLock)
|
|
|
|
{
|
|
|
|
/* Uninitialize and free the lock */
|
|
|
|
FsRtlUninitializeFileLock(FileLock);
|
|
|
|
ExFreeToPagedLookasideList(&FsRtlFileLockLookasideList, FileLock);
|
|
|
|
}
|