2013-03-15 20:29:21 +05:30
|
|
|
#include <iostream>
|
2013-03-21 21:14:28 +05:30
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2013-03-15 20:29:21 +05:30
|
|
|
#include <getopt.h>
|
|
|
|
#include <libgen.h>
|
2013-03-21 21:14:28 +05:30
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2013-03-15 20:29:21 +05:30
|
|
|
|
2013-08-16 20:59:41 +05:30
|
|
|
#include "base/error_state.h"
|
2015-01-16 18:24:09 +05:30
|
|
|
#include "base/error_string.h"
|
2017-10-05 18:17:10 +05:30
|
|
|
#include "base/file_utils.h"
|
2013-08-16 20:59:41 +05:30
|
|
|
#include "base/nested_output.h"
|
2014-08-27 18:31:31 +05:30
|
|
|
#include "caching/commands.h"
|
2013-10-02 15:20:42 +05:30
|
|
|
#include "caching/metadata.h"
|
|
|
|
#include "persistent-data/block.h"
|
|
|
|
#include "persistent-data/file_utils.h"
|
2013-12-11 22:58:14 +05:30
|
|
|
#include "persistent-data/space_map.h"
|
2013-10-02 15:20:42 +05:30
|
|
|
#include "persistent-data/space-maps/core.h"
|
2013-10-11 15:03:32 +05:30
|
|
|
#include "version.h"
|
2013-08-16 20:59:41 +05:30
|
|
|
|
2013-10-02 15:20:42 +05:30
|
|
|
using namespace boost;
|
|
|
|
using namespace caching;
|
|
|
|
using namespace persistent_data;
|
2013-03-15 20:29:21 +05:30
|
|
|
using namespace std;
|
2013-03-21 21:14:28 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
2013-03-15 20:29:21 +05:30
|
|
|
|
|
|
|
namespace {
|
2013-10-02 15:20:42 +05:30
|
|
|
class reporter_base {
|
|
|
|
public:
|
|
|
|
reporter_base(nested_output &o)
|
|
|
|
: out_(o),
|
|
|
|
err_(NO_ERROR) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~reporter_base() {}
|
|
|
|
|
|
|
|
nested_output &out() {
|
|
|
|
return out_;
|
|
|
|
}
|
|
|
|
|
|
|
|
nested_output::nest push() {
|
|
|
|
return out_.push();
|
|
|
|
}
|
|
|
|
|
|
|
|
base::error_state get_error() const {
|
|
|
|
return err_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mplus_error(error_state err) {
|
|
|
|
err_ = combine_errors(err_, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nested_output &out_;
|
|
|
|
error_state err_;
|
|
|
|
};
|
|
|
|
|
2013-10-07 19:51:45 +05:30
|
|
|
class superblock_reporter : public superblock_damage::damage_visitor, reporter_base {
|
2013-10-02 15:20:42 +05:30
|
|
|
public:
|
|
|
|
superblock_reporter(nested_output &o)
|
|
|
|
: reporter_base(o) {
|
|
|
|
}
|
|
|
|
|
2013-10-07 19:51:45 +05:30
|
|
|
virtual void visit(superblock_damage::superblock_corrupt const &d) {
|
2013-10-02 15:20:42 +05:30
|
|
|
out() << "superblock is corrupt" << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = push();
|
2013-10-07 19:51:45 +05:30
|
|
|
out() << d.get_desc() << end_message();
|
|
|
|
}
|
|
|
|
|
|
|
|
mplus_error(FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(superblock_damage::superblock_invalid const &d) {
|
|
|
|
out() << "superblock is invalid" << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = push();
|
|
|
|
out() << d.get_desc() << end_message();
|
2013-10-02 15:20:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
mplus_error(FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
using reporter_base::get_error;
|
|
|
|
};
|
|
|
|
|
2013-10-08 14:06:20 +05:30
|
|
|
class mapping_reporter : public mapping_array_damage::damage_visitor, reporter_base {
|
2013-10-02 15:20:42 +05:30
|
|
|
public:
|
|
|
|
mapping_reporter(nested_output &o)
|
|
|
|
: reporter_base(o) {
|
|
|
|
}
|
2013-10-08 14:06:20 +05:30
|
|
|
|
|
|
|
virtual void visit(mapping_array_damage::missing_mappings const &d) {
|
2013-10-08 15:30:05 +05:30
|
|
|
out() << "missing mappings " << d.keys_ << ":" << end_message();
|
2013-10-08 14:06:20 +05:30
|
|
|
{
|
|
|
|
nested_output::nest _ = push();
|
|
|
|
out() << d.get_desc() << end_message();
|
|
|
|
}
|
|
|
|
|
|
|
|
mplus_error(FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(mapping_array_damage::invalid_mapping const &d) {
|
|
|
|
out() << "invalid mapping:" << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = push();
|
|
|
|
out() << d.get_desc()
|
|
|
|
<< " [cblock = " << d.cblock_
|
|
|
|
<< ", oblock = " << d.m_.oblock_
|
|
|
|
<< ", flags = " << d.m_.flags_
|
|
|
|
<< "]" << end_message();
|
|
|
|
}
|
|
|
|
|
|
|
|
mplus_error(FATAL);
|
|
|
|
}
|
2013-10-08 15:30:05 +05:30
|
|
|
|
|
|
|
using reporter_base::get_error;
|
2013-10-02 15:20:42 +05:30
|
|
|
};
|
|
|
|
|
2013-10-08 15:30:05 +05:30
|
|
|
class hint_reporter : public hint_array_damage::damage_visitor, reporter_base {
|
2013-10-02 15:20:42 +05:30
|
|
|
public:
|
|
|
|
hint_reporter(nested_output &o)
|
|
|
|
: reporter_base(o) {
|
|
|
|
}
|
2013-10-08 15:30:05 +05:30
|
|
|
|
|
|
|
virtual void visit(hint_array_damage::missing_hints const &d) {
|
|
|
|
out() << "missing mappings " << d.keys_ << ":" << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = push();
|
|
|
|
out() << d.get_desc() << end_message();
|
|
|
|
}
|
|
|
|
|
|
|
|
mplus_error(FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
using reporter_base::get_error;
|
2013-10-02 15:20:42 +05:30
|
|
|
};
|
|
|
|
|
2013-10-11 14:52:02 +05:30
|
|
|
class discard_reporter : public bitset_detail::bitset_visitor, reporter_base {
|
|
|
|
public:
|
|
|
|
discard_reporter(nested_output &o)
|
|
|
|
: reporter_base(o) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(uint32_t index, bool value) {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(bitset_detail::missing_bits const &d) {
|
|
|
|
out() << "missing discard bits " << d.keys_ << end_message();
|
|
|
|
mplus_error(FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
using reporter_base::get_error;
|
|
|
|
};
|
|
|
|
|
2013-12-11 22:58:14 +05:30
|
|
|
class space_map_reporter : public space_map_detail::visitor, reporter_base {
|
|
|
|
public:
|
|
|
|
space_map_reporter(nested_output &o)
|
|
|
|
: reporter_base(o) {
|
|
|
|
}
|
|
|
|
|
|
|
|
using reporter_base::get_error;
|
|
|
|
};
|
|
|
|
|
2013-10-02 15:20:42 +05:30
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
struct flags {
|
|
|
|
flags()
|
2013-10-11 14:52:02 +05:30
|
|
|
: check_mappings_(true),
|
|
|
|
check_hints_(true),
|
|
|
|
check_discards_(true),
|
2013-10-02 15:20:42 +05:30
|
|
|
ignore_non_fatal_errors_(false),
|
2015-06-09 18:31:22 +05:30
|
|
|
quiet_(false),
|
|
|
|
clear_needs_check_on_success_(false) {
|
2013-10-02 15:20:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool check_mappings_;
|
|
|
|
bool check_hints_;
|
2013-10-11 14:52:02 +05:30
|
|
|
bool check_discards_;
|
2013-10-02 15:20:42 +05:30
|
|
|
bool ignore_non_fatal_errors_;
|
|
|
|
bool quiet_;
|
2015-06-09 18:31:22 +05:30
|
|
|
bool clear_needs_check_on_success_;
|
2013-10-02 15:20:42 +05:30
|
|
|
};
|
|
|
|
|
2013-03-21 21:14:28 +05:30
|
|
|
struct stat guarded_stat(string const &path) {
|
|
|
|
struct stat info;
|
|
|
|
|
|
|
|
int r = ::stat(path.c_str(), &info);
|
|
|
|
if (r) {
|
|
|
|
ostringstream msg;
|
2015-01-16 18:24:09 +05:30
|
|
|
msg << path << ": " << error_string(errno);
|
2013-03-21 21:14:28 +05:30
|
|
|
throw runtime_error(msg.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2015-06-09 18:31:22 +05:30
|
|
|
void clear_needs_check(string const &path) {
|
|
|
|
block_manager<>::ptr bm = open_bm(path, block_manager<>::READ_WRITE);
|
|
|
|
|
|
|
|
superblock sb = read_superblock(bm);
|
|
|
|
sb.flags.clear_flag(superblock_flags::NEEDS_CHECK);
|
|
|
|
write_superblock(bm, sb);
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:28:56 +05:30
|
|
|
error_state metadata_check(string const &path, flags const &fs,
|
|
|
|
bool &needs_check_set) {
|
2013-10-02 15:20:42 +05:30
|
|
|
nested_output out(cerr, 2);
|
|
|
|
if (fs.quiet_)
|
|
|
|
out.disable();
|
|
|
|
|
2017-10-05 18:17:10 +05:30
|
|
|
if (file_utils::get_file_length(path) < persistent_data::MD_BLOCK_SIZE) {
|
|
|
|
out << "Metadata device/file too small. Is this binary metadata?"
|
|
|
|
<< end_message();
|
|
|
|
return FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
block_manager<>::ptr bm = open_bm(path, block_manager<>::READ_ONLY);
|
|
|
|
|
2013-10-02 15:20:42 +05:30
|
|
|
superblock_reporter sb_rep(out);
|
|
|
|
mapping_reporter mapping_rep(out);
|
|
|
|
hint_reporter hint_rep(out);
|
2013-10-11 14:52:02 +05:30
|
|
|
discard_reporter discard_rep(out);
|
2013-10-02 15:20:42 +05:30
|
|
|
|
|
|
|
out << "examining superblock" << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = out.push();
|
2013-10-07 19:51:45 +05:30
|
|
|
check_superblock(bm, bm->get_nr_blocks(), sb_rep);
|
2013-03-21 21:14:28 +05:30
|
|
|
}
|
|
|
|
|
2017-10-05 18:17:10 +05:30
|
|
|
if (sb_rep.get_error() == FATAL) {
|
|
|
|
if (check_for_xml(bm))
|
|
|
|
out << "This looks like XML. cache_check only checks the binary metadata format." << end_message();
|
2013-10-02 15:20:42 +05:30
|
|
|
return FATAL;
|
2017-10-05 18:17:10 +05:30
|
|
|
}
|
2013-10-02 15:20:42 +05:30
|
|
|
|
2013-10-07 19:51:45 +05:30
|
|
|
superblock sb = read_superblock(bm);
|
2017-10-05 16:23:40 +05:30
|
|
|
transaction_manager::ptr tm = open_tm(bm, SUPERBLOCK_LOCATION);
|
2013-10-02 15:20:42 +05:30
|
|
|
|
2017-03-27 18:28:56 +05:30
|
|
|
needs_check_set = sb.flags.get_flag(superblock_flags::NEEDS_CHECK);
|
|
|
|
|
2013-10-02 15:20:42 +05:30
|
|
|
if (fs.check_mappings_) {
|
|
|
|
out << "examining mapping array" << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = out.push();
|
2014-08-26 15:44:49 +05:30
|
|
|
mapping_array ma(*tm, mapping_array::ref_counter(), sb.mapping_root, sb.cache_blocks);
|
2016-09-30 20:51:20 +05:30
|
|
|
check_mapping_array(ma, mapping_rep, sb.version);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sb.version >= 2) {
|
|
|
|
persistent_data::bitset dirty(*tm, *sb.dirty_root, sb.cache_blocks);
|
|
|
|
// FIXME: is there no bitset checker?
|
2013-10-02 15:20:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.check_hints_) {
|
2013-10-11 14:52:02 +05:30
|
|
|
if (!sb.hint_root)
|
|
|
|
out << "no hint array present" << end_message();
|
|
|
|
|
|
|
|
else {
|
|
|
|
out << "examining hint array" << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = out.push();
|
2014-08-26 15:44:49 +05:30
|
|
|
hint_array ha(*tm, sb.policy_hint_size, sb.hint_root, sb.cache_blocks);
|
2013-10-11 14:52:02 +05:30
|
|
|
ha.check(hint_rep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.check_discards_) {
|
|
|
|
if (!sb.discard_root)
|
|
|
|
out << "no discard bitset present" << end_message();
|
|
|
|
|
|
|
|
else {
|
|
|
|
out << "examining discard bitset" << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = out.push();
|
2014-08-26 15:44:49 +05:30
|
|
|
persistent_data::bitset discards(*tm, sb.discard_root, sb.discard_nr_blocks);
|
2016-09-30 20:51:20 +05:30
|
|
|
// FIXME: is there no bitset checker?
|
2013-10-11 14:52:02 +05:30
|
|
|
}
|
2013-10-02 15:20:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 14:52:02 +05:30
|
|
|
// FIXME: make an error class that's an instance of mplus
|
2013-10-08 15:30:05 +05:30
|
|
|
return combine_errors(sb_rep.get_error(),
|
|
|
|
combine_errors(mapping_rep.get_error(),
|
2013-10-11 14:52:02 +05:30
|
|
|
combine_errors(hint_rep.get_error(),
|
|
|
|
discard_rep.get_error())));
|
2013-03-21 21:14:28 +05:30
|
|
|
}
|
|
|
|
|
2013-10-02 15:20:42 +05:30
|
|
|
int check(string const &path, flags const &fs) {
|
2013-10-04 20:38:31 +05:30
|
|
|
error_state err;
|
2017-03-27 18:28:56 +05:30
|
|
|
bool needs_check_set;
|
2013-03-21 21:14:28 +05:30
|
|
|
struct stat info = guarded_stat(path);
|
|
|
|
|
|
|
|
if (!S_ISREG(info.st_mode) && !S_ISBLK(info.st_mode)) {
|
|
|
|
ostringstream msg;
|
|
|
|
msg << path << ": " << "Not a block device or regular file";
|
|
|
|
throw runtime_error(msg.str());
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:28:56 +05:30
|
|
|
err = metadata_check(path, fs, needs_check_set);
|
2013-10-11 15:34:06 +05:30
|
|
|
|
2015-06-09 18:31:22 +05:30
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if (fs.ignore_non_fatal_errors_)
|
|
|
|
success = (err == FATAL) ? false : true;
|
|
|
|
else
|
|
|
|
success = (err == NO_ERROR) ? true : false;
|
|
|
|
|
2017-03-27 18:28:56 +05:30
|
|
|
if (success && fs.clear_needs_check_on_success_ && needs_check_set)
|
2015-06-09 18:31:22 +05:30
|
|
|
clear_needs_check(path);
|
|
|
|
|
2013-10-11 15:34:06 +05:30
|
|
|
return err == NO_ERROR ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_with_exception_handling(string const &path, flags const &fs) {
|
|
|
|
int r;
|
2013-03-15 20:29:21 +05:30
|
|
|
try {
|
2013-10-11 15:34:06 +05:30
|
|
|
r = check(path, fs);
|
2013-10-02 15:20:42 +05:30
|
|
|
|
2013-03-15 20:29:21 +05:30
|
|
|
} catch (std::exception &e) {
|
2013-10-02 15:20:42 +05:30
|
|
|
if (!fs.quiet_)
|
2013-03-15 20:29:21 +05:30
|
|
|
cerr << e.what() << endl;
|
2013-10-11 15:34:06 +05:30
|
|
|
r = 1;
|
2013-03-15 20:29:21 +05:30
|
|
|
}
|
|
|
|
|
2013-10-11 15:34:06 +05:30
|
|
|
return r;
|
|
|
|
|
2013-03-15 20:29:21 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 21:14:28 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
cache_check_cmd::cache_check_cmd()
|
|
|
|
: command("cache_check")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cache_check_cmd::usage(std::ostream &out) const
|
|
|
|
{
|
|
|
|
out << "Usage: " << get_name() << " [options] {device|file}" << endl
|
|
|
|
<< "Options:" << endl
|
|
|
|
<< " {-q|--quiet}" << endl
|
|
|
|
<< " {-h|--help}" << endl
|
|
|
|
<< " {-V|--version}" << endl
|
|
|
|
<< " {--clear-needs-check-flag}" << endl
|
|
|
|
<< " {--super-block-only}" << endl
|
|
|
|
<< " {--skip-mappings}" << endl
|
|
|
|
<< " {--skip-hints}" << endl
|
|
|
|
<< " {--skip-discards}" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cache_check_cmd::run(int argc, char **argv)
|
2013-03-15 20:29:21 +05:30
|
|
|
{
|
|
|
|
int c;
|
2013-10-02 15:20:42 +05:30
|
|
|
flags fs;
|
2013-03-15 20:29:21 +05:30
|
|
|
const char shortopts[] = "qhV";
|
|
|
|
const struct option longopts[] = {
|
2013-10-08 16:04:10 +05:30
|
|
|
{ "quiet", no_argument, NULL, 'q' },
|
2014-01-09 01:35:55 +05:30
|
|
|
{ "super-block-only", no_argument, NULL, 1 },
|
2013-10-08 16:04:10 +05:30
|
|
|
{ "skip-mappings", no_argument, NULL, 2 },
|
|
|
|
{ "skip-hints", no_argument, NULL, 3 },
|
2013-10-11 14:52:02 +05:30
|
|
|
{ "skip-discards", no_argument, NULL, 4 },
|
2015-06-09 18:31:22 +05:30
|
|
|
{ "clear-needs-check-flag", no_argument, NULL, 5 },
|
2013-10-08 16:04:10 +05:30
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
2013-03-15 20:29:21 +05:30
|
|
|
{ NULL, no_argument, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
|
|
|
switch(c) {
|
2013-10-08 16:04:10 +05:30
|
|
|
case 1:
|
|
|
|
fs.check_mappings_ = false;
|
|
|
|
fs.check_hints_ = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
fs.check_mappings_ = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
fs.check_hints_ = false;
|
|
|
|
break;
|
|
|
|
|
2013-10-11 14:52:02 +05:30
|
|
|
case 4:
|
|
|
|
fs.check_discards_ = false;
|
|
|
|
break;
|
|
|
|
|
2015-06-09 18:31:22 +05:30
|
|
|
case 5:
|
|
|
|
fs.clear_needs_check_on_success_ = true;
|
|
|
|
break;
|
|
|
|
|
2013-03-15 20:29:21 +05:30
|
|
|
case 'h':
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cout);
|
2013-03-15 20:29:21 +05:30
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'q':
|
2013-10-02 15:20:42 +05:30
|
|
|
fs.quiet_ = true;
|
2013-03-15 20:29:21 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V':
|
2013-10-11 15:03:32 +05:30
|
|
|
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
|
2013-03-15 20:29:21 +05:30
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2013-03-15 20:29:21 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == optind) {
|
|
|
|
cerr << "No input file provided." << endl;
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2013-03-21 21:14:28 +05:30
|
|
|
return 1;
|
2013-03-15 20:29:21 +05:30
|
|
|
}
|
|
|
|
|
2013-10-11 15:34:06 +05:30
|
|
|
return check_with_exception_handling(argv[optind], fs);
|
2013-03-15 21:54:17 +05:30
|
|
|
}
|
2013-03-21 21:14:28 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|