Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Before Posting...
Please check out the Community Guidelines in the Announcements and Administration Category.

Reading contiguous data on disk and passing it interleaved MDL

craniumratcraniumrat Member Posts: 7
edited September 2019 in NTDEV


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 */


  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 7,631

    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


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


    Peter Viscarola

  • craniumratcraniumrat Member Posts: 7

    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:

  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 7,631

    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 Viscarola

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Upcoming OSR Seminars
Kernel Debugging 30 Mar 2020 OSR Seminar Space
Developing Minifilters 20 Apr 2020 OSR Seminar Space & ONLINE
Writing WDF Drivers 11 May 2020 OSR Seminar Space & ONLINE
Internals & Software Drivers 28 Sept 2020 Dulles, VA