Synchronous I/O Responses
Although the Windows operating system is designed for asynchronous I/O, most applications issue synchronous I/O requests. Drivers can also issue both synchronous and asynchronous requests and can respond to requests either synchronously or asynchronously.
To determine whether a request was completed synchronously or asynchronously, a driver checks the status returned by IoCallDriver, as the following code sample shows:
KEVENT event;
KeInitializeEvent(&event, NotificationEvent, FALSE);
IoCopyCurrentIrpStackLocationToNext(Irp);
IoSetCompletionRoutine(Irp,
CatchIrpRoutine,
&event,
TRUE,
TRUE,
TRUE
);
status = IoCallDriver(DeviceObject, Irp);
//
// Check for synchronous or asynchronous completion.
//
if (status == STATUS_PENDING) {
// Code to handle asynchronous response
// omitted for now.
}
return status;
The driver initializes an event, sets the I/O stack location, sets an IoCompletion routine, and calls IoCallDriver to forward the IRP. The status returned by IoCallDriver indicates whether lower drivers are handling the IRP synchronously or asynchronously. If the request is being handled asynchronously, IoCallDriver returns STATUS_PENDING. If lower drivers respond synchronously, IoCallDriver returns the completion status that was returned by the next lower driver. As the code sample shows, the driver simply returns that same completion status.
When an IRP is completed synchronously, the driver returns the IRP’s completion status from its dispatch routine. Drivers above it in the device stack can get the status in either of two ways:
· In the dispatch routine, from the value returned by IoCallDriver.
· In the IoCompletion routine, from the IoStatus.Status field of the IRP.
When the I/O Manager calls the driver’s IoCompletion routine, the driver owns the IRP and thus can access the IoStatus.Status field. If the driver does not set an IoCompletion routine, it does not own the IRP after it calls IoCallDriver and therefore must not access fields within the IRP.
Figure 4 shows the two ways a driver or application can get the status of an IRP. For ease of explanation, the figure shows the IoCompletion routines in the same I/O stack location as the parameters with which they are called, instead of one location lower.
Figure 4. Status returned by IoCallDriver and available to IoCompletion routine
On the left side of Figure 4, the IoCallDriver routine returns the completion status reported by the next lower driver. On the right, the IoCompletion routines read the status from the IoStatus.Status field of the IRP. If the IRP completes synchronously, the IoCompletion routine for each driver is called before IoCallDriver returns, so the status value is available to the IoCompletion routine before it is available to the dispatch routine.
Figure 4 shows that driver C returns STATUS_SUCCESS, driver B returns STATUS_RETRY, and driver A returns STATUS_ERROR. The final status of the IRP is available only to the initiator of the request; other drivers can read only the status returned by the next-lower driver.