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"
|
|
|
|
#include "base/nested_output.h"
|
2013-10-02 15:20:42 +05:30
|
|
|
#include "caching/metadata.h"
|
|
|
|
#include "persistent-data/block.h"
|
|
|
|
#include "persistent-data/file_utils.h"
|
|
|
|
#include "persistent-data/space-maps/core.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) {
|
|
|
|
out() << "missing mappings:" << end_message();
|
|
|
|
{
|
|
|
|
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-02 15:20:42 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
class hint_reporter : public reporter_base {
|
|
|
|
public:
|
|
|
|
hint_reporter(nested_output &o)
|
|
|
|
: reporter_base(o) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
transaction_manager::ptr open_tm(block_manager<>::ptr bm) {
|
|
|
|
space_map::ptr sm(new core_map(bm->get_nr_blocks()));
|
2013-10-07 19:51:45 +05:30
|
|
|
sm->inc(SUPERBLOCK_LOCATION);
|
2013-10-02 15:20:42 +05:30
|
|
|
transaction_manager::ptr tm(new transaction_manager(bm, sm));
|
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
struct flags {
|
|
|
|
flags()
|
|
|
|
: check_mappings_(false),
|
|
|
|
check_hints_(false),
|
|
|
|
ignore_non_fatal_errors_(false),
|
|
|
|
quiet_(false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool check_mappings_;
|
|
|
|
bool check_hints_;
|
|
|
|
bool ignore_non_fatal_errors_;
|
|
|
|
bool quiet_;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
char buffer[128], *ptr;
|
|
|
|
|
|
|
|
ptr = ::strerror_r(errno, buffer, sizeof(buffer));
|
|
|
|
msg << path << ": " << ptr;
|
|
|
|
throw runtime_error(msg.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2013-10-02 15:20:42 +05:30
|
|
|
error_state metadata_check(block_manager<>::ptr bm, flags const &fs) {
|
|
|
|
error_state err = NO_ERROR;
|
2013-03-21 21:14:28 +05:30
|
|
|
|
2013-10-02 15:20:42 +05:30
|
|
|
nested_output out(cerr, 2);
|
|
|
|
if (fs.quiet_)
|
|
|
|
out.disable();
|
|
|
|
|
|
|
|
superblock_reporter sb_rep(out);
|
|
|
|
mapping_reporter mapping_rep(out);
|
|
|
|
hint_reporter hint_rep(out);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-02 15:20:42 +05:30
|
|
|
if (sb_rep.get_error() == FATAL)
|
|
|
|
return FATAL;
|
|
|
|
|
2013-10-07 19:51:45 +05:30
|
|
|
superblock sb = read_superblock(bm);
|
2013-10-02 15:20:42 +05:30
|
|
|
transaction_manager::ptr tm = open_tm(bm);
|
|
|
|
|
|
|
|
if (fs.check_mappings_) {
|
|
|
|
out << "examining mapping array" << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = out.push();
|
|
|
|
mapping_array ma(tm, mapping_array::ref_counter(), sb.mapping_root, sb.cache_blocks);
|
|
|
|
// check_mapping_array(ma, mapping_rep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.check_hints_) {
|
|
|
|
out << "examining hint array" << end_message();
|
|
|
|
{
|
|
|
|
nested_output::nest _ = out.push();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
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;
|
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());
|
|
|
|
}
|
|
|
|
|
2013-03-15 20:29:21 +05:30
|
|
|
try {
|
2013-10-02 15:20:42 +05:30
|
|
|
block_manager<>::ptr bm = open_bm(path, block_io<>::READ_ONLY);
|
2013-10-04 20:38:31 +05:30
|
|
|
err = metadata_check(bm, fs);
|
2013-10-02 15:20:42 +05:30
|
|
|
#if 0
|
2013-03-15 20:29:21 +05:30
|
|
|
if (maybe_errors) {
|
2013-10-02 15:20:42 +05:30
|
|
|
if (!fs.quiet_)
|
2013-03-15 20:29:21 +05:30
|
|
|
cerr << error_selector(*maybe_errors, 3);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-10-02 15:20:42 +05:30
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-04 20:38:31 +05:30
|
|
|
return err == NO_ERROR ? 0 : 1;
|
2013-03-15 20:29:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void usage(ostream &out, string const &cmd) {
|
|
|
|
out << "Usage: " << cmd << " [options] {device|file}" << endl
|
|
|
|
<< "Options:" << endl
|
|
|
|
<< " {-q|--quiet}" << endl
|
|
|
|
<< " {-h|--help}" << endl
|
|
|
|
<< " {-V|--version}" << endl;
|
|
|
|
}
|
2013-03-21 21:14:28 +05:30
|
|
|
|
|
|
|
char const *TOOLS_VERSION = "0.1.6";
|
2013-03-15 20:29:21 +05:30
|
|
|
}
|
|
|
|
|
2013-03-21 21:14:28 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2013-03-15 20:29:21 +05:30
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
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[] = {
|
|
|
|
{ "quiet", no_argument, NULL, 'q'},
|
|
|
|
{ "help", no_argument, NULL, 'h'},
|
|
|
|
{ "version", no_argument, NULL, 'V'},
|
|
|
|
{ NULL, no_argument, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
|
|
|
switch(c) {
|
|
|
|
case 'h':
|
|
|
|
usage(cout, basename(argv[0]));
|
|
|
|
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-03-21 21:14:28 +05:30
|
|
|
cout << TOOLS_VERSION << endl;
|
2013-03-15 20:29:21 +05:30
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage(cerr, basename(argv[0]));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == optind) {
|
|
|
|
cerr << "No input file provided." << endl;
|
|
|
|
usage(cerr, basename(argv[0]));
|
2013-03-21 21:14:28 +05:30
|
|
|
return 1;
|
2013-03-15 20:29:21 +05:30
|
|
|
}
|
|
|
|
|
2013-10-04 20:38:31 +05:30
|
|
|
return check(argv[optind], fs);
|
2013-03-15 21:54:17 +05:30
|
|
|
}
|
2013-03-21 21:14:28 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|