2013-05-23 17:45:00 +05:30
|
|
|
#include <iostream>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <libgen.h>
|
2013-05-23 18:27:57 +05:30
|
|
|
#include <sstream>
|
2013-05-23 17:45:00 +05:30
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "version.h"
|
|
|
|
|
2013-05-23 19:36:57 +05:30
|
|
|
#include "persistent-data/data-structures/btree_damage_visitor.h"
|
2013-05-28 16:50:05 +05:30
|
|
|
#include "persistent-data/run.h"
|
2013-05-23 19:36:57 +05:30
|
|
|
#include "persistent-data/space-maps/core.h"
|
2013-09-11 16:10:46 +05:30
|
|
|
#include "persistent-data/file_utils.h"
|
2014-08-27 18:31:31 +05:30
|
|
|
#include "thin-provisioning/commands.h"
|
2013-05-23 17:45:00 +05:30
|
|
|
#include "thin-provisioning/superblock.h"
|
|
|
|
#include "thin-provisioning/mapping_tree.h"
|
2017-10-05 16:23:40 +05:30
|
|
|
#include "thin-provisioning/metadata.h"
|
2013-05-24 18:13:55 +05:30
|
|
|
#include "thin-provisioning/rmap_visitor.h"
|
2013-05-23 17:45:00 +05:30
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace thin_provisioning;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2013-05-23 19:36:57 +05:30
|
|
|
using namespace mapping_tree_detail;
|
|
|
|
|
2013-05-24 18:13:55 +05:30
|
|
|
typedef rmap_visitor::region region;
|
|
|
|
typedef rmap_visitor::rmap_region rmap_region;
|
2013-05-23 19:36:57 +05:30
|
|
|
|
|
|
|
class damage_visitor {
|
|
|
|
public:
|
|
|
|
virtual void visit(btree_path const &path, btree_detail::damage const &d) {
|
2017-10-05 12:52:58 +05:30
|
|
|
throw std::runtime_error("Damage in mapping tree, please run thin_check.\n");
|
2013-05-23 19:36:57 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-24 18:13:55 +05:30
|
|
|
void display_rmap(ostream &out, vector<rmap_region> const &rmap) {
|
2013-08-08 15:19:59 +05:30
|
|
|
vector<rmap_region>::const_iterator it;
|
|
|
|
for (it = rmap.begin(); it != rmap.end(); ++it) {
|
|
|
|
rmap_region const &r = *it;
|
2013-05-24 18:13:55 +05:30
|
|
|
out << "data " << r.data_begin
|
|
|
|
<< ".." << r.data_end
|
|
|
|
<< " -> thin(" << r.thin_dev
|
|
|
|
<< ") " << r.thin_begin
|
|
|
|
<< ".." << (r.thin_begin + (r.data_end - r.data_begin))
|
|
|
|
<< endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-23 17:45:00 +05:30
|
|
|
int rmap(string const &path, vector<region> const ®ions) {
|
2013-05-23 19:36:57 +05:30
|
|
|
damage_visitor dv;
|
2013-05-24 18:13:55 +05:30
|
|
|
rmap_visitor rv;
|
2013-05-23 19:36:57 +05:30
|
|
|
|
|
|
|
try {
|
2013-08-08 15:19:59 +05:30
|
|
|
vector<region>::const_iterator it;
|
|
|
|
for (it = regions.begin(); it != regions.end(); ++it)
|
|
|
|
rv.add_data_region(*it);
|
2013-05-24 18:13:55 +05:30
|
|
|
|
2020-04-30 19:00:01 +05:30
|
|
|
block_manager::ptr bm = open_bm(path);
|
2017-10-05 16:23:40 +05:30
|
|
|
transaction_manager::ptr tm =
|
|
|
|
open_tm(bm, superblock_detail::SUPERBLOCK_LOCATION);
|
2013-05-23 19:36:57 +05:30
|
|
|
|
|
|
|
superblock_detail::superblock sb = read_superblock(bm);
|
2014-08-26 15:44:49 +05:30
|
|
|
mapping_tree mtree(*tm, sb.data_mapping_root_,
|
2013-05-23 19:36:57 +05:30
|
|
|
mapping_tree_detail::block_traits::ref_counter(tm->get_sm()));
|
|
|
|
|
2013-12-11 22:58:14 +05:30
|
|
|
btree_visit_values(mtree, rv, dv);
|
2013-05-24 18:13:55 +05:30
|
|
|
rv.complete();
|
|
|
|
display_rmap(cout, rv.get_rmap());
|
2013-05-23 19:36:57 +05:30
|
|
|
|
|
|
|
} catch (std::exception const &e) {
|
|
|
|
cerr << e.what();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-05-23 18:27:57 +05:30
|
|
|
return 0;
|
2013-05-23 17:45:00 +05:30
|
|
|
}
|
|
|
|
|
2013-05-23 18:27:57 +05:30
|
|
|
region parse_region(string const &str) {
|
|
|
|
istringstream in(str);
|
|
|
|
|
|
|
|
char dots[2] = {'\0', '\0'};
|
|
|
|
block_address begin, end;
|
|
|
|
|
|
|
|
in >> begin;
|
|
|
|
in.read(dots, sizeof(dots));
|
|
|
|
if (dots[0] != '.' || dots[1] != '.')
|
|
|
|
throw runtime_error("badly formed region (no dots)");
|
|
|
|
in >> end;
|
|
|
|
|
|
|
|
if (in.fail())
|
|
|
|
throw runtime_error("badly formed region (couldn't parse numbers)");
|
|
|
|
|
|
|
|
if (end <= begin)
|
|
|
|
throw runtime_error("badly formed region (end <= begin)");
|
|
|
|
|
|
|
|
return region(begin, end);
|
|
|
|
};
|
2013-05-23 17:45:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
thin_rmap_cmd::thin_rmap_cmd()
|
|
|
|
: command("thin_rmap")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
thin_rmap_cmd::usage(std::ostream &out) const
|
|
|
|
{
|
|
|
|
out << "Usage: " << get_name() << " [options] {device|file}" << endl
|
|
|
|
<< "Options:" << endl
|
|
|
|
<< " {-h|--help}" << endl
|
|
|
|
<< " {-V|--version}" << endl
|
|
|
|
<< " {--region <block range>}*" << endl
|
|
|
|
<< "Where:" << endl
|
|
|
|
<< " <block range> is of the form <begin>..<one-past-the-end>" << endl
|
|
|
|
<< " for example 5..45 denotes blocks 5 to 44 inclusive, but not block 45" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thin_rmap_cmd::run(int argc, char **argv)
|
2013-05-23 17:45:00 +05:30
|
|
|
{
|
|
|
|
int c;
|
|
|
|
vector<region> regions;
|
|
|
|
char const shortopts[] = "hV";
|
|
|
|
option const longopts[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h'},
|
|
|
|
{ "version", no_argument, NULL, 'V'},
|
|
|
|
{ "region", required_argument, NULL, 1},
|
|
|
|
{ NULL, no_argument, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cout);
|
2013-05-23 17:45:00 +05:30
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'V':
|
|
|
|
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// region
|
2013-08-15 20:56:17 +05:30
|
|
|
try {
|
|
|
|
regions.push_back(parse_region(optarg));
|
|
|
|
|
|
|
|
} catch (std::exception const &e) {
|
|
|
|
cerr << e.what();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-05-23 17:45:00 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2013-05-23 17:45:00 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == optind) {
|
|
|
|
cerr << "No input file provided." << endl;
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2013-05-23 17:45:00 +05:30
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-09-19 15:35:00 +05:30
|
|
|
if (!regions.size()) {
|
|
|
|
cerr << "No regions provided." << endl;
|
|
|
|
usage(cerr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-05-23 17:45:00 +05:30
|
|
|
return rmap(argv[optind], regions);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|