Reading contiguous data on disk and passing it interleaved MDL

Hi,

I have the following scenario: In my incoming Read callback, I have an incoming MDL, but the data on the disk is supposed to be read in to different parts of the incoming MDL. Rather than creating a bunch of IoBuildPartialMdls and sending a bunch of individual reads to the disk, is it possible to create a new MDL and chaining a bunch of IoBuildPartialMdl’s of the original MDL and send it down to disk. Pseudo-code below:

NTSTATUS ReadWrite(_In_ PDEVICE_OBJECT deviceObject, PIRP irp)
{
    PMDL incomingMdl = irp->MdlAddress;
    PMDL toDiskMdl = NULL;
    UINT32 toDiskLength = 0;
    PMDL lastChainMdl = NULL;
    PVOID virtualAddress = (INT_PTR)MmGetMdlVirtualAddress(incomingMdl);

    for (int i = 0; i < count; ++i) 
    {
       if (i == 0)
       {
            toDiskMdl = IoAllocateMdl(incomingMdl, incomingMdlLength, FALSE, FALSE, NULL);
            latChainMdl = toDiskMdl;
       }
 
       partialBufferOffset = /* calculate offset based on i */;
       partialBufferLength = /* calculate length based on i */;
       PMDL partialMdl = NULL;
       IoBuildPartialMdl(toDiskMdl, partialMdl, (INT_PTR)virtualAddress + partialBufferOffset, partialBufferLength);
       lastChainMdl->Next = partialMdl;
       lastChainMdl = partialMdl;
       toDiskLength += partialBufferLength;
    }

    /* Now that toDiskMdl is created, send an IRP down to disk with toDiskMdl and toDiskLength */
    /* Complete the incoming IRP in the completion routine */
}

is it possible to create a new MDL and chaining a bunch of IoBuildPartialMdl’s of the original MDL and send it down to disk

No.

“Chained MDLs” are not uniformly or universally implemented in the storage stack.

Peter

I was doing this based off the statement:

“Drivers can traverse the MDL chain by using the Next member of each MDL to access the next MDL in the chain. Drivers can manually insert MDLs into the chain by updating the Next members.”

from this page: https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/using-mdls.

I was doing this based off the statement

That statement is correct. It is a general statement of what drivers “can” do.

It does not, however, say anything at all about whether chained MDLs are supported in specifics stacks in the operating system.

Chained MDLs were initially designed for use in the Network stack. Support for chained MDLs has been implemented rather raggedly throughout other stacks in the system over the years, as particular devs have seen the need, had the opportunity, or simply “felt like it.”

You’re not the first person to wonder, nor are you the first person to wish this facility was uniformly implemented throughout the OS. But, alas…

Peter