2014-01-30 03:07:25 +05:30
|
|
|
#include <fstream>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "version.h"
|
2014-01-31 22:01:27 +05:30
|
|
|
#include "base/indented_stream.h"
|
2014-01-30 03:07:25 +05:30
|
|
|
#include "era/era_array.h"
|
|
|
|
#include "era/writeset_tree.h"
|
|
|
|
#include "era/metadata.h"
|
|
|
|
#include "era/xml_format.h"
|
|
|
|
#include "persistent-data/file_utils.h"
|
|
|
|
|
2014-01-31 22:01:27 +05:30
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
|
|
|
using namespace boost;
|
2014-01-30 03:07:25 +05:30
|
|
|
using namespace era;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct flags {
|
2014-01-31 22:01:27 +05:30
|
|
|
flags()
|
|
|
|
: metadata_snapshot_(false) {
|
2014-01-30 03:07:25 +05:30
|
|
|
}
|
2014-01-31 22:01:27 +05:30
|
|
|
|
|
|
|
bool metadata_snapshot_;
|
|
|
|
optional<uint32_t> era_threshold_;
|
2014-01-30 03:07:25 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
2014-01-31 22:01:27 +05:30
|
|
|
void walk_array(era_array const &array, uint32_t nr_blocks,
|
|
|
|
uint32_t threshold, set<uint32_t> &blocks) {
|
|
|
|
for (uint32_t b = 0; b < nr_blocks; b++) {
|
|
|
|
uint32_t era = array.get(b);
|
|
|
|
if (era >= threshold)
|
|
|
|
blocks.insert(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class writesets_marked_since : public writeset_tree_detail::writeset_visitor {
|
|
|
|
public:
|
|
|
|
writesets_marked_since(uint32_t threshold, set<uint32_t> &blocks)
|
|
|
|
: current_era_(0),
|
|
|
|
threshold_(threshold),
|
|
|
|
blocks_(blocks) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeset_begin(uint32_t era, uint32_t nr_bits) {
|
|
|
|
current_era_ = era;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bit(uint32_t index, bool value) {
|
|
|
|
if (value && current_era_ >= threshold_)
|
|
|
|
blocks_.insert(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeset_end() {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t current_era_;
|
|
|
|
uint32_t threshold_;
|
|
|
|
set<uint32_t> &blocks_;
|
|
|
|
};
|
|
|
|
|
|
|
|
void raise_metadata_damage() {
|
|
|
|
throw std::runtime_error("metadata contains errors (run era_check for details).");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fatal_writeset_tree_damage : public writeset_tree_detail::damage_visitor {
|
|
|
|
void visit(writeset_tree_detail::missing_eras const &d) {
|
|
|
|
raise_metadata_damage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(writeset_tree_detail::damaged_writeset const &d) {
|
|
|
|
raise_metadata_damage();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void walk_writesets(metadata const &md, uint32_t threshold, set<uint32_t> &result) {
|
|
|
|
writesets_marked_since v(threshold, result);
|
|
|
|
fatal_writeset_tree_damage dv;
|
|
|
|
|
|
|
|
walk_writeset_tree(md.tm_, *md.writeset_tree_, v, dv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mark_blocks_since(metadata const &md, uint32_t threshold, set<uint32_t> &result) {
|
|
|
|
walk_array(*md.era_array_, md.sb_.nr_blocks, threshold, result);
|
|
|
|
walk_writesets(md, threshold, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
2014-02-12 17:19:41 +05:30
|
|
|
template <typename Iterator>
|
|
|
|
pair<uint32_t, uint32_t> next_run(Iterator &it, Iterator end) {
|
|
|
|
uint32_t b, e;
|
|
|
|
|
|
|
|
b = *it++;
|
|
|
|
e = b + 1;
|
|
|
|
while (it != end && *it == e) {
|
|
|
|
e++;
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_pair(b, e);
|
|
|
|
}
|
|
|
|
|
2014-01-31 22:01:27 +05:30
|
|
|
void emit_blocks(ostream &out, set<uint32_t> const &blocks) {
|
|
|
|
indented_stream o(out);
|
|
|
|
|
|
|
|
o.indent();
|
|
|
|
o << "<blocks>" << endl;
|
|
|
|
|
|
|
|
o.inc();
|
|
|
|
{
|
2014-02-12 17:19:41 +05:30
|
|
|
set<uint32_t>::const_iterator it = blocks.begin();
|
|
|
|
while (it != blocks.end()) {
|
2014-01-31 22:01:27 +05:30
|
|
|
o.indent();
|
2014-02-12 17:19:41 +05:30
|
|
|
|
|
|
|
pair<uint32_t, uint32_t> range = next_run(it, blocks.end());
|
|
|
|
if (range.second - range.first == 1)
|
|
|
|
o << "<block block=\"" << range.first << "\"/>" << endl;
|
|
|
|
|
|
|
|
else
|
|
|
|
o << "<range begin=\"" << range.first
|
|
|
|
<< "\" end = \"" << range.second << "\"/>" << endl;
|
2014-01-31 22:01:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
o.dec();
|
|
|
|
|
|
|
|
o.indent();
|
|
|
|
o << "</blocks>" << endl;
|
2014-01-30 03:07:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
string const STDOUT_PATH("-");
|
|
|
|
|
|
|
|
bool want_stdout(string const &output) {
|
|
|
|
return output == STDOUT_PATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
int invalidate(string const &dev, string const &output, flags const &fs) {
|
|
|
|
try {
|
2014-01-31 22:01:27 +05:30
|
|
|
set<uint32_t> blocks;
|
2014-07-22 21:11:39 +05:30
|
|
|
block_manager<>::ptr bm = open_bm(dev, block_manager<>::READ_ONLY);
|
2014-01-30 03:07:25 +05:30
|
|
|
|
2014-01-31 22:01:27 +05:30
|
|
|
if (fs.metadata_snapshot_) {
|
|
|
|
superblock sb = read_superblock(bm);
|
|
|
|
if (!sb.metadata_snap)
|
|
|
|
throw runtime_error("no metadata snapshot taken.");
|
|
|
|
|
|
|
|
metadata::ptr md(new metadata(bm, *sb.metadata_snap));
|
|
|
|
mark_blocks_since(*md, *fs.era_threshold_, blocks);
|
|
|
|
|
2014-01-30 03:07:25 +05:30
|
|
|
} else {
|
2014-01-31 22:01:27 +05:30
|
|
|
metadata::ptr md(new metadata(bm, metadata::OPEN));
|
|
|
|
mark_blocks_since(*md, *fs.era_threshold_, blocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (want_stdout(output))
|
|
|
|
emit_blocks(cout, blocks);
|
|
|
|
|
|
|
|
else {
|
2014-01-30 03:07:25 +05:30
|
|
|
ofstream out(output.c_str());
|
2014-01-31 22:01:27 +05:30
|
|
|
emit_blocks(out, blocks);
|
2014-01-30 03:07:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
cerr << e.what() << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage(ostream &out, string const &cmd) {
|
2014-01-31 22:01:27 +05:30
|
|
|
out << "Usage: " << cmd << " [options] --written-since <era> {device|file}" << endl
|
2014-01-30 03:07:25 +05:30
|
|
|
<< "Options:" << endl
|
|
|
|
<< " {-h|--help}" << endl
|
|
|
|
<< " {-o <xml file>}" << endl
|
|
|
|
<< " {-V|--version}" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
flags fs;
|
|
|
|
string output("-");
|
|
|
|
char const shortopts[] = "ho:V";
|
|
|
|
|
|
|
|
option const longopts[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "output", required_argument, NULL, 'o' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
2014-01-31 22:01:27 +05:30
|
|
|
{ "metadata-snapshot", no_argument, NULL, 1},
|
|
|
|
{ "written-since", required_argument, NULL, 2},
|
2014-01-30 03:07:25 +05:30
|
|
|
{ NULL, no_argument, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
|
|
|
switch(c) {
|
2014-01-31 22:01:27 +05:30
|
|
|
case 1:
|
|
|
|
fs.metadata_snapshot_ = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
fs.era_threshold_ = lexical_cast<uint32_t>(optarg);
|
|
|
|
break;
|
|
|
|
|
2014-01-30 03:07:25 +05:30
|
|
|
case 'h':
|
|
|
|
usage(cout, basename(argv[0]));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
output = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-31 22:01:27 +05:30
|
|
|
if (!fs.era_threshold_) {
|
|
|
|
cerr << "Please specify --written-since" << endl;
|
|
|
|
usage(cerr, basename(argv[0]));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-01-30 03:07:25 +05:30
|
|
|
return invalidate(argv[optind], output, fs);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|