IO Request Packets (IRPs)

< BACK  NEXT >
[oR]

I/O Request Packets (IRPs)

Almost all I/O under Windows 2000 is packet-driven. Each separate I/O transaction is described by a work order that tells the driver what to do and tracks the progress of the request through the I/O subsystem. These work orders take the form of a data structure called an I/O Request Packet (IRP), and this section describes their use and purpose.

  1. With each user-mode request for I/O, the I/O Manager allocates an IRP from nonpaged system memory. Based on the file handle and I/O function requested by the user, the I/O Manager passes the IRP to the appropriate driver dispatch routine.

  2. The dispatch routine checks the parameters of the request, and if valid, passes the IRP to the driver's Start I/O routine.

  3. The Start I/O routine uses the contents of the IRP to begin a device operation.

  4. When the operation is complete, the driver's DpcForIsr routine stores a final status code in the IRP and returns it to the I/O Manager.

  5. The I/O Manager uses the information in the IRP to complete the request and send the user the final status.

The procedure just described is, in fact, a simplified model of IRP processing. It only applies in a flat driver model. When drivers are layered, one upon the other, the procedure is more complex. A single IRP may travel through several layers of drivers before the request is completed. Additionally, higher-level drivers can create new IRPs and disperse them to other drivers.

Layout of an IRP

An IRP is a variable-sized structure allocated from nonpaged pool. Figure 4.1 shows that an IRP has two sections.

Figure 4.1. Structure of an IRP.
graphics/04fig01.gif
  • A header area containing general bookkeeping information

  • One or more parameter blocks called I/O stack locations

IRP HEADER

This area of the IRP holds various pieces of information about the overall I/O request. Some parts of the header are directly accessible to the driver, while other pieces are the exclusive property of the I/O Manager. Table 4.1 lists the fields in the header that a driver is allowed to touch.

The IoStatus member holds the final status of the I/O operation. When a driver is ready to complete the processing of an IRP, it sets this Status field of this block to a STATUS_XXX value. At the same time, a driver should set the information field of the status block either to 0 (if there is an error) or to a function code-specific value (for example, the number of bytes transferred).

The AssociatedIrp.SystemBuffer, MdlAddress, and UserBuffer fields play various roles in managing the driver's access to data buffers. Later chapters explain how to use these fields if a driver performs either Buffered or direct I/O.

I/O STACK LOCATIONS

The main purpose of an I/O stack location is to hold function code and parameters of an I/O request. By examining the MajorFunction field of the stack locations, a driver can decide what operation to perform and how to interpret the Parameters union. Table 4.2 describes some of the commonly used members of an I/O stack location.

For requests sent directly to a lowest-level driver, the corresponding IRP has only one I/O stack location. For requests sent to a higher-level driver, the I/O Manager creates an IRP with separate I/O stack locations for each driver layer. In other words, the size of an I/O stack is really the number of I/O layers that participate in an I/O request. Every driver in the hierarchy is allowed to touch only its own stack location. If a driver chooses to call a lower-layer driver, it must ensure that a new stack location has been correctly created beneath it.

Table 4.1. Externally Visible Fields of an IRP Header
IRP Header Fields
Field Description
IO_STATUS_BLOCK IoStatus Contains status of the I/O request
PVOID AssociatedIrp.SystemBuffer Points to a system space buffer if device per- forms buffered I/O
PMDL MdlAddress Points to a Memory Descriptor List for a user- space buffer if device performs direct I/O
PVOID UserBuffer User-space address of I/O buffer
BOOLEAN Cancel Indicates the IRP has been canceled

Table 4.2. Selected Contents of IRP Stack Location
IO_STACK_LOCATION, *PIO_STACK_LOCATION
Field Contents
UCHAR MajorFunction IRP_MJ_XXX function specifying the operation
UCHAR MinorFunction Used by file system and SCSI drivers
union Parameters Typed union keyed to MajorFunction code
struct Read Parameters for IRP_MJ_READ
ULONG Length
ULONG Key
LARGE_INTEGER ByteOffset
struct Write Parameters for IRP_MJ_WRITE
ULONG Length
ULONG Key
LARGE_INTEGER ByteOffset
struct DeviceIoControl Parameters for IRP_MJ_DEVICE_CONTROL
ULONG OutputBufferLength
ULONG InputBufferLength
ULONG IoControlCode
PVOID Type3InputBuffer
struct Others Available to driver
PVOID Argument1-Argument4
PDEVICE_OBJECT DeviceObject Target device for this I/O request
PFILE_OBJECT FileObject File object for this request, if any

When a driver passes an IRP to a lower-level driver, the I/O Manager automatically pushes the I/O stack-pointer so that it points at the I/O stack location belonging to the lower driver. When the lower driver releases the IRP, the I/O stack pointer is popped so that it again points to the stack location of the higher driver. Chapter 15 explains in detail how to work with this mechanism.

Manipulating IRPs

Some IRP access functions operate only on the IRP header. Others deal specifically with the IRP's I/O stack locations. It is important to know whether an access function needs the pointer to the entire IRP or a pointer to an IRP stack location. The following sections describe each group of access functions.

Table 4.3. Functions that Work with the Whole IRP
IRP Access Functions
Function Description Called by...
IoStartPacket Sends IRP to Start I/O routine Dispatch
IoCompleteRequest Indicates that all processing is done DpcForIsr
IoStartNextPacket Sends next IRP to Start I/O DpcForIsr
IoCallDriver Sends IRP to another driver Dispatch
IoAllocateIrp Requests additional IRP Dispatch
IoFreeIrp Releases driver-allocated IRP I/O Completion

IRPS AS A WHOLE

The I/O Manager exports a variety of functions that work with IRPs. Table 4.3 lists the most common ones. Later chapters explain how to use them.

IRP STACK LOCATIONS

The I/O Manager also provides several functions that drivers can use to access an IRP's stack locations. These functions are listed in Table 4. 4.

Table 4.4. IO_STACK_LOCATION Access Functions
IO_STACK_LOCATION Functions
Function Description Called by...
IoGetCurrentIrpStackLocation Gets pointer to caller's stack slot (Various)
IoMarkIrpPending Marks caller's stack slot as as needing further processing Dispatch
IoGetNextIrpStackLocation Gets pointer to stack slot for next lower driver Dispatch
IoSetNextIrpStackLocation Pushes the I/O stack pointer one location Dispatch
IoSetCompleteRoutine Attaches I/O Completion routine to the next lower driver's I/O stack slot Dispatch

< BACK  NEXT >


The Windows 2000 Device Driver Book(c) A Guide for Programmers
The Windows 2000 Device Driver Book: A Guide for Programmers (2nd Edition)
ISBN: 0130204315
EAN: 2147483647
Year: 2000
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net