2012-02-27 19:37:16 +05:30
|
|
|
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
|
|
|
|
#include "version.h"
|
|
|
|
|
2013-05-21 20:19:20 +05:30
|
|
|
#include "persistent-data/space-maps/core.h"
|
2013-05-21 19:08:33 +05:30
|
|
|
#include "thin-provisioning/device_tree.h"
|
|
|
|
#include "thin-provisioning/file_utils.h"
|
|
|
|
#include "thin-provisioning/mapping_tree.h"
|
|
|
|
#include "thin-provisioning/superblock.h"
|
|
|
|
|
2012-02-27 19:37:16 +05:30
|
|
|
using namespace persistent_data;
|
|
|
|
using namespace std;
|
|
|
|
using namespace thin_provisioning;
|
|
|
|
|
2013-05-21 19:08:33 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2012-02-27 19:37:16 +05:30
|
|
|
namespace {
|
2013-05-21 19:35:25 +05:30
|
|
|
|
|
|
|
class end_message {};
|
|
|
|
|
|
|
|
class nested_output {
|
|
|
|
public:
|
|
|
|
nested_output(ostream &out, unsigned step)
|
|
|
|
: out_(out),
|
|
|
|
step_(step),
|
|
|
|
beginning_of_line_(true),
|
|
|
|
indent_(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
nested_output &operator <<(T const &t) {
|
|
|
|
if (beginning_of_line_) {
|
|
|
|
beginning_of_line_ = false;
|
|
|
|
indent();
|
|
|
|
}
|
|
|
|
|
|
|
|
out_ << t;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
nested_output &operator <<(end_message const &m) {
|
|
|
|
beginning_of_line_ = true;
|
|
|
|
out_ << endl;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void inc_indent() {
|
|
|
|
indent_ += step_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dec_indent() {
|
|
|
|
indent_ -= step_;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct nest {
|
|
|
|
nest(nested_output &out)
|
|
|
|
: out_(out) {
|
|
|
|
out_.inc_indent();
|
|
|
|
}
|
|
|
|
|
|
|
|
~nest() {
|
|
|
|
out_.dec_indent();
|
|
|
|
}
|
|
|
|
|
|
|
|
nested_output &out_;
|
|
|
|
};
|
|
|
|
|
|
|
|
nest push() {
|
|
|
|
return nest(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void indent() {
|
|
|
|
for (unsigned i = 0; i < indent_; i++)
|
|
|
|
out_ << ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
ostream &out_;
|
|
|
|
unsigned step_;
|
|
|
|
|
|
|
|
bool beginning_of_line_;
|
|
|
|
unsigned indent_;
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
2013-05-21 17:16:07 +05:30
|
|
|
enum error_state {
|
2013-05-21 19:08:33 +05:30
|
|
|
NO_ERROR,
|
2013-05-21 17:16:07 +05:30
|
|
|
NON_FATAL, // eg, lost blocks
|
|
|
|
FATAL // needs fixing before pool can be activated
|
|
|
|
};
|
|
|
|
|
2013-05-21 19:08:33 +05:30
|
|
|
error_state
|
|
|
|
combine_errors(error_state lhs, error_state rhs) {
|
|
|
|
switch (lhs) {
|
|
|
|
case NO_ERROR:
|
|
|
|
return rhs;
|
|
|
|
|
|
|
|
case NON_FATAL:
|
|
|
|
return (rhs == FATAL) ? FATAL : lhs;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 20:19:20 +05:30
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
block_manager<>::ptr
|
|
|
|
open_bm(string const &path) {
|
2013-05-21 19:08:33 +05:30
|
|
|
block_address nr_blocks = get_nr_blocks(path);
|
|
|
|
typename block_io<>::mode m = block_io<>::READ_ONLY;
|
|
|
|
return block_manager<>::ptr(new block_manager<>(path, nr_blocks, 1, m));
|
|
|
|
}
|
|
|
|
|
2013-05-21 20:19:20 +05:30
|
|
|
transaction_manager::ptr
|
|
|
|
open_tm(block_manager<>::ptr bm) {
|
|
|
|
space_map::ptr sm(new core_map(bm->get_nr_blocks()));
|
|
|
|
sm->inc(superblock_detail::SUPERBLOCK_LOCATION);
|
|
|
|
transaction_manager::ptr tm(new transaction_manager(bm, sm));
|
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
2013-05-21 19:08:33 +05:30
|
|
|
class superblock_reporter : public superblock_detail::damage_visitor {
|
|
|
|
public:
|
2013-05-21 19:35:25 +05:30
|
|
|
superblock_reporter(nested_output &out)
|
2013-05-21 19:08:33 +05:30
|
|
|
: out_(out),
|
|
|
|
err_(NO_ERROR) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(superblock_detail::superblock_corruption const &d) {
|
2013-05-21 19:35:25 +05:30
|
|
|
out_ << "superblock is corrupt" << end_message();
|
2013-05-21 20:19:20 +05:30
|
|
|
{
|
|
|
|
auto _ = out_.push();
|
|
|
|
out_ << d.desc_ << end_message();
|
|
|
|
}
|
2013-05-21 19:08:33 +05:30
|
|
|
err_ = combine_errors(err_, FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
error_state get_error() const {
|
|
|
|
return err_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-05-21 19:35:25 +05:30
|
|
|
nested_output &out_;
|
2013-05-21 19:08:33 +05:30
|
|
|
error_state err_;
|
|
|
|
};
|
|
|
|
|
2013-05-21 20:19:20 +05:30
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
class devices_reporter : public device_tree_detail::damage_visitor {
|
|
|
|
public:
|
|
|
|
devices_reporter(nested_output &out)
|
|
|
|
: out_(out),
|
|
|
|
err_(NO_ERROR) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(device_tree_detail::missing_devices const &d) {
|
|
|
|
out_ << "missing devices: " << d.keys_ << end_message();
|
|
|
|
{
|
|
|
|
auto _ = out_.push();
|
|
|
|
out_ << d.desc_ << end_message();
|
|
|
|
}
|
|
|
|
|
|
|
|
err_ = combine_errors(err_, FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
error_state get_error() const {
|
|
|
|
return err_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nested_output &out_;
|
|
|
|
error_state err_;
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
2013-05-22 19:25:28 +05:30
|
|
|
class mapping_reporter : public mapping_tree_detail::damage_visitor {
|
|
|
|
public:
|
|
|
|
mapping_reporter(nested_output &out)
|
|
|
|
: out_(out) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(mapping_tree_detail::missing_devices const &d) {
|
|
|
|
out_ << "missing all mappings for devices: " << d.keys_ << end_message();
|
|
|
|
{
|
|
|
|
auto _ = out_.push();
|
|
|
|
out_ << d.desc_ << end_message();
|
|
|
|
}
|
|
|
|
err_ = combine_errors(err_, FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(mapping_tree_detail::missing_mappings const &d) {
|
|
|
|
out_ << "thin device " << d.thin_dev_ << " is missing mappings " << d.keys_ << end_message();
|
|
|
|
{
|
|
|
|
auto _ = out_.push();
|
|
|
|
out_ << d.desc_ << end_message();
|
|
|
|
}
|
|
|
|
err_ = combine_errors(err_, FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nested_output &out_;
|
|
|
|
error_state err_;
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
2013-05-21 19:08:33 +05:30
|
|
|
error_state metadata_check(string const &path) {
|
|
|
|
block_manager<>::ptr bm = open_bm(path);
|
|
|
|
|
2013-05-21 19:35:25 +05:30
|
|
|
nested_output out(cerr, 2);
|
2013-05-21 20:19:20 +05:30
|
|
|
superblock_reporter sb_rep(out);
|
|
|
|
devices_reporter dev_rep(out);
|
2013-05-22 19:25:28 +05:30
|
|
|
mapping_reporter mapping_rep(out);
|
2013-05-21 20:19:20 +05:30
|
|
|
|
2013-05-21 19:35:25 +05:30
|
|
|
out << "examining superblock" << end_message();
|
|
|
|
{
|
|
|
|
auto _ = out.push();
|
|
|
|
check_superblock(bm, sb_rep);
|
2013-05-21 20:19:20 +05:30
|
|
|
}
|
2013-05-21 19:35:25 +05:30
|
|
|
|
2013-05-21 20:19:20 +05:30
|
|
|
if (sb_rep.get_error() == FATAL)
|
|
|
|
return FATAL;
|
|
|
|
|
2013-05-22 19:25:28 +05:30
|
|
|
superblock_detail::superblock sb = read_superblock(bm);
|
|
|
|
transaction_manager::ptr tm = open_tm(bm);
|
|
|
|
|
2013-05-21 20:19:20 +05:30
|
|
|
out << "examining devices tree" << end_message();
|
|
|
|
{
|
|
|
|
auto _ = out.push();
|
|
|
|
device_tree dtree(tm, sb.device_details_root_,
|
|
|
|
device_tree_detail::device_details_traits::ref_counter());
|
|
|
|
check_device_tree(dtree, dev_rep);
|
2013-05-21 19:35:25 +05:30
|
|
|
}
|
2013-05-21 20:19:20 +05:30
|
|
|
|
2013-05-22 19:25:28 +05:30
|
|
|
out << "examining mapping tree" << end_message();
|
|
|
|
{
|
|
|
|
auto _ = out.push();
|
|
|
|
mapping_tree mtree(tm, sb.data_mapping_root_,
|
|
|
|
mapping_tree_detail::block_traits::ref_counter(tm->get_sm()));
|
|
|
|
check_mapping_tree(mtree, mapping_rep);
|
|
|
|
}
|
|
|
|
|
2013-05-21 20:19:20 +05:30
|
|
|
return combine_errors(sb_rep.get_error(),
|
|
|
|
dev_rep.get_error());
|
2013-05-21 19:08:33 +05:30
|
|
|
}
|
|
|
|
|
2012-03-02 18:29:59 +05:30
|
|
|
int check(string const &path, bool quiet) {
|
2013-05-21 19:08:33 +05:30
|
|
|
error_state err;
|
|
|
|
|
2012-03-02 15:30:31 +05:30
|
|
|
try {
|
2013-05-21 19:08:33 +05:30
|
|
|
// FIXME: use quiet flag (pass different reporter)
|
|
|
|
err = metadata_check(path);
|
|
|
|
|
2012-03-02 15:30:31 +05:30
|
|
|
} catch (std::exception &e) {
|
2013-05-21 19:08:33 +05:30
|
|
|
|
2012-03-02 18:29:59 +05:30
|
|
|
if (!quiet)
|
2012-03-13 17:40:13 +05:30
|
|
|
cerr << e.what() << endl;
|
2012-02-27 19:37:16 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:08:33 +05:30
|
|
|
return (err == NO_ERROR) ? 0 : 1;
|
2012-02-27 19:37:16 +05:30
|
|
|
}
|
|
|
|
|
2012-03-13 19:36:10 +05:30
|
|
|
void usage(ostream &out, string const &cmd) {
|
|
|
|
out << "Usage: " << cmd << " [options] {device|file}" << endl
|
|
|
|
<< "Options:" << endl
|
|
|
|
<< " {-q|--quiet}" << endl
|
|
|
|
<< " {-h|--help}" << endl
|
2013-04-11 18:10:47 +05:30
|
|
|
<< " {-V|--version}" << endl
|
|
|
|
<< " {--super-block-only}" << endl;
|
2012-02-27 19:37:16 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
2013-04-23 19:51:44 +05:30
|
|
|
bool quiet = false, superblock_only = false;
|
2012-03-02 18:40:53 +05:30
|
|
|
const char shortopts[] = "qhV";
|
2012-02-27 19:37:16 +05:30
|
|
|
const struct option longopts[] = {
|
2012-03-02 18:29:59 +05:30
|
|
|
{ "quiet", no_argument, NULL, 'q'},
|
2012-02-27 19:37:16 +05:30
|
|
|
{ "help", no_argument, NULL, 'h'},
|
|
|
|
{ "version", no_argument, NULL, 'V'},
|
2013-04-23 19:51:44 +05:30
|
|
|
{ "super-block-only", no_argument, NULL, 1},
|
2012-02-27 19:37:16 +05:30
|
|
|
{ NULL, no_argument, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
|
|
|
switch(c) {
|
|
|
|
case 'h':
|
2012-03-13 19:36:10 +05:30
|
|
|
usage(cout, basename(argv[0]));
|
2012-02-27 19:37:16 +05:30
|
|
|
return 0;
|
2012-03-02 18:29:59 +05:30
|
|
|
|
|
|
|
case 'q':
|
|
|
|
quiet = true;
|
|
|
|
break;
|
|
|
|
|
2012-02-27 19:37:16 +05:30
|
|
|
case 'V':
|
2012-03-13 18:02:30 +05:30
|
|
|
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
|
2012-02-27 19:37:16 +05:30
|
|
|
return 0;
|
2012-03-13 18:02:30 +05:30
|
|
|
|
2013-04-23 19:51:44 +05:30
|
|
|
case 1:
|
|
|
|
superblock_only = true;
|
|
|
|
break;
|
|
|
|
|
2012-03-06 00:04:05 +05:30
|
|
|
default:
|
2012-03-13 19:36:10 +05:30
|
|
|
usage(cerr, basename(argv[0]));
|
2012-03-06 00:04:05 +05:30
|
|
|
return 1;
|
2012-02-27 19:37:16 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 00:04:05 +05:30
|
|
|
if (argc == optind) {
|
2012-03-13 19:36:10 +05:30
|
|
|
cerr << "No input file provided." << endl;
|
|
|
|
usage(cerr, basename(argv[0]));
|
2012-02-27 19:37:16 +05:30
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-04-23 19:51:44 +05:30
|
|
|
|
2012-03-05 17:16:07 +05:30
|
|
|
return check(argv[optind], quiet);
|
2012-02-27 19:37:16 +05:30
|
|
|
}
|