Introduction to Cancel Routines
Any driver in which IRPs can be held in a pending state for an indefinite interval must have one or more Cancel routines. For example, a keyboard driver might wait indefinitely for a user to press a key. Conversely, if a driver will never queue more IRPs than it can complete in five minutes, it probably does not need a Cancel routine.
Suppose a user-mode thread makes an I/O request, which is queued by a highest-level device driver's dispatch routine, and the requesting thread is terminated while the IRP is queued. IRPs queued on behalf of a terminated thread should be canceled. Consequently, the driver must set a driver-supplied Cancel routine in each IRP that it queues.
A driver that creates associated IRPs must cancel them when the master IRP is canceled. Because associated IRPs are not associated with a requesting thread, the master IRP's Cancel routine is responsible for canceling any associated IRPs when the master IRP is canceled.
The number of Cancel routines any driver has depends on the driver's design. In general, a driver should have a Cancel routine for each stage in its I/O processing at which an IRP might be held in a pending state for an indefinite interval. Such pending IRPs are said to be held in a cancelable state.
Consider the following design guidelines:
The highest-level driver in a chain of layered drivers must have at least one Cancel routine if it queues IRPs or otherwise holds IRPs in a cancelable state. It can have more than one Cancel routine, if necessary.
Lower-level drivers in which IRPs can be held in a cancelable state for relatively long intervals also should have one or more Cancel routines.
If a driver manages its own internal queues of IRPs, it should have a separate Cancel routine for each of its queues.
Some highest-level drivers for interactive devices, such as keyboard, mouse, sound, parallel class and serial drivers, must have Cancel routines. Some lower-level drivers, such as a parallel port driver that holds IRPs queued for some number of higher-level class drivers for relatively long intervals, also should have Cancel routines.
Mass-storage device drivers, along with intermediate drivers layered over them, are unlikely to have Cancel routines. It is the responsibility of a file system driver to handle the cancellation of file I/O requests, while the IRPs input to lower-level mass-storage drivers are usually processed to completion too quickly to be cancelable.