/* -*- 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:
 *
 *        write.c
 *
 * Abstract:
 *
 *        Likewise IO (LWIO) - SRV
 *
 *        Protocols API - SMBV2
 *
 *        Write
 *
 * Authors: Krishna Ganugapati (kganugapati@likewise.com)
 *          Sriram Nambakam (snambakam@likewise.com)
 *
 *
 */

#include "includes.h"

static
NTSTATUS
SrvBuildWriteState_SMB_V2(
    IN PSMB2_WRITE_REQUEST_HEADER pRequestHeader,
    IN PBYTE pData,
    IN PLWIO_SRV_FILE_2 pFile,
    IN ULONG ulKey,
    IN SMB_PROTOCOL_DIALECT Dialect,
    OUT PSRV_WRITE_STATE_SMB_V2* ppWriteState
    );

static
NTSTATUS
SrvExecuteWrite_SMB_V2(
    PSRV_WRITE_STATE_SMB_V2 pWriteState,
    PSRV_EXEC_CONTEXT pExecContext
    );

static
NTSTATUS
SrvExecuteWriteZctIo_SMB_V2(
    PSRV_WRITE_STATE_SMB_V2 pWriteState,
    PSRV_EXEC_CONTEXT pExecContext
    );

static
NTSTATUS
SrvExecuteWriteZctComplete_SMB_V2(
    PSRV_WRITE_STATE_SMB_V2 pWriteState,
    PSRV_EXEC_CONTEXT pExecContext
    );

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

static
VOID
SrvFreeWriteStateHandle_SMB_V2(
    HANDLE hWriteState
    );

static
VOID
SrvFreeWriteState_SMB_V2(
    PSRV_WRITE_STATE_SMB_V2 pWriteState
    );

static
NTSTATUS
SrvBuildWriteResponse_SMB_V2(
    PSRV_EXEC_CONTEXT pExecContext
    );

NTSTATUS
SrvProcessWrite_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_WRITE_STATE_SMB_V2    pWriteState  = 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 (pWriteState == NULL)
    {
        ULONG                      iMsg           = pCtxSmb2->iMsg;
        PSRV_MESSAGE_SMB_V2        pSmbRequest    = &pCtxSmb2->pRequests[iMsg];
        PSMB2_WRITE_REQUEST_HEADER pRequestHeader = NULL; // Do not free
        PBYTE                      pData          = 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 = SMB2UnmarshalWriteRequest(
                        pSmbRequest,
                        &pRequestHeader,
                        &pData);
        BAIL_ON_NT_STATUS(ntStatus);

        SRV_LOG_DEBUG(
                pExecContext->pLogContext,
                SMB_PROTOCOL_VERSION_2,
                pSmbRequest->pHeader->command,
                "Write 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),"
                "flags(0x%x),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,
                pRequestHeader->fid.ullPersistentId,
                pRequestHeader->fid.ullVolatileId,
                pRequestHeader->ulChannel,
                pRequestHeader->usWriteChannelInfoLength,
                pRequestHeader->usWriteChannelInfoOffset,
                pRequestHeader->ulFlags,
                pRequestHeader->ulDataLength,
                (long long)pRequestHeader->ullFileOffset);

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

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

        pExecContext->ullAsyncId = pAsyncState->ullAsyncId;

        ntStatus = SrvBuildWriteState_SMB_V2(
                            pRequestHeader,
                            pData,
                            pFile,
                            0,
                            pConnection->Dialect,
                            &pWriteState);
        BAIL_ON_NT_STATUS(ntStatus);

        pCtxSmb2->hState = pWriteState;
        pCtxSmb2->pfnStateRelease = &SrvFreeWriteStateHandle_SMB_V2;

        pAsyncState->hAsyncState = SrvAcquireExecContext(pExecContext);
    }

    switch (pWriteState->stage)
    {
        case SRV_WRITE_STAGE_SMB_V2_INITIAL:

            pWriteState->Offset = pWriteState->pRequestHeader->ullFileOffset;
            pWriteState->ulLength = pWriteState->pRequestHeader->ulDataLength;

            pWriteState->stage = SRV_WRITE_STAGE_SMB_V2_ATTEMPT_WRITE;

            // intentional fall through

        case SRV_WRITE_STAGE_SMB_V2_ATTEMPT_WRITE:

            ntStatus = SrvExecuteWrite_SMB_V2(pWriteState, pExecContext);
            BAIL_ON_NT_STATUS(ntStatus);

            pWriteState->stage = SRV_WRITE_STAGE_SMB_V2_ZCT_IO;


            // intentional fall through

        case SRV_WRITE_STAGE_SMB_V2_ZCT_IO:

            if (pWriteState->Zct.pZct)
            {
                ntStatus = SrvExecuteWriteZctIo_SMB_V2(
                                pWriteState,
                                pExecContext);
                BAIL_ON_NT_STATUS(ntStatus);
            }

            pWriteState->stage = SRV_WRITE_STAGE_SMB_V2_ZCT_COMPLETE;

            // intentional fall through

        case SRV_WRITE_STAGE_SMB_V2_ZCT_COMPLETE:

            if (pWriteState->Zct.pZct)
            {
                LwZctDestroy(&pWriteState->Zct.pZct);

                SrvProtocolTransportResumeFromZct(
                    pWriteState->Zct.pPausedConnection, 
                    pExecContext->ullAsyncId);
                SrvConnectionRelease(pWriteState->Zct.pPausedConnection);
                pWriteState->Zct.pPausedConnection = NULL;

                ntStatus = SrvExecuteWriteZctComplete_SMB_V2(
                                pWriteState,
                                pExecContext);
                BAIL_ON_NT_STATUS(ntStatus);
            }

            pWriteState->stage = SRV_WRITE_STAGE_SMB_V2_BUILD_RESPONSE;

            // intentional fall through

        case SRV_WRITE_STAGE_SMB_V2_BUILD_RESPONSE:

            ntStatus = SrvBuildWriteResponse_SMB_V2(pExecContext);
            BAIL_ON_NT_STATUS(ntStatus);

            pWriteState->stage = SRV_WRITE_STAGE_SMB_V2_DONE;

            // intentional fall through

        case SRV_WRITE_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;

        default:

            SrvReleaseExecContextAsync(pExecContext);

            break;
    }

    goto cleanup;
}

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

static
NTSTATUS
SrvBuildWriteState_SMB_V2(
    IN PSMB2_WRITE_REQUEST_HEADER pRequestHeader,
    IN PBYTE pData,
    IN PLWIO_SRV_FILE_2 pFile,
    IN ULONG ulKey,
    IN SMB_PROTOCOL_DIALECT Dialect,
    OUT PSRV_WRITE_STATE_SMB_V2* ppWriteState
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    PSRV_WRITE_STATE_SMB_V2 pWriteState = NULL;

    ntStatus = SrvAllocateMemory(
                    sizeof(SRV_WRITE_STATE_SMB_V2),
                    (PVOID*)&pWriteState);
    BAIL_ON_NT_STATUS(ntStatus);

    pWriteState->stage = SRV_WRITE_STAGE_SMB_V2_INITIAL;

    pWriteState->pRequestHeader = pRequestHeader;
    pWriteState->pData          = pData;

    pWriteState->pFile          = SrvFile2Acquire(pFile);

    pWriteState->ulKey          = ulKey;

    if ((Dialect >= SMB_PROTOCOL_DIALECT_SMB_2_1) &&
        (pRequestHeader->ulFlags & SMB2_WRITE_FLAGS_WRITE_THROUGH))
    {
        SetFlag(pWriteState->IoFlags, IO_FLAG_WRITE_THROUGH);
    }

    *ppWriteState = pWriteState;

cleanup:

    return ntStatus;

error:

    *ppWriteState = NULL;

    if (pWriteState)
    {
        SrvFreeWriteState_SMB_V2(pWriteState);
    }

    goto cleanup;
}

static
NTSTATUS
SrvExecuteWrite_SMB_V2(
    PSRV_WRITE_STATE_SMB_V2 pWriteState,
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                 ntStatus = STATUS_SUCCESS;
    PSRV_EXEC_CONTEXT_SMB_V2 pCtxSmb2 = pExecContext->pProtocolContext;

    if (pWriteState->Zct.pZct)
    {
        if (!pWriteState->bStartedIo)
        {
            SrvPrepareExecContextAsync(pExecContext);
            pWriteState->bStartedIo = TRUE;

            ntStatus = IoPrepareZctWriteFile(
                            pWriteState->pFile->hFile,
                            pExecContext->pAcb,
                            &pExecContext->ioStatusBlock,
                            pWriteState->IoFlags,
                            pWriteState->Zct.pZct,
                            pWriteState->ulLength,
                            &pWriteState->Offset,
                            &pWriteState->ulKey,
                            &pWriteState->Zct.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);
            pWriteState->bStartedIo = FALSE;
            LwZctDestroy(&pWriteState->Zct.pZct);
            pExecContext->ioStatusBlock.Status = ntStatus = STATUS_SUCCESS;

            // Read rest of data into packet.
            ntStatus = SrvProtocolTransportContinueAsNonZct(
                    pExecContext->pConnection,
                    pExecContext);
            // For now, returns STATUS_PENDING.
            LWIO_ASSERT(STATUS_PENDING == ntStatus);

            SrvConnectionRelease(pWriteState->Zct.pPausedConnection);
            pWriteState->Zct.pPausedConnection = NULL;

            BAIL_ON_NT_STATUS(ntStatus);
        }
        else
        {
            BAIL_ON_NT_STATUS(ntStatus);

            ULONG zctLength = LwZctGetLength(pWriteState->Zct.pZct);
            LWIO_ASSERT(zctLength == pWriteState->ulLength);
        }
    }

    // This will also retry as non-ZCT if ZCT failed above.
    if (!pWriteState->Zct.pZct)
    {
        if (!pWriteState->bStartedIo)
        {
            SrvPrepareExecContextAsync(pExecContext);
            pWriteState->bStartedIo = TRUE;

            ntStatus = IoWriteFile(
                            pWriteState->pFile->hFile,
                            pExecContext->pAcb,
                            &pExecContext->ioStatusBlock,
                            pWriteState->IoFlags,
                            pWriteState->pData,
                            pWriteState->ulLength,
                            &pWriteState->Offset,
                            &pWriteState->ulKey);
            BAIL_ON_NT_STATUS(ntStatus);
        }

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

    pWriteState->ulBytesWritten =
            pExecContext->ioStatusBlock.BytesTransferred;

cleanup:

    if (ntStatus != STATUS_PENDING)
    {
        SrvReleaseExecContextAsync(pExecContext);
        pWriteState->bStartedIo = FALSE;
    }

    return ntStatus;

error:

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

    goto cleanup;
}

static
NTSTATUS
SrvExecuteWriteZctIo_SMB_V2(
    PSRV_WRITE_STATE_SMB_V2 pWriteState,
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    PSRV_EXEC_CONTEXT pZctContext = NULL;

    if (pWriteState->Zct.ulSkipBytes)
    {
        // Skip extra padding
        LW_ZCT_ENTRY entry;

        ntStatus = SrvAllocateMemory(
                        pWriteState->Zct.ulSkipBytes,
                        OUT_PPVOID(&pWriteState->Zct.pPadding));
        BAIL_ON_NT_STATUS(ntStatus);

        entry.Type = LW_ZCT_ENTRY_TYPE_MEMORY;
        entry.Length = pWriteState->Zct.ulSkipBytes;
        entry.Data.Memory.Buffer = pWriteState->Zct.pPadding;

        ntStatus = LwZctPrepend(pWriteState->Zct.pZct, &entry, 1);
        BAIL_ON_NT_STATUS(ntStatus);

        pWriteState->Zct.ulPaddingSize = pWriteState->Zct.ulSkipBytes;
        pWriteState->Zct.ulSkipBytes = 0;
    }

    ntStatus = LwZctPrepareIo(pWriteState->Zct.pZct);
    BAIL_ON_NT_STATUS(ntStatus);

    if (pWriteState->Zct.ulDataBytesResident)
    {
        // Copy any bytes we already have
        ntStatus = LwZctReadBufferIo(
                        pWriteState->Zct.pZct,
                        pWriteState->pData,
                        pWriteState->Zct.ulDataBytesResident,
                        &pWriteState->ulBytesWritten,
                        NULL);
        LWIO_ASSERT(STATUS_MORE_PROCESSING_REQUIRED != ntStatus);
        BAIL_ON_NT_STATUS(ntStatus);

        LWIO_ASSERT(pWriteState->ulBytesWritten ==
            pWriteState->Zct.ulDataBytesResident);

        pWriteState->Zct.ulDataBytesResident = 0;
    }

    LWIO_ASSERT(pWriteState->Zct.ulDataBytesMissing > 0);

    pZctContext = SrvAcquireExecContext(pExecContext);

    // Do ZCT read from the transport for remaining bytes
    ntStatus = SrvProtocolTransportReceiveZct(
                    pExecContext->pConnection,
                    pExecContext->ullAsyncId,
                    pWriteState->Zct.pZct,
                    SrvExecuteWriteReceiveZctCB_SMB_V2,
                    pZctContext);
    BAIL_ON_NT_STATUS(ntStatus);

    // completed synchronously
    SrvReleaseExecContext(pZctContext);

    pWriteState->ulBytesWritten += pWriteState->Zct.ulDataBytesMissing -
                                   pWriteState->Zct.ulPaddingSize;

cleanup:

    return ntStatus;

error:

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

    goto cleanup;
}

static
NTSTATUS
SrvExecuteWriteZctComplete_SMB_V2(
    PSRV_WRITE_STATE_SMB_V2 pWriteState,
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;

    if (!pWriteState->bStartedIo)
    {
        SrvPrepareExecContextAsync(pExecContext);
        pWriteState->bStartedIo = TRUE;

        ntStatus = IoCompleteZctWriteFile(
                        pWriteState->pFile->hFile,
                        pExecContext->pAcb,
                        &pExecContext->ioStatusBlock,
                        pWriteState->IoFlags,
                        pWriteState->Zct.pZctCompletion,
                        pWriteState->ulBytesWritten);
    }
    else
    {
        ntStatus = pExecContext->ioStatusBlock.Status;
    }
    BAIL_ON_NT_STATUS(ntStatus);

cleanup:

    if (ntStatus != STATUS_PENDING)
    {
        SrvReleaseExecContextAsync(pExecContext);
        pWriteState->bStartedIo = FALSE;
    }

    return ntStatus;

error:

    if (ntStatus != STATUS_PENDING)
    {
        LWIO_LOG_ERROR("Failed to complete ZCT write file (status = 0x%08x)",
                       ntStatus);
    }

    goto cleanup;
}

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

    LWIO_LOCK_MUTEX(bInLock, &pExecContext->execMutex);
    pWriteState->stage = SRV_WRITE_STAGE_SMB_V2_ZCT_COMPLETE;
    if (Status == STATUS_SUCCESS)
    {
        pWriteState->ulBytesWritten += pWriteState->Zct.ulDataBytesMissing -
                                       pWriteState->Zct.ulPaddingSize;
    }
    LWIO_UNLOCK_MUTEX(bInLock, &pExecContext->execMutex);

    LWIO_LOG_DEBUG(
        "queue exec context = %p (conn = %p)",
        pContext,
        pExecContext->pConnection);

    SrvScheduleOrExecuteExecContext(pExecContext);

    SrvReleaseExecContext(pExecContext);
}

static
VOID
SrvFreeWriteStateHandle_SMB_V2(
    HANDLE hWriteState
    )
{
    SrvFreeWriteState_SMB_V2(hWriteState);
}

static
VOID
SrvFreeWriteState_SMB_V2(
    PSRV_WRITE_STATE_SMB_V2 pWriteState
    )
{
    if (pWriteState->Zct.pZct)
    {
        LwZctDestroy(&pWriteState->Zct.pZct);
    }

    if (pWriteState->Zct.pPadding)
    {
        SrvFreeMemory(pWriteState->Zct.pPadding);
    }

    if (pWriteState->Zct.pPausedConnection)
    {
        SrvProtocolTransportResumeFromZct(
            pWriteState->Zct.pPausedConnection,
            /*AsyncId has already been removed*/0);
        SrvConnectionRelease(pWriteState->Zct.pPausedConnection);
    }

    if (pWriteState->pFile)
    {
        SrvFile2Release(pWriteState->pFile);
    }

    SrvFreeMemory(pWriteState);
}

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

    ntStatus = SMB2MarshalHeader(
                    pOutBuffer,
                    ulOffset,
                    ulBytesAvailable,
                    COM2_WRITE,
                    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 = SMB2MarshalWriteResponse(
                    pOutBuffer,
                    ulOffset,
                    ulBytesAvailable,
                    pWriteState->ulBytesWritten,
                    (pWriteState->pRequestHeader->ulDataLength -
                     pWriteState->ulBytesWritten),
                    &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;
}

NTSTATUS
SrvDetectZctWrite_SMB_V2(
    IN PSRV_CONNECTION pConnection,
    IN PSMB_PACKET pPacket,
    OUT PSRV_EXEC_CONTEXT* ppZctExecContext
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    PSRV_EXEC_CONTEXT_SMB_V2 pCtxSmb2 = NULL;
    NETBIOS_HEADER* pNetBiosHeader = (NETBIOS_HEADER*) pPacket->pRawBuffer;
    PBYTE pBuffer          = pPacket->pRawBuffer + sizeof(NETBIOS_HEADER);
    ULONG ulBytesAvailable = pPacket->bufferUsed - sizeof(NETBIOS_HEADER);
    ULONG ulRequestOffset = 0;
    PSMB2_WRITE_REQUEST_HEADER pWrite = NULL;
    PBYTE pData = NULL;
    SMB2_FID fid = { 0 };
    ULONG ulLength = 0;
    ULONG ulDataOffset = 0;
    ULONG ulZctThreshold = 0;
    LW_ZCT_ENTRY_MASK zctWriteMask = 0;
    LW_ZCT_ENTRY_MASK zctSocketMask = 0;
    SRV_ZCT_WRITE_STATE_SMB_V2 zctState = { 0 };
    PLWIO_SRV_SESSION_2 pSession = NULL;
    PLWIO_SRV_TREE_2 pTree = NULL;
    PLWIO_SRV_FILE_2 pFile = NULL;
    PSRV_EXEC_CONTEXT pExecContext = NULL;
    PSRV_WRITE_STATE_SMB_V2 pWriteState = NULL;

    if (ulBytesAvailable < sizeof(SMB2_HEADER))
    {
        ntStatus = STATUS_MORE_PROCESSING_REQUIRED;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    pPacket->pSMB2Header = (PSMB2_HEADER)pBuffer;
    pBuffer += sizeof(SMB2_HEADER);
    ulBytesAvailable -= sizeof(SMB2_HEADER);

    if (pPacket->pSMB2Header->smb[0] != 0xFE)
    {
        ntStatus = STATUS_INVALID_NETWORK_RESPONSE;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    pPacket->protocolVer = SMB_PROTOCOL_VERSION_2;

    if (SMB2PacketIsSigned(pPacket))
    {
        // Cannot do ZCT on signed requests
        ntStatus = STATUS_SUCCESS;
        goto cleanup;
    }

    // TODO: Support chaining.
    if (pPacket->pSMB2Header->ulChainOffset)
    {
        // Cannot do ZCT on chained command
        ntStatus = STATUS_SUCCESS;
        goto cleanup;
    }

    ulRequestOffset = LwRtlPointerToOffset(pPacket->pSMB2Header, pBuffer);

    switch (pPacket->pSMB2Header->command)
    {
        case COM2_WRITE:
        {
            ULONG ulRequired = sizeof(SMB2_WRITE_REQUEST_HEADER);

            if (ulBytesAvailable < ulRequired)
            {
                ntStatus = STATUS_MORE_PROCESSING_REQUIRED;
                BAIL_ON_NT_STATUS(ntStatus);
            }

            pWrite = (PSMB2_WRITE_REQUEST_HEADER) pBuffer;

            fid = pWrite->fid;
            ulLength = pWrite->ulDataLength;
            ulDataOffset = pWrite->usDataOffset;

            if (ulDataOffset < ulRequestOffset)
            {
                ntStatus = STATUS_INVALID_BUFFER_SIZE;
                BAIL_ON_NT_STATUS(ntStatus);
            }
            ulDataOffset -= ulRequestOffset;

            if (ulDataOffset < sizeof(SMB2_WRITE_REQUEST_HEADER))
            {
                ntStatus = STATUS_INVALID_BUFFER_SIZE;
                BAIL_ON_NT_STATUS(ntStatus);
            }

            break;
        }
        default:
            ntStatus = STATUS_SUCCESS;
            goto cleanup;
    }

    // TODO: guard against overflow
    if ((ulRequestOffset + ulDataOffset + ulLength) > pNetBiosHeader->len)
    {
        ntStatus = STATUS_INVALID_BUFFER_SIZE;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    if (ulBytesAvailable >= (ulDataOffset + ulLength))
    {
        // Have all the data, so do not use ZCT
        ntStatus = STATUS_SUCCESS;
        goto cleanup;
    }

    // Do not use ZCT if threshold disabled or the I/O size < threshold.
    ulZctThreshold = pConnection->serverProperties.ulZctWriteThreshold;
    if ((ulZctThreshold == 0) ||
        (ulLength < ulZctThreshold))
    {
        // Should not use ZCT
        ntStatus = STATUS_SUCCESS;
        goto cleanup;
    }

    // Compute what's missing, present, and what can be skipped.
    zctState.ulDataBytesMissing = (ulDataOffset + ulLength) - ulBytesAvailable;
    if (ulBytesAvailable > ulDataOffset)
    {
        zctState.ulDataBytesResident = ulBytesAvailable - ulDataOffset;
        LWIO_ASSERT(
            zctState.ulDataBytesResident ==
            (ulLength - zctState.ulDataBytesMissing));
    }
    else
    {
        zctState.ulSkipBytes = ulDataOffset - ulBytesAvailable;
    }

    // TODO-Do not do ZCT if very few bytes missing?

    ntStatus = SrvConnection2FindSession_inlock(
                    pConnection,
                    pPacket->pSMB2Header->ullSessionId,
                    &pSession);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = SrvSession2FindTree(
                    pSession,
                    pPacket->pSMB2Header->ulTid,
                    &pTree);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = SrvTree2FindFile(
                    pTree,
                    &fid,
                    &pFile);
    if (STATUS_INVALID_HANDLE == ntStatus)
    {
        // TODO: Perhaps short-circuit
        ntStatus = STATUS_SUCCESS;
        goto cleanup;
    }
    BAIL_ON_NT_STATUS(ntStatus);

    IoGetZctSupportMaskFile(
            pFile->hFile,
            NULL,
            &zctWriteMask);

    zctSocketMask = LwZctGetSystemSupportedMask(LW_ZCT_IO_TYPE_READ_SOCKET);
    if (!(zctWriteMask & zctSocketMask))
    {
        ntStatus = STATUS_SUCCESS;
        goto cleanup;
    }

    ntStatus = LwZctCreate(&zctState.pZct, LW_ZCT_IO_TYPE_READ_SOCKET);
    BAIL_ON_NT_STATUS(ntStatus);

    // Note that building the context takes its own reference.
    ntStatus = SrvBuildExecContext(pConnection, pPacket, FALSE, &pExecContext);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = SrvBuildExecContext_SMB_V2(pExecContext);
    BAIL_ON_NT_STATUS(ntStatus);

    pCtxSmb2 = pExecContext->pProtocolContext;
    pCtxSmb2->pRequests[0].ulMessageSize = pNetBiosHeader->len;

    pCtxSmb2->pSession = pSession;
    pSession = NULL;

    pCtxSmb2->pTree = pTree;
    pTree = NULL;

    pCtxSmb2->pFile = SrvFile2Acquire(pFile);

    pData = (PBYTE) LwRtlOffsetToPointer(pBuffer, ulDataOffset);

    switch (pPacket->pSMB2Header->command)
    {
        case COM2_WRITE:
        {
            // gets reference to file.
            ntStatus = SrvBuildWriteState_SMB_V2(
                            pWrite,
                            pData,
                            pFile,
                            0,
                            pConnection->Dialect,
                            &pWriteState);
            BAIL_ON_NT_STATUS(ntStatus);

            pWriteState->Zct = zctState;
            zctState.pZct = NULL;

            pWriteState->Zct.pPausedConnection =
                SrvConnectionAcquire(pConnection);

            pCtxSmb2->hState = pWriteState;
            pCtxSmb2->pfnStateRelease = SrvFreeWriteStateHandle_SMB_V2;

            break;
        }

        default:
        {
            LWIO_LOG_ERROR(
                "Unexpected SMB command while processing SMB2 ZCT (0x%04x)",
                pPacket->pSMB2Header->command);
            ntStatus = STATUS_ASSERTION_FAILURE;
            BAIL_ON_NT_STATUS(ntStatus);
        }
    }

    *ppZctExecContext = pExecContext;

    ntStatus = STATUS_SUCCESS;

cleanup:

    if (pFile)
    {
        SrvFile2Release(pFile);
    }

    if (pTree)
    {
        SrvTree2Release(pTree);
    }

    if (pSession)
    {
        SrvSession2Release(pSession);
    }

    return ntStatus;

error:

    if (pExecContext)
    {
        SrvReleaseExecContext(pExecContext);
    }

    goto cleanup;
}
