Blame vdo/base/recoveryUtils.h

Packit Service 310c69
/*
Packit Service 310c69
 * Copyright (c) 2020 Red Hat, Inc.
Packit Service 310c69
 *
Packit Service 310c69
 * This program is free software; you can redistribute it and/or
Packit Service 310c69
 * modify it under the terms of the GNU General Public License
Packit Service 310c69
 * as published by the Free Software Foundation; either version 2
Packit Service 310c69
 * of the License, or (at your option) any later version.
Packit Service 310c69
 * 
Packit Service 310c69
 * This program is distributed in the hope that it will be useful,
Packit Service 310c69
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 310c69
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 310c69
 * GNU General Public License for more details.
Packit Service 310c69
 * 
Packit Service 310c69
 * You should have received a copy of the GNU General Public License
Packit Service 310c69
 * along with this program; if not, write to the Free Software
Packit Service 310c69
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service 310c69
 * 02110-1301, USA. 
Packit Service 310c69
 *
Packit Service 310c69
 * $Id: //eng/vdo-releases/aluminum/src/c++/vdo/base/recoveryUtils.h#5 $
Packit Service 310c69
 */
Packit Service 310c69
Packit Service 310c69
#ifndef RECOVERY_UTILS_H
Packit Service 310c69
#define RECOVERY_UTILS_H
Packit Service 310c69
Packit Service 310c69
#include "constants.h"
Packit Service 310c69
#include "packedRecoveryJournalBlock.h"
Packit Service 310c69
#include "recoveryJournalEntry.h"
Packit Service 310c69
#include "recoveryJournalInternals.h"
Packit Service 310c69
#include "types.h"
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Get the block header for a block at a position in the journal data.
Packit Service 310c69
 *
Packit Service 310c69
 * @param journal      The recovery journal
Packit Service 310c69
 * @param journalData  The recovery journal data
Packit Service 310c69
 * @param sequence     The sequence number
Packit Service 310c69
 *
Packit Service 310c69
 * @return A pointer to a packed recovery journal block header.
Packit Service 310c69
 **/
Packit Service 310c69
__attribute__((warn_unused_result))
Packit Service 310c69
static inline
Packit Service 310c69
PackedJournalHeader *getJournalBlockHeader(RecoveryJournal *journal,
Packit Service 310c69
                                           char            *journalData,
Packit Service 310c69
                                           SequenceNumber   sequence)
Packit Service 310c69
{
Packit Service 310c69
  off_t blockOffset = (getRecoveryJournalBlockNumber(journal, sequence)
Packit Service 310c69
                       * VDO_BLOCK_SIZE);
Packit Service 310c69
  return (PackedJournalHeader *) &journalData[blockOffset];
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Determine whether the given header describes a valid block for the
Packit Service 310c69
 * given journal. A block is not valid if it is unformatted, or if it
Packit Service 310c69
 * is older than the last successful recovery or reformat.
Packit Service 310c69
 *
Packit Service 310c69
 * @param journal  The journal to use
Packit Service 310c69
 * @param header   The unpacked block header to check
Packit Service 310c69
 *
Packit Service 310c69
 * @return True if the header is valid
Packit Service 310c69
 **/
Packit Service 310c69
__attribute__((warn_unused_result))
Packit Service 310c69
static inline
Packit Service 310c69
bool isValidRecoveryJournalBlock(const RecoveryJournal     *journal,
Packit Service 310c69
                                 const RecoveryBlockHeader *header)
Packit Service 310c69
{
Packit Service 310c69
  return ((header->metadataType == VDO_METADATA_RECOVERY_JOURNAL)
Packit Service 310c69
          && (header->nonce == journal->nonce)
Packit Service 310c69
          && (header->recoveryCount == journal->recoveryCount));
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Determine whether the given header describes the exact block indicated.
Packit Service 310c69
 *
Packit Service 310c69
 * @param journal   The journal to use
Packit Service 310c69
 * @param header    The unpacked block header to check
Packit Service 310c69
 * @param sequence  The expected sequence number
Packit Service 310c69
 *
Packit Service 310c69
 * @return True if the block matches
Packit Service 310c69
 **/
Packit Service 310c69
__attribute__((warn_unused_result))
Packit Service 310c69
static inline
Packit Service 310c69
bool isExactRecoveryJournalBlock(const RecoveryJournal     *journal,
Packit Service 310c69
                                 const RecoveryBlockHeader *header,
Packit Service 310c69
                                 SequenceNumber             sequence)
Packit Service 310c69
{
Packit Service 310c69
  return ((header->sequenceNumber == sequence)
Packit Service 310c69
          && isValidRecoveryJournalBlock(journal, header));
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Determine whether the header of the given sector could describe a
Packit Service 310c69
 * valid sector for the given journal block header.
Packit Service 310c69
 *
Packit Service 310c69
 * @param header  The unpacked block header to compare against
Packit Service 310c69
 * @param sector  The packed sector to check
Packit Service 310c69
 *
Packit Service 310c69
 * @return True if the sector matches the block header
Packit Service 310c69
 **/
Packit Service 310c69
__attribute__((warn_unused_result))
Packit Service 310c69
static inline
Packit Service 310c69
bool isValidRecoveryJournalSector(const RecoveryBlockHeader *header,
Packit Service 310c69
                                  const PackedJournalSector *sector)
Packit Service 310c69
{
Packit Service 310c69
  return ((header->checkByte == sector->checkByte)
Packit Service 310c69
          && (header->recoveryCount == sector->recoveryCount));
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Load the journal data off the disk.
Packit Service 310c69
 *
Packit Service 310c69
 * @param [in]  journal         The recovery journal to load
Packit Service 310c69
 * @param [in]  parent          The completion to notify when the load is
Packit Service 310c69
 *                              complete
Packit Service 310c69
 * @param [out] journalDataPtr  A pointer to the journal data buffer (it is the
Packit Service 310c69
 *                              caller's responsibility to free this buffer)
Packit Service 310c69
 **/
Packit Service 310c69
void loadJournalAsync(RecoveryJournal  *journal,
Packit Service 310c69
                      VDOCompletion    *parent,
Packit Service 310c69
                      char            **journalDataPtr);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Find the tail and the head of the journal by searching for the highest
Packit Service 310c69
 * sequence number in a block with a valid nonce, and the highest head value
Packit Service 310c69
 * among the blocks with valid nonces.
Packit Service 310c69
 *
Packit Service 310c69
 * @param [in]  journal             The recovery journal
Packit Service 310c69
 * @param [in]  journalData         The journal data read from disk
Packit Service 310c69
 * @param [out] tailPtr             A pointer to return the tail found, or if
Packit Service 310c69
 *                                  no higher block is found, the value
Packit Service 310c69
 *                                  currently in the journal
Packit Service 310c69
 * @param [out] blockMapHeadPtr     A pointer to return the block map head
Packit Service 310c69
 * @param [out] slabJournalHeadPtr  An optional pointer to return the slab
Packit Service 310c69
 *                                  journal head
Packit Service 310c69
 *
Packit Service 310c69
 * @return  True if there were valid journal blocks
Packit Service 310c69
 **/
Packit Service 310c69
bool findHeadAndTail(RecoveryJournal *journal,
Packit Service 310c69
                     char            *journalData,
Packit Service 310c69
                     SequenceNumber  *tailPtr,
Packit Service 310c69
                     SequenceNumber  *blockMapHeadPtr,
Packit Service 310c69
                     SequenceNumber  *slabJournalHeadPtr);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Validate a recovery journal entry.
Packit Service 310c69
 *
Packit Service 310c69
 * @param vdo    The VDO
Packit Service 310c69
 * @param entry  The entry to validate
Packit Service 310c69
 *
Packit Service 310c69
 * @return VDO_SUCCESS or an error
Packit Service 310c69
 **/
Packit Service 310c69
int validateRecoveryJournalEntry(const VDO                  *vdo,
Packit Service 310c69
                                 const RecoveryJournalEntry *entry)
Packit Service 310c69
  __attribute__((warn_unused_result));
Packit Service 310c69
Packit Service 310c69
#endif // RECOVERY_UTILS_H