SaAisErrorT saCkptCheckpointClose(SaCkptCheckpointHandleT checkpointHandle)
{
    ClRcT          rc    = CL_OK;
    SaAisErrorT    safRc = SA_AIS_OK;

    /*
     * Call the corresponding ckpt client library function.
     */
    rc =  clCkptCheckpointClose((ClCkptHdlT) checkpointHandle);
    
    /*
     * Translate the clovis error type to SAF error type.
     */
    clErrorTxlate(rc, &safRc);
    
    return safRc;
}
/*******************************************************************************
Feature  API: clTcCkptDelete

Description : This API will first call Checkpoint Close and then 
              Checkpoint delete. The latter function requires the
			  name given to the checkpoint, during Checkpoint open.

Arguments In: 
	1. ClTcCkptDataT : contains the ckpt handle
	2. Checkpoint Name

Arguments Out:
	1. ClTcCkptDataT : returns time taken to write the section

Return Value:
	integer 0 if success, non zero if failure
*******************************************************************************/
int clTcCkptDelete ( 
	const char*	   ckpt_name,
	ClTcCkptDataT* ckpt_data )
{
	ClRcT 		ret_code = CL_OK;
	ClNameT 	ckpt_name_t={0};
    ClTimeT startTime = 0;
    ClTimeT endTime = 0;
	ClTimeT time_taken_us =0;

	/* Initialize name struct for ckpt
	 */
	strncpy(ckpt_name_t.value, ckpt_name,CL_MAX_NAME_LENGTH-1);
	ckpt_name_t.length = strlen(ckpt_name_t.value);

	/* time check 1 start 
	 */
    startTime = clOsalStopWatchTimeGet();

	/* Delete checkpoint (test: check memory profile to ensure all resources
	 * are actually released)
	 */
	ret_code = clCkptCheckpointDelete( ckpt_svc_hdl, &ckpt_name_t);
	if ( ret_code != CL_OK )
	{
		printf("clTcCkptDelete: Failed to delete checkpoint %s: 0x%d \n",
			   ckpt_name, ret_code); 
	}
	ret_code = clCkptCheckpointClose((ClCkptHdlT)ckpt_data->ckpt_hdl);
	if ( ret_code != CL_OK )
	{
		printf("clTcCkptDelete: Failed to close checkpoint %s: 0x%d \n",
			   ckpt_name, ret_code); 
	}


	/* time check 1 end 
	 */
    endTime = clOsalStopWatchTimeGet();
	time_taken_us = endTime - startTime;

	ckpt_data->time_taken_ms = 0;
	ckpt_data->time_taken_us  = time_taken_us;

	return ret_code;
}
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;
}
/*
 * FINALIZE: This function closes the checkpoint and finalizes the
 * checkpoint service handle.
 */
ClRcT clCachedCkptFinalize(ClCachedCkptSvcInfoT *serviceInfo)
{
    ClRcT rc;
    ClUint32T shmSize = serviceInfo->cachSize;

    rc = clOsalMunmap(serviceInfo->cache, shmSize);
    if(rc != CL_OK)
        clLogError("CCK", "FIN", "clOsalMunmap(): error code [0x%x].", rc);
    serviceInfo->cache = NULL; // Clear the pointer out after unmap to avoid accessing invalid memory

    rc = clOsalShmClose(&serviceInfo->fd);
    if(rc != CL_OK)
        clLogError("CCK", "FIN", "clOsalShmClose(): error code [0x%x].", rc);

    rc = clOsalSemDelete(serviceInfo->cacheSem);
    if(rc != CL_OK)
        clLogError("CCK", "FIN", "Failed to destroy cache lock. error code [0x%x].", rc);

    if(serviceInfo->ckptHandle)
    {
        rc = clCkptCheckpointClose(serviceInfo->ckptHandle);
        if (rc != CL_OK)
        {
            clLogError("CCK", "FIN", "Failed to close checkpoint. error code [0x%x].", rc);
        }
        serviceInfo->ckptHandle =  CL_HANDLE_INVALID_VALUE;
    }

    if(serviceInfo->ckptSvcHandle)
    {
        rc = clCkptFinalize(serviceInfo->ckptSvcHandle);
        if (rc != CL_OK)
        {
            clLogError("CCK", "FIN", "Failed to finalize checkpoint service instance. error code [0x%x].", rc);
        }
        serviceInfo->ckptSvcHandle =  CL_HANDLE_INVALID_VALUE;
    }

    return rc;
}
ClRcT
clLogMasterCkptGet(void)
{
    ClRcT                                rc              = CL_OK;
    ClCkptCheckpointCreationAttributesT  ckptAttr        = {0};
    ClCkptOpenFlagsT                     openFlags       = 0;
    ClLogSvrCommonEoDataT                *pCommonEoEntry = NULL;
    ClLogMasterEoDataT                   *pMasterEoEntry = NULL;
    ClTimeT                              timeout         = 5000L;
    ClIocNodeAddressT                    localAddr = clIocLocalAddressGet();
    ClInt32T tries = 0;
    static ClTimerTimeOutT delay = {  0,  100 };

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogMasterEoEntryGet(&pMasterEoEntry, &pCommonEoEntry);
    if( CL_OK != rc )
    {
        return rc;
    }
    ckptAttr.creationFlags     = CL_CKPT_CHECKPOINT_COLLOCATED | CL_CKPT_ALL_OPEN_ARE_REPLICAS;
    ckptAttr.checkpointSize    = pMasterEoEntry->maxFiles * pMasterEoEntry->sectionSize;
    ckptAttr.retentionDuration = CL_LOGMASTER_CKPT_RETENTION_DURATION;
    ckptAttr.maxSections       = pMasterEoEntry->maxFiles;
    ckptAttr.maxSectionSize    = pMasterEoEntry->sectionSize;
    ckptAttr.maxSectionIdSize  = pMasterEoEntry->sectionIdSize;

    //   openFlags = CL_CKPT_CHECKPOINT_WRITE | CL_CKPT_CHECKPOINT_READ;
    //rc = clCkptCheckpointOpen(pCommonEoEntry->hSvrCkpt, &gLogMasterCkptName,
    //                          NULL, openFlags, 0,  &pMasterEoEntry->hCkpt);
    //    if( CL_ERR_NOT_EXIST == CL_GET_ERROR_CODE(rc) )
    //  {
    openFlags = CL_CKPT_CHECKPOINT_CREATE | CL_CKPT_CHECKPOINT_WRITE | CL_CKPT_CHECKPOINT_READ;

reopen:
    rc = clCkptCheckpointOpen(pCommonEoEntry->hSvrCkpt, &gLogMasterCkptName, &ckptAttr,openFlags, timeout, &pMasterEoEntry->hCkpt);
    if( (CL_OK != rc) && (CL_ERR_ALREADY_EXIST != rc) )
    {
        /*
         * No replica found and we are the only master.
         * Delete and try re-opening the checkpoint
         */
        if(CL_GET_ERROR_CODE(rc) == CL_ERR_NO_RESOURCE && pCommonEoEntry->masterAddr == localAddr)
        {
            if(tries++ < 1)
            {
                clLogNotice("CKP", "GET", "No replica for log checkpoint."
                            "Deleting ckpt [%.*s] and retrying the ckpt open",
                            gLogMasterCkptName.length, gLogMasterCkptName.value);
                clCkptCheckpointDelete(pCommonEoEntry->hSvrCkpt, &gLogMasterCkptName);
                clOsalTaskDelay(delay);
                goto reopen;
            }
        }
        CL_LOG_DEBUG_ERROR(("clCkptCheckpointOpen(): rc[0x%x]", rc));
        return rc;
    }
    //     if( CL_ERR_ALREADY_EXIST != CL_GET_ERROR_CODE(rc))
    {   /* Create default section */
    }
    //    return rc;
    //    }

    if(pCommonEoEntry->masterAddr == localAddr)
    {
        rc = clLogMasterStateRecover(pCommonEoEntry, pMasterEoEntry, CL_FALSE);
        if( CL_OK != rc )
        {
            CL_LOG_CLEANUP(clCkptCheckpointClose(pMasterEoEntry->hCkpt),
                           CL_OK);
        }
    } /* else you are standby so just keep the ckpt opened */

    CL_LOG_DEBUG_TRACE(("Exit: rc[0x%x]", rc));
    return rc;
}
ClRcT
clLogStreamOwnerGlobalCkptGet(ClLogSOEoDataT         *pSoEoEntry,
                              ClLogSvrCommonEoDataT  *pCommonEoData,
                              ClBoolT                *pCreateCkpt)
{
    ClRcT             rc        = CL_OK;
   // ClCkptOpenFlagsT  openFlags = CL_CKPT_CHECKPOINT_WRITE |
     //                             CL_CKPT_CHECKPOINT_READ;

    CL_LOG_DEBUG_TRACE(("Enter"));

    *pCreateCkpt = CL_FALSE;
#if 0
    rc = clCkptCheckpointOpen(pCommonEoData->hSvrCkpt, &gSOSvrCkptName, 
                              NULL, openFlags, 0,  &pSoEoEntry->hCkpt);
    if( CL_ERR_NOT_EXIST == CL_GET_ERROR_CODE(rc) )
    {
#endif
        rc = clLogStreamOwnerCheckpointCreate(pSoEoEntry, 
                                              (SaNameT *) &gSOSvrCkptName, 
                                               &pSoEoEntry->hCkpt);
        if( (CL_OK != rc) && (CL_ERR_ALREADY_EXIST != CL_GET_ERROR_CODE(rc)) )
        {
            CL_LOG_DEBUG_ERROR(("clCkptCheckpointOpen(): rc[0x %x]", rc));
            return rc;
        }
        if( CL_OK == rc )
        {
            *pCreateCkpt = CL_TRUE;
//            return rc;
        }
  //  }
    if( pCommonEoData->masterAddr == clIocLocalAddressGet() )
    {
        rc = clLogStreamOwnerGlobalStateRecover(pCommonEoData->masterAddr, CL_FALSE);
    if( CL_OK != rc )
    {
            CL_LOG_CLEANUP(clCkptCheckpointClose(pSoEoEntry->hCkpt), CL_OK);
        }
    }

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

ClRcT
clLogStreamOwnerLocalCkptDelete(void)
{
    ClRcT                  rc             = CL_OK;
    ClLogSvrCommonEoDataT  *pCommonEoData = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

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

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

    CL_LOG_CLEANUP(clCkptLibraryCkptDelete(pCommonEoData->hLibCkpt,
                                           (SaNameT *) &gSOLocalCkptName), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
/**
 * INIT: The below function initializes the checkpoint service client
 * and opens a checkpoint to store data.
 */
ClRcT clCachedCkptInitialize(ClCachedCkptSvcInfoT *serviceInfo,
                             const SaNameT *ckptName,
                             const SaCkptCheckpointCreationAttributesT *ckptAttributes,
                             SaCkptCheckpointOpenFlagsT openFlags,
                             ClUint32T cachSize)
{
    ClRcT			rc = CL_OK;
    SaCkptHandleT		ckptSvcHandle = 0;
    SaCkptCheckpointHandleT	ckptHandle = 0;
    SaVersionT			ckptVersion = {'B', 0x01, 0x01};
    ClTimerTimeOutT		delay = { 0, 500};
    ClInt32T			tries = 0;
    ClCharT cacheName[CL_MAX_NAME_LENGTH];
    ClUint32T                   shmSize = clCachedCkptShmSizeGet(cachSize);
    ClUint32T  *numberOfSections;
    ClUint32T  *sizeOfCache;

    serviceInfo->cachSize = shmSize;

    if(serviceInfo->ckptHandle != CL_HANDLE_INVALID_VALUE)
    {
        clLogWarning("CCK", "INI", "Checkpoint already initialized. Skipping initialization");
        return CL_ERR_ALREADY_EXIST;
    }

    snprintf(cacheName, sizeof(cacheName), "%s_%d", ckptName->value, gIocLocalBladeAddress);

    rc = clOsalSemCreate((ClUint8T*)cacheName, 1, &serviceInfo->cacheSem);
    if(rc != CL_OK)
    {
        ClOsalSemIdT semId = 0;

        if(clOsalSemIdGet((ClUint8T*)cacheName, &semId) != CL_OK)
        {
            clLogError("CCK", "INI", "cache semaphore creation error while fetching semaphore id");
            goto out1;
        }

        if(clOsalSemDelete(semId) != CL_OK)
        {
            clLogError("CCK", "INI", "cache semaphore creation error while deleting old semaphore id");
            goto out1;
        }

        rc = clOsalSemCreate((ClUint8T*)cacheName, 1, &serviceInfo->cacheSem);
    }

    if(ckptAttributes || openFlags)
    {
        if(serviceInfo->ckptSvcHandle == CL_HANDLE_INVALID_VALUE)
        {
            /* Initialize checkpoint service instance */
            do
            {
                rc = clCkptInitialize(&ckptSvcHandle, NULL, (ClVersionT *)&ckptVersion);
            } while(rc != CL_OK && tries++ < 100 && clOsalTaskDelay(delay) == CL_OK);

            if(rc != CL_OK)
            {
                clLogError("CCK", "INI", "Failed to initialize checkpoint service instance. error code [0x%x].", rc);
                goto out2;
            }

            serviceInfo->ckptSvcHandle = ckptSvcHandle;
        }

        /* Create the checkpoint for read and write. */
        do
        {
            rc = clCkptCheckpointOpen(serviceInfo->ckptSvcHandle,
                                      (SaNameT *)ckptName,
                                      (ClCkptCheckpointCreationAttributesT *)ckptAttributes,
                                      openFlags,
                                      0L,
                                      &ckptHandle);
        } while(rc != CL_OK && tries++ < 100 && clOsalTaskDelay(delay) == CL_OK);

        if(rc != CL_OK)
        {
            clLogError("CCK", "INI", "Failed to open checkpoint. error code [0x%x].", rc);
            goto out3;
        }

        serviceInfo->ckptHandle = ckptHandle;
    }

    /* Create shm */

    rc = clOsalShmOpen((ClCharT *)cacheName, CL_CACHED_CKPT_SHM_EXCL_CREATE_FLAGS,
                       CL_CACHED_CKPT_SHM_MODE, &serviceInfo->fd);
    if( CL_ERR_ALREADY_EXIST == CL_GET_ERROR_CODE(rc) )
    {
        rc = clOsalShmOpen((ClCharT *)cacheName, CL_CACHED_CKPT_SHM_OPEN_FLAGS,
                           CL_CACHED_CKPT_SHM_MODE, &serviceInfo->fd);
        if( CL_OK != rc )
        {
            clLogError("CCK", "INI", "Could not open shared memory.");
            goto out4;
        }
    }

    rc = clOsalFtruncate(serviceInfo->fd, shmSize);
    if( CL_OK != rc )
    {
        clLogError("CCK", "INI", "clOsalFtruncate(): error code [0x%x].", rc);
        goto out5;
    }

    rc = clOsalMmap(0, shmSize, CL_CACHED_CKPT_MMAP_PROT_FLAGS,
                    CL_CACHED_CKPT_MMAP_FLAGS, serviceInfo->fd, 0, (void **) &serviceInfo->cache);
    if( CL_OK != rc )
    {
        clLogError("CCK", "INI", "clOsalMmap(): error code [0x%x].", rc);
        goto out5;
    }

    numberOfSections = (ClUint32T *)(serviceInfo->cache);
    sizeOfCache = (ClUint32T *)(numberOfSections + 1);
    *numberOfSections = 0;
    *sizeOfCache = 0;

    clOsalMsync(serviceInfo->cache, shmSize, MS_SYNC);

    goto out1;

out5:
    clOsalShmClose(&serviceInfo->fd);
out4:
    if(serviceInfo->ckptHandle)
    {
        clCkptCheckpointClose(serviceInfo->ckptHandle);
        serviceInfo->ckptHandle =  CL_HANDLE_INVALID_VALUE;
    }
out3:
    if(serviceInfo->ckptSvcHandle)
    {
        clCkptFinalize(serviceInfo->ckptSvcHandle);
        serviceInfo->ckptSvcHandle =  CL_HANDLE_INVALID_VALUE;
    }
out2:
    clOsalSemDelete(serviceInfo->cacheSem);
out1:
    return rc;
}