ClRcT
clLogStreamOwnerCkptDsIdDelete(ClLogSOEoDataT  *pSoEoEntry,
                               ClUint32T       dsId) 
{
    ClRcT           rc      = CL_OK; 
    ClLogSvrCommonEoDataT  *pCommonEoData = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogStreamOwnerEoEntryGet(NULL, &pCommonEoData);
    if( CL_OK != rc )
    {
        return rc;
    }    

    rc = clBitmapBitClear(pSoEoEntry->hDsIdMap, dsId);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBitmapBitClear(): rc[0x %x]", rc));
        return rc;
    }    
    CL_LOG_CLEANUP(clCkptLibraryCkptDataSetDelete(pCommonEoData->hLibCkpt,
                                                  (SaNameT *) &gSOLocalCkptName, dsId),
                  CL_OK);
    CL_LOG_CLEANUP(clCkptLibraryCkptDataSetWrite(pCommonEoData->hLibCkpt, 
                                                 (SaNameT *) &gSOLocalCkptName,
                                                 CL_LOG_SO_DSID_START,
                                                 pSoEoEntry),
                 CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}    
void
clLogStreamOwnerDataDump(ClCharT **retval)
{
    ClRcT                  rc              = CL_OK;
    ClDebugPrintHandleT    msg             = 0;
    ClLogSOEoDataT         *pSoEoEntry     = NULL;
    ClLogSvrCommonEoDataT  *pCommonEoEntry = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));
    
    clDebugPrintInitialize(&msg);

    rc = clLogStreamOwnerEoEntryGet(&pSoEoEntry, &pCommonEoEntry);
    if( CL_OK != rc )
    {
        return ;
    }    
    rc = clLogSOLock(pSoEoEntry, CL_LOG_STREAM_GLOBAL);
    if( CL_OK != rc )
    {
        return ;
    }
    clLogStreamOwnerEoDataPrint(pSoEoEntry, &msg);

    if( CL_HANDLE_INVALID_VALUE != pSoEoEntry->hGStreamOwnerTable )
    {
        rc = clCntWalk(pSoEoEntry->hGStreamOwnerTable, clLogSOTableWalkForPrint, 
                &msg, 0);
        if( CL_OK != rc )
        {
            CL_LOG_DEBUG_ERROR(("clCntWalk(): rc[0x %x]", rc));
            CL_LOG_CLEANUP(clLogSOUnlock(pSoEoEntry, CL_LOG_STREAM_GLOBAL), CL_OK);
            return ;
        }
    }
    CL_LOG_CLEANUP(clLogSOUnlock(pSoEoEntry, CL_LOG_STREAM_GLOBAL), CL_OK);

    rc = clLogSOLock(pSoEoEntry, CL_LOG_STREAM_LOCAL);
    if( CL_OK != rc )
    {
        return ;
    }
    if( CL_HANDLE_INVALID_VALUE != pSoEoEntry->hLStreamOwnerTable )
    {
        rc = clCntWalk(pSoEoEntry->hLStreamOwnerTable, clLogSOTableWalkForPrint, 
                &msg, 0);
        if( CL_OK != rc )
        {
            CL_LOG_DEBUG_ERROR(("clCntWalk(): rc[0x %x]", rc));
            CL_LOG_CLEANUP(clLogSOUnlock(pSoEoEntry, CL_LOG_STREAM_LOCAL), CL_OK);
            return ;
        }
    }
    CL_LOG_CLEANUP(clLogSOUnlock(pSoEoEntry, CL_LOG_STREAM_LOCAL), CL_OK);

    clDebugPrintFinalize(&msg, retval);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return ;
}
static ClRcT
clLogSvrSOFGResponseProcess(ClLogFilterT  *pStreamFilter,
                            ClPtrT        pCookie)
{
    ClRcT                  rc                 = CL_OK;
    ClLogSvrEoDataT        *pSvrEoEntry       = NULL;
    ClCntNodeHandleT       hStreamOwnerNode   = (ClCntNodeHandleT) pCookie;
    ClLogSvrStreamDataT    *pStreamData       = NULL;
    ClLogSvrCommonEoDataT  *pSvrCommonEoEntry = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogSvrEoEntryGet(&pSvrEoEntry, &pSvrCommonEoEntry);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clOsalMutexLock_L(&pSvrEoEntry->svrStreamTableLock);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clCntNodeUserDataGet(pSvrEoEntry->hSvrStreamTable, hStreamOwnerNode,
                              (ClCntDataHandleT *) &pStreamData);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clCntNodeUserDataGet(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&pSvrEoEntry->svrStreamTableLock), CL_OK);
        return rc;
    }

    rc = clLogServerStreamMutexLock(pStreamData);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalMutexLock(): rc[0x %x]\n", rc));
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&pSvrEoEntry->svrStreamTableLock), CL_OK);
        return rc;
    }
    CL_LOG_CLEANUP(clOsalMutexUnlock_L(&pSvrEoEntry->svrStreamTableLock), CL_OK);

    /*
     * Log Server will not intimate the client about this filter update,
     * as it is recreating the state for itself and filter may or may
     * not have been updated during that transient period.
     */
    rc = clLogFilterAssign(pStreamData->pStreamHeader, pStreamFilter);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogFilterAssign(): rc[0x %x]\n", rc));
    }

    CL_LOG_CLEANUP(clLogServerStreamMutexUnlock(pStreamData),CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
static ClRcT
clLogSvrDsIdMapRecreate(ClUint32T  dsId,
                        ClAddrT    pBuffer,
                        ClUint32T  buffSize,
                        ClPtrT     pCookie)
{
    ClRcT            rc           = CL_OK;
    ClLogSvrEoDataT  *pSvrEoEntry = NULL;
    ClBufferHandleT  inMsg        = CL_HANDLE_INVALID_VALUE;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((NULL == pBuffer), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((CL_LOG_DSID_START != dsId),
                     CL_LOG_RC(CL_ERR_INVALID_PARAMETER));

    rc = clLogSvrEoEntryGet(&pSvrEoEntry, NULL);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clBufferCreate(&inMsg);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate(): rc[0x %x]", rc));
        return rc;
    }
    rc = clBufferNBytesWrite(inMsg, (ClUint8T *) pBuffer, buffSize);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferNBytesWrite(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&inMsg), CL_OK);
        return rc;
    }

    rc = clXdrUnmarshallClUint32T(inMsg, &pSvrEoEntry->nextDsId);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrUnmarshallClUint32T(): rc[0x %x]\n", rc));
        CL_LOG_CLEANUP(clBufferDelete(&inMsg), CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("DsIdCnt: %d", pSvrEoEntry->nextDsId));

    rc = clLogBitmapUnpack(inMsg, pSvrEoEntry->hDsIdMap);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogBitmapUnpack(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&inMsg), CL_OK);
        return rc;
    }
    CL_LOG_CLEANUP(clBufferDelete(&inMsg), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
static ClRcT
clLogMasterFileEntryRecover(ClLogMasterEoDataT      *pMasterEoEntry,
                            ClCkptIOVectorElementT  *pIoVector,
                            ClUint32T               *pErrIndex)
{
    ClRcT                rc            = CL_OK;
    ClBufferHandleT      hFileEntryBuf = CL_HANDLE_INVALID_VALUE;
    ClVersionT version = {0};

    CL_LOG_DEBUG_TRACE(("Enter: size %lld", pIoVector->readSize));

    rc = clBufferCreate(&hFileEntryBuf);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate(): rc[0x %x]", rc));
        return rc;
    }

    rc = clBufferNBytesWrite(hFileEntryBuf, (ClUint8T*) pIoVector->dataBuffer, pIoVector->readSize);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferNBytesWrite(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&hFileEntryBuf), CL_OK);
        return rc;
    }

    rc = clXdrUnmarshallClVersionT(hFileEntryBuf, &version);

    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrUnmarshallClVersionT(): rc[%#x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&hFileEntryBuf), CL_OK);
        return rc;
    }

    switch(CL_VERSION_CODE(version.releaseCode, version.majorVersion, version.minorVersion))
    {
    case CL_VERSION_CODE(CL_RELEASE_VERSION, CL_MAJOR_VERSION, CL_MINOR_VERSION):
    {
        rc = fileEntryRecoverBaseVersion(pMasterEoEntry, hFileEntryBuf);
    }
    break;
    default:
        rc = CL_LOG_RC(CL_ERR_VERSION_MISMATCH);
        clLogError("FILE", "RECOVER", "Version [%d.%d.%d] unsupported",
                   version.releaseCode, version.majorVersion, version.minorVersion);
        break;
    }

    CL_LOG_CLEANUP(clBufferDelete(&hFileEntryBuf), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogMasterCompData2BufferGet(ClLogCompDataT  *pCompData,
                              ClUint8T        **ppBuffer,
                              ClUint32T       *pDataSize)
{
    ClRcT            rc  = CL_OK;
    ClBufferHandleT  msg = CL_HANDLE_INVALID_VALUE;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clBufferCreate(&msg);
    if(CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate(): rc[0x %x]", rc));
        return rc;
    }

    rc = VDECL_VER(clXdrMarshallClLogCompDataT, 4, 0, 0)(pCompData, msg, 0);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogMarshallClLogCompDataT(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        return rc;
    }

    rc = clBufferLengthGet(msg, pDataSize);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferLengthGet(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        return rc;
    }

    *ppBuffer = (ClUint8T*) clHeapCalloc(*pDataSize, sizeof(ClUint8T));
    if( NULL == *ppBuffer )
    {
        CL_LOG_DEBUG_ERROR(("clHeapCalloc()"));
        CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }

    rc = clBufferNBytesRead(msg, *ppBuffer, pDataSize);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferNBytesRead(): rc[0x %x]", rc));
        clHeapFree(*ppBuffer);
        CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        return rc;
    }
    CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return CL_OK;
}
ClRcT
clLogSODsIdMapRecreate(ClUint32T  dsId,
                       ClAddrT    pBuffer,
                       ClUint32T  size,
                           ClPtrT      cookie)
{
    ClRcT                   rc        = CL_OK;
    ClBufferHandleT        msg            = CL_HANDLE_INVALID_VALUE;
    ClLogSOEoDataT         *pSoEoEntry    = NULL; 
    ClLogSvrCommonEoDataT  *pCommonEoData = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((CL_LOG_SO_DSID_START != dsId),
                     CL_LOG_RC(CL_ERR_INVALID_PARAMETER));
    CL_LOG_PARAM_CHK((NULL == pBuffer), CL_LOG_RC(CL_ERR_NULL_POINTER));

    rc = clLogStreamOwnerEoEntryGet(&pSoEoEntry, &pCommonEoData);
    if( CL_OK != rc )
    {
        return rc;
    }    

    rc = clBufferCreate(&msg);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate(): rc[0x %x]", rc));
        return rc;
    }
    rc = clBufferNBytesWrite(msg, (ClUint8T *) pBuffer, size); 
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferNBytesRead(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        return rc;
    }

    rc = clXdrUnmarshallClUint32T(msg, &pSoEoEntry->dsIdCnt);
    if( CL_OK != rc )
    {
       CL_LOG_DEBUG_ERROR(("clXdrUnmarshallClUint32T(): rc[0x %x]", rc));
       CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        return rc;
    }    
    CL_LOG_DEBUG_VERBOSE(("dsIdCnt : %d", pSoEoEntry->dsIdCnt));

    rc = clLogBitmapUnpack(msg, pSoEoEntry->hDsIdMap);

    CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit: rc[0x %x]", rc));
    return rc;
}    
ClRcT
clLogMasterCompTableStateRecover(ClLogMasterEoDataT  *pMasterEoEntry,
                                 ClUint8T            *pBuffer,
                                 ClUint32T           dataSize)
{
    ClRcT            rc       = CL_OK;
    ClBufferHandleT  msg      = CL_HANDLE_INVALID_VALUE;
    ClUint32T versionCode = 0;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clBufferCreate(&msg);
    if(CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate(): rc[0x %x]", rc));
        return rc;
    }
    rc = clBufferNBytesWrite(msg, pBuffer, dataSize);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferNBytesWrite(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        return rc;
    }

    rc = clXdrUnmarshallClUint32T(msg, &versionCode);
    if( CL_OK != rc)
    {
        CL_LOG_DEBUG_ERROR(("clXdrUnmarshallClUint32T(): rc[%#x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        return rc;
    }
    switch(versionCode)
    {

    case CL_VERSION_CODE(CL_RELEASE_VERSION, CL_MAJOR_VERSION, CL_MINOR_VERSION):
    {
        rc = compTableStateRecoverBaseVersion(msg);
    }
    break;

    default:
        clLogError("COMP", "TBL-RECOVER", "Version [%d.%d.%d] is not supported",
                   CL_VERSION_RELEASE(versionCode), CL_VERSION_MAJOR(versionCode),
                   CL_VERSION_MINOR(versionCode));
        rc = CL_LOG_RC(CL_ERR_VERSION_MISMATCH);
    }

    clBufferDelete(&msg);

    return rc;
}
static ClRcT
clLogSvrStreamEntryRecreate(ClUint32T  dsId,
                            ClAddrT    pBuffer,
                            ClUint32T  buffSize,
                            ClPtrT     cookie)
{

    ClRcT                  rc                 = CL_OK;
    ClLogSvrEoDataT        *pSvrEoEntry       = NULL;
    ClBufferHandleT        inMsg              = CL_HANDLE_INVALID_VALUE;
    ClLogSvrCommonEoDataT  *pSvrCommonEoEntry = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((NULL == pBuffer), CL_LOG_RC(CL_ERR_NULL_POINTER));

    rc = clLogSvrEoEntryGet(&pSvrEoEntry, &pSvrCommonEoEntry);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clBufferCreate(&inMsg);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate(): rc[0x %x]", rc));
        return rc;
    }
    rc = clBufferNBytesWrite(inMsg, (ClUint8T *) pBuffer, buffSize);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferNBytesWrite(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&inMsg), CL_OK);
        return rc;
    }

    rc = clLogSvrStreamEntryUnpackNAdd(pSvrEoEntry, pSvrCommonEoEntry, inMsg, dsId);
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clBufferDelete(&inMsg), CL_OK);
        return rc;
    }

    CL_LOG_CLEANUP(clBufferDelete(&inMsg), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
void clLogSvrTerminate(SaInvocationT invocation, const SaNameT *compName)
{
    
    ClRcT            rc           = CL_OK;
    ClLogSvrEoDataT  *pSvrEoEntry = NULL;
    //ClHandleT        hCpm         = CL_HANDLE_INVALID_VALUE;
    CL_LOG_DEBUG_TRACE(("Enter"));
    clLogInfo("SVR", "MAI", "Unregistering with cpm...... [%.*s]\n", compName->length, compName->value);
    gClLogSvrExiting = CL_TRUE;
    rc = clLogSvrEoEntryGet(&pSvrEoEntry, NULL);
    if( CL_OK != rc )
    {
        return ;
    }
    //hCpm = pSvrEoEntry->hCpm;
    saAmfComponentUnregister(amfHandle,compName, NULL);
#if 1
    CL_LOG_CLEANUP(clLogSvrShutdown(), CL_OK);
#endif    
    saAmfResponse(amfHandle, invocation, SA_AIS_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));

    unblockNow = CL_TRUE;
    //(void)hCpm;

}
ClRcT
clLogStreamOwnerLocalCkptCreate(ClCkptSvcHdlT   hLibInit) 
{    
    ClRcT    rc       = CL_OK;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clCkptLibraryCkptCreate(hLibInit, (SaNameT *) &gSOLocalCkptName);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clCkptLibraryCreate(): rc[0x %x]", rc));
        return rc;
    }    

    rc = clCkptLibraryCkptDataSetCreate(hLibInit, 
                                        (SaNameT *) &gSOLocalCkptName, 
                                        CL_LOG_DSID_START, 0, 0,
                                        clLogSODsIdMapPack,
                                        clLogSODsIdMapRecreate);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clCkptLibraryCkptDataSetCreate(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clCkptLibraryCkptDelete(hLibInit, 
                                               (SaNameT *) &gSOLocalCkptName),
                       CL_OK);
    }    
        
    CL_LOG_DEBUG_TRACE(("Checkpoint is created: %s  %d \n",
                gSOLocalCkptName.value, CL_LOG_DSID_START));
    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}    
ClRcT
clLogFlusherCondSignal(ClLogStreamHeaderT  *pHeader)
{
    ClRcT  rc = CL_OK;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clOsalMutexLock_L(&pHeader->lock_for_join);
    if( CL_OK != rc )
    {
        clLogError("LOG", "FLS", "clOsalMutexLock_L(): rc[0x %x]", rc);
        return rc;
    }    

    pHeader->streamStatus = CL_LOG_STREAM_THREAD_EXIT;
    rc = clOsalCondSignal_L(&pHeader->cond_for_join);
    if( CL_OK != rc )
    {
        clLogError("LOG", "FLS", "clOsalMutexLock_L(): rc[0x %x]", rc);
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&pHeader->lock_for_join), CL_OK);
        return rc;
    }    

    rc = clOsalMutexUnlock_L(&pHeader->lock_for_join);
    if( CL_OK != rc )
    {
        clLogError("LOG", "FLS", "clOsalMutexUnlock_L(): rc[0x %x]", rc);
        return rc;
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}    
static ClRcT fileEntryRecoverBaseVersion(ClLogMasterEoDataT *pMasterEoEntry,
        ClBufferHandleT hFileEntryBuf)
{
    ClRcT rc = CL_OK;
    ClLogFileKeyT        fileKey       = {{0}};
    ClLogFileKeyT        *pFileKey     = NULL;
    ClLogFileDataT       *pFileData    = NULL;

    rc = clLogMasterFileKeyUnpack(&fileKey, hFileEntryBuf);
    if( CL_OK != rc )
    {
        return rc;
    }
    clLogInfo(CL_LOG_AREA_MASTER, CL_LOG_CTX_CKPT_READ,
              "Recreating files fileName: %.*s fileLocation: %.*s",
              fileKey.fileName.length, fileKey.fileName.pValue,
              fileKey.fileLocation.length, fileKey.fileLocation.pValue);

    rc = clLogFileKeyCreate(&fileKey.fileName, &fileKey.fileLocation,
                            pMasterEoEntry->maxFiles, &pFileKey);
    if( CL_OK != rc )
    {
        clHeapFree(fileKey.fileName.pValue);
        clHeapFree(fileKey.fileLocation.pValue);
        return rc;
    }
    clHeapFree(fileKey.fileName.pValue);
    clHeapFree(fileKey.fileLocation.pValue);

    /* find out the filelocation is available */
    pFileData = (ClLogFileDataT*) clHeapCalloc(1, sizeof(ClLogFileDataT));
    if( NULL == pFileData )
    {
        CL_LOG_DEBUG_ERROR(("clHeapCalloc()"));
        clLogFileKeyDestroy(pFileKey);
        return rc;
    }

    pFileData->nActiveStreams = 0;
    rc = clLogMasterFileDataRecreate(pFileData, hFileEntryBuf);
    if( CL_OK != rc )
    {
        clLogFileKeyDestroy(pFileKey);
        return rc;
    }

    rc = clCntNodeAdd(pMasterEoEntry->hMasterFileTable,
                      (ClCntKeyHandleT) pFileKey,
                      (ClCntDataHandleT) pFileData, NULL);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clCntNodeAdd()"));
        CL_LOG_CLEANUP(clCntDelete(pFileData->hStreamTable), CL_OK); //FIXME
        clHeapFree(pFileData);
        clLogFileKeyDestroy(pFileKey);
        return rc;
    }

    return rc;
}
ClRcT
clLogClntStreamListUnpack(ClUint32T         numStreams,
                          ClUint32T         buffLen,
                          ClUint8T          *pBuffer,
                          ClLogStreamInfoT  **ppLogStreams)
{
    ClRcT            rc  = CL_OK;
    ClBufferHandleT  msg = CL_HANDLE_INVALID_VALUE;

    CL_LOG_DEBUG_TRACE(("Enter"));

    *ppLogStreams = clHeapCalloc(numStreams, sizeof(ClLogStreamInfoT));
    if( NULL == *ppLogStreams )
    {
        CL_LOG_DEBUG_ERROR(("clHeapCalloc()"));
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }

    rc = clBufferCreate(&msg);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate(): rc[0x %x]", rc));
        clHeapFree(*ppLogStreams);
        return rc;
    }
    rc = clBufferNBytesWrite(msg, pBuffer, buffLen);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferNBytesWrite(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        clHeapFree(*ppLogStreams);
        return rc;
    }

    rc = clLogClntStreamListGet(msg, *ppLogStreams);
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        clHeapFree(*ppLogStreams);
        return rc;
    }

    CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
   
    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogStreamOwnerCkptDsIdGet(ClLogSOEoDataT  *pSoEoEntry, 
                            ClUint32T       *pDsId)
{
    ClRcT             rc         = CL_OK;
    ClLogSvrCommonEoDataT  *pCommonEoData = NULL;
    ClUint32T              dsId           = 0;

    CL_LOG_DEBUG_TRACE(("Enter"));
    
    rc = clLogStreamOwnerEoEntryGet(NULL, &pCommonEoData);
    if( CL_OK != rc )
    {
        return rc;
    }    

    dsId = pSoEoEntry->dsIdCnt;
    dsId++;
    rc = clBitmapBitSet(pSoEoEntry->hDsIdMap, dsId);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBitmapBitSet(): rc[0x %x]", rc));
        return rc;
    }    
    rc = clCkptLibraryCkptDataSetCreate(pCommonEoData->hLibCkpt, 
                                        (SaNameT *) &gSOLocalCkptName,
                                        dsId, 0, 0, 
                                        clLogStreamOwnerEntrySerialiser,
                                        clLogSOStreamEntryRecreate);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clCkptLibraryCkptDataSetCreate(): rc[0x %x]",
                    rc));
        CL_LOG_CLEANUP(clBitmapBitClear(pSoEoEntry->hDsIdMap, dsId), CL_OK);
        return rc;
    }    
    pSoEoEntry->dsIdCnt = dsId;
    *pDsId = dsId;

    CL_LOG_CLEANUP(clCkptLibraryCkptDataSetWrite(pCommonEoData->hLibCkpt, 
                                       (SaNameT *) &gSOLocalCkptName,
                                       CL_LOG_SO_DSID_START,
                                       pSoEoEntry), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}    
/*
 * Function - clLogStreamOwnerLocalCheckpoint()
 * - Do Local Checkpointing.
 * - If the dataset is not yet created, create the dataSet
 * - Write onto it.
 */
ClRcT
clLogStreamOwnerLocalCheckpoint(ClLogSOEoDataT         *pSoEoEntry,
                                SaNameT                *pStreamName,
                                SaNameT                *pStreamScopeNode,
                                ClLogStreamOwnerDataT  *pStreamOwnerData)
{
    ClRcT                  rc              = CL_OK;
    ClBufferHandleT     msg               = CL_HANDLE_INVALID_VALUE;
    ClLogSvrCommonEoDataT  *pCommonEoData = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogStreamOwnerEoEntryGet(NULL, &pCommonEoData);
    if( CL_OK != rc )
    {
        return rc;
    }    
    rc = clBufferCreate(&msg);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate(): rc[0x %x]", rc));
        return rc;
    }    
    rc = clLogStreamOwnerEntryPack(pStreamName, pStreamScopeNode, 
                                   pStreamOwnerData, msg);
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        return rc;
    }    
    rc = clCkptLibraryCkptDataSetWrite(pCommonEoData->hLibCkpt,
                                        (SaNameT *) &gSOLocalCkptName, 
                                        pStreamOwnerData->dsId,
                                        msg);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(( "clCkptLibraryCkptDataSetWrite(): rc[0x %x]", rc));
    }    
    CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        
    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}    
void
clLogClntStreamDeleteCb(ClCntKeyHandleT   userKey,
                        ClCntDataHandleT  userData)
{
    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_CLEANUP(
        clLogClntStreamDataCleanup((ClLogClntStreamDataT *) userData), CL_OK);
    clHeapFree((ClLogClntStreamDataT *) userData);
    clLogStreamKeyDestroy((ClLogStreamKeyT *) userKey);

    CL_LOG_DEBUG_TRACE(("Exit"));
}
ClRcT
clLogSvrEoDataInit(ClLogSvrEoDataT        *pSvrEoEntry,
                   ClLogSvrCommonEoDataT  *pSvrCommonEoEntry)
{
    ClRcT               rc = CL_OK;

    CL_LOG_DEBUG_TRACE(("Enter"));

    pSvrEoEntry->hSvrStreamTable = CL_HANDLE_INVALID_VALUE;
    pSvrEoEntry->nextDsId        = CL_LOG_DSID_START;

    rc = clOsalMutexInit_L(&pSvrEoEntry->svrStreamTableLock);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalMutexCreate(): rc[0x %x]", rc));
        return rc;
    }

    rc = clBitmapCreate(&pSvrEoEntry->hDsIdMap, pSvrCommonEoEntry->maxStreams);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBitmapCreate(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clOsalMutexDestroy_L(&pSvrEoEntry->svrStreamTableLock),
                       CL_OK);
        return rc;
    }
    rc = clHandleDatabaseCreate(NULL, &pSvrEoEntry->hFlusherDB); 
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clHandleDatabaseCreate(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBitmapDestroy(pSvrEoEntry->hDsIdMap), CL_OK);
        CL_LOG_CLEANUP(clOsalMutexDestroy_L(&pSvrEoEntry->svrStreamTableLock),
                       CL_OK);
        return rc;
    }

    rc = clLogConfigDataGet(NULL, NULL, NULL, &pSvrEoEntry->maxFlushLimit);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogConfigDataGet(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clHandleDatabaseDestroy(pSvrEoEntry->hFlusherDB), CL_OK);
        CL_LOG_CLEANUP(clBitmapDestroy(pSvrEoEntry->hDsIdMap), CL_OK);
        CL_LOG_CLEANUP(clOsalMutexDestroy_L(&pSvrEoEntry->svrStreamTableLock),
                       CL_OK);
        return rc;
    }
    pSvrEoEntry->hCpm           = CL_HANDLE_INVALID_VALUE;    
    pSvrEoEntry->logCompId      = 0;
    pSvrEoEntry->gmsInit        = CL_FALSE; 
    pSvrEoEntry->evtInit        = CL_FALSE; 
    pSvrEoEntry->ckptInit       = CL_FALSE; 
    pSvrEoEntry->logInit        = CL_FALSE;
    pSvrEoEntry->hTimer         = CL_FALSE;

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogSvrEoDataFinalize(void)
{
    ClRcT            rc           = CL_OK;
    ClLogSvrEoDataT  *pSvrEoEntry = NULL;

	CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogSvrEoEntryGet(&pSvrEoEntry, NULL);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogSvrEoEntryGet(): rc[0x%x]", rc));
        return rc;
    }

    rc = clOsalMutexLock_L(&pSvrEoEntry->svrStreamTableLock);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalMutexLock_L(): rc[0x %x]", rc));
        return rc;
    }

    if( CL_HANDLE_INVALID_VALUE != pSvrEoEntry->hSvrStreamTable )
    {
        CL_LOG_CLEANUP(clCntDelete(pSvrEoEntry->hSvrStreamTable), CL_OK);
        pSvrEoEntry->hSvrStreamTable = CL_HANDLE_INVALID_VALUE;
    }
    pSvrEoEntry->logInit = CL_FALSE;
    CL_LOG_CLEANUP(clBitmapDestroy(pSvrEoEntry->hDsIdMap), CL_OK);
    CL_LOG_CLEANUP(clOsalMutexUnlock_L(&pSvrEoEntry->svrStreamTableLock),
            CL_OK);
    CL_LOG_CLEANUP(clOsalMutexDestroy_L(&pSvrEoEntry->svrStreamTableLock),
            CL_OK);

	CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogFileClose(ClLogFileHandleT  hFileHdlr)
{
    ClRcT             rc   = CL_OK;
    ClLogFileHandleT  hLog = CL_HANDLE_INVALID_VALUE;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogFileHdlrFileHandleDelete(hFileHdlr, &hLog);
    if( CL_OK != rc )
    {
        return rc;
    }
    CL_LOG_CLEANUP(clLogInitHandleBitmapFileRemove(hLog, hFileHdlr), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogInitHandleBitmapFileRemove(ClLogHandleT      hLog, 
                                ClLogFileHandleT  hFileHdlr)
{
    ClRcT                  rc            = CL_OK;
    ClLogInitHandleDataT  *pData         = NULL;
    ClLogClntEoDataT      *pClntEoEntry  = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));
        
    rc = clLogClntEoEntryGet(&pClntEoEntry);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogClntEoEntryGet(): rc[0x %x]", rc));
        return rc;
    }

    rc = clHandleCheckout(pClntEoEntry->hClntHandleDB, hLog,
                          (void **) (&pData)); 
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clHandleCheckout(): rc[0x %x]\n", rc));
        return rc;
    }

    rc = clBitmapBitClear(pData->hStreamBitmap, hFileHdlr);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBitmapBitSet(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clHandleCheckin(pClntEoEntry->hClntHandleDB, hLog),
                       CL_OK);
        return rc;
    }

    rc = clHandleCheckin(pClntEoEntry->hClntHandleDB, hLog); 
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clHandleCheckin(): rc[0x %x]", rc));
        return rc;
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
} 
ClRcT
clLogMasterCkptDestroy(void)
{
    ClRcT                  rc              = CL_OK;
    ClLogSvrCommonEoDataT  *pCommonEoEntry = NULL;
    ClLogMasterEoDataT     *pMasterEoEntry = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogMasterEoEntryGet(&pMasterEoEntry, &pCommonEoEntry);
    if( CL_OK != rc )
    {
        return rc;
    }
    CL_LOG_CLEANUP(clCkptCheckpointClose(pMasterEoEntry->hCkpt), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogSOStreamEntryRecreate(ClUint32T  dsId,
                           ClAddrT    pBuffer,
                           ClUint32T  size,
                           ClPtrT     cookie)
{
    ClRcT                  rc              = CL_OK;
    ClBufferHandleT        msg            = CL_HANDLE_INVALID_VALUE;
    ClLogSvrCommonEoDataT  *pCommonEoData = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogStreamOwnerEoEntryGet(NULL, &pCommonEoData);
    if( CL_OK != rc )
    {
        return rc;
    }    

    rc = clBufferCreate(&msg);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate()"));
        return rc;
    }    
    rc = clBufferNBytesWrite(msg, (ClUint8T *) pBuffer, size);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferNBytesWrite(): rc[0x %x]", rc));
        clBufferDelete(&msg);
        return rc;
    }    
    rc = clLogSOStreamEntryUnpackNAdd(pCommonEoData, msg);

    CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);

    if(CL_GET_ERROR_CODE(rc) == CL_ERR_DUPLICATE)
        rc = CL_OK;

    CL_LOG_DEBUG_TRACE(("Exit: rc[0x %x]", rc));
    return rc;
}
ClRcT
clLogSvrStreamCurrentFilterGet(ClCntNodeHandleT  hStreamOwnerNode,
                               SaNameT           *pStreamName,
                               SaNameT           *pStreamNodeName,
                               ClPtrT            pStreamHeader)
{
    ClRcT              rc              = CL_OK;
    ClIdlHandleT       hStreamOwnerIdl = CL_HANDLE_INVALID_VALUE;
    ClLogStreamScopeT  streamScope     = CL_LOG_STREAM_GLOBAL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogStreamScopeGet(pStreamNodeName, &streamScope);
    if( CL_OK != rc )
    {
        return rc;
    }
    rc = clLogSvrIdlHandleInitialize(streamScope, &hStreamOwnerIdl);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogSvrIdlHandleInitialize(): rc[0x %x]\n", rc));
        return rc;
    }

#if 0
    FIXME - streamOwnerShould expose the function
    rc = clLogStreamOwnerFilterGetClientAsync(hStreamOwnerIdl, pStreamName,
            streamScope, pStreamNodeName,
            &filter, clLogSvrSOFGResponse,
            (void *) hStreamOwnerNode);
#endif
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogStreamOwnerFilterGetClientAsync(): rc[0x %x]\n",
                            rc));
    }
    CL_LOG_CLEANUP(clIdlHandleFinalize(hStreamOwnerIdl), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogSOTableWalkForPrint(ClCntKeyHandleT   key,
                         ClCntDataHandleT  data,
                         ClCntArgHandleT   arg,
                         ClUint32T         size)
{
    ClRcT                  rc                = CL_OK;
    ClLogStreamOwnerDataT  *pStreamOwnerData = (ClLogStreamOwnerDataT *) data;
    ClLogStreamKeyT        *pStreamKey       = (ClLogStreamKeyT *) key;
    ClDebugPrintHandleT    msg               = *((ClDebugPrintHandleT *) arg);

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clOsalMutexLock_L(&pStreamOwnerData->nodeLock);
    if( CL_OK != rc )
    {
        return rc;
    }
    clDebugPrint(msg, "StreamKey:\n");
    clDebugPrint(msg, "StreamName    : %s\n", pStreamKey->streamName.value);
    clDebugPrint(msg, "StreamNode    : %s\n", pStreamKey->streamScopeNode.value);
    clDebugPrint(msg, "streamNameLen : %d\n", pStreamKey->streamName.length);
    clDebugPrint(msg, "streamScopeLen: %d\n", pStreamKey->streamScopeNode.length);
    clDebugPrint(msg, "--------------------------------------------------\n");
    clDebugPrint(msg, "StreamOwnerData:\n");
    clDebugPrint(msg, "streamId      : %d\n", pStreamOwnerData->streamId);
    clDebugPrint(msg, "MulticastAddr : %lld\n", pStreamOwnerData->streamMcastAddr);
    clDebugPrint(msg, "nodeStatus    : %d\n", pStreamOwnerData->nodeStatus);
    clDebugPrint(msg, "dataSetId     : %d\n", pStreamOwnerData->dsId);
    clDebugPrint(msg, "isNewStream   : %d\n", pStreamOwnerData->isNewStream);
    clLogStreamOwnerAttributesPrint(&pStreamOwnerData->streamAttr, (void**) arg);
    rc = clCntWalk(pStreamOwnerData->hCompTable, clLogCompTableWalkForPrint,
                   arg, 0);
    CL_LOG_CLEANUP(clOsalMutexUnlock_L(&pStreamOwnerData->nodeLock), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
static ClRcT
clLogFileHdlrFileHandleDelete(ClLogFileHandleT  hFileHdlr,
                              ClLogHandleT      *phLog)
{
    ClRcT               rc            = CL_OK;
    ClLogClntFileHdlrInfoT  *pData        = NULL;
    ClLogClntEoDataT    *pClntEoEntry = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogClntEoEntryGet(&pClntEoEntry);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clHandleCheckout(pClntEoEntry->hClntHandleDB, hFileHdlr,
                          (void **) &pData);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clHandleCheckout(): rc[0x %x]", rc));
        return rc;
    }

    *phLog = pData->hLog;
    clLogClntFileKeyDestroy(pData->pFileKey); 

    rc = clHandleCheckin(pClntEoEntry->hClntHandleDB, hFileHdlr);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clHandleCheckin(); rc[0x %x]", rc));
    }
    CL_LOG_CLEANUP(clHandleDestroy(pClntEoEntry->hClntHandleDB, hFileHdlr), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogClntStreamOpen(ClLogHandleT        hLog,
                    SaNameT             *pStreamName,
                    SaNameT             *pNodeName,
                    ClStringT           *pShmName,
                    ClUint32T           shmSize,
                    ClLogStreamHandleT  *phStream,
                    ClUint32T           recSize)
{
    ClRcT                 rc              = CL_OK;
    ClLogClntEoDataT      *pClntEoEntry   = NULL;
    ClBoolT               addedTable      = CL_FALSE;
    ClBoolT               addedEntry      = CL_FALSE;
    ClCntNodeHandleT      hClntStreamNode = CL_HANDLE_INVALID_VALUE;
    ClLogClntStreamDataT  *pUserData      = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((NULL == pStreamName), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((NULL == pNodeName), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((NULL == pShmName), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((sizeof(ClLogStreamHeaderT) > shmSize),
                      CL_LOG_RC(CL_ERR_INVALID_PARAMETER));
    CL_LOG_PARAM_CHK((NULL == phStream), CL_LOG_RC(CL_ERR_NULL_POINTER));

    rc = clLogClntEoEntryGet(&pClntEoEntry);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogClntEoEntryGet(): rc[0x %x]", rc));
        return rc;
    }

    rc = clOsalMutexLock_L(&(pClntEoEntry->clntStreamTblLock));
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalMutexLock(): rc[0x %x]", rc));
        return rc;
    }

    if( CL_HANDLE_INVALID_VALUE == pClntEoEntry->hClntStreamTable )
    {
        rc = clCntHashtblCreate(pClntEoEntry->maxStreams,
                                clLogStreamKeyCompare,
                                clLogStreamHashFn,
                                clLogClntStreamDeleteCb,
                                clLogClntStreamDeleteCb,
                                CL_CNT_UNIQUE_KEY,
                                &(pClntEoEntry->hClntStreamTable));
        if( CL_OK != rc )
        {
            CL_LOG_DEBUG_ERROR(("clCntHashtblCreate(): rc[0x %x]", rc));
            CL_LOG_CLEANUP(
                clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),CL_OK);
            return rc;
        }
        addedTable = CL_TRUE;
        CL_LOG_DEBUG_VERBOSE(("Created the HashTable"));
    }

    rc = clLogClntStreamEntryGet(pClntEoEntry, pStreamName, pNodeName,
                                 pShmName, shmSize, &hClntStreamNode,
                                 &addedEntry);
    if( CL_OK != rc)
    {
        if( CL_TRUE == addedTable )
        {
            CL_LOG_CLEANUP(clCntDelete(pClntEoEntry->hClntStreamTable), CL_OK);
        }
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),
                       CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("Got the stream entry"));

    rc = clLogHandleStreamHandleCreate(hLog, hClntStreamNode, phStream);
    if( CL_OK != rc)
    {
        if( CL_TRUE == addedEntry )
        {
            CL_LOG_CLEANUP(clCntNodeDelete(pClntEoEntry->hClntStreamTable,
                                           hClntStreamNode), CL_OK);
        }
        if( CL_TRUE == addedTable )
        {
            CL_LOG_CLEANUP(clCntDelete(pClntEoEntry->hClntStreamTable), CL_OK);
        }
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),
                       CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("Created the streamhandle"));

    rc = clCntNodeUserDataGet(pClntEoEntry->hClntStreamTable, hClntStreamNode,
                              (ClCntDataHandleT *) &pUserData);
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clHandleDestroy(pClntEoEntry->hClntHandleDB,
                                             *phStream), CL_OK);
        *phStream = CL_HANDLE_INVALID_VALUE;
        if( CL_TRUE == addedEntry )
        {
            CL_LOG_CLEANUP(clCntNodeDelete(pClntEoEntry->hClntStreamTable,
                                           hClntStreamNode), CL_OK);
        }

        if( CL_TRUE == addedTable )
        {
            CL_LOG_CLEANUP(clCntDelete(pClntEoEntry->hClntStreamTable), CL_OK);
        }
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),
                       CL_OK);
        return rc;
    }
#ifdef NO_SAF
    pUserData->recSize=recSize;
#endif
    CL_LOG_DEBUG_VERBOSE(("Got stream entry"));
    rc = clBitmapBitSet(pUserData->hStreamBitmap, CL_HDL_IDX(*phStream));
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clHandleDestroy(pClntEoEntry->hClntHandleDB, *phStream),
                       CL_OK);
        *phStream = CL_HANDLE_INVALID_VALUE;
        if( CL_TRUE == addedEntry )
        {
            CL_LOG_CLEANUP(clCntNodeDelete(pClntEoEntry->hClntStreamTable,
                                           hClntStreamNode), CL_OK);
        }

        if( CL_TRUE == addedTable )
        {
            CL_LOG_CLEANUP(clCntDelete(pClntEoEntry->hClntStreamTable), CL_OK);
        }
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),
                       CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("Updated stream bitmap"));

    rc = clLogHandleInitHandleStreamAdd(hLog, *phStream);
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clBitmapBitClear(pUserData->hStreamBitmap, CL_HDL_IDX(*phStream)),
                       CL_OK);
        CL_LOG_CLEANUP(clHandleDestroy(pClntEoEntry->hClntHandleDB, *phStream),
                       CL_OK);
        *phStream = CL_HANDLE_INVALID_VALUE;
        if( CL_TRUE == addedEntry )
        {
            CL_LOG_CLEANUP(clCntNodeDelete(pClntEoEntry->hClntStreamTable,
                                           hClntStreamNode), CL_OK);
        }

        if( CL_TRUE == addedTable )
        {
            CL_LOG_CLEANUP(clCntDelete(pClntEoEntry->hClntStreamTable), CL_OK);
        }
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),
                       CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("Updated init handle"));

    rc = clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock));
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalMutexUnlock(): rc[0x %x]", rc));
        return rc;
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogClntStreamEntryAdd(ClCntHandleT       hClntTable,
                        ClLogStreamKeyT    *pStreamKey,
                        ClStringT          *pShmName,
                        ClUint32T          shmSize,
                        ClCntNodeHandleT   *phStreamNode)
{
    ClRcT                  rc           = CL_OK;
    ClLogClntStreamDataT   *pStreamData = NULL;
#ifndef NO_SAF
    ClInt32T               fd           = 0;
    ClUint32T              headerSize   = 0;
    ClInt32T               tries        = 0;
    struct stat statbuf;
#endif
    CL_LOG_DEBUG_TRACE(("Enter"));

    pStreamData = (ClLogClntStreamDataT*) clHeapCalloc(1, sizeof(ClLogClntStreamDataT));
    if( pStreamData == NULL )
    {
        CL_LOG_DEBUG_ERROR(("clHeapCalloc()"));
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }
    
    pStreamData->shmName.pValue = (ClCharT*) clHeapCalloc(pShmName->length, sizeof(ClCharT));
    if( NULL == pStreamData->shmName.pValue )
    {
        CL_LOG_DEBUG_ERROR(("clHeapCalloc()"));
        clHeapFree(pStreamData);
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }

    memcpy(pStreamData->shmName.pValue, pShmName->pValue, pShmName->length);
    CL_LOG_DEBUG_VERBOSE(("Opening Shared Memory Segment: %s", pStreamData->shmName.pValue));
#ifndef NO_SAF
    rc = clOsalShmOpen_L(pStreamData->shmName.pValue, CL_LOG_SHM_OPEN_FLAGS, CL_LOG_SHM_MODE, &fd);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalShmOpen(): rc[0x %x]", rc));
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        return rc;
    }
    /*
     * Check to be safe w.r.t SIGBUS in case a parallel ftruncate from log server is in progress
     */
    memset(&statbuf, 0, sizeof(statbuf));
    while(tries++ < 3 && !fstat(fd, &statbuf))
    {
        if((ClUint32T) statbuf.st_size < shmSize)
        {
            sleep(1);
        }
        else break;
    }
    if(!statbuf.st_size)
    {
        CL_LOG_DEBUG_ERROR(("fstat on shared segment with size 0"));
        CL_LOG_CLEANUP(clOsalShmClose_L(fd), CL_OK);
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        return CL_LOG_RC(CL_ERR_LIBRARY);
    }
    rc = clOsalMmap_L(NULL, shmSize, CL_LOG_MMAP_PROT_FLAGS, CL_LOG_MMAP_FLAGS, fd, 0, (void **) &pStreamData->pStreamHeader);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalMmap(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clOsalShmClose_L(fd), CL_OK);
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        return rc;
    }
    CL_LOG_CLEANUP(clOsalShmClose_L(fd), CL_OK);
    CL_LOG_DEBUG_VERBOSE(("Mmapped Shared Memory Segment"));

    pStreamData->pStreamHeader->shmSize = shmSize;
    headerSize                  = CL_LOG_HEADER_SIZE_GET(pStreamData->pStreamHeader->maxMsgs, pStreamData->pStreamHeader->maxComps);
    pStreamData->pStreamRecords = ((ClUint8T *) (pStreamData->pStreamHeader)) + headerSize;
    CL_LOG_DEBUG_VERBOSE(("pStreamHeader : %p", (void *) pStreamData->pStreamHeader));
    CL_LOG_DEBUG_VERBOSE(("msgMap: %p", (void *) (pStreamData->pStreamHeader + 1)));
    CL_LOG_DEBUG_VERBOSE(("pStreamRecords: %p", pStreamData->pStreamRecords));
#endif
    rc = clBitmapCreate(&(pStreamData->hStreamBitmap), 0);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBitmapCreate(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clOsalMunmap_L(pStreamData->pStreamHeader, shmSize),
                       CL_OK);
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        return rc;
    }

    /*
     * Create the shared lock for the sem.
     */
    rc = clLogClientStreamSharedMutexInit(pStreamData, pShmName);

    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalProcessSharedMutexInit(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBitmapDestroy(pStreamData->hStreamBitmap), CL_OK);
        CL_LOG_CLEANUP(clOsalMunmap_L(pStreamData->pStreamHeader, shmSize), CL_OK);
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        return rc;
    }

    rc = clCntNodeAddAndNodeGet(hClntTable,(ClCntKeyHandleT) pStreamKey, (ClCntDataHandleT) pStreamData, NULL, phStreamNode);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clCntNodeAddAndNodeGet(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBitmapDestroy(pStreamData->hStreamBitmap), CL_OK);
        CL_LOG_CLEANUP(clLogClientStreamMutexDestroy(pStreamData), CL_OK);
        CL_LOG_CLEANUP(clOsalMunmap_L(pStreamData->pStreamHeader, shmSize), CL_OK);
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        
        return rc;
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
/*
 * Called with the log server flusher stream lock. held.
 */
static ClRcT
clLogFlusherRecordsMcast(ClLogSvrStreamDataT  *pStreamData,
                         ClUint32T            nRecords)
{
    ClRcT      rc       = CL_OK;
    ClLogStreamHeaderT  *pHeader  = pStreamData->pStreamHeader;
    ClUint8T            *pRecords = pStreamData->pStreamRecords;
    ClUint32T           startIdx  = 0;
    ClUint32T           buffLen   = 0;
    ClHandleT           hFlusher  = CL_HANDLE_INVALID_VALUE;     
    ClTimerHandleT      hTimer    = CL_HANDLE_INVALID_VALUE;  
    ClIdlHandleT        hIdlHdl   = CL_HANDLE_INVALID_VALUE;
    ClIocNodeAddressT   localAddr = 0;
    ClUint8T            *pBuffer  = NULL;
    ClUint32T           size      = 0;
    ClUint32T           firstBatch = 0;
    ClUint32T           secondPatch = 0;
    ClBoolT             doMulticast = 
        ( (0 < (pStreamData->ackersCount + pStreamData->nonAckersCount)) &&
          (pHeader->streamMcastAddr.iocMulticastAddress != 0) )? CL_TRUE: CL_FALSE;

    clLogDebug("SVR", "FLU", "Enter: nRecords: %u seqNum: %u fileOwnerAddr:[%d]", nRecords,
            pStreamData->seqNum, pStreamData->fileOwnerAddr);

    if( (pStreamData->ackersCount + pStreamData->nonAckersCount) == 1 && 
        (pStreamData->fileOwnerAddr == clIocLocalAddressGet()) )
    {
        doMulticast = CL_FALSE;
    }
    /* 
     * If some fileowner has registered for this stream, if then multicast,
     * otherwise just send it to local guy 
     */
    if( CL_TRUE == doMulticast )
    {
        rc = clLogIdlHandleInitialize(pHeader->streamMcastAddr, &hIdlHdl);
        if( CL_OK != rc )
        {
            return rc;
        }
        if( pStreamData->ackersCount > 0 )
        {
            rc = clLogFlusherCookieHandleCreate(nRecords, &hTimer, &hFlusher);
            if( CL_OK != rc )
            {
                CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
                return rc;
            }
            CL_LOG_DEBUG_TRACE(("hFlusher: %#llX", hFlusher));
        }
    }

    localAddr = clIocLocalAddressGet();
    startIdx = pHeader->startAck % pHeader->maxRecordCount;
    if(nRecords > pHeader->maxRecordCount)
        nRecords = pHeader->maxRecordCount;

    buffLen = nRecords * pHeader->recordSize;
    /*
     * Drop the server stream mutex unlock now and grab it again after file owner writes
     * and before exiting out of the function
     */
    clLogDebug(CL_LOG_AREA_SVR, "FLU", "startIdx: %u maxRec: %u nRecords: %u startIdx: %d recordIdx: %d", startIdx,
                    pHeader->maxRecordCount, nRecords, pHeader->startAck, pHeader->recordIdx);
    if( (startIdx + nRecords) <= pHeader->maxRecordCount )
    {
        pBuffer = pRecords + (startIdx * pHeader->recordSize);
        if( pStreamData->fileOwnerAddr == localAddr ) 
        {
            /* File onwer function which flushes the data into file */
            rc = clLogFileOwnerEntryFindNPersist(&pStreamData->fileName, 
                    &pStreamData->fileLocation, nRecords, pBuffer);
            if( CL_OK != rc )
            {
                return rc;
            }
        }
        if( doMulticast == CL_TRUE )
        {
            rc = VDECL_VER(clLogClntFileHdlrDataReceiveClientAsync, 4, 0, 0)(hIdlHdl, 
                    pHeader->streamMcastAddr.iocMulticastAddress, 
                    pStreamData->seqNum, localAddr, 
                    hFlusher, nRecords, buffLen, pBuffer,
                    NULL, 0);
            CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
        }
        CL_LOG_DEBUG_VERBOSE(("Copied from: %p to %u", pRecords + startIdx,
                    nRecords * pHeader->recordSize));
    }
    else
    {
        CL_LOG_DEBUG_TRACE(("startIdx: %u maxRec: %u nRecords: %u", startIdx,
                    pHeader->maxRecordCount, nRecords));
        firstBatch = pHeader->maxRecordCount - startIdx;
        pBuffer = pRecords + (startIdx * pHeader->recordSize);
        secondPatch = nRecords - firstBatch;
        if( pStreamData->fileOwnerAddr == localAddr )
        {
            /*
             *  Make two calls to fileowner function, so that no need to
             *  allocate memory, it just direct copy from shared memory 
             */
            rc = clLogFileOwnerEntryFindNPersist(&pStreamData->fileName, 
                    &pStreamData->fileLocation, firstBatch, pBuffer);
            if( CL_OK != rc )
            {
                return rc;
            }
            rc = clLogFileOwnerEntryFindNPersist(&pStreamData->fileName, 
                    &pStreamData->fileLocation, secondPatch, pRecords);
            if( CL_OK != rc )
            {
                return rc;
            }
        }
        if( doMulticast == CL_TRUE )
        {
            pBuffer = clHeapCalloc(buffLen, sizeof(ClUint8T));
            if( NULL == pBuffer )
            {
                clLogError("LOG", "FLS", "clHeapCalloc() : rc[0x %x]", rc);
                CL_LOG_CLEANUP(clLogFlusherCookieHandleDestroy(hFlusher, CL_FALSE), CL_OK);
                CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
                return CL_LOG_RC(CL_ERR_NO_MEMORY);
            }
            size = firstBatch * pHeader->recordSize;
            memcpy(pBuffer, pRecords + (startIdx * pHeader->recordSize), size);
            memcpy(pBuffer + size, pRecords, buffLen - size); 
            rc = VDECL_VER(clLogClntFileHdlrDataReceiveClientAsync, 4, 0, 0)(hIdlHdl, 
                    pHeader->streamMcastAddr.iocMulticastAddress, 
                    pStreamData->seqNum, localAddr, 
                    hFlusher, nRecords, buffLen, pBuffer,
                    NULL, 0);
            clHeapFree(pBuffer);
            CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
        }
    }
    if( (doMulticast == CL_TRUE) && (CL_OK != rc) )
    {
        clLogError("LOG", "FLS", "VDECL_VER(clLogClntFileHdlrDataReceiveClientAsync, 4, 0, 0)(): "
                "rc[0x %x]", rc);
        CL_LOG_CLEANUP(clLogFlusherCookieHandleDestroy(hFlusher, CL_FALSE), CL_OK);
        return rc;
    }    

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
static ClRcT
clLogFlusherCookieHandleCreate(ClUint32T       numRecords,
                               ClTimerHandleT  *phTimer, 
                               ClHandleT       *phFlusher)
{
    ClRcT     rc   = CL_OK;
    ClLogFlushCookieT  *pFlushCookie = NULL;
    ClLogSvrEoDataT    *pSvrEoEntry  = NULL;
    ClTimerTimeOutT    timeout       = {0, 8000L};
    ClHandleT          *pTimerArg    = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogSvrEoEntryGet(&pSvrEoEntry, NULL);
    if(  CL_OK != rc )
    {
        return rc;
    }    
    if( CL_FALSE == pSvrEoEntry->logInit )
    {
        clLogError("LOG", "FLS", "Log Service has terminated...");
        return CL_OK;
    }
    CL_LOG_DEBUG_TRACE(("hFlusherDB: %p ", (ClPtrT) pSvrEoEntry->hFlusherDB));

    rc = clHandleCreate(pSvrEoEntry->hFlusherDB, sizeof(ClLogFlushCookieT), 
                        phFlusher);
    if( CL_OK != rc )
    {
        clLogError("LOG", "FLS", "clHandleCreate(): rc[0x %x]", rc);
        return rc;
    }    
    pTimerArg = (ClHandleT*) clHeapCalloc(1, sizeof(ClHandleT));

    if( NULL == pTimerArg )
    {
    	clLogError("LOG", "FLS", "clHeapCalloc() : rc[0x %x]", rc);
        CL_LOG_CLEANUP(clHandleDestroy(pSvrEoEntry->hFlusherDB, *phFlusher),
                       CL_OK);
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }

    *pTimerArg = *phFlusher;

    rc = clTimerCreateAndStart(timeout, CL_TIMER_ONE_SHOT, 
                               CL_TIMER_SEPARATE_CONTEXT, 
                               clLogFlusherTimerCallback,
                               (ClPtrT)(ClWordT) pTimerArg, phTimer);
    if( CL_OK != rc )
    {
        clLogError("LOG", "FLS", "clTimerCreate(): rc[0x %x]", rc);
        CL_LOG_CLEANUP(clHandleDestroy(pSvrEoEntry->hFlusherDB, *phFlusher),
                       CL_OK);
        clHeapFree(pTimerArg);
        return rc;
    }    

    rc = clHandleCheckout(pSvrEoEntry->hFlusherDB, *phFlusher, 
            (void **) &pFlushCookie);
    if( CL_OK != rc )
    {
        clLogError("LOG", "FLS", "clHandleCheckout(): rc[0x %x]", rc);
        CL_LOG_CLEANUP(clTimerDelete(phTimer), CL_OK);
        CL_LOG_CLEANUP(clHandleDestroy(pSvrEoEntry->hFlusherDB, *phFlusher),
                CL_OK);
        return rc;
    }    
    pFlushCookie->numRecords = numRecords;
    pFlushCookie->hTimer     = *phTimer;
    rc = clHandleCheckin(pSvrEoEntry->hFlusherDB, *phFlusher);
    if( CL_OK != rc )
    {
        clLogError("LOG", "FLS", "clHandleCheckin(): rc[0x %x]", rc);
        CL_LOG_CLEANUP(clTimerDelete(phTimer), CL_OK);
        CL_LOG_CLEANUP(clHandleDestroy(pSvrEoEntry->hFlusherDB, *phFlusher),
                CL_OK);
    }    

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}