static VOID SrvTree2Free( PLWIO_SRV_TREE_2 pTree ) { LWIO_LOG_DEBUG("Freeing tree [object:0x%x][tid:%u]", pTree, pTree->ulTid); // Cannot be in the parent since parent would have a reference. LWIO_ASSERT(!SrvTree2IsInParent_inlock(pTree)); if (pTree->pMutex) { pthread_rwlock_destroy(&pTree->mutex); pTree->pMutex = NULL; } if (pTree->pFileCollection) { LwRtlRBTreeFree(pTree->pFileCollection); } if (pTree->hFile) { IoCloseFile(pTree->hFile); } if (pTree->pShareInfo) { SrvShareReleaseInfo(pTree->pShareInfo); } if (pTree->resource.ulResourceId) { PSRV_RESOURCE pResource = NULL; SrvElementsUnregisterResource(pTree->resource.ulResourceId, &pResource); pTree->resource.ulResourceId = 0; } // Release parent at the end if (pTree->pSession) { SrvSession2Release(pTree->pSession); } SrvFreeMemory(pTree); }
NTSTATUS SrvSession2Create( ULONG64 ullUid, PLWIO_SRV_SESSION_2* ppSession ) { NTSTATUS ntStatus = 0; PLWIO_SRV_SESSION_2 pSession = NULL; LWIO_LOG_DEBUG("Creating session [uid:%lu]", ullUid); ntStatus = SrvAllocateMemory( sizeof(LWIO_SRV_SESSION_2), (PVOID*)&pSession); BAIL_ON_NT_STATUS(ntStatus); pSession->refcount = 1; pthread_rwlock_init(&pSession->mutex, NULL); pSession->pMutex = &pSession->mutex; pSession->ullUid = ullUid; ntStatus = WireGetCurrentNTTime(&pSession->llBirthTime); BAIL_ON_NT_STATUS(ntStatus); pSession->llLastActivityTime = pSession->llBirthTime; LWIO_LOG_DEBUG("Associating session [object:0x%x][uid:%lu]", pSession, ullUid); ntStatus = LwRtlRBTreeCreate( &SrvSession2TreeCompare, NULL, &SrvSession2TreeRelease, &pSession->pTreeCollection); BAIL_ON_NT_STATUS(ntStatus); ntStatus = SrvFinderCreateRepository(&pSession->hFinderRepository); BAIL_ON_NT_STATUS(ntStatus); SRV_ELEMENTS_INCREMENT_SESSIONS; *ppSession = pSession; cleanup: return ntStatus; error: *ppSession = NULL; if (pSession) { SrvSession2Release(pSession); } goto cleanup; }
NTSTATUS SrvProcessFlush_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; PSRV_FLUSH_STATE_SMB_V2 pFlushState = NULL; PLWIO_SRV_SESSION_2 pSession = NULL; PLWIO_SRV_TREE_2 pTree = NULL; PLWIO_SRV_FILE_2 pFile = NULL; BOOLEAN bInLock = FALSE; pFlushState = (PSRV_FLUSH_STATE_SMB_V2)pCtxSmb2->hState; if (pFlushState) { InterlockedIncrement(&pFlushState->refCount); } else { ULONG iMsg = pCtxSmb2->iMsg; PSRV_MESSAGE_SMB_V2 pSmbRequest = &pCtxSmb2->pRequests[iMsg]; PSMB2_FID pFid = 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 = SMB2UnmarshalFlushRequest(pSmbRequest, &pFid); BAIL_ON_NT_STATUS(ntStatus); ntStatus = SrvTree2FindFile_SMB_V2( pCtxSmb2, pTree, pFid, LwIsSetFlag( pSmbRequest->pHeader->ulFlags, SMB2_FLAGS_RELATED_OPERATION), &pFile); BAIL_ON_NT_STATUS(ntStatus); ntStatus = SrvBuildFlushState_SMB_V2( pFid, pFile, &pFlushState); BAIL_ON_NT_STATUS(ntStatus); pCtxSmb2->hState = pFlushState; InterlockedIncrement(&pFlushState->refCount); pCtxSmb2->pfnStateRelease = &SrvReleaseFlushStateHandle_SMB_V2; } LWIO_LOCK_MUTEX(bInLock, &pFlushState->mutex); switch (pFlushState->stage) { case SRV_FLUSH_STAGE_SMB_V2_INITIAL: pFlushState->stage = SRV_FLUSH_STAGE_SMB_V2_FLUSH_COMPLETED; SrvPrepareFlushStateAsync_SMB_V2(pFlushState, pExecContext); ntStatus = IoFlushBuffersFile( pFlushState->pFile->hFile, pFlushState->pAcb, &pFlushState->ioStatusBlock); BAIL_ON_NT_STATUS(ntStatus); SrvReleaseFlushStateAsync_SMB_V2(pFlushState); // completed synchronously // intentional fall through case SRV_FLUSH_STAGE_SMB_V2_FLUSH_COMPLETED: ntStatus = pFlushState->ioStatusBlock.Status; BAIL_ON_NT_STATUS(ntStatus); pFlushState->stage = SRV_FLUSH_STAGE_SMB_V2_BUILD_RESPONSE; // intentional fall through case SRV_FLUSH_STAGE_SMB_V2_BUILD_RESPONSE: ntStatus = SrvBuildFlushResponse_SMB_V2(pExecContext); BAIL_ON_NT_STATUS(ntStatus); pFlushState->stage = SRV_FLUSH_STAGE_SMB_V2_DONE; // intentional fall through case SRV_FLUSH_STAGE_SMB_V2_DONE: break; } cleanup: if (pFile) { SrvFile2Release(pFile); } if (pTree) { SrvTree2Release(pTree); } if (pSession) { SrvSession2Release(pSession); } if (pFlushState) { LWIO_UNLOCK_MUTEX(bInLock, &pFlushState->mutex); SrvReleaseFlushState_SMB_V2(pFlushState); } 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: if (pFlushState) { SrvReleaseFlushStateAsync_SMB_V2(pFlushState); } break; } goto cleanup; }
NTSTATUS SrvProcessGetInfo_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; PSRV_GET_INFO_STATE_SMB_V2 pGetInfoState = NULL; PLWIO_SRV_SESSION_2 pSession = NULL; PLWIO_SRV_TREE_2 pTree = NULL; PLWIO_SRV_FILE_2 pFile = NULL; BOOLEAN bInLock = FALSE; pGetInfoState = (PSRV_GET_INFO_STATE_SMB_V2)pCtxSmb2->hState; if (pGetInfoState) { InterlockedIncrement(&pGetInfoState->refCount); } else { ULONG iMsg = pCtxSmb2->iMsg; PSRV_MESSAGE_SMB_V2 pSmbRequest = &pCtxSmb2->pRequests[iMsg]; PSMB2_GET_INFO_REQUEST_HEADER pRequestHeader = NULL; // Do not free PBYTE pInputBuffer = NULL; ULONG ulInputBufferLength = 0; 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 = SMB2UnmarshalGetInfoRequest( pSmbRequest, &pRequestHeader, &pInputBuffer, &ulInputBufferLength); BAIL_ON_NT_STATUS(ntStatus); SRV_LOG_DEBUG( pExecContext->pLogContext, SMB_PROTOCOL_VERSION_2, pSmbRequest->pHeader->command, "Get Info 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)," "info-class(0x%x),info-type(0x%x),flags(0x%x)," "input-buffer-length(%u),input-buffer-offset(%u)," "output-buffer-length(%u),additional-info(%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->ucInfoClass, pRequestHeader->ucInfoType, pRequestHeader->ulFlags, pRequestHeader->ulInputBufferLen, pRequestHeader->usInputBufferOffset, pRequestHeader->ulOutputBufferLen, pRequestHeader->ulAdditionalInfo); ntStatus = SrvTree2FindFile_SMB_V2( pCtxSmb2, pTree, &pRequestHeader->fid, &pFile); BAIL_ON_NT_STATUS(ntStatus); ntStatus = SrvBuildGetInfoState_SMB_V2( pRequestHeader, pFile, pInputBuffer, ulInputBufferLength, &pGetInfoState); BAIL_ON_NT_STATUS(ntStatus); pCtxSmb2->hState = pGetInfoState; InterlockedIncrement(&pGetInfoState->refCount); pCtxSmb2->pfnStateRelease = &SrvReleaseGetInfoStateHandle_SMB_V2; } LWIO_LOCK_MUTEX(bInLock, &pGetInfoState->mutex); switch (pGetInfoState->stage) { case SRV_GET_INFO_STAGE_SMB_V2_INITIAL: pGetInfoState->stage = SRV_GET_INFO_STAGE_SMB_V2_ATTEMPT_IO; // Intentional fall through case SRV_GET_INFO_STAGE_SMB_V2_ATTEMPT_IO: ntStatus = SrvQueryInfo_SMB_V2(pExecContext); BAIL_ON_NT_STATUS(ntStatus); pGetInfoState->stage = SRV_GET_INFO_STAGE_SMB_V2_BUILD_RESPONSE; // Intentional fall through case SRV_GET_INFO_STAGE_SMB_V2_BUILD_RESPONSE: ntStatus = SrvBuildGetInfoResponse_SMB_V2(pExecContext); BAIL_ON_NT_STATUS(ntStatus); pGetInfoState->stage = SRV_GET_INFO_STAGE_SMB_V2_DONE; // Intentional fall through case SRV_GET_INFO_STAGE_SMB_V2_DONE: break; } cleanup: if (pFile) { SrvFile2Release(pFile); } if (pTree) { SrvTree2Release(pTree); } if (pSession) { SrvSession2Release(pSession); } if (pGetInfoState) { LWIO_UNLOCK_MUTEX(bInLock, &pGetInfoState->mutex); SrvReleaseGetInfoState_SMB_V2(pGetInfoState); } 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: if (pGetInfoState) { SrvReleaseGetInfoStateAsync_SMB_V2(pGetInfoState); } break; } goto cleanup; }
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; }