2005-03-12 19:58:53 +00:00
|
|
|
/*
|
2006-06-30 17:53:00 +00:00
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
2015-10-04 11:54:25 +00:00
|
|
|
* FILE: ntoskrnl/io/iomgr/iocomp.c
|
2006-06-30 18:14:54 +00:00
|
|
|
* PURPOSE: I/O Wrappers (called Completion Ports) for Kernel Queues
|
2006-06-30 17:53:00 +00:00
|
|
|
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
|
|
|
|
* Thomas Weidenmueller (w3seek@reactos.org)
|
1998-10-05 04:00:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
2004-08-15 16:39:12 +00:00
|
|
|
#include <ntoskrnl.h>
|
2003-03-19 23:17:52 +00:00
|
|
|
#define NDEBUG
|
2008-08-30 16:31:06 +00:00
|
|
|
#include <debug.h>
|
1998-10-05 04:00:59 +00:00
|
|
|
|
2005-05-15 17:59:33 +00:00
|
|
|
POBJECT_TYPE IoCompletionType;
|
2003-03-19 23:17:52 +00:00
|
|
|
|
2008-11-04 21:55:55 +00:00
|
|
|
GENERAL_LOOKASIDE IoCompletionPacketLookaside;
|
2003-03-19 23:17:52 +00:00
|
|
|
|
2006-06-30 17:53:00 +00:00
|
|
|
GENERIC_MAPPING IopCompletionMapping =
|
2003-03-19 23:17:52 +00:00
|
|
|
{
|
2012-09-02 22:06:42 +00:00
|
|
|
STANDARD_RIGHTS_READ | IO_COMPLETION_QUERY_STATE,
|
|
|
|
STANDARD_RIGHTS_WRITE | IO_COMPLETION_MODIFY_STATE,
|
|
|
|
STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
|
2005-03-12 19:58:53 +00:00
|
|
|
IO_COMPLETION_ALL_ACCESS
|
2003-03-19 23:17:52 +00:00
|
|
|
};
|
|
|
|
|
2006-06-30 17:53:00 +00:00
|
|
|
static const INFORMATION_CLASS_INFO IoCompletionInfoClass[] =
|
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
/* IoCompletionBasicInformation */
|
2006-06-30 17:53:00 +00:00
|
|
|
ICI_SQ_SAME(sizeof(IO_COMPLETION_BASIC_INFORMATION), sizeof(ULONG), ICIF_QUERY),
|
2005-03-12 19:58:53 +00:00
|
|
|
};
|
2005-02-28 17:40:15 +00:00
|
|
|
|
2006-06-30 17:53:00 +00:00
|
|
|
/* PRIVATE FUNCTIONS *********************************************************/
|
2005-02-28 17:40:15 +00:00
|
|
|
|
2006-11-12 22:39:09 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
IopUnloadSafeCompletion(IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN PIRP Irp,
|
|
|
|
IN PVOID Context)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
2008-11-04 21:55:55 +00:00
|
|
|
PIO_UNLOAD_SAFE_COMPLETION_CONTEXT UnsafeContext = Context;
|
2006-11-12 22:39:09 +00:00
|
|
|
|
|
|
|
/* Reference the device object */
|
|
|
|
ObReferenceObject(UnsafeContext->DeviceObject);
|
|
|
|
|
|
|
|
/* Call the completion routine */
|
|
|
|
Status= UnsafeContext->CompletionRoutine(DeviceObject,
|
|
|
|
Irp,
|
|
|
|
UnsafeContext->Context);
|
|
|
|
|
|
|
|
/* Dereference the device object */
|
|
|
|
ObDereferenceObject(UnsafeContext->DeviceObject);
|
|
|
|
|
|
|
|
/* Free our context */
|
|
|
|
ExFreePool(UnsafeContext);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
VOID
|
2006-06-30 17:53:00 +00:00
|
|
|
NTAPI
|
2008-11-04 21:55:55 +00:00
|
|
|
IopFreeMiniPacket(PIOP_MINI_COMPLETION_PACKET Packet)
|
2005-05-13 05:41:24 +00:00
|
|
|
{
|
|
|
|
PKPRCB Prcb = KeGetCurrentPrcb();
|
|
|
|
PNPAGED_LOOKASIDE_LIST List;
|
2006-06-30 17:53:00 +00:00
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Use the P List */
|
2006-06-30 17:53:00 +00:00
|
|
|
List = (PNPAGED_LOOKASIDE_LIST)Prcb->
|
|
|
|
PPLookasideList[LookasideCompletionList].P;
|
2005-05-13 05:41:24 +00:00
|
|
|
List->L.TotalFrees++;
|
2006-06-30 17:53:00 +00:00
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Check if the Free was within the Depth or not */
|
|
|
|
if (ExQueryDepthSList(&List->L.ListHead) >= List->L.Depth)
|
|
|
|
{
|
|
|
|
/* Let the balancer know */
|
|
|
|
List->L.FreeMisses++;
|
2006-06-30 17:53:00 +00:00
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Use the L List */
|
2006-06-30 17:53:00 +00:00
|
|
|
List = (PNPAGED_LOOKASIDE_LIST)Prcb->
|
|
|
|
PPLookasideList[LookasideCompletionList].L;
|
2005-05-13 05:41:24 +00:00
|
|
|
List->L.TotalFrees++;
|
|
|
|
|
|
|
|
/* Check if the Free was within the Depth or not */
|
|
|
|
if (ExQueryDepthSList(&List->L.ListHead) >= List->L.Depth)
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
2005-05-13 05:41:24 +00:00
|
|
|
/* All lists failed, use the pool */
|
|
|
|
List->L.FreeMisses++;
|
|
|
|
ExFreePool(Packet);
|
2008-02-29 09:43:36 +00:00
|
|
|
return;
|
2005-05-13 05:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-30 17:53:00 +00:00
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* The free was within dhe Depth */
|
2010-07-16 00:34:26 +00:00
|
|
|
InterlockedPushEntrySList(&List->L.ListHead, (PSLIST_ENTRY)Packet);
|
2005-05-13 05:41:24 +00:00
|
|
|
}
|
|
|
|
|
2005-05-09 01:38:29 +00:00
|
|
|
VOID
|
2006-06-30 17:53:00 +00:00
|
|
|
NTAPI
|
2005-02-07 10:33:44 +00:00
|
|
|
IopDeleteIoCompletion(PVOID ObjectBody)
|
2003-03-19 23:17:52 +00:00
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
PKQUEUE Queue = ObjectBody;
|
|
|
|
PLIST_ENTRY FirstEntry;
|
|
|
|
PLIST_ENTRY CurrentEntry;
|
2005-05-13 05:41:24 +00:00
|
|
|
PIRP Irp;
|
2008-11-04 21:55:55 +00:00
|
|
|
PIOP_MINI_COMPLETION_PACKET Packet;
|
2005-03-12 19:58:53 +00:00
|
|
|
|
|
|
|
/* Rundown the Queue */
|
|
|
|
FirstEntry = KeRundownQueue(Queue);
|
2006-06-30 17:53:00 +00:00
|
|
|
if (FirstEntry)
|
|
|
|
{
|
|
|
|
/* Loop the packets */
|
2005-03-12 19:58:53 +00:00
|
|
|
CurrentEntry = FirstEntry;
|
2006-06-30 17:53:00 +00:00
|
|
|
do
|
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Get the Packet */
|
2006-06-30 17:53:00 +00:00
|
|
|
Packet = CONTAINING_RECORD(CurrentEntry,
|
2008-11-04 21:55:55 +00:00
|
|
|
IOP_MINI_COMPLETION_PACKET,
|
2006-06-30 17:53:00 +00:00
|
|
|
ListEntry);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Go to next Entry */
|
|
|
|
CurrentEntry = CurrentEntry->Flink;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Check if it's part of an IRP, or a separate packet */
|
2008-11-04 21:55:55 +00:00
|
|
|
if (Packet->PacketType == IopCompletionPacketIrp)
|
2005-05-13 05:41:24 +00:00
|
|
|
{
|
|
|
|
/* Get the IRP and free it */
|
|
|
|
Irp = CONTAINING_RECORD(Packet, IRP, Tail.Overlay.ListEntry);
|
|
|
|
IoFreeIrp(Irp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use common routine */
|
2008-11-04 21:55:55 +00:00
|
|
|
IopFreeMiniPacket(Packet);
|
2005-05-13 05:41:24 +00:00
|
|
|
}
|
2005-03-12 19:58:53 +00:00
|
|
|
} while (FirstEntry != CurrentEntry);
|
|
|
|
}
|
2003-03-19 23:17:52 +00:00
|
|
|
}
|
|
|
|
|
2006-06-30 17:53:00 +00:00
|
|
|
/* PUBLIC FUNCTIONS **********************************************************/
|
|
|
|
|
2004-06-23 21:42:50 +00:00
|
|
|
/*
|
2005-03-12 19:58:53 +00:00
|
|
|
* @implemented
|
2004-06-23 21:42:50 +00:00
|
|
|
*/
|
|
|
|
NTSTATUS
|
2006-06-30 17:53:00 +00:00
|
|
|
NTAPI
|
2005-03-12 19:58:53 +00:00
|
|
|
IoSetIoCompletion(IN PVOID IoCompletion,
|
|
|
|
IN PVOID KeyContext,
|
|
|
|
IN PVOID ApcContext,
|
|
|
|
IN NTSTATUS IoStatus,
|
|
|
|
IN ULONG_PTR IoStatusInformation,
|
|
|
|
IN BOOLEAN Quota)
|
2004-06-23 21:42:50 +00:00
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
PKQUEUE Queue = (PKQUEUE)IoCompletion;
|
2005-05-13 05:41:24 +00:00
|
|
|
PNPAGED_LOOKASIDE_LIST List;
|
|
|
|
PKPRCB Prcb = KeGetCurrentPrcb();
|
2008-11-04 21:55:55 +00:00
|
|
|
PIOP_MINI_COMPLETION_PACKET Packet;
|
2005-03-12 19:58:53 +00:00
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Get the P List */
|
2006-06-30 17:53:00 +00:00
|
|
|
List = (PNPAGED_LOOKASIDE_LIST)Prcb->
|
|
|
|
PPLookasideList[LookasideCompletionList].P;
|
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Try to allocate the Packet */
|
|
|
|
List->L.TotalAllocates++;
|
|
|
|
Packet = (PVOID)InterlockedPopEntrySList(&List->L.ListHead);
|
2006-06-30 17:53:00 +00:00
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Check if that failed, use the L list if it did */
|
|
|
|
if (!Packet)
|
|
|
|
{
|
|
|
|
/* Let the balancer know */
|
|
|
|
List->L.AllocateMisses++;
|
2006-06-30 17:53:00 +00:00
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Get L List */
|
2006-06-30 17:53:00 +00:00
|
|
|
List = (PNPAGED_LOOKASIDE_LIST)Prcb->
|
|
|
|
PPLookasideList[LookasideCompletionList].L;
|
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Try to allocate the Packet */
|
|
|
|
List->L.TotalAllocates++;
|
|
|
|
Packet = (PVOID)InterlockedPopEntrySList(&List->L.ListHead);
|
|
|
|
}
|
2006-06-30 17:53:00 +00:00
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Still failed, use pool */
|
|
|
|
if (!Packet)
|
|
|
|
{
|
|
|
|
/* Let the balancer know */
|
|
|
|
List->L.AllocateMisses++;
|
2006-06-30 17:53:00 +00:00
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Allocate from Nonpaged Pool */
|
|
|
|
Packet = ExAllocatePoolWithTag(NonPagedPool, sizeof(*Packet), IOC_TAG);
|
|
|
|
}
|
2006-06-30 17:53:00 +00:00
|
|
|
|
2005-05-13 05:41:24 +00:00
|
|
|
/* Make sure we have one by now... */
|
|
|
|
if (Packet)
|
|
|
|
{
|
|
|
|
/* Set up the Packet */
|
2008-11-04 21:55:55 +00:00
|
|
|
Packet->PacketType = IopCompletionPacketMini;
|
|
|
|
Packet->KeyContext = KeyContext;
|
|
|
|
Packet->ApcContext = ApcContext;
|
|
|
|
Packet->IoStatus = IoStatus;
|
|
|
|
Packet->IoStatusInformation = IoStatusInformation;
|
2005-05-13 05:41:24 +00:00
|
|
|
|
|
|
|
/* Insert the Queue */
|
|
|
|
KeInsertQueue(Queue, &Packet->ListEntry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-30 17:53:00 +00:00
|
|
|
/* Out of memory, fail */
|
2005-05-13 05:41:24 +00:00
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2005-03-12 19:58:53 +00:00
|
|
|
|
|
|
|
/* Return Success */
|
|
|
|
return STATUS_SUCCESS;
|
2004-06-23 21:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-11-12 22:39:09 +00:00
|
|
|
* @implemented
|
2004-06-23 21:42:50 +00:00
|
|
|
*/
|
|
|
|
NTSTATUS
|
2006-06-30 17:53:00 +00:00
|
|
|
NTAPI
|
2005-03-12 19:58:53 +00:00
|
|
|
IoSetCompletionRoutineEx(IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN PIRP Irp,
|
|
|
|
IN PIO_COMPLETION_ROUTINE CompletionRoutine,
|
|
|
|
IN PVOID Context,
|
|
|
|
IN BOOLEAN InvokeOnSuccess,
|
|
|
|
IN BOOLEAN InvokeOnError,
|
|
|
|
IN BOOLEAN InvokeOnCancel)
|
2004-06-23 21:42:50 +00:00
|
|
|
{
|
2006-11-12 22:39:09 +00:00
|
|
|
PIO_UNLOAD_SAFE_COMPLETION_CONTEXT UnloadContext;
|
|
|
|
|
|
|
|
/* Allocate the context */
|
|
|
|
UnloadContext = ExAllocatePoolWithTag(NonPagedPool,
|
|
|
|
sizeof(*UnloadContext),
|
2009-08-24 18:19:53 +00:00
|
|
|
'sUoI');
|
2006-11-12 22:39:09 +00:00
|
|
|
if (!UnloadContext) return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
|
|
|
|
/* Set up the context */
|
|
|
|
UnloadContext->DeviceObject = DeviceObject;
|
|
|
|
UnloadContext->Context = Context;
|
|
|
|
UnloadContext->CompletionRoutine = CompletionRoutine;
|
|
|
|
|
|
|
|
/* Now set the completion routine */
|
|
|
|
IoSetCompletionRoutine(Irp,
|
|
|
|
IopUnloadSafeCompletion,
|
|
|
|
UnloadContext,
|
|
|
|
InvokeOnSuccess,
|
|
|
|
InvokeOnError,
|
|
|
|
InvokeOnCancel);
|
|
|
|
return STATUS_SUCCESS;
|
2004-06-23 21:42:50 +00:00
|
|
|
}
|
|
|
|
|
1999-06-18 22:11:21 +00:00
|
|
|
NTSTATUS
|
2006-06-30 17:53:00 +00:00
|
|
|
NTAPI
|
2005-03-12 19:58:53 +00:00
|
|
|
NtCreateIoCompletion(OUT PHANDLE IoCompletionHandle,
|
2006-06-30 17:53:00 +00:00
|
|
|
IN ACCESS_MASK DesiredAccess,
|
|
|
|
IN POBJECT_ATTRIBUTES ObjectAttributes,
|
|
|
|
IN ULONG NumberOfConcurrentThreads)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
PKQUEUE Queue;
|
|
|
|
HANDLE hIoCompletionHandle;
|
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2009-08-26 17:31:02 +00:00
|
|
|
NTSTATUS Status;
|
2005-03-12 19:58:53 +00:00
|
|
|
PAGED_CODE();
|
2005-02-28 17:40:15 +00:00
|
|
|
|
2006-06-30 17:53:00 +00:00
|
|
|
/* Check if this was a user-mode call */
|
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
/* Wrap probing in SEH */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
|
|
|
/* Probe the handle */
|
2005-08-21 19:04:23 +00:00
|
|
|
ProbeForWriteHandle(IoCompletionHandle);
|
2006-06-30 17:53:00 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2005-03-12 19:58:53 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-03-12 19:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the Object */
|
|
|
|
Status = ObCreateObject(PreviousMode,
|
2005-05-15 17:59:33 +00:00
|
|
|
IoCompletionType,
|
2005-03-12 19:58:53 +00:00
|
|
|
ObjectAttributes,
|
|
|
|
PreviousMode,
|
|
|
|
NULL,
|
|
|
|
sizeof(KQUEUE),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(PVOID*)&Queue);
|
2006-06-30 17:53:00 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Initialize the Queue */
|
|
|
|
KeInitializeQueue(Queue, NumberOfConcurrentThreads);
|
|
|
|
|
|
|
|
/* Insert it */
|
|
|
|
Status = ObInsertObject(Queue,
|
|
|
|
NULL,
|
|
|
|
DesiredAccess,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&hIoCompletionHandle);
|
2006-06-30 17:53:00 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Protect writing the handle in SEH */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
|
|
|
/* Write the handle back */
|
2005-03-12 19:58:53 +00:00
|
|
|
*IoCompletionHandle = hIoCompletionHandle;
|
2006-06-30 17:53:00 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
|
|
|
/* Get the exception code */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2006-06-30 17:53:00 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-03-12 19:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Return Status */
|
|
|
|
return Status;
|
1998-10-05 04:00:59 +00:00
|
|
|
}
|
|
|
|
|
1999-06-18 22:11:21 +00:00
|
|
|
NTSTATUS
|
2006-06-30 17:53:00 +00:00
|
|
|
NTAPI
|
2005-03-12 19:58:53 +00:00
|
|
|
NtOpenIoCompletion(OUT PHANDLE IoCompletionHandle,
|
|
|
|
IN ACCESS_MASK DesiredAccess,
|
|
|
|
IN POBJECT_ATTRIBUTES ObjectAttributes)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
|
|
|
HANDLE hIoCompletionHandle;
|
2009-08-26 17:31:02 +00:00
|
|
|
NTSTATUS Status;
|
2005-03-12 19:58:53 +00:00
|
|
|
PAGED_CODE();
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-06-30 17:53:00 +00:00
|
|
|
/* Check if this was a user-mode call */
|
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
/* Wrap probing in SEH */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
|
|
|
/* Probe the handle */
|
2005-08-21 19:04:23 +00:00
|
|
|
ProbeForWriteHandle(IoCompletionHandle);
|
2006-06-30 17:53:00 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2005-03-12 19:58:53 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-03-12 19:58:53 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Open the Object */
|
|
|
|
Status = ObOpenObjectByName(ObjectAttributes,
|
2005-05-15 17:59:33 +00:00
|
|
|
IoCompletionType,
|
2005-03-12 19:58:53 +00:00
|
|
|
PreviousMode,
|
2006-05-25 20:50:58 +00:00
|
|
|
NULL,
|
2005-03-12 19:58:53 +00:00
|
|
|
DesiredAccess,
|
|
|
|
NULL,
|
|
|
|
&hIoCompletionHandle);
|
2006-06-30 17:53:00 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Protect writing the handle in SEH */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
|
|
|
/* Write the handle back */
|
2005-03-12 19:58:53 +00:00
|
|
|
*IoCompletionHandle = hIoCompletionHandle;
|
2006-06-30 17:53:00 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
|
|
|
/* Get the exception code */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2006-06-30 17:53:00 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-03-12 19:58:53 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
|
|
|
/* Return Status */
|
2005-03-12 19:58:53 +00:00
|
|
|
return Status;
|
1998-10-05 04:00:59 +00:00
|
|
|
}
|
|
|
|
|
1999-06-18 22:11:21 +00:00
|
|
|
NTSTATUS
|
2006-06-30 17:53:00 +00:00
|
|
|
NTAPI
|
2005-03-12 19:58:53 +00:00
|
|
|
NtQueryIoCompletion(IN HANDLE IoCompletionHandle,
|
|
|
|
IN IO_COMPLETION_INFORMATION_CLASS IoCompletionInformationClass,
|
|
|
|
OUT PVOID IoCompletionInformation,
|
|
|
|
IN ULONG IoCompletionInformationLength,
|
|
|
|
OUT PULONG ResultLength OPTIONAL)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
PKQUEUE Queue;
|
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2009-08-26 17:31:02 +00:00
|
|
|
NTSTATUS Status;
|
2005-03-12 19:58:53 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Check buffers and parameters */
|
2005-10-10 13:03:55 +00:00
|
|
|
Status = DefaultQueryInfoBufferCheck(IoCompletionInformationClass,
|
|
|
|
IoCompletionInfoClass,
|
2006-06-30 17:53:00 +00:00
|
|
|
sizeof(IoCompletionInfoClass) /
|
|
|
|
sizeof(IoCompletionInfoClass[0]),
|
2005-10-10 13:03:55 +00:00
|
|
|
IoCompletionInformation,
|
|
|
|
IoCompletionInformationLength,
|
|
|
|
ResultLength,
|
2010-01-13 22:35:43 +00:00
|
|
|
NULL,
|
2005-10-10 13:03:55 +00:00
|
|
|
PreviousMode);
|
2006-06-30 17:53:00 +00:00
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
2005-03-12 19:58:53 +00:00
|
|
|
|
|
|
|
/* Get the Object */
|
|
|
|
Status = ObReferenceObjectByHandle(IoCompletionHandle,
|
2003-03-19 23:17:52 +00:00
|
|
|
IO_COMPLETION_QUERY_STATE,
|
2005-05-15 17:59:33 +00:00
|
|
|
IoCompletionType,
|
2005-03-12 19:58:53 +00:00
|
|
|
PreviousMode,
|
2003-03-19 23:17:52 +00:00
|
|
|
(PVOID*)&Queue,
|
|
|
|
NULL);
|
2006-06-30 17:53:00 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Protect write in SEH */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Return Info */
|
2006-06-30 17:53:00 +00:00
|
|
|
((PIO_COMPLETION_BASIC_INFORMATION)IoCompletionInformation)->
|
|
|
|
Depth = KeReadStateQueue(Queue);
|
|
|
|
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Return Result Length if needed */
|
2006-06-30 17:53:00 +00:00
|
|
|
if (ResultLength)
|
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
*ResultLength = sizeof(IO_COMPLETION_BASIC_INFORMATION);
|
|
|
|
}
|
2006-06-30 17:53:00 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
|
|
|
/* Get exception code */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2006-06-30 17:53:00 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2008-10-31 13:16:01 +00:00
|
|
|
|
|
|
|
/* Dereference the queue */
|
|
|
|
ObDereferenceObject(Queue);
|
2005-03-12 19:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return Status */
|
|
|
|
return Status;
|
|
|
|
}
|
2005-02-28 17:40:15 +00:00
|
|
|
|
1999-06-18 22:11:21 +00:00
|
|
|
NTSTATUS
|
2006-06-30 17:53:00 +00:00
|
|
|
NTAPI
|
|
|
|
NtRemoveIoCompletion(IN HANDLE IoCompletionHandle,
|
|
|
|
OUT PVOID *KeyContext,
|
|
|
|
OUT PVOID *ApcContext,
|
2005-03-12 19:58:53 +00:00
|
|
|
OUT PIO_STATUS_BLOCK IoStatusBlock,
|
2006-06-30 17:53:00 +00:00
|
|
|
IN PLARGE_INTEGER Timeout OPTIONAL)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
LARGE_INTEGER SafeTimeout;
|
|
|
|
PKQUEUE Queue;
|
2008-11-04 21:55:55 +00:00
|
|
|
PIOP_MINI_COMPLETION_PACKET Packet;
|
2005-03-12 19:58:53 +00:00
|
|
|
PLIST_ENTRY ListEntry;
|
|
|
|
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
|
2009-08-26 17:31:02 +00:00
|
|
|
NTSTATUS Status;
|
2006-06-30 17:53:00 +00:00
|
|
|
PIRP Irp;
|
|
|
|
PVOID Apc, Key;
|
|
|
|
IO_STATUS_BLOCK IoStatus;
|
2005-03-12 19:58:53 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
2006-06-30 17:53:00 +00:00
|
|
|
/* Check if the call was from user mode */
|
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
/* Protect probes in SEH */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
|
|
|
/* Probe the pointers */
|
|
|
|
ProbeForWritePointer(KeyContext);
|
|
|
|
ProbeForWritePointer(ApcContext);
|
|
|
|
|
|
|
|
/* Probe the I/O Status Block */
|
2006-08-30 21:45:17 +00:00
|
|
|
ProbeForWriteIoStatusBlock(IoStatusBlock);
|
2006-06-30 17:53:00 +00:00
|
|
|
if (Timeout)
|
|
|
|
{
|
|
|
|
/* Probe and capture the timeout */
|
2005-08-21 19:04:23 +00:00
|
|
|
SafeTimeout = ProbeForReadLargeInteger(Timeout);
|
2005-03-12 19:58:53 +00:00
|
|
|
Timeout = &SafeTimeout;
|
|
|
|
}
|
2006-06-30 17:53:00 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2005-03-12 19:58:53 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-03-12 19:58:53 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Open the Object */
|
|
|
|
Status = ObReferenceObjectByHandle(IoCompletionHandle,
|
2003-03-19 23:17:52 +00:00
|
|
|
IO_COMPLETION_MODIFY_STATE,
|
2005-05-15 17:59:33 +00:00
|
|
|
IoCompletionType,
|
2005-03-12 19:58:53 +00:00
|
|
|
PreviousMode,
|
2003-03-19 23:17:52 +00:00
|
|
|
(PVOID*)&Queue,
|
|
|
|
NULL);
|
2006-06-30 17:53:00 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Remove queue */
|
|
|
|
ListEntry = KeRemoveQueue(Queue, PreviousMode, Timeout);
|
|
|
|
|
|
|
|
/* If we got a timeout or user_apc back, return the status */
|
2010-01-13 22:35:43 +00:00
|
|
|
if (((NTSTATUS)(ULONG_PTR)ListEntry == STATUS_TIMEOUT) ||
|
|
|
|
((NTSTATUS)(ULONG_PTR)ListEntry == STATUS_USER_APC))
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
|
|
|
/* Set this as the status */
|
2010-01-13 22:35:43 +00:00
|
|
|
Status = (NTSTATUS)(ULONG_PTR)ListEntry;
|
2006-06-30 17:53:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Get the Packet Data */
|
2006-06-30 17:53:00 +00:00
|
|
|
Packet = CONTAINING_RECORD(ListEntry,
|
2008-11-04 21:55:55 +00:00
|
|
|
IOP_MINI_COMPLETION_PACKET,
|
2006-06-30 17:53:00 +00:00
|
|
|
ListEntry);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-06-22 20:30:49 +00:00
|
|
|
/* Check if this is piggybacked on an IRP */
|
2008-11-04 21:55:55 +00:00
|
|
|
if (Packet->PacketType == IopCompletionPacketIrp)
|
2006-06-22 20:30:49 +00:00
|
|
|
{
|
|
|
|
/* Get the IRP */
|
2006-06-30 17:53:00 +00:00
|
|
|
Irp = CONTAINING_RECORD(ListEntry,
|
|
|
|
IRP,
|
|
|
|
Tail.Overlay.ListEntry);
|
2005-03-12 19:58:53 +00:00
|
|
|
|
2006-06-30 17:53:00 +00:00
|
|
|
/* Save values */
|
|
|
|
Key = Irp->Tail.CompletionKey;
|
|
|
|
Apc = Irp->Overlay.AsynchronousParameters.UserApcContext;
|
|
|
|
IoStatus = Irp->IoStatus;
|
2006-06-22 20:30:49 +00:00
|
|
|
|
2006-06-30 17:53:00 +00:00
|
|
|
/* Free the IRP */
|
2006-06-22 20:30:49 +00:00
|
|
|
IoFreeIrp(Irp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-30 17:53:00 +00:00
|
|
|
/* Save values */
|
2008-11-04 21:55:55 +00:00
|
|
|
Key = Packet->KeyContext;
|
|
|
|
Apc = Packet->ApcContext;
|
|
|
|
IoStatus.Status = Packet->IoStatus;
|
|
|
|
IoStatus.Information = Packet->IoStatusInformation;
|
2005-03-12 19:58:53 +00:00
|
|
|
|
2006-06-30 17:53:00 +00:00
|
|
|
/* Free the packet */
|
2008-11-04 21:55:55 +00:00
|
|
|
IopFreeMiniPacket(Packet);
|
2006-06-22 20:30:49 +00:00
|
|
|
}
|
2006-06-30 17:53:00 +00:00
|
|
|
|
|
|
|
/* Enter SEH to write back the values */
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
|
|
|
/* Write the values to caller */
|
|
|
|
*ApcContext = Apc;
|
|
|
|
*KeyContext = Key;
|
|
|
|
*IoStatusBlock = IoStatus;
|
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
2006-06-30 17:53:00 +00:00
|
|
|
{
|
|
|
|
/* Get the exception code */
|
2008-11-24 13:40:26 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2006-06-30 17:53:00 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2005-03-12 19:58:53 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Dereference the Object */
|
|
|
|
ObDereferenceObject(Queue);
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Return status */
|
|
|
|
return Status;
|
1998-10-05 04:00:59 +00:00
|
|
|
}
|
|
|
|
|
1999-06-18 22:11:21 +00:00
|
|
|
NTSTATUS
|
2006-06-30 17:53:00 +00:00
|
|
|
NTAPI
|
2005-03-12 19:58:53 +00:00
|
|
|
NtSetIoCompletion(IN HANDLE IoCompletionPortHandle,
|
|
|
|
IN PVOID CompletionKey,
|
|
|
|
IN PVOID CompletionContext,
|
|
|
|
IN NTSTATUS CompletionStatus,
|
|
|
|
IN ULONG CompletionInformation)
|
1998-10-05 04:00:59 +00:00
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
PKQUEUE Queue;
|
|
|
|
PAGED_CODE();
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Get the Object */
|
|
|
|
Status = ObReferenceObjectByHandle(IoCompletionPortHandle,
|
2003-03-19 23:17:52 +00:00
|
|
|
IO_COMPLETION_MODIFY_STATE,
|
2005-05-15 17:59:33 +00:00
|
|
|
IoCompletionType,
|
2005-03-12 19:58:53 +00:00
|
|
|
ExGetPreviousMode(),
|
2003-03-19 23:17:52 +00:00
|
|
|
(PVOID*)&Queue,
|
|
|
|
NULL);
|
2006-06-30 17:53:00 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Set the Completion */
|
2005-05-09 01:38:29 +00:00
|
|
|
Status = IoSetIoCompletion(Queue,
|
|
|
|
CompletionKey,
|
2005-03-12 19:58:53 +00:00
|
|
|
CompletionContext,
|
2005-05-09 01:38:29 +00:00
|
|
|
CompletionStatus,
|
|
|
|
CompletionInformation,
|
2005-03-12 19:58:53 +00:00
|
|
|
TRUE);
|
2006-06-30 17:53:00 +00:00
|
|
|
|
|
|
|
/* Dereference the object */
|
2005-03-12 19:58:53 +00:00
|
|
|
ObDereferenceObject(Queue);
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-03-12 19:58:53 +00:00
|
|
|
/* Return status */
|
|
|
|
return Status;
|
1998-10-05 04:00:59 +00:00
|
|
|
}
|