introduce checksum_exception

This commit is contained in:
Joe Thornber 2012-02-27 14:07:16 +00:00
parent d4a3e56d54
commit a4b41e90ad
6 changed files with 127 additions and 9 deletions

View File

@ -18,11 +18,13 @@
#include "btree.h" #include "btree.h"
#include "errors.h"
#include "checksum.h" #include "checksum.h"
#include "transaction_manager.h" #include "transaction_manager.h"
#include <iostream> #include <iostream>
using namespace base;
using namespace btree_detail; using namespace btree_detail;
using namespace persistent_data; using namespace persistent_data;
using namespace std; using namespace std;
@ -37,10 +39,10 @@ namespace {
crc32c sum(BTREE_CSUM_XOR); crc32c sum(BTREE_CSUM_XOR);
sum.append(&n->flags, MD_BLOCK_SIZE - sizeof(uint32_t)); sum.append(&n->flags, MD_BLOCK_SIZE - sizeof(uint32_t));
if (sum.get_sum() != to_cpu<uint32_t>(n->csum)) if (sum.get_sum() != to_cpu<uint32_t>(n->csum))
throw runtime_error("bad checksum in btree node"); throw checksum_error("bad checksum in btree node");
if (to_cpu<uint64_t>(n->blocknr) != location) if (to_cpu<uint64_t>(n->blocknr) != location)
throw runtime_error("bad block nr in btree node"); throw checksum_error("bad block nr in btree node");
} }
virtual void prepare(block_manager<>::buffer &b, block_address location) const { virtual void prepare(block_manager<>::buffer &b, block_address location) const {

View File

@ -160,7 +160,7 @@ namespace persistent_data {
<< ", sum was " << sum.get_sum() << ", sum was " << sum.get_sum()
<< ", on disk " << n.get_checksum(); << ", on disk " << n.get_checksum();
errs_->add_child(out.str()); errs_->add_child(out.str());
throw runtime_error(out.str()); throw checksum_error(out.str());
} }
} }
@ -172,7 +172,7 @@ namespace persistent_data {
<< n.get_location() << n.get_location()
<< ", claims " << n.get_block_nr(); << ", claims " << n.get_block_nr();
errs_->add_child(out.str()); errs_->add_child(out.str());
throw runtime_error(out.str()); throw checksum_error(out.str());
} }
} }

37
errors.h Normal file
View File

@ -0,0 +1,37 @@
// Copyright (C) 2012 Red Hat, Inc. All rights reserved.
//
// This file is part of the thin-provisioning-tools source.
//
// thin-provisioning-tools is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// thin-provisioning-tools is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>.
#ifndef THINP_EXCEPTION_H
#define THINP_EXCEPTION_H
#include <stdexcept>
//----------------------------------------------------------------
namespace base {
class checksum_error : public std::runtime_error {
public:
explicit checksum_error(std::string const &what)
: std::runtime_error(what) {
}
};
}
//----------------------------------------------------------------
#endif

View File

@ -45,7 +45,7 @@ namespace {
crc32c sum(SUPERBLOCK_CSUM_SEED); crc32c sum(SUPERBLOCK_CSUM_SEED);
sum.append(&sbd->flags_, MD_BLOCK_SIZE - sizeof(uint32_t)); sum.append(&sbd->flags_, MD_BLOCK_SIZE - sizeof(uint32_t));
if (sum.get_sum() != to_cpu<uint32_t>(sbd->csum_)) if (sum.get_sum() != to_cpu<uint32_t>(sbd->csum_))
throw runtime_error("bad checksum in superblock"); throw checksum_error("bad checksum in superblock");
} }
virtual void prepare(block_manager<>::buffer &b, block_address location) const { virtual void prepare(block_manager<>::buffer &b, block_address location) const {

View File

@ -43,10 +43,10 @@ namespace {
crc32c sum(BITMAP_CSUM_XOR); crc32c sum(BITMAP_CSUM_XOR);
sum.append(&data->not_used, MD_BLOCK_SIZE - sizeof(uint32_t)); sum.append(&data->not_used, MD_BLOCK_SIZE - sizeof(uint32_t));
if (sum.get_sum() != to_cpu<uint32_t>(data->csum)) if (sum.get_sum() != to_cpu<uint32_t>(data->csum))
throw runtime_error("bad checksum in space map bitmap"); throw checksum_error("bad checksum in space map bitmap");
if (to_cpu<uint64_t>(data->blocknr) != location) if (to_cpu<uint64_t>(data->blocknr) != location)
throw runtime_error("bad block nr in space map bitmap"); throw checksum_error("bad block nr in space map bitmap");
} }
virtual void prepare(block_manager<>::buffer &b, block_address location) const { virtual void prepare(block_manager<>::buffer &b, block_address location) const {
@ -75,10 +75,10 @@ namespace {
crc32c sum(INDEX_CSUM_XOR); crc32c sum(INDEX_CSUM_XOR);
sum.append(&mi->padding_, MD_BLOCK_SIZE - sizeof(uint32_t)); sum.append(&mi->padding_, MD_BLOCK_SIZE - sizeof(uint32_t));
if (sum.get_sum() != to_cpu<uint32_t>(mi->csum_)) if (sum.get_sum() != to_cpu<uint32_t>(mi->csum_))
throw runtime_error("bad checksum in metadata index block"); throw checksum_error("bad checksum in metadata index block");
if (to_cpu<uint64_t>(mi->blocknr_) != location) if (to_cpu<uint64_t>(mi->blocknr_) != location)
throw runtime_error("bad block nr in metadata index block"); throw checksum_error("bad block nr in metadata index block");
} }
virtual void prepare(block_manager<>::buffer &b, block_address location) const { virtual void prepare(block_manager<>::buffer &b, block_address location) const {

79
thin_check.cc Normal file
View File

@ -0,0 +1,79 @@
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
//
// This file is part of the thin-provisioning-tools source.
//
// thin-provisioning-tools is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// thin-provisioning-tools is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>.
#include <iostream>
#include <getopt.h>
#include <libgen.h>
#include "metadata.h"
#include "metadata_checker.h"
#include "version.h"
using namespace persistent_data;
using namespace std;
using namespace thin_provisioning;
namespace {
int check(string const &path) {
metadata::ptr md(new metadata(path, metadata::OPEN));
optional<error_set::ptr> maybe_errors = metadata_check(md);
if (maybe_errors) {
cerr << error_selector(*maybe_errors, 3);
return 1;
}
return 0;
}
void usage(string const &cmd) {
cerr << "Usage: " << cmd << " {device|file}" << endl;
cerr << "Options:" << endl;
cerr << " {-h|--help}" << endl;
cerr << " {-V|--version}" << endl;
}
}
int main(int argc, char **argv)
{
int c;
const char shortopts[] = "hV";
const struct option longopts[] = {
{ "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(basename(argv[0]));
return 0;
case 'V':
cerr << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0;
}
}
if (argc != 2) {
usage(basename(argv[0]));
exit(1);
}
return check(argv[1]);
}