|
Packit Service |
b3514a |
/*
|
|
Packit Service |
b3514a |
* Copyright (c) 2020 Red Hat, Inc.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
b3514a |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
b3514a |
* as published by the Free Software Foundation; either version 2
|
|
Packit Service |
b3514a |
* of the License, or (at your option) any later version.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
b3514a |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
b3514a |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
b3514a |
* GNU General Public License for more details.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
b3514a |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
b3514a |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
b3514a |
* 02110-1301, USA.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* $Id: //eng/vdo-releases/aluminum/src/c++/vdo/base/recoveryUtils.c#4 $
|
|
Packit Service |
b3514a |
*/
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
#include "recoveryUtils.h"
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
#include "logger.h"
|
|
Packit Service |
b3514a |
#include "memoryAlloc.h"
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
#include "completion.h"
|
|
Packit Service |
b3514a |
#include "extent.h"
|
|
Packit Service |
b3514a |
#include "packedRecoveryJournalBlock.h"
|
|
Packit Service |
b3514a |
#include "recoveryJournalEntry.h"
|
|
Packit Service |
b3514a |
#include "recoveryJournalInternals.h"
|
|
Packit Service |
b3514a |
#include "slabDepot.h"
|
|
Packit Service |
b3514a |
#include "vdoInternal.h"
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**
|
|
Packit Service |
b3514a |
* Finish loading the journal by freeing the extent and notifying the parent.
|
|
Packit Service |
b3514a |
* This callback is registered in loadJournalAsync().
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* @param completion The load extent
|
|
Packit Service |
b3514a |
**/
|
|
Packit Service |
b3514a |
static void finishJournalLoad(VDOCompletion *completion)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
int result = completion->result;
|
|
Packit Service |
b3514a |
VDOCompletion *parent = completion->parent;
|
|
Packit Service |
b3514a |
VDOExtent *extent = asVDOExtent(completion);
|
|
Packit Service |
b3514a |
freeExtent(&extent);
|
|
Packit Service |
b3514a |
finishCompletion(parent, result);
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
void loadJournalAsync(RecoveryJournal *journal,
|
|
Packit Service |
b3514a |
VDOCompletion *parent,
|
|
Packit Service |
b3514a |
char **journalDataPtr)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
int result = ALLOCATE(journal->size * VDO_BLOCK_SIZE, char, __func__,
|
|
Packit Service |
b3514a |
journalDataPtr);
|
|
Packit Service |
b3514a |
if (result != VDO_SUCCESS) {
|
|
Packit Service |
b3514a |
finishCompletion(parent, result);
|
|
Packit Service |
b3514a |
return;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
VDOExtent *extent;
|
|
Packit Service |
b3514a |
result = createExtent(parent->layer, VIO_TYPE_RECOVERY_JOURNAL,
|
|
Packit Service |
b3514a |
VIO_PRIORITY_METADATA, journal->size,
|
|
Packit Service |
b3514a |
*journalDataPtr, &extent);
|
|
Packit Service |
b3514a |
if (result != VDO_SUCCESS) {
|
|
Packit Service |
b3514a |
finishCompletion(parent, result);
|
|
Packit Service |
b3514a |
return;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
prepareCompletion(&extent->completion, finishJournalLoad, finishJournalLoad,
|
|
Packit Service |
b3514a |
parent->callbackThreadID, parent);
|
|
Packit Service |
b3514a |
readMetadataExtent(extent,
|
|
Packit Service |
b3514a |
getFixedLayoutPartitionOffset(journal->partition));
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**
|
|
Packit Service |
b3514a |
* Determine whether the given header describe a valid block for the
|
|
Packit Service |
b3514a |
* given journal that could appear at the given offset in the journal.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* @param journal The journal to use
|
|
Packit Service |
b3514a |
* @param header The unpacked block header to check
|
|
Packit Service |
b3514a |
* @param offset An offset indicating where the block was in the journal
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* @return True if the header matches
|
|
Packit Service |
b3514a |
**/
|
|
Packit Service |
b3514a |
__attribute__((warn_unused_result))
|
|
Packit Service |
b3514a |
static bool isCongruentRecoveryJournalBlock(RecoveryJournal *journal,
|
|
Packit Service |
b3514a |
const RecoveryBlockHeader *header,
|
|
Packit Service |
b3514a |
PhysicalBlockNumber offset)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
PhysicalBlockNumber expectedOffset
|
|
Packit Service |
b3514a |
= getRecoveryJournalBlockNumber(journal, header->sequenceNumber);
|
|
Packit Service |
b3514a |
return ((expectedOffset == offset)
|
|
Packit Service |
b3514a |
&& isValidRecoveryJournalBlock(journal, header));
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
bool findHeadAndTail(RecoveryJournal *journal,
|
|
Packit Service |
b3514a |
char *journalData,
|
|
Packit Service |
b3514a |
SequenceNumber *tailPtr,
|
|
Packit Service |
b3514a |
SequenceNumber *blockMapHeadPtr,
|
|
Packit Service |
b3514a |
SequenceNumber *slabJournalHeadPtr)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
SequenceNumber highestTail = journal->tail;
|
|
Packit Service |
b3514a |
SequenceNumber blockMapHeadMax = 0;
|
|
Packit Service |
b3514a |
SequenceNumber slabJournalHeadMax = 0;
|
|
Packit Service |
b3514a |
bool foundEntries = false;
|
|
Packit Service |
b3514a |
for (PhysicalBlockNumber i = 0; i < journal->size; i++) {
|
|
Packit Service |
b3514a |
PackedJournalHeader *packedHeader
|
|
Packit Service |
b3514a |
= getJournalBlockHeader(journal, journalData, i);
|
|
Packit Service |
b3514a |
RecoveryBlockHeader header;
|
|
Packit Service |
b3514a |
unpackRecoveryBlockHeader(packedHeader, &header);
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
if (!isCongruentRecoveryJournalBlock(journal, &header, i)) {
|
|
Packit Service |
b3514a |
// This block is old, unformatted, or doesn't belong at this location.
|
|
Packit Service |
b3514a |
continue;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
if (header.sequenceNumber >= highestTail) {
|
|
Packit Service |
b3514a |
foundEntries = true;
|
|
Packit Service |
b3514a |
highestTail = header.sequenceNumber;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
if (header.blockMapHead > blockMapHeadMax) {
|
|
Packit Service |
b3514a |
blockMapHeadMax = header.blockMapHead;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
if (header.slabJournalHead > slabJournalHeadMax) {
|
|
Packit Service |
b3514a |
slabJournalHeadMax = header.slabJournalHead;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
*tailPtr = highestTail;
|
|
Packit Service |
b3514a |
if (!foundEntries) {
|
|
Packit Service |
b3514a |
return false;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
*blockMapHeadPtr = blockMapHeadMax;
|
|
Packit Service |
b3514a |
if (slabJournalHeadPtr != NULL) {
|
|
Packit Service |
b3514a |
*slabJournalHeadPtr = slabJournalHeadMax;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
return true;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
int validateRecoveryJournalEntry(const VDO *vdo,
|
|
Packit Service |
b3514a |
const RecoveryJournalEntry *entry)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
if ((entry->slot.pbn >= vdo->config.physicalBlocks)
|
|
Packit Service |
b3514a |
|| (entry->slot.slot >= BLOCK_MAP_ENTRIES_PER_PAGE)
|
|
Packit Service |
b3514a |
|| !isValidLocation(&entry->mapping)
|
|
Packit Service |
b3514a |
|| !isPhysicalDataBlock(vdo->depot, entry->mapping.pbn)) {
|
|
Packit Service |
b3514a |
return logErrorWithStringError(VDO_CORRUPT_JOURNAL, "Invalid entry:"
|
|
Packit Service |
b3514a |
" (%llu, %" PRIu16 ") to %" PRIu64
|
|
Packit Service |
b3514a |
" (%s) is not within bounds",
|
|
Packit Service |
b3514a |
entry->slot.pbn, entry->slot.slot,
|
|
Packit Service |
b3514a |
entry->mapping.pbn,
|
|
Packit Service |
b3514a |
getJournalOperationName(entry->operation));
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
if ((entry->operation == BLOCK_MAP_INCREMENT)
|
|
Packit Service |
b3514a |
&& (isCompressed(entry->mapping.state)
|
|
Packit Service |
b3514a |
|| (entry->mapping.pbn == ZERO_BLOCK))) {
|
|
Packit Service |
b3514a |
return logErrorWithStringError(VDO_CORRUPT_JOURNAL, "Invalid entry:"
|
|
Packit Service |
b3514a |
" (%llu, %" PRIu16 ") to %" PRIu64
|
|
Packit Service |
b3514a |
" (%s) is not a valid tree mapping",
|
|
Packit Service |
b3514a |
entry->slot.pbn, entry->slot.slot,
|
|
Packit Service |
b3514a |
entry->mapping.pbn,
|
|
Packit Service |
b3514a |
getJournalOperationName(entry->operation));
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
return VDO_SUCCESS;
|
|
Packit Service |
b3514a |
}
|