/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*-
 * ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
 * Editor Settings: expandtabs and use 4 spaces for indentation */

/*
 * Copyright Likewise Software
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.  You should have received a copy of the GNU General
 * Public License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * LIKEWISE SOFTWARE MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING
 * TERMS AS WELL.  IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT
 * WITH LIKEWISE SOFTWARE, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE
 * TERMS OF THAT SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE GNU
 * GENERAL PUBLIC LICENSE, NOTWITHSTANDING THE ABOVE NOTICE.  IF YOU
 * HAVE QUESTIONS, OR WISH TO REQUEST A COPY OF THE ALTERNATE LICENSING
 * TERMS OFFERED BY LIKEWISE SOFTWARE, PLEASE CONTACT LIKEWISE SOFTWARE AT
 * license@likewisesoftware.com
 */

/*
 * Copyright (C) Likewise Software. All rights reserved.
 *
 * Module Name:
 *
 *        read.c
 *
 * Abstract:
 *
 *        Likewise IO (LWIO) - SRV
 *
 *        Protocols API - SMBV2
 *
 *        Read
 *
 * Authors: Krishna Ganugapati (kganugapati@likewise.com)
 *          Sriram Nambakam (snambakam@likewise.com)
 *
 *
 */

#include "includes.h"

static
NTSTATUS
SrvBuildReadState_SMB_V2(
    IN PSMB2_READ_REQUEST_HEADER pRequestHeader,
    IN PLWIO_SRV_FILE_2 pFile,
    OUT PSRV_READ_STATE_SMB_V2*   ppReadState
    );

static
NTSTATUS
SrvBuildZctReadState_SMB_V2(
    PSRV_READ_STATE_SMB_V2 pReadState,
    PSRV_EXEC_CONTEXT pExecContext
    );

static
NTSTATUS
SrvAttemptReadIo_SMB_V2(
    PSRV_READ_STATE_SMB_V2 pReadState,
    PSRV_EXEC_CONTEXT pExecContext
    );

static
NTSTATUS
SrvSendZctReadResponse_SMB_V2(
    PSRV_READ_STATE_SMB_V2 pReadState,
    PSRV_EXEC_CONTEXT pExecContext
    );

static
NTSTATUS
SrvCompleteZctRead_SMB_V2(
    PSRV_READ_STATE_SMB_V2 pReadState,
    PSRV_EXEC_CONTEXT pExecContext
    );

static
VOID
SrvExecuteReadSendZctCB_SMB_V2(
    IN PVOID pContext,
    IN NTSTATUS Status
    );

static
VOID
SrvFreeReadStateHandle_SMB_V2(
    HANDLE hReadState
    );

static
VOID
SrvFreeReadState_SMB_V2(
    PSRV_READ_STATE_SMB_V2 pReadState
    );

static
NTSTATUS
SrvBuildReadResponse_SMB_V2(
    PSRV_EXEC_CONTEXT pExecContext
    );

////////////////////////////////////////////////////////////////////////

NTSTATUS
SrvProcessRead_SMB_V2(
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                   ntStatus     = STATUS_SUCCESS;
    PLWIO_SRV_CONNECTION       pConnection  = pExecContext->pConnection;
    PSRV_EXEC_CONTEXT_SMB_V2   pCtxSmb2     = pExecContext->pProtocolContext;
    PSRV_READ_STATE_SMB_V2     pReadState   = pCtxSmb2->hState;
    PLWIO_SRV_SESSION_2        pSession     = NULL;
    PLWIO_SRV_TREE_2           pTree        = NULL;
    PLWIO_SRV_FILE_2           pFile        = NULL;
    PLWIO_ASYNC_STATE          pAsyncState  = NULL;

    if (pReadState == NULL)
    {
        ULONG                     iMsg           = pCtxSmb2->iMsg;
        PSRV_MESSAGE_SMB_V2       pSmbRequest    = &pCtxSmb2->pRequests[iMsg];
        PSMB2_READ_REQUEST_HEADER pRequestHeader = NULL; // Do not free

        ntStatus = SrvConnection2FindSession_SMB_V2(
                        pCtxSmb2,
                        pConnection,
                        pSmbRequest->pHeader->ullSessionId,
                        &pSession);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvSetStatSession2Info(pExecContext, pSession);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvSession2FindTree_SMB_V2(
                        pCtxSmb2,
                        pSession,
                        pSmbRequest->pHeader->ulTid,
                        &pTree);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SMB2UnmarshalReadRequest(pSmbRequest, &pRequestHeader);
        BAIL_ON_NT_STATUS(ntStatus);

        SRV_LOG_DEBUG(
            pExecContext->pLogContext,
            SMB_PROTOCOL_VERSION_2,
            pSmbRequest->pHeader->command,
            "Read request params: "
            "command(%u),uid(%llu),cmd-seq(%llu),pid(%u),tid(%u),"
            "credits(%u),flags(0x%x),chain-offset(%u),"
            "file-id(persistent:0x%x,volatile:0x%x),"
            "channel(%u),channel-info-length(%u),channel-info-offset(%u),"
            "minimum-count(%u),data-length(%u),data-offset(%llu)",
            pSmbRequest->pHeader->command,
            (long long)pSmbRequest->pHeader->ullSessionId,
            (long long)pSmbRequest->pHeader->ullCommandSequence,
            pSmbRequest->pHeader->ulPid,
            pSmbRequest->pHeader->ulTid,
            pSmbRequest->pHeader->Credits,
            pSmbRequest->pHeader->ulFlags,
            pSmbRequest->pHeader->ulChainOffset,
            (long long)pRequestHeader->fid.ullPersistentId,
            (long long)pRequestHeader->fid.ullVolatileId,
            pRequestHeader->ulChannel,
            pRequestHeader->usReadChannelInfoLength,
            pRequestHeader->usReadChannelInfoOffset,
            pRequestHeader->ulMinimumCount,
            pRequestHeader->ulDataLength,
            (long long)pRequestHeader->ullFileOffset);

        ntStatus = SrvTree2FindFile_SMB_V2(
                        pCtxSmb2,
                        pTree,
                        &pRequestHeader->fid,
                        &pFile);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvConnection2CreateAsyncState(
                       pConnection,
                       COM2_READ,
                       pSmbRequest->pHeader->ullCommandSequence,
                       &SrvCancelExecContextHandle,
                       &SrvReleaseExecContextHandle,
                       &pAsyncState);
        BAIL_ON_NT_STATUS(ntStatus);

        pExecContext->ullAsyncId = pAsyncState->ullAsyncId;

        ntStatus = SrvBuildReadState_SMB_V2(
                        pRequestHeader,
                        pFile,
                        &pReadState);
        BAIL_ON_NT_STATUS(ntStatus);

        pCtxSmb2->hState = pReadState;
        pCtxSmb2->pfnStateRelease = &SrvFreeReadStateHandle_SMB_V2;

        pAsyncState->hAsyncState = SrvAcquireExecContext(pExecContext);
    }

    switch (pReadState->stage)
    {
        case SRV_READ_STAGE_SMB_V2_INITIAL:

            pReadState->Offset = pReadState->pRequestHeader->ullFileOffset;

            pReadState->ulBytesToRead =
                pReadState->pRequestHeader->ulDataLength;

            ntStatus = SrvBuildZctReadState_SMB_V2(pReadState, pExecContext);
            BAIL_ON_NT_STATUS(ntStatus);

            pReadState->stage = SRV_READ_STAGE_SMB_V2_ATTEMPT_READ;

            // Intentional fall through

        case SRV_READ_STAGE_SMB_V2_ATTEMPT_READ:

            ntStatus = SrvAttemptReadIo_SMB_V2(pReadState, pExecContext);
            BAIL_ON_NT_STATUS(ntStatus);

            pReadState->stage = SRV_READ_STAGE_SMB_V2_ATTEMPT_READ_COMPLETED;

            // intentional fall through

        case SRV_READ_STAGE_SMB_V2_ATTEMPT_READ_COMPLETED:

            ntStatus = pExecContext->ioStatusBlock.Status;
            BAIL_ON_NT_STATUS(ntStatus);

            pReadState->ulBytesRead =
                pExecContext->ioStatusBlock.BytesTransferred;
            if (pReadState->ulBytesRead <
                            pReadState->pRequestHeader->ulMinimumCount)
            {
                ntStatus = STATUS_END_OF_FILE;
                BAIL_ON_NT_STATUS(ntStatus);
            }

            pReadState->stage = SRV_READ_STAGE_SMB_V2_BUILD_RESPONSE;

            // intentional fall through

        case SRV_READ_STAGE_SMB_V2_BUILD_RESPONSE:

            ntStatus = SrvBuildReadResponse_SMB_V2(pExecContext);
            BAIL_ON_NT_STATUS(ntStatus);

            if (pReadState->pZct)
            {
                ntStatus = SrvSendZctReadResponse_SMB_V2(
                                pReadState,
                                pExecContext);
                BAIL_ON_NT_STATUS(ntStatus);
            }

            pReadState->stage = SRV_READ_STAGE_SMB_V2_ZCT_COMPLETE;

            // intentional fall through

        case SRV_READ_STAGE_SMB_V2_ZCT_COMPLETE:

            if (pReadState->pZctCompletion)
            {
                ntStatus = SrvCompleteZctRead_SMB_V2(pReadState, pExecContext);
                if (ntStatus != STATUS_PENDING)
                {
                    // A ZCT response was already sent, so eat up
                    // any error here to prevent the protocol execute
                    // layer from returning an error.
                    ntStatus = STATUS_SUCCESS;
                }
                BAIL_ON_NT_STATUS(ntStatus);
            }

            pReadState->stage = SRV_READ_STAGE_SMB_V2_DONE;

            // intentional fall through

        case SRV_READ_STAGE_SMB_V2_DONE:

            break;
    }

cleanup:

    if (pFile)
    {
        SrvFile2Release(pFile);
    }

    if (pTree)
    {
        SrvTree2Release(pTree);
    }

    if (pSession)
    {
        SrvSession2Release(pSession);
    }

    if (pAsyncState)
    {
        SrvAsyncStateRelease(pAsyncState);
    }

    return ntStatus;

error:

    switch (ntStatus)
    {
        case STATUS_PENDING:

            // TODO: Add an indicator to the file object to trigger a
            //       cleanup if the connection gets closed and all the
            //       files involved have to be closed

            break;

        case STATUS_FILE_IS_A_DIRECTORY:

            ntStatus = STATUS_INVALID_DEVICE_REQUEST;

            // Intentional fall through

        default:

            SrvReleaseExecContextAsync(pExecContext);

            break;
    }

    goto cleanup;
}

static
NTSTATUS
SrvBuildReadState_SMB_V2(
    IN PSMB2_READ_REQUEST_HEADER pRequestHeader,
    IN PLWIO_SRV_FILE_2 pFile,
    OUT PSRV_READ_STATE_SMB_V2*   ppReadState
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    PSRV_READ_STATE_SMB_V2 pReadState = NULL;

    ntStatus = SrvAllocateMemory(
                    sizeof(SRV_READ_STATE_SMB_V2),
                    (PVOID*)&pReadState);
    BAIL_ON_NT_STATUS(ntStatus);

    pReadState->stage = SRV_READ_STAGE_SMB_V2_INITIAL;

    pReadState->pRequestHeader = pRequestHeader;

    pReadState->pFile = SrvFile2Acquire(pFile);

    *ppReadState = pReadState;

cleanup:

    return ntStatus;

error:

    *ppReadState = NULL;

    if (pReadState)
    {
        SrvFreeReadState_SMB_V2(pReadState);
    }

    goto cleanup;
}

static
NTSTATUS
SrvBuildZctReadState_SMB_V2(
    PSRV_READ_STATE_SMB_V2 pReadState,
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    PLWIO_SRV_CONNECTION       pConnection  = pExecContext->pConnection;
    PSRV_EXEC_CONTEXT_SMB_V2   pCtxSmb2     = pExecContext->pProtocolContext;
    ULONG ulZctThreshold = 0;
    LW_ZCT_ENTRY_MASK zctReadMask = 0;
    LW_ZCT_ENTRY_MASK zctSocketMask = 0;

    // Do not use ZCT if chained or if signing active.
    if (pCtxSmb2->ulNumRequests != 1 ||
        pCtxSmb2->pRequests[0].pHeader->ulFlags & SMB2_FLAGS_SIGNED)
    {
        goto cleanup;
    }

    // Do not use ZCT if threshold disabled or the I/O size < threshold.
    ulZctThreshold = pConnection->serverProperties.ulZctReadThreshold;
    if ((ulZctThreshold == 0) ||
        (pReadState->ulBytesToRead < ulZctThreshold))
    {
        goto cleanup;
    }

    // Use ZCT only if file and system masks intersect.

    IoGetZctSupportMaskFile(
            pReadState->pFile->hFile,
            &zctReadMask,
            NULL);

    zctSocketMask = LwZctGetSystemSupportedMask(LW_ZCT_IO_TYPE_WRITE_SOCKET);
    if (zctReadMask & zctSocketMask)
    {
        ntStatus = LwZctCreate(
                        &pReadState->pZct,
                        LW_ZCT_IO_TYPE_WRITE_SOCKET);
        BAIL_ON_NT_STATUS(ntStatus);
    }

cleanup:

    return ntStatus;

error:

    LwZctDestroy(&pReadState->pZct);

    goto cleanup;
}

static
NTSTATUS
SrvAttemptReadIo_SMB_V2(
    PSRV_READ_STATE_SMB_V2 pReadState,
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                 ntStatus = STATUS_SUCCESS;
    PSRV_EXEC_CONTEXT_SMB_V2 pCtxSmb2 = pExecContext->pProtocolContext;

    if (pReadState->pZct)
    {
        if (!pReadState->bStartedRead)
        {
            SrvPrepareExecContextAsync(pExecContext);
            pReadState->bStartedRead = TRUE;

            ntStatus = IoPrepareZctReadFile(
                            pReadState->pFile->hFile,
                            pExecContext->pAcb,
                            &pExecContext->ioStatusBlock,
                            0,
                            pReadState->pZct,
                            pReadState->ulBytesToRead,
                            &pReadState->Offset,
                            &pReadState->ulKey,
                            &pReadState->pZctCompletion);
            if (STATUS_PENDING == ntStatus)
            {
                BAIL_ON_NT_STATUS(ntStatus);
            }

            // completed synchronously
        }
        else
        {
            // completed asynchronously
            ntStatus = pExecContext->ioStatusBlock.Status;
        }

        if (ntStatus == STATUS_NOT_SUPPORTED)
        {
            // Reset state and retry (below) as non-ZCT.
            SrvReleaseExecContextAsync(pExecContext);
            pReadState->bStartedRead = FALSE;
            LwZctDestroy(&pReadState->pZct);
            pExecContext->ioStatusBlock.Status = ntStatus = STATUS_SUCCESS;
        }

        BAIL_ON_NT_STATUS(ntStatus);
    }

    // This will retry as non-ZCT if ZCT failed above
    if (!pReadState->pZct)
    {
        if (!pReadState->bStartedRead)
        {
            SrvPrepareExecContextAsync(pExecContext);
            pReadState->bStartedRead = TRUE;

            if (!pReadState->pData && pReadState->ulBytesToRead > 0)
            {
                ntStatus = SrvAllocateMemory(
                                pReadState->ulBytesToRead,
                                (PVOID*)&pReadState->pData);
                BAIL_ON_NT_STATUS(ntStatus);
            }

            ntStatus = IoReadFile(
                            pReadState->pFile->hFile,
                            pExecContext->pAcb,
                            &pExecContext->ioStatusBlock,
                            0,
                            pReadState->pData,
                            pReadState->ulBytesToRead,
                            &pReadState->Offset,
                            &pReadState->ulKey);
            BAIL_ON_NT_STATUS(ntStatus);
        }

        ntStatus = pExecContext->ioStatusBlock.Status;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    pReadState->ulBytesRead = pExecContext->ioStatusBlock.BytesTransferred;

cleanup:

    if (ntStatus != STATUS_PENDING)
    {
        SrvReleaseExecContextAsync(pExecContext);
        pReadState->bStartedRead = FALSE;
    }

    return ntStatus;

error:

    if (ntStatus == STATUS_PENDING)
    {
        if (SrvTree2IsNamedPipe(pReadState->pFile->pTree) &&
            !pExecContext->bInterimSent &&
            !pCtxSmb2->InterimResponseTimer)
        {
            SrvTimedInterimResponse_SMB_V2(
                pExecContext,
                SRV_SMB2_INTERIM_RESPONSE_TIMEOUT);
            // Ignore error.  Has already been logged
        }
    }
    else if (pReadState->pZct)
    {
        // No data, must treat this as non-ZCT for the rest of processing.
        LwZctDestroy(&pReadState->pZct);
    }

    goto cleanup;
}

static
NTSTATUS
SrvSendZctReadResponse_SMB_V2(
    PSRV_READ_STATE_SMB_V2 pReadState,
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS ntStatus = 0;
    PLWIO_SRV_CONNECTION       pConnection  = pExecContext->pConnection;
    PSRV_EXEC_CONTEXT_SMB_V2   pCtxSmb2     = pExecContext->pProtocolContext;
    ULONG                      iMsg         = pCtxSmb2->iMsg;
    PSRV_MESSAGE_SMB_V2        pSmbResponse = &pCtxSmb2->pResponses[iMsg];
    PSRV_EXEC_CONTEXT          pZctContext  = NULL;
    LW_ZCT_ENTRY entry = { 0 };

    pExecContext->pSmbResponse->bufferUsed += pSmbResponse->ulMessageSize;

    SMBPacketMarshallFooter(pExecContext->pSmbResponse);

    entry.Type = LW_ZCT_ENTRY_TYPE_MEMORY;
    entry.Length = pExecContext->pSmbResponse->bufferUsed -
                   pReadState->ulBytesRead;
    entry.Data.Memory.Buffer = pExecContext->pSmbResponse->pRawBuffer;

    ntStatus = LwZctPrepend(pReadState->pZct, &entry, 1);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = LwZctPrepareIo(pReadState->pZct);
    BAIL_ON_NT_STATUS(ntStatus);

    pSmbResponse->ulZctMessageSize = pSmbResponse->ulMessageSize;

    pZctContext = SrvAcquireExecContext(pExecContext);

    ntStatus = SrvProtocolTransportSendZctResponse(
                    pConnection,
                    pExecContext->ullAsyncId,
                    pReadState->pZct,
                    pExecContext->pStatInfo,
                    SrvExecuteReadSendZctCB_SMB_V2,
                    pZctContext);
    BAIL_ON_NT_STATUS(ntStatus);

    SrvReleaseExecContext(pZctContext); // completed synchronously

cleanup:

    if (ntStatus == STATUS_SUCCESS || ntStatus == STATUS_PENDING)
    {
        pExecContext->usCreditsGranted = 0;
    }

    // TODO: Perhaps only if SrvProtocolTransportSendZctResponse() was
    // called and did not return STATUS_INSUFFICIENT_RESOURCES.
    // Never send out response via non-ZCT
    pSmbResponse->ulMessageSize = 0;
    pExecContext->pSmbResponse->bufferUsed = sizeof(NETBIOS_HEADER);

    return ntStatus;

error:

    if (pZctContext && (ntStatus != STATUS_PENDING))
    {
        SrvReleaseExecContext(pZctContext);
    }

    goto cleanup;
}

static
NTSTATUS
SrvCompleteZctRead_SMB_V2(
    PSRV_READ_STATE_SMB_V2 pReadState,
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;

    if (!pReadState->bStartedRead)
    {
        SrvPrepareExecContextAsync(pExecContext);
        pReadState->bStartedRead = TRUE;

        ntStatus = IoCompleteZctReadFile(
                        pReadState->pFile->hFile,
                        pExecContext->pAcb,
                        &pExecContext->ioStatusBlock,
                        0,
                        pReadState->pZctCompletion);
    }
    else
    {
        ntStatus = pExecContext->ioStatusBlock.Status;
    }
    BAIL_ON_NT_STATUS(ntStatus);

cleanup:

    if (ntStatus != STATUS_PENDING)
    {
        SrvReleaseExecContextAsync(pExecContext);
        pReadState->bStartedRead = FALSE;
    }

    return ntStatus;

error:

    if (ntStatus != STATUS_PENDING)
    {
        LWIO_LOG_ERROR("Failed to complete ZCT read file [status:0x%x]",
                       ntStatus);
    }

    goto cleanup;
}

static
VOID
SrvExecuteReadSendZctCB_SMB_V2(
    IN PVOID pContext,
    IN NTSTATUS Status
    )
{
    PSRV_EXEC_CONTEXT          pExecContext = pContext;
    PSRV_EXEC_CONTEXT_SMB_V2   pCtxSmb2     = pExecContext->pProtocolContext;
    PSRV_READ_STATE_SMB_V2     pReadState   = pCtxSmb2->hState;
    BOOLEAN                    bInLock      = FALSE;

    LWIO_LOCK_MUTEX(bInLock, &pExecContext->execMutex);
    pReadState->stage = SRV_READ_STAGE_SMB_V2_ZCT_COMPLETE;
    LWIO_UNLOCK_MUTEX(bInLock, &pExecContext->execMutex);

    SrvScheduleOrExecuteExecContext(pExecContext);

    SrvReleaseExecContext(pExecContext);
}

static
VOID
SrvFreeReadStateHandle_SMB_V2(
    HANDLE hReadState
    )
{
    SrvFreeReadState_SMB_V2(hReadState);
}

static
VOID
SrvFreeReadState_SMB_V2(
    PSRV_READ_STATE_SMB_V2 pReadState
    )
{
    if (pReadState->pFile)
    {
        SrvFile2Release(pReadState->pFile);
    }

    if (pReadState->pData)
    {
        SrvFreeMemory(pReadState->pData);
    }

    if (pReadState->pZct)
    {
        LwZctDestroy(&pReadState->pZct);
    }

    SrvFreeMemory(pReadState);
}

static
NTSTATUS
SrvBuildReadResponse_SMB_V2(
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                   ntStatus      = STATUS_SUCCESS;
    PSRV_EXEC_CONTEXT_SMB_V2   pCtxSmb2      = pExecContext->pProtocolContext;
    PSRV_READ_STATE_SMB_V2     pReadState    = pCtxSmb2->hState;
    ULONG                      iMsg          = pCtxSmb2->iMsg;
    PSRV_MESSAGE_SMB_V2        pSmbRequest   = &pCtxSmb2->pRequests[iMsg];
    PSRV_MESSAGE_SMB_V2        pSmbResponse  = &pCtxSmb2->pResponses[iMsg];
    ULONG ulDataOffset     = 0L;
    PBYTE pOutBuffer       = pSmbResponse->pBuffer;
    ULONG ulBytesAvailable = pSmbResponse->ulBytesAvailable;
    ULONG ulOffset         = 0;
    ULONG ulBytesUsed      = 0;
    ULONG ulTotalBytesUsed = 0;

    ntStatus = SMB2MarshalHeader(
                    pOutBuffer,
                    ulOffset,
                    ulBytesAvailable,
                    COM2_READ,
                    pSmbRequest->CreditCharge,
                    pExecContext->usCreditsGranted,
                    pSmbRequest->pHeader->ulPid,
                    pSmbRequest->pHeader->ullCommandSequence,
                    pCtxSmb2->pTree->ulTid,
                    pCtxSmb2->pSession->ullUid,
                    pExecContext->bInterimSent ? pExecContext->ullAsyncId : 0,
                    STATUS_SUCCESS,
                    TRUE,
                    LwIsSetFlag(
                        pSmbRequest->pHeader->ulFlags,
                        SMB2_FLAGS_RELATED_OPERATION),
                    &pSmbResponse->pHeader,
                    &pSmbResponse->ulHeaderSize);
    BAIL_ON_NT_STATUS(ntStatus);

    pOutBuffer       += pSmbResponse->ulHeaderSize;
    ulOffset         += pSmbResponse->ulHeaderSize;
    ulBytesAvailable -= pSmbResponse->ulHeaderSize;
    ulTotalBytesUsed += pSmbResponse->ulHeaderSize;

    ntStatus = SMB2MarshalReadResponse(
                    pOutBuffer,
                    ulOffset,
                    ulBytesAvailable,
                    NULL,
                    pReadState->pRequestHeader->ulDataLength,
                    0,
                    &ulDataOffset,
                    &ulBytesUsed);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = SMB2MarshalReadResponse(
                    pOutBuffer,
                    ulOffset,
                    ulBytesAvailable,
                    pReadState->pZct ? NULL : pReadState->pData,
                    pReadState->ulBytesRead,
                    pReadState->ulRemaining,
                    &ulDataOffset,
                    &ulBytesUsed);
    BAIL_ON_NT_STATUS(ntStatus);

    // pOutBuffer       += ulBytesUsed;
    // ulOffset         += ulBytesUsed;
    // ulBytesAvailable -= ulBytesUsed;
    ulTotalBytesUsed += ulBytesUsed;

    pSmbResponse->ulMessageSize = ulTotalBytesUsed;

cleanup:

    return ntStatus;

error:

    if (ulTotalBytesUsed)
    {
        pSmbResponse->pHeader = NULL;
        pSmbResponse->ulHeaderSize = 0;
        memset(pSmbResponse->pBuffer, 0, ulTotalBytesUsed);
    }

    pSmbResponse->ulMessageSize = 0;

    goto cleanup;
}
