2013-09-11 16:10:46 +05:30
|
|
|
#include <fstream>
|
2013-08-19 17:10:03 +05:30
|
|
|
#include <getopt.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "version.h"
|
2013-09-23 15:45:41 +05:30
|
|
|
#include "caching/mapping_array.h"
|
2013-09-19 18:15:56 +05:30
|
|
|
#include "caching/metadata.h"
|
2013-09-11 16:10:46 +05:30
|
|
|
#include "caching/xml_format.h"
|
2013-09-19 18:15:56 +05:30
|
|
|
#include "persistent-data/file_utils.h"
|
2013-08-19 17:10:03 +05:30
|
|
|
|
|
|
|
using namespace std;
|
2013-09-11 16:10:46 +05:30
|
|
|
using namespace caching;
|
2013-08-19 17:10:03 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2013-10-08 16:24:22 +05:30
|
|
|
struct flags {
|
|
|
|
flags()
|
|
|
|
: repair_(false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool repair_;
|
|
|
|
};
|
|
|
|
|
2013-09-19 18:15:56 +05:30
|
|
|
string to_string(unsigned char const *data) {
|
|
|
|
// FIXME: we're assuming the data is zero terminated here
|
|
|
|
return std::string(reinterpret_cast<char const *>(data));
|
|
|
|
}
|
|
|
|
|
2013-10-09 14:18:20 +05:30
|
|
|
void raise_metadata_damage() {
|
|
|
|
throw std::runtime_error("metadata contains errors (run cache_check for details).\n"
|
|
|
|
"perhaps you wanted to run with --repair");
|
|
|
|
}
|
|
|
|
|
2013-09-19 18:15:56 +05:30
|
|
|
//--------------------------------
|
|
|
|
|
2013-10-08 17:11:40 +05:30
|
|
|
class mapping_emitter : public mapping_visitor {
|
|
|
|
public:
|
|
|
|
mapping_emitter(emitter::ptr e)
|
|
|
|
: e_(e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(block_address cblock, mapping const &m) {
|
|
|
|
if (m.flags_ & M_VALID)
|
|
|
|
e_->mapping(cblock, m.oblock_, m.flags_ & M_DIRTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
emitter::ptr e_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ignore_mapping_damage : public mapping_array_damage::damage_visitor {
|
2013-10-09 14:18:20 +05:30
|
|
|
virtual void visit(mapping_array_damage::missing_mappings const &d) {}
|
|
|
|
virtual void visit(mapping_array_damage::invalid_mapping const &d) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class fatal_mapping_damage : public mapping_array_damage::damage_visitor {
|
|
|
|
public:
|
|
|
|
virtual void visit(mapping_array_damage::missing_mappings const &d) {
|
|
|
|
raise_metadata_damage();
|
2013-10-08 17:11:40 +05:30
|
|
|
}
|
|
|
|
|
2013-10-09 14:18:20 +05:30
|
|
|
virtual void visit(mapping_array_damage::invalid_mapping const &d) {
|
|
|
|
raise_metadata_damage();
|
2013-10-08 17:11:40 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-09 14:18:20 +05:30
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
class hint_emitter : public hint_visitor {
|
2013-10-08 17:11:40 +05:30
|
|
|
public:
|
2013-10-09 14:18:20 +05:30
|
|
|
hint_emitter(emitter::ptr e)
|
|
|
|
: e_(e) {
|
2013-10-08 17:11:40 +05:30
|
|
|
}
|
|
|
|
|
2013-10-09 14:18:20 +05:30
|
|
|
virtual void visit(block_address cblock, std::vector<unsigned char> const &data) {
|
|
|
|
e_->hint(cblock, data);
|
2013-10-08 17:11:40 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-10-09 14:18:20 +05:30
|
|
|
emitter::ptr e_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ignore_hint_damage : public hint_array_damage::damage_visitor {
|
|
|
|
virtual void visit(hint_array_damage::missing_hints const &d) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class fatal_hint_damage : public hint_array_damage::damage_visitor {
|
|
|
|
virtual void visit(hint_array_damage::missing_hints const &d) {
|
|
|
|
raise_metadata_damage();
|
2013-10-08 17:11:40 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
string const STDOUT_PATH("-");
|
|
|
|
|
|
|
|
bool want_stdout(string const &output) {
|
|
|
|
return output == STDOUT_PATH;
|
|
|
|
}
|
|
|
|
|
2013-10-08 16:24:22 +05:30
|
|
|
int dump_(string const &dev, ostream &out, flags const &fs) {
|
2013-09-19 18:15:56 +05:30
|
|
|
block_manager<>::ptr bm = open_bm(dev, block_io<>::READ_ONLY);
|
|
|
|
metadata::ptr md(new metadata(bm, metadata::OPEN));
|
2013-09-11 16:10:46 +05:30
|
|
|
emitter::ptr e = create_xml_emitter(out);
|
2013-09-19 18:15:56 +05:30
|
|
|
|
|
|
|
superblock const &sb = md->sb_;
|
2013-09-26 16:06:01 +05:30
|
|
|
e->begin_superblock(to_string(sb.uuid), sb.data_block_size,
|
|
|
|
sb.cache_blocks, to_string(sb.policy_name),
|
|
|
|
sb.policy_hint_size);
|
2013-09-23 15:45:41 +05:30
|
|
|
|
|
|
|
e->begin_mappings();
|
2013-10-08 17:11:40 +05:30
|
|
|
{
|
2013-10-09 14:18:20 +05:30
|
|
|
namespace mad = mapping_array_damage;
|
|
|
|
|
2013-10-08 17:11:40 +05:30
|
|
|
mapping_emitter me(e);
|
|
|
|
ignore_mapping_damage ignore;
|
|
|
|
fatal_mapping_damage fatal;
|
2013-10-09 14:18:20 +05:30
|
|
|
mad::damage_visitor &dv = fs.repair_ ?
|
|
|
|
static_cast<mad::damage_visitor &>(ignore) :
|
|
|
|
static_cast<mad::damage_visitor &>(fatal);
|
2013-10-08 17:11:40 +05:30
|
|
|
walk_mapping_array(*md->mappings_, me, dv);
|
|
|
|
}
|
|
|
|
e->end_mappings();
|
2013-09-23 15:45:41 +05:30
|
|
|
|
2013-10-08 17:11:40 +05:30
|
|
|
// walk hints
|
2013-10-09 14:18:20 +05:30
|
|
|
e->begin_hints();
|
|
|
|
{
|
|
|
|
using namespace hint_array_damage;
|
|
|
|
|
|
|
|
hint_emitter he(e);
|
|
|
|
ignore_hint_damage ignore;
|
|
|
|
fatal_hint_damage fatal;
|
|
|
|
damage_visitor &dv = fs.repair_ ?
|
|
|
|
static_cast<damage_visitor &>(ignore) :
|
|
|
|
static_cast<damage_visitor &>(fatal);
|
|
|
|
md->hints_->walk(he, dv);
|
|
|
|
}
|
|
|
|
e->end_hints();
|
2013-09-23 15:45:41 +05:30
|
|
|
|
2013-10-09 14:18:20 +05:30
|
|
|
// FIXME: walk discards
|
2013-09-23 15:45:41 +05:30
|
|
|
|
2013-09-19 18:15:56 +05:30
|
|
|
e->end_superblock();
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-08 16:24:22 +05:30
|
|
|
int dump(string const &dev, string const &output, flags const &fs) {
|
2013-09-19 18:15:56 +05:30
|
|
|
try {
|
|
|
|
if (want_stdout(output))
|
2013-10-08 16:24:22 +05:30
|
|
|
return dump_(dev, cout, fs);
|
2013-09-19 18:15:56 +05:30
|
|
|
else {
|
|
|
|
ofstream out(output.c_str());
|
2013-10-08 16:24:22 +05:30
|
|
|
return dump_(dev, out, fs);
|
2013-09-19 18:15:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
cerr << e.what() << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
2013-08-19 17:10:03 +05:30
|
|
|
void usage(ostream &out, string const &cmd) {
|
|
|
|
out << "Usage: " << cmd << " [options] {device|file}" << endl
|
|
|
|
<< "Options:" << endl
|
|
|
|
<< " {-h|--help}" << endl
|
2013-09-11 16:10:46 +05:30
|
|
|
<< " {-o <xml file>}" << endl
|
2013-10-08 16:24:22 +05:30
|
|
|
<< " {-V|--version}" << endl
|
|
|
|
<< " {--repair}" << endl;
|
2013-08-19 17:10:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
2013-10-08 16:24:22 +05:30
|
|
|
flags fs;
|
2013-09-11 16:10:46 +05:30
|
|
|
string output("-");
|
|
|
|
char const shortopts[] = "ho:V";
|
2013-08-19 17:10:03 +05:30
|
|
|
|
|
|
|
option const longopts[] = {
|
2013-10-08 16:24:22 +05:30
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "output", required_argument, NULL, 'o' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "repair", no_argument, NULL, 1 },
|
2013-08-19 17:10:03 +05:30
|
|
|
{ NULL, no_argument, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
|
|
|
switch(c) {
|
2013-10-08 16:24:22 +05:30
|
|
|
case 1:
|
|
|
|
fs.repair_ = true;
|
|
|
|
break;
|
|
|
|
|
2013-08-19 17:10:03 +05:30
|
|
|
case 'h':
|
|
|
|
usage(cout, basename(argv[0]));
|
|
|
|
return 0;
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
case 'o':
|
|
|
|
output = optarg;
|
|
|
|
break;
|
|
|
|
|
2013-08-19 17:10:03 +05:30
|
|
|
case 'V':
|
|
|
|
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage(cerr, basename(argv[0]));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == optind) {
|
|
|
|
cerr << "No input file provided." << endl;
|
|
|
|
usage(cerr, basename(argv[0]));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-08 16:24:22 +05:30
|
|
|
return dump(argv[optind], output, fs);
|
2013-08-19 17:10:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|