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

Sept/Oct 2019 Issue of The NT Insider available


Download PDF here: http://insider.osr.com/2019/ntinsider_2019_01.pdf

It’s a particularly BIG issue, too: 40 pages of technical goodness, ranging from WDF to Minifilters. Check it out.
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 4 in NTDEV

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

Comments

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

    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

    Peter Viscarola
    OSR
    @OSRDrivers

  • 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: https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/using-mdls.

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

    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

    Peter Viscarola
    OSR
    @OSRDrivers

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
Writing WDF Drivers 21 Oct 2019 OSR Seminar Space & ONLINE
Internals & Software Drivers 18 Nov 2019 Dulles, VA
Kernel Debugging 30 Mar 2020 OSR Seminar Space
Developing Minifilters 27 Apr 2020 OSR Seminar Space & ONLINE