2011-12-16 00:04:31 +05:30
|
|
|
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
2011-12-06 19:23:05 +05:30
|
|
|
//
|
2011-12-06 19:13:56 +05:30
|
|
|
// This file is part of the thin-provisioning-tools source.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is free software: you can redistribute it
|
|
|
|
// and/or modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation, either version 3 of
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is distributed in the hope that it will be
|
|
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with thin-provisioning-tools. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-07-21 10:33:53 +05:30
|
|
|
#include "base/error_state.h"
|
2020-02-16 14:13:31 +05:30
|
|
|
#include "base/nested_output.h"
|
2013-09-11 16:10:46 +05:30
|
|
|
#include "persistent-data/file_utils.h"
|
2020-05-28 19:05:51 +05:30
|
|
|
#include "persistent-data/space-maps/core.h"
|
2013-04-23 19:51:44 +05:30
|
|
|
#include "thin-provisioning/metadata.h"
|
2013-01-02 18:25:41 +05:30
|
|
|
#include "thin-provisioning/metadata_checker.h"
|
2020-02-16 14:13:31 +05:30
|
|
|
#include "thin-provisioning/metadata_counter.h"
|
|
|
|
#include "thin-provisioning/superblock.h"
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-05-28 19:05:51 +05:30
|
|
|
using namespace boost;
|
2013-04-25 20:57:07 +05:30
|
|
|
using namespace persistent_data;
|
2011-10-28 16:37:21 +05:30
|
|
|
using namespace thin_provisioning;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
namespace {
|
|
|
|
class superblock_reporter : public superblock_detail::damage_visitor {
|
|
|
|
public:
|
|
|
|
superblock_reporter(nested_output &out)
|
|
|
|
: out_(out),
|
|
|
|
err_(NO_ERROR) {
|
|
|
|
}
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
virtual void visit(superblock_detail::superblock_corruption const &d) {
|
|
|
|
out_ << "superblock is corrupt" << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = out_.push();
|
|
|
|
out_ << d.desc_ << end_message();
|
|
|
|
}
|
|
|
|
err_ << FATAL;
|
|
|
|
}
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
base::error_state get_error() const {
|
|
|
|
return err_;
|
|
|
|
}
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
private:
|
|
|
|
nested_output &out_;
|
|
|
|
error_state err_;
|
|
|
|
};
|
2013-04-29 20:42:05 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
//--------------------------------
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
class devices_reporter : public device_tree_detail::damage_visitor {
|
|
|
|
public:
|
|
|
|
devices_reporter(nested_output &out)
|
|
|
|
: out_(out),
|
|
|
|
err_(NO_ERROR) {
|
|
|
|
}
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
virtual void visit(device_tree_detail::missing_devices const &d) {
|
|
|
|
out_ << "missing devices: " << d.keys_ << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = out_.push();
|
|
|
|
out_ << d.desc_ << end_message();
|
|
|
|
}
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
err_ << FATAL;
|
|
|
|
}
|
2013-04-29 20:42:05 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
error_state get_error() const {
|
|
|
|
return err_;
|
|
|
|
}
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
private:
|
|
|
|
nested_output &out_;
|
|
|
|
error_state err_;
|
|
|
|
};
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
//--------------------------------
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-05-28 19:05:51 +05:30
|
|
|
class data_ref_counter : public mapping_tree_detail::mapping_visitor {
|
|
|
|
public:
|
|
|
|
data_ref_counter(space_map::ptr sm)
|
|
|
|
: sm_(sm) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(btree_path const &path, mapping_tree_detail::block_time const &bt) {
|
|
|
|
sm_->inc(bt.block_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
space_map::ptr sm_;
|
|
|
|
};
|
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
class mapping_reporter : public mapping_tree_detail::damage_visitor {
|
|
|
|
public:
|
|
|
|
mapping_reporter(nested_output &out)
|
|
|
|
: out_(out),
|
|
|
|
err_(NO_ERROR) {
|
|
|
|
}
|
2013-04-29 20:42:05 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
virtual void visit(mapping_tree_detail::missing_devices const &d) {
|
|
|
|
out_ << "missing all mappings for devices: " << d.keys_ << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = out_.push();
|
|
|
|
out_ << d.desc_ << end_message();
|
|
|
|
}
|
|
|
|
err_ << FATAL;
|
|
|
|
}
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
virtual void visit(mapping_tree_detail::missing_mappings const &d) {
|
|
|
|
out_ << "thin device " << d.thin_dev_ << " is missing mappings " << d.keys_ << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = out_.push();
|
|
|
|
out_ << d.desc_ << end_message();
|
|
|
|
}
|
|
|
|
err_ << FATAL;
|
|
|
|
}
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
error_state get_error() const {
|
|
|
|
return err_;
|
|
|
|
}
|
2013-04-29 18:01:33 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
private:
|
|
|
|
nested_output &out_;
|
|
|
|
error_state err_;
|
|
|
|
};
|
2013-04-29 18:01:33 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
//--------------------------------
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-04-30 19:00:01 +05:30
|
|
|
error_state examine_superblock(block_manager::ptr bm,
|
2020-06-09 18:49:59 +05:30
|
|
|
block_address sb_location,
|
2020-02-16 14:13:31 +05:30
|
|
|
nested_output &out) {
|
|
|
|
out << "examining superblock" << end_message();
|
|
|
|
nested_output::nest _ = out.push();
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
superblock_reporter sb_rep(out);
|
2020-06-09 18:49:59 +05:30
|
|
|
check_superblock(bm, sb_rep, sb_location);
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
return sb_rep.get_error();
|
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
error_state examine_devices_tree_(transaction_manager::ptr tm,
|
|
|
|
superblock_detail::superblock const &sb,
|
2020-07-02 20:33:23 +05:30
|
|
|
nested_output &out,
|
2020-07-14 12:22:02 +05:30
|
|
|
bool ignore_non_fatal) {
|
2020-02-16 14:13:31 +05:30
|
|
|
out << "examining devices tree" << end_message();
|
|
|
|
nested_output::nest _ = out.push();
|
2012-03-02 15:30:31 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
devices_reporter dev_rep(out);
|
|
|
|
device_tree dtree(*tm, sb.device_details_root_,
|
|
|
|
device_tree_detail::device_details_traits::ref_counter());
|
2020-07-02 20:33:23 +05:30
|
|
|
check_device_tree(dtree, dev_rep, ignore_non_fatal);
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
return dev_rep.get_error();
|
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
error_state examine_top_level_mapping_tree_(transaction_manager::ptr tm,
|
|
|
|
superblock_detail::superblock const &sb,
|
2020-07-02 20:33:23 +05:30
|
|
|
nested_output &out,
|
2020-07-14 12:22:02 +05:30
|
|
|
bool ignore_non_fatal) {
|
2020-02-16 14:13:31 +05:30
|
|
|
out << "examining top level of mapping tree" << end_message();
|
|
|
|
nested_output::nest _ = out.push();
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
mapping_reporter mapping_rep(out);
|
|
|
|
dev_tree dtree(*tm, sb.data_mapping_root_,
|
|
|
|
mapping_tree_detail::mtree_traits::ref_counter(*tm));
|
2020-07-02 20:33:23 +05:30
|
|
|
check_mapping_tree(dtree, mapping_rep, ignore_non_fatal);
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
return mapping_rep.get_error();
|
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
error_state examine_mapping_tree_(transaction_manager::ptr tm,
|
|
|
|
superblock_detail::superblock const &sb,
|
2020-05-28 19:05:51 +05:30
|
|
|
nested_output &out,
|
2020-07-02 20:33:23 +05:30
|
|
|
optional<space_map::ptr> data_sm,
|
2020-07-14 12:22:02 +05:30
|
|
|
bool ignore_non_fatal) {
|
2020-02-16 14:13:31 +05:30
|
|
|
out << "examining mapping tree" << end_message();
|
|
|
|
nested_output::nest _ = out.push();
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
mapping_reporter mapping_rep(out);
|
|
|
|
mapping_tree mtree(*tm, sb.data_mapping_root_,
|
|
|
|
mapping_tree_detail::block_traits::ref_counter(tm->get_sm()));
|
2020-05-28 19:05:51 +05:30
|
|
|
|
|
|
|
if (data_sm) {
|
|
|
|
data_ref_counter dcounter(*data_sm);
|
2020-07-02 20:33:23 +05:30
|
|
|
walk_mapping_tree(mtree, dcounter, mapping_rep, ignore_non_fatal);
|
2020-05-28 19:05:51 +05:30
|
|
|
} else
|
2020-07-02 20:33:23 +05:30
|
|
|
check_mapping_tree(mtree, mapping_rep, ignore_non_fatal);
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
return mapping_rep.get_error();
|
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
error_state examine_top_level_mapping_tree(transaction_manager::ptr tm,
|
|
|
|
superblock_detail::superblock const &sb,
|
2020-07-02 20:33:23 +05:30
|
|
|
nested_output &out,
|
2020-07-14 12:22:02 +05:30
|
|
|
bool ignore_non_fatal) {
|
2020-07-02 20:33:23 +05:30
|
|
|
error_state err = examine_devices_tree_(tm, sb, out, ignore_non_fatal);
|
|
|
|
err << examine_top_level_mapping_tree_(tm, sb, out, ignore_non_fatal);
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
return err;
|
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
error_state examine_mapping_tree(transaction_manager::ptr tm,
|
|
|
|
superblock_detail::superblock const &sb,
|
2020-05-28 19:05:51 +05:30
|
|
|
nested_output &out,
|
2020-07-14 12:22:02 +05:30
|
|
|
optional<space_map::ptr> data_sm,
|
|
|
|
bool ignore_non_fatal) {
|
2020-07-02 20:33:23 +05:30
|
|
|
error_state err = examine_devices_tree_(tm, sb, out, ignore_non_fatal);
|
|
|
|
err << examine_mapping_tree_(tm, sb, out, data_sm, ignore_non_fatal);
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
return err;
|
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-07-14 15:27:29 +05:30
|
|
|
error_state compare_metadata_space_maps(space_map::ptr actual,
|
|
|
|
block_counter const &expected,
|
|
|
|
nested_output &out) {
|
2020-02-16 14:13:31 +05:30
|
|
|
error_state err = NO_ERROR;
|
2020-07-14 15:27:29 +05:30
|
|
|
block_address nr_blocks = actual->get_nr_blocks();
|
|
|
|
|
|
|
|
for (block_address b = 0; b < nr_blocks; b++) {
|
|
|
|
ref_t c_actual = actual->get_count(b);
|
|
|
|
ref_t c_expected = expected.get_count(b);
|
2020-02-16 14:13:31 +05:30
|
|
|
|
|
|
|
if (c_actual != c_expected) {
|
|
|
|
out << "metadata reference counts differ for block " << b
|
|
|
|
<< ", expected " << c_expected
|
|
|
|
<< ", but got " << c_actual
|
|
|
|
<< end_message();
|
|
|
|
|
|
|
|
err << (c_actual > c_expected ? NON_FATAL : FATAL);
|
2011-10-28 16:37:21 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
return err;
|
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-07-14 15:27:29 +05:30
|
|
|
error_state collect_leaked_blocks(space_map::ptr actual,
|
|
|
|
block_counter const &expected,
|
|
|
|
std::set<block_address> &leaked) {
|
|
|
|
error_state err = NO_ERROR;
|
|
|
|
block_address nr_blocks = actual->get_nr_blocks();
|
|
|
|
|
|
|
|
for (block_address b = 0; b < nr_blocks; b++) {
|
|
|
|
ref_t c_actual = actual->get_count(b);
|
|
|
|
ref_t c_expected = expected.get_count(b);
|
|
|
|
|
|
|
|
if (c_actual == c_expected)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (c_actual < c_expected) {
|
|
|
|
err << FATAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Theoretically, the ref-count of a leaked block
|
|
|
|
// should be only one. Here a leaked ref-count of two
|
|
|
|
// is allowed.
|
|
|
|
if (c_expected || c_actual >= 3)
|
|
|
|
err << NON_FATAL;
|
|
|
|
else if (c_actual > 0)
|
|
|
|
leaked.insert(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-08-12 09:07:39 +05:30
|
|
|
void mark_sm_shadowed(transaction_manager::ptr tm, persistent_space_map::ptr sm) {
|
|
|
|
block_counter bc(true);
|
|
|
|
sm->count_metadata(bc);
|
|
|
|
|
|
|
|
block_address nr_blocks = sm->get_nr_blocks();
|
|
|
|
for (block_address b = 0; b < nr_blocks; b++) {
|
|
|
|
if (bc.get_count(b))
|
|
|
|
tm->mark_shadowed(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 15:27:29 +05:30
|
|
|
error_state clear_leaked_blocks(space_map::ptr actual,
|
|
|
|
block_counter const &expected) {
|
|
|
|
error_state err = NO_ERROR;
|
|
|
|
std::set<block_address> leaked;
|
|
|
|
|
|
|
|
err << collect_leaked_blocks(actual, expected, leaked);
|
|
|
|
if (err != NO_ERROR)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
for (auto const &b : leaked)
|
|
|
|
actual->set_count(b, 0);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_state check_metadata_space_map_counts(transaction_manager::ptr tm,
|
|
|
|
superblock_detail::superblock const &sb,
|
|
|
|
block_counter &bc,
|
|
|
|
nested_output &out) {
|
|
|
|
out << "checking space map counts" << end_message();
|
|
|
|
nested_output::nest _ = out.push();
|
|
|
|
|
2020-08-11 20:12:32 +05:30
|
|
|
if (!count_metadata(tm, sb, bc))
|
|
|
|
return FATAL;
|
2020-07-14 15:27:29 +05:30
|
|
|
|
|
|
|
// Finally we need to check the metadata space map agrees
|
|
|
|
// with the counts we've just calculated.
|
|
|
|
space_map::ptr metadata_sm =
|
|
|
|
open_metadata_sm(*tm, static_cast<void const*>(&sb.metadata_space_map_root_));
|
|
|
|
return compare_metadata_space_maps(metadata_sm, bc, out);
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:05:51 +05:30
|
|
|
error_state compare_space_maps(space_map::ptr actual, space_map::ptr expected,
|
|
|
|
nested_output &out)
|
|
|
|
{
|
|
|
|
error_state err = NO_ERROR;
|
|
|
|
auto nr_blocks = actual->get_nr_blocks();
|
|
|
|
|
|
|
|
if (expected->get_nr_blocks() != nr_blocks) {
|
|
|
|
out << "internal error: nr blocks in space maps differ"
|
|
|
|
<< end_message();
|
|
|
|
err << FATAL;
|
|
|
|
} else {
|
|
|
|
for (block_address b = 0; b < nr_blocks; b++) {
|
|
|
|
auto a_count = actual->get_count(b);
|
2020-08-10 01:00:19 +05:30
|
|
|
auto e_count = expected->get_count(b);
|
2020-07-14 12:22:02 +05:30
|
|
|
|
2020-05-28 19:05:51 +05:30
|
|
|
if (a_count != e_count) {
|
|
|
|
out << "data reference counts differ for block " << b
|
|
|
|
<< ", expected " << e_count
|
|
|
|
<< ", but got " << a_count
|
|
|
|
<< end_message();
|
|
|
|
err << (a_count > e_count ? NON_FATAL : FATAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
void print_info(transaction_manager::ptr tm,
|
|
|
|
superblock_detail::superblock const &sb,
|
|
|
|
nested_output &out)
|
|
|
|
{
|
|
|
|
out << "TRANSACTION_ID=" << sb.trans_id_ << "\n"
|
|
|
|
<< "METADATA_FREE_BLOCKS=" << tm->get_sm()->get_nr_free()
|
|
|
|
<< end_message();
|
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
block_address mapping_root(superblock_detail::superblock const &sb, check_options const &opts)
|
|
|
|
{
|
|
|
|
return opts.override_mapping_root_ ? *opts.override_mapping_root_ : sb.data_mapping_root_;
|
2011-10-28 16:37:21 +05:30
|
|
|
}
|
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
//--------------------------------
|
|
|
|
|
2020-05-28 19:13:03 +05:30
|
|
|
class metadata_checker {
|
2013-04-23 19:51:44 +05:30
|
|
|
public:
|
2020-07-14 15:27:29 +05:30
|
|
|
metadata_checker(std::string const &path,
|
2020-05-28 19:13:03 +05:30
|
|
|
check_options check_opts,
|
|
|
|
output_options output_opts)
|
2020-07-14 15:27:29 +05:30
|
|
|
: path_(path),
|
2020-02-16 14:13:31 +05:30
|
|
|
options_(check_opts),
|
|
|
|
out_(cerr, 2),
|
2020-07-14 15:27:29 +05:30
|
|
|
info_out_(cout, 0),
|
2020-08-11 20:12:32 +05:30
|
|
|
expected_rc_(true), // set stop on the first error
|
2020-07-14 15:27:29 +05:30
|
|
|
err_(NO_ERROR) {
|
2020-02-16 14:13:31 +05:30
|
|
|
|
|
|
|
if (output_opts == OUTPUT_QUIET) {
|
|
|
|
out_.disable();
|
|
|
|
info_out_.disable();
|
|
|
|
}
|
2020-07-14 15:27:29 +05:30
|
|
|
|
|
|
|
sb_location_ = get_superblock_location();
|
2013-04-23 19:51:44 +05:30
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-07-14 15:27:29 +05:30
|
|
|
void check() {
|
|
|
|
block_manager::ptr bm = open_bm(path_, block_manager::READ_ONLY,
|
|
|
|
!options_.use_metadata_snap_);
|
2012-05-17 16:58:23 +05:30
|
|
|
|
2020-07-14 15:27:29 +05:30
|
|
|
err_ = examine_superblock(bm, sb_location_, out_);
|
|
|
|
if (err_ == FATAL) {
|
|
|
|
if (check_for_xml(bm))
|
2020-02-16 14:13:31 +05:30
|
|
|
out_ << "This looks like XML. thin_check only checks the binary metadata format." << end_message();
|
2020-07-14 15:27:29 +05:30
|
|
|
return;
|
2013-04-23 19:51:44 +05:30
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-07-14 15:27:29 +05:30
|
|
|
transaction_manager::ptr tm = open_tm(bm, sb_location_);
|
|
|
|
superblock_detail::superblock sb = read_superblock(bm, sb_location_);
|
2020-02-16 14:13:31 +05:30
|
|
|
sb.data_mapping_root_ = mapping_root(sb, options_);
|
2013-04-23 19:51:44 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
print_info(tm, sb, info_out_);
|
2013-04-23 19:51:44 +05:30
|
|
|
|
2020-05-28 19:05:51 +05:30
|
|
|
if (options_.sm_opts_ == check_options::SPACE_MAP_FULL) {
|
2020-08-12 05:59:16 +05:30
|
|
|
// data block reference counting is disabled
|
|
|
|
// until that there's a better solution in space
|
|
|
|
// and time complexity
|
2020-05-28 19:05:51 +05:30
|
|
|
space_map::ptr data_sm{open_disk_sm(*tm, &sb.data_space_map_root_)};
|
2020-08-12 05:59:16 +05:30
|
|
|
optional<space_map::ptr> core_sm;
|
2020-07-14 15:27:29 +05:30
|
|
|
err_ << examine_data_mappings(tm, sb, options_.data_mapping_opts_, out_, core_sm);
|
|
|
|
|
|
|
|
if (err_ == FATAL)
|
|
|
|
return;
|
2020-05-28 19:05:51 +05:30
|
|
|
|
|
|
|
// if we're checking everything, and there were no errors,
|
|
|
|
// then we should check the space maps too.
|
2020-07-14 15:27:29 +05:30
|
|
|
err_ << examine_metadata_space_map(tm, sb, options_.sm_opts_, out_, expected_rc_);
|
2013-04-23 19:51:44 +05:30
|
|
|
|
2020-08-12 05:59:16 +05:30
|
|
|
// verify ref-counts of data blocks
|
2020-08-11 20:12:32 +05:30
|
|
|
if (err_ != FATAL && core_sm)
|
2020-07-14 15:27:29 +05:30
|
|
|
err_ << compare_space_maps(data_sm, *core_sm, out_);
|
2020-05-28 19:05:51 +05:30
|
|
|
} else
|
2020-07-14 15:27:29 +05:30
|
|
|
err_ << examine_data_mappings(tm, sb, options_.data_mapping_opts_, out_,
|
|
|
|
optional<space_map::ptr>());
|
|
|
|
}
|
2013-04-23 19:51:44 +05:30
|
|
|
|
2020-08-12 09:07:39 +05:30
|
|
|
bool fix_metadata_leaks(bool open_transaction) {
|
2020-07-14 15:27:29 +05:30
|
|
|
if (!verify_preconditions_before_fixing()) {
|
|
|
|
out_ << "metadata has not been fully examined" << end_message();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip if the metadata cannot be fixed, or there's no leaked blocks
|
|
|
|
if (err_ == FATAL)
|
|
|
|
return false;
|
|
|
|
else if (err_ == NO_ERROR)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
block_manager::ptr bm = open_bm(path_, block_manager::READ_WRITE);
|
|
|
|
superblock_detail::superblock sb = read_superblock(bm, sb_location_);
|
|
|
|
transaction_manager::ptr tm = open_tm(bm, sb_location_);
|
|
|
|
persistent_space_map::ptr metadata_sm =
|
|
|
|
open_metadata_sm(*tm, static_cast<void const*>(&sb.metadata_space_map_root_));
|
|
|
|
tm->set_sm(metadata_sm);
|
|
|
|
|
2020-08-12 09:07:39 +05:30
|
|
|
if (!open_transaction)
|
|
|
|
mark_sm_shadowed(tm, metadata_sm);
|
|
|
|
|
2020-07-14 15:27:29 +05:30
|
|
|
err_ = clear_leaked_blocks(metadata_sm, expected_rc_);
|
|
|
|
|
|
|
|
if (err_ != NO_ERROR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
metadata_sm->commit();
|
|
|
|
metadata_sm->copy_root(&sb.metadata_space_map_root_, sizeof(sb.metadata_space_map_root_));
|
|
|
|
write_superblock(bm, sb);
|
|
|
|
|
|
|
|
out_ << "fixed metadata leaks" << end_message();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-21 10:33:53 +05:30
|
|
|
bool clear_needs_check_flag() {
|
|
|
|
if (!verify_preconditions_before_fixing()) {
|
|
|
|
out_ << "metadata has not been fully examined" << end_message();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err_ != NO_ERROR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
block_manager::ptr bm = open_bm(path_, block_manager::READ_WRITE);
|
|
|
|
superblock_detail::superblock sb = read_superblock(bm);
|
2020-08-06 22:36:46 +05:30
|
|
|
|
|
|
|
if (!sb.get_needs_check_flag())
|
|
|
|
return true;
|
|
|
|
|
2020-07-21 10:33:53 +05:30
|
|
|
sb.set_needs_check_flag(false);
|
|
|
|
write_superblock(bm, sb);
|
|
|
|
|
|
|
|
out_ << "cleared needs_check flag" << end_message();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_status() const {
|
|
|
|
if (options_.ignore_non_fatal_)
|
|
|
|
return (err_ == FATAL) ? false : true;
|
|
|
|
|
|
|
|
return (err_ == NO_ERROR) ? true : false;
|
2013-04-23 19:51:44 +05:30
|
|
|
}
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-04-23 19:51:44 +05:30
|
|
|
private:
|
2020-07-14 15:27:29 +05:30
|
|
|
block_address
|
|
|
|
get_superblock_location() {
|
|
|
|
block_address sb_location = superblock_detail::SUPERBLOCK_LOCATION;
|
|
|
|
|
|
|
|
if (options_.use_metadata_snap_) {
|
|
|
|
block_manager::ptr bm = open_bm(path_, block_manager::READ_ONLY,
|
|
|
|
!options_.use_metadata_snap_);
|
|
|
|
superblock_detail::superblock sb = read_superblock(bm, sb_location);
|
|
|
|
sb_location = sb.metadata_snap_;
|
|
|
|
if (sb_location == superblock_detail::SUPERBLOCK_LOCATION)
|
|
|
|
throw runtime_error("No metadata snapshot found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb_location;
|
|
|
|
}
|
|
|
|
|
2020-07-02 20:33:23 +05:30
|
|
|
error_state
|
2020-02-16 14:13:31 +05:30
|
|
|
examine_data_mappings(transaction_manager::ptr tm,
|
|
|
|
superblock_detail::superblock const &sb,
|
|
|
|
check_options::data_mapping_options option,
|
2020-05-28 19:05:51 +05:30
|
|
|
nested_output &out,
|
|
|
|
optional<space_map::ptr> data_sm) {
|
2020-02-16 14:13:31 +05:30
|
|
|
error_state err = NO_ERROR;
|
|
|
|
|
|
|
|
switch (option) {
|
|
|
|
case check_options::DATA_MAPPING_LEVEL1:
|
2020-07-02 20:33:23 +05:30
|
|
|
err << examine_top_level_mapping_tree(tm, sb, out, options_.ignore_non_fatal_);
|
2020-02-16 14:13:31 +05:30
|
|
|
break;
|
|
|
|
case check_options::DATA_MAPPING_LEVEL2:
|
2020-07-02 20:33:23 +05:30
|
|
|
err << examine_mapping_tree(tm, sb, out, data_sm, options_.ignore_non_fatal_);
|
2020-02-16 14:13:31 +05:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break; // do nothing
|
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
return err;
|
2011-10-28 16:37:21 +05:30
|
|
|
}
|
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
static error_state
|
|
|
|
examine_metadata_space_map(transaction_manager::ptr tm,
|
|
|
|
superblock_detail::superblock const &sb,
|
2020-05-28 19:05:51 +05:30
|
|
|
check_options::space_map_options option,
|
2020-07-14 15:27:29 +05:30
|
|
|
nested_output &out,
|
|
|
|
block_counter &bc) {
|
2020-02-16 14:13:31 +05:30
|
|
|
error_state err = NO_ERROR;
|
|
|
|
|
|
|
|
switch (option) {
|
2020-05-28 19:05:51 +05:30
|
|
|
case check_options::SPACE_MAP_FULL:
|
2020-07-14 15:27:29 +05:30
|
|
|
err << check_metadata_space_map_counts(tm, sb, bc, out);
|
2020-02-16 14:13:31 +05:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break; // do nothing
|
|
|
|
}
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
return err;
|
2013-04-25 20:57:07 +05:30
|
|
|
}
|
|
|
|
|
2020-07-14 15:27:29 +05:30
|
|
|
bool verify_preconditions_before_fixing() const {
|
|
|
|
if (options_.use_metadata_snap_ ||
|
|
|
|
!!options_.override_mapping_root_ ||
|
|
|
|
options_.sm_opts_ != check_options::SPACE_MAP_FULL ||
|
|
|
|
options_.data_mapping_opts_ != check_options::DATA_MAPPING_LEVEL2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!expected_rc_.get_counts().size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const &path_;
|
2020-02-16 14:13:31 +05:30
|
|
|
check_options options_;
|
|
|
|
nested_output out_;
|
|
|
|
nested_output info_out_;
|
2020-07-14 15:27:29 +05:30
|
|
|
block_address sb_location_;
|
|
|
|
block_counter expected_rc_;
|
|
|
|
base::error_state err_; // metadata state
|
2020-02-16 14:13:31 +05:30
|
|
|
};
|
|
|
|
}
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
//----------------------------------------------------------------
|
2013-04-25 20:57:07 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
check_options::check_options()
|
2020-06-12 18:11:47 +05:30
|
|
|
: use_metadata_snap_(false),
|
2020-07-14 12:36:53 +05:30
|
|
|
data_mapping_opts_(DATA_MAPPING_LEVEL2),
|
2020-07-14 09:12:57 +05:30
|
|
|
sm_opts_(SPACE_MAP_FULL),
|
2020-07-14 15:27:29 +05:30
|
|
|
ignore_non_fatal_(false),
|
2020-07-21 10:33:53 +05:30
|
|
|
fix_metadata_leaks_(false),
|
2020-08-12 09:07:39 +05:30
|
|
|
clear_needs_check_(false),
|
|
|
|
open_transaction_(false) {
|
2020-02-16 14:13:31 +05:30
|
|
|
}
|
2013-04-23 19:51:44 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
void check_options::set_superblock_only() {
|
2020-07-14 12:36:53 +05:30
|
|
|
data_mapping_opts_ = DATA_MAPPING_NONE;
|
2020-05-28 19:05:51 +05:30
|
|
|
sm_opts_ = SPACE_MAP_NONE;
|
2013-04-23 19:51:44 +05:30
|
|
|
}
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
void check_options::set_skip_mappings() {
|
2020-07-14 12:36:53 +05:30
|
|
|
data_mapping_opts_ = DATA_MAPPING_LEVEL1;
|
2020-05-28 19:05:51 +05:30
|
|
|
sm_opts_ = SPACE_MAP_NONE;
|
2020-02-16 14:13:31 +05:30
|
|
|
}
|
2011-10-28 16:37:21 +05:30
|
|
|
|
2020-02-16 14:13:31 +05:30
|
|
|
void check_options::set_override_mapping_root(block_address b) {
|
|
|
|
override_mapping_root_ = b;
|
|
|
|
}
|
2013-04-23 19:51:44 +05:30
|
|
|
|
2020-06-09 18:49:59 +05:30
|
|
|
void check_options::set_metadata_snap() {
|
2020-06-12 18:11:47 +05:30
|
|
|
use_metadata_snap_ = true;
|
2020-06-09 18:49:59 +05:30
|
|
|
sm_opts_ = SPACE_MAP_NONE;
|
|
|
|
}
|
2020-07-02 20:33:23 +05:30
|
|
|
|
|
|
|
void check_options::set_ignore_non_fatal() {
|
|
|
|
ignore_non_fatal_ = true;
|
|
|
|
}
|
2020-07-14 15:27:29 +05:30
|
|
|
|
2020-08-12 20:55:24 +05:30
|
|
|
void check_options::set_auto_repair() {
|
2020-07-14 15:27:29 +05:30
|
|
|
fix_metadata_leaks_ = true;
|
2020-08-12 20:55:24 +05:30
|
|
|
clear_needs_check_ = true;
|
2020-07-14 15:27:29 +05:30
|
|
|
}
|
|
|
|
|
2020-07-21 10:33:53 +05:30
|
|
|
void check_options::set_clear_needs_check() {
|
|
|
|
clear_needs_check_ = true;
|
|
|
|
}
|
|
|
|
|
2020-07-14 15:27:29 +05:30
|
|
|
bool check_options::check_conformance() {
|
2020-07-21 10:33:53 +05:30
|
|
|
if (fix_metadata_leaks_ || clear_needs_check_) {
|
2020-07-14 15:27:29 +05:30
|
|
|
if (ignore_non_fatal_) {
|
|
|
|
cerr << "cannot perform fix by ignoring non-fatal errors" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_metadata_snap_) {
|
|
|
|
cerr << "cannot perform fix within metadata snap" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!!override_mapping_root_) {
|
|
|
|
cerr << "cannot perform fix with an overridden mapping root" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data_mapping_opts_ != DATA_MAPPING_LEVEL2 ||
|
|
|
|
sm_opts_ != SPACE_MAP_FULL) {
|
|
|
|
cerr << "cannot perform fix without a full examination" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2020-07-21 10:33:53 +05:30
|
|
|
bool
|
2020-07-14 15:27:29 +05:30
|
|
|
thin_provisioning::check_metadata(std::string const &path,
|
2020-05-28 19:13:03 +05:30
|
|
|
check_options const &check_opts,
|
|
|
|
output_options output_opts)
|
2013-04-23 19:51:44 +05:30
|
|
|
{
|
2020-07-14 15:27:29 +05:30
|
|
|
metadata_checker checker(path, check_opts, output_opts);
|
|
|
|
|
|
|
|
checker.check();
|
|
|
|
if (check_opts.fix_metadata_leaks_)
|
2020-08-12 09:07:39 +05:30
|
|
|
checker.fix_metadata_leaks(check_opts.open_transaction_);
|
2020-08-12 20:55:24 +05:30
|
|
|
if (check_opts.clear_needs_check_)
|
2020-07-21 10:33:53 +05:30
|
|
|
checker.clear_needs_check_flag();
|
2020-07-14 15:27:29 +05:30
|
|
|
|
2020-07-21 10:33:53 +05:30
|
|
|
return checker.get_status();
|
2011-10-28 16:37:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|