static VOID SrvTreeAsyncStateRelease( PVOID pAsyncState ) { SrvAsyncStateRelease((PLWIO_ASYNC_STATE)pAsyncState); }
NTSTATUS SrvProcessNotifyCompletion_SMB_V2( PSRV_EXEC_CONTEXT pExecContext ) { NTSTATUS ntStatus = STATUS_SUCCESS; PLWIO_SRV_CONNECTION pConnection = pExecContext->pConnection; PSRV_PROTOCOL_EXEC_CONTEXT pCtxProtocol = pExecContext->pProtocolContext; PSRV_EXEC_CONTEXT_SMB_V2 pCtxSmb2 = pCtxProtocol->pSmb2Context; ULONG iMsg = pCtxSmb2->iMsg; PSRV_MESSAGE_SMB_V2 pSmbRequest = &pCtxSmb2->pRequests[iMsg]; PLWIO_SRV_SESSION_2 pSession = NULL; PLWIO_SRV_TREE_2 pTree = NULL; BOOLEAN bInLock = FALSE; PLWIO_ASYNC_STATE pAsyncState = NULL; ULONG64 ullAsyncId = 0LL; PSRV_NOTIFY_STATE_SMB_V2 pNotifyState = NULL; ntStatus = SrvConnection2FindSession_SMB_V2( pCtxSmb2, pConnection, pSmbRequest->pHeader->ullSessionId, &pSession); BAIL_ON_NT_STATUS(ntStatus); ntStatus = SMB2GetAsyncId(pSmbRequest->pHeader, &ullAsyncId); BAIL_ON_NT_STATUS(ntStatus); ntStatus = SrvConnection2FindAsyncState(pConnection, ullAsyncId, &pAsyncState); if (ntStatus == STATUS_NOT_FOUND) { // The request must have been rundown. ntStatus = STATUS_CANCELLED; } BAIL_ON_NT_STATUS(ntStatus); SrvConnection2RemoveAsyncState(pConnection, ullAsyncId); pNotifyState = (PSRV_NOTIFY_STATE_SMB_V2)pAsyncState->hAsyncState; ntStatus = SrvSession2FindTree_SMB_V2( pCtxSmb2, pSession, pNotifyState->ulTid, &pTree); BAIL_ON_NT_STATUS(ntStatus); if (pSmbRequest->pHeader->error == STATUS_CANCELLED) { if (SrvFile2IsRundown(pNotifyState->pFile)) { pSmbRequest->pHeader->error = STATUS_NOTIFY_CLEANUP; pNotifyState->ioStatusBlock.Status = STATUS_NOTIFY_CLEANUP; pNotifyState->ioStatusBlock.BytesTransferred = 0; } } LWIO_LOCK_MUTEX(bInLock, &pNotifyState->mutex); switch (pSmbRequest->pHeader->error) { case STATUS_CANCELLED: ntStatus = STATUS_CANCELLED; break; case STATUS_NOTIFY_CLEANUP: case STATUS_NOTIFY_ENUM_DIR: case STATUS_SUCCESS: pNotifyState->ulBytesUsed = pNotifyState->ioStatusBlock.BytesTransferred; ntStatus = SrvBuildNotifyResponse_SMB_V2( pExecContext, pNotifyState); break; default: ntStatus = STATUS_INTERNAL_ERROR; break; } BAIL_ON_NT_STATUS(ntStatus); cleanup: if (pNotifyState) { LWIO_UNLOCK_MUTEX(bInLock, &pNotifyState->mutex); } if (pAsyncState) { SrvAsyncStateRelease(pAsyncState); } if (pTree) { SrvTree2Release(pTree); } if (pSession) { SrvSession2Release(pSession); } return ntStatus; error: // Need to build error response here since exec context is internal. ntStatus = SrvBuildErrorResponse_SMB_V2( pExecContext, ullAsyncId, ntStatus); goto cleanup; }
NTSTATUS SrvProcessNotify_SMB_V2( PSRV_EXEC_CONTEXT pExecContext ) { NTSTATUS ntStatus = STATUS_SUCCESS; PLWIO_SRV_CONNECTION pConnection = pExecContext->pConnection; PSRV_PROTOCOL_EXEC_CONTEXT pCtxProtocol = pExecContext->pProtocolContext; PSRV_EXEC_CONTEXT_SMB_V2 pCtxSmb2 = pCtxProtocol->pSmb2Context; ULONG iMsg = pCtxSmb2->iMsg; PSRV_MESSAGE_SMB_V2 pSmbRequest = &pCtxSmb2->pRequests[iMsg]; PLWIO_SRV_SESSION_2 pSession = NULL; PLWIO_SRV_TREE_2 pTree = NULL; BOOLEAN bInLock = FALSE; PSRV_NOTIFY_STATE_SMB_V2 pNotifyState = NULL; PLWIO_ASYNC_STATE pAsyncState = NULL; BOOLEAN bUnregisterAsync = FALSE; PSRV_NOTIFY_REQUEST_STATE_SMB_V2 pNotifyRequestState = NULL; pNotifyRequestState = (PSRV_NOTIFY_REQUEST_STATE_SMB_V2)pCtxSmb2->hState; if (pNotifyRequestState) { InterlockedIncrement(&pNotifyRequestState->refCount); } else { PSMB2_NOTIFY_CHANGE_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 = SMB2UnmarshalNotifyRequest(pSmbRequest, &pRequestHeader); BAIL_ON_NT_STATUS(ntStatus); SRV_LOG_DEBUG( pExecContext->pLogContext, SMB_PROTOCOL_VERSION_2, pSmbRequest->pHeader->command, "Change notify 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)," "flags(0x%x),completion-filter(%u),output-buffer-length(%u)", pSmbRequest->pHeader->command, (long long)pSmbRequest->pHeader->ullSessionId, (long long)pSmbRequest->pHeader->ullCommandSequence, pSmbRequest->pHeader->ulPid, pSmbRequest->pHeader->ulTid, pSmbRequest->pHeader->usCredits, pSmbRequest->pHeader->ulFlags, pSmbRequest->pHeader->ulChainOffset, (long long)pRequestHeader->fid.ullPersistentId, (long long)pRequestHeader->fid.ullVolatileId, pRequestHeader->usFlags, pRequestHeader->ulCompletionFilter, pRequestHeader->ulOutputBufferLength); ntStatus = SrvBuildNotifyRequestState_SMB_V2( pRequestHeader, &pNotifyRequestState); BAIL_ON_NT_STATUS(ntStatus); pCtxSmb2->hState = pNotifyRequestState; InterlockedIncrement(&pNotifyRequestState->refCount); pCtxSmb2->pfnStateRelease = &SrvReleaseNotifyRequestStateHandle_SMB_V2; ntStatus = SrvTree2FindFile_SMB_V2( pCtxSmb2, pTree, &pRequestHeader->fid, &pNotifyRequestState->pFile); BAIL_ON_NT_STATUS(ntStatus); } LWIO_LOCK_MUTEX(bInLock, &pNotifyRequestState->mutex); switch (pNotifyRequestState->stage) { case SRV_NOTIFY_STAGE_SMB_V2_INITIAL: if (pNotifyRequestState->pRequestHeader->ulOutputBufferLength > SMB_CN_MAX_BUFFER_SIZE) { ntStatus = STATUS_INVALID_PARAMETER; BAIL_ON_NT_STATUS(ntStatus); } // A change notify request can occur as the only message in // a request, or the last in a chained request if (pCtxSmb2->iMsg != (pCtxSmb2->ulNumRequests - 1)) { ntStatus = STATUS_INTERNAL_ERROR; BAIL_ON_NT_STATUS(ntStatus); } ntStatus = SrvConnection2CreateAsyncState( pConnection, COM2_NOTIFY, &SrvCancelNotifyState_SMB_V2, &SrvNotifyStateReleaseHandle_SMB_V2, &pAsyncState); BAIL_ON_NT_STATUS(ntStatus); bUnregisterAsync = TRUE; ntStatus = SrvNotifyCreateState_SMB_V2( pAsyncState->ullAsyncId, pExecContext->pConnection, pCtxSmb2->pSession, pCtxSmb2->pTree, pNotifyRequestState->pFile, pSmbRequest->pHeader->usEpoch, pSmbRequest->pHeader->ullCommandSequence, pSmbRequest->pHeader->ulPid, pNotifyRequestState->pRequestHeader->ulCompletionFilter, (pNotifyRequestState->pRequestHeader->usFlags & SMB2_NOTIFY_FLAGS_WATCH_TREE), pNotifyRequestState->pRequestHeader->ulOutputBufferLength, &pNotifyState); BAIL_ON_NT_STATUS(ntStatus); pAsyncState->hAsyncState = SrvNotifyStateAcquire_SMB_V2(pNotifyState); pNotifyRequestState->stage = SRV_NOTIFY_STAGE_SMB_V2_ATTEMPT_IO; // intentional fall through case SRV_NOTIFY_STAGE_SMB_V2_ATTEMPT_IO: ntStatus = SrvExecuteChangeNotify_SMB_V2( pExecContext, pNotifyState); switch (ntStatus) { case STATUS_PENDING: { // TODO: Might have to cancel the entire operation // NTSTATUS ntStatus2 = SrvBuildInterimResponse_SMB_V2( pExecContext, pNotifyState->ullAsyncId); if (ntStatus2 != STATUS_SUCCESS) { LWIO_LOG_ERROR( "Failed to create interim response [code:0x%8x]", ntStatus2); } bUnregisterAsync = FALSE; } break; case STATUS_SUCCESS: // completed synchronously; remove asynchronous state // SrvConnection2RemoveAsyncState( pConnection, pAsyncState->ullAsyncId); pNotifyState->ullAsyncId = 0LL; bUnregisterAsync = FALSE; break; default: break; } BAIL_ON_NT_STATUS(ntStatus); pNotifyRequestState->stage = SRV_NOTIFY_STAGE_SMB_V2_BUILD_RESPONSE; // intentional fall through case SRV_NOTIFY_STAGE_SMB_V2_BUILD_RESPONSE: ntStatus = SrvBuildNotifyResponse_SMB_V2( pExecContext, pNotifyState); BAIL_ON_NT_STATUS(ntStatus); pNotifyRequestState->stage = SRV_NOTIFY_STAGE_SMB_V2_DONE; // intentional fall through case SRV_NOTIFY_STAGE_SMB_V2_DONE: break; } cleanup: if (pNotifyRequestState) { LWIO_UNLOCK_MUTEX(bInLock, &pNotifyRequestState->mutex); SrvReleaseNotifyRequestState_SMB_V2(pNotifyRequestState); } if (pNotifyState) { if (bUnregisterAsync) { SrvConnection2RemoveAsyncState( pConnection, pNotifyState->ullAsyncId); } SrvNotifyStateRelease_SMB_V2(pNotifyState); } if (pAsyncState) { SrvAsyncStateRelease(pAsyncState); } if (pTree) { SrvTree2Release(pTree); } if (pSession) { SrvSession2Release(pSession); } return ntStatus; error: switch (ntStatus) { case STATUS_PENDING: break; default: if (pNotifyState) { SrvReleaseNotifyStateAsync_SMB_V2(pNotifyState); } break; } goto cleanup; }