2015-08-19 14:11:14 +05:30
|
|
|
// Copyright (C) 2015 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 "version.h"
|
|
|
|
|
|
|
|
#include "base/application.h"
|
|
|
|
#include "base/error_state.h"
|
2015-08-19 17:23:11 +05:30
|
|
|
#include "base/progress_monitor.h"
|
2015-08-21 17:40:49 +05:30
|
|
|
#include "persistent-data/data-structures/btree_damage_visitor.h"
|
2015-08-19 14:11:14 +05:30
|
|
|
#include "persistent-data/file_utils.h"
|
|
|
|
#include "persistent-data/space-maps/core.h"
|
|
|
|
#include "persistent-data/space-maps/disk.h"
|
2015-08-24 21:25:53 +05:30
|
|
|
#include "thin-provisioning/cache_stream.h"
|
2015-09-08 21:39:41 +05:30
|
|
|
#include "thin-provisioning/fixed_chunk_stream.h"
|
2015-08-24 21:25:53 +05:30
|
|
|
#include "thin-provisioning/pool_stream.h"
|
2015-08-19 14:11:14 +05:30
|
|
|
#include "thin-provisioning/commands.h"
|
|
|
|
#include "thin-provisioning/device_tree.h"
|
|
|
|
#include "thin-provisioning/mapping_tree.h"
|
2017-10-05 16:23:40 +05:30
|
|
|
#include "thin-provisioning/metadata.h"
|
2015-08-21 17:40:49 +05:30
|
|
|
#include "thin-provisioning/rmap_visitor.h"
|
2015-08-24 21:25:53 +05:30
|
|
|
#include "thin-provisioning/superblock.h"
|
2015-09-03 17:32:29 +05:30
|
|
|
#include "thin-provisioning/variable_chunk_stream.h"
|
2015-08-19 14:11:14 +05:30
|
|
|
|
2019-10-25 15:02:56 +05:30
|
|
|
#include <boost/compute/detail/sha1.hpp>
|
2015-08-19 17:37:56 +05:30
|
|
|
#include <boost/lexical_cast.hpp>
|
2015-08-21 17:40:49 +05:30
|
|
|
#include <boost/optional.hpp>
|
2015-08-24 15:48:31 +05:30
|
|
|
#include <deque>
|
2015-08-19 17:02:57 +05:30
|
|
|
#include <vector>
|
|
|
|
|
2015-08-19 14:11:14 +05:30
|
|
|
using namespace base;
|
2015-08-21 17:40:49 +05:30
|
|
|
using namespace boost;
|
|
|
|
using namespace persistent_data;
|
2015-08-19 14:11:14 +05:30
|
|
|
using namespace std;
|
|
|
|
using namespace thin_provisioning;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2015-08-21 17:40:49 +05:30
|
|
|
bool factor_of(block_address f, block_address n) {
|
2021-06-01 22:53:13 +05:30
|
|
|
return f && (n % f) == 0;
|
2015-08-21 17:40:49 +05:30
|
|
|
}
|
|
|
|
|
2015-08-19 17:37:56 +05:30
|
|
|
uint64_t parse_int(string const &str, string const &desc) {
|
|
|
|
try {
|
|
|
|
return boost::lexical_cast<uint64_t>(str);
|
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "Couldn't parse " << desc << ": '" << str << "'";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; // never get here
|
|
|
|
}
|
|
|
|
|
2015-08-19 14:11:14 +05:30
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
struct flags {
|
2015-08-19 17:02:57 +05:30
|
|
|
flags()
|
2015-09-04 19:57:48 +05:30
|
|
|
: cache_mem(64 * 1024 * 1024),
|
|
|
|
content_based_chunks(false) {
|
2015-08-19 14:11:14 +05:30
|
|
|
}
|
2015-08-19 17:02:57 +05:30
|
|
|
|
2015-08-21 17:40:49 +05:30
|
|
|
string data_dev;
|
|
|
|
optional<string> metadata_dev;
|
|
|
|
optional<unsigned> block_size;
|
2015-08-19 17:02:57 +05:30
|
|
|
unsigned cache_mem;
|
2015-09-04 19:57:48 +05:30
|
|
|
bool content_based_chunks;
|
2015-08-19 14:11:14 +05:30
|
|
|
};
|
|
|
|
|
2015-08-21 17:40:49 +05:30
|
|
|
using namespace mapping_tree_detail;
|
|
|
|
|
2015-08-19 17:02:57 +05:30
|
|
|
class duplicate_counter {
|
|
|
|
public:
|
2015-09-03 17:32:29 +05:30
|
|
|
duplicate_counter()
|
|
|
|
: non_zero_dups_(0),
|
2015-08-25 13:44:40 +05:30
|
|
|
zero_dups_(0) {
|
2015-08-19 17:02:57 +05:30
|
|
|
}
|
|
|
|
|
2015-09-03 17:32:29 +05:30
|
|
|
void add_duplicate(block_address len) {
|
|
|
|
non_zero_dups_ += len;
|
2015-08-19 17:02:57 +05:30
|
|
|
}
|
|
|
|
|
2015-09-03 17:32:29 +05:30
|
|
|
void add_zero_duplicate(block_address len) {
|
|
|
|
zero_dups_ += len;
|
2015-08-25 13:44:40 +05:30
|
|
|
}
|
|
|
|
|
2015-08-19 17:02:57 +05:30
|
|
|
block_address get_total() const {
|
2015-08-25 13:44:40 +05:30
|
|
|
return non_zero_dups_ + zero_dups_;
|
|
|
|
}
|
|
|
|
|
|
|
|
block_address get_non_zeroes() const {
|
|
|
|
return non_zero_dups_;
|
|
|
|
}
|
|
|
|
|
|
|
|
block_address get_zeroes() const {
|
|
|
|
return zero_dups_;
|
2015-08-19 17:02:57 +05:30
|
|
|
}
|
|
|
|
|
2015-09-07 20:10:35 +05:30
|
|
|
void display_results(chunk_stream const &stream) const {
|
|
|
|
block_address meg = 1024 * 1024;
|
|
|
|
cout << "\n\n"
|
|
|
|
<< stream.size() / meg << "m examined, "
|
|
|
|
<< get_non_zeroes() / meg << "m duplicates, "
|
|
|
|
<< get_zeroes() / meg << "m zeroes\n";
|
|
|
|
}
|
|
|
|
|
2015-08-19 17:02:57 +05:30
|
|
|
private:
|
2015-08-25 13:44:40 +05:30
|
|
|
block_address non_zero_dups_;
|
|
|
|
block_address zero_dups_;
|
2015-08-19 17:02:57 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
class duplicate_detector {
|
|
|
|
public:
|
2015-09-08 21:39:41 +05:30
|
|
|
void scan_with_variable_sized_chunks(chunk_stream &stream) {
|
2021-06-01 22:53:13 +05:30
|
|
|
if (!stream.size())
|
|
|
|
return;
|
2015-09-08 21:39:41 +05:30
|
|
|
variable_chunk_stream vstream(stream, 4096);
|
|
|
|
scan(vstream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scan_with_fixed_sized_chunks(chunk_stream &stream, block_address chunk_size) {
|
2021-06-01 22:53:13 +05:30
|
|
|
if (!stream.size())
|
|
|
|
return;
|
2015-09-08 21:39:41 +05:30
|
|
|
fixed_chunk_stream fstream(stream, chunk_size);
|
|
|
|
scan(fstream);
|
|
|
|
}
|
|
|
|
|
|
|
|
duplicate_counter const &get_results() const {
|
|
|
|
return results_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-09-07 20:10:35 +05:30
|
|
|
void scan(chunk_stream &stream) {
|
|
|
|
block_address total_seen(0);
|
2016-02-24 20:01:51 +05:30
|
|
|
unique_ptr<progress_monitor> pbar = create_progress_bar("Examining data");
|
2015-09-07 20:10:35 +05:30
|
|
|
|
|
|
|
do {
|
|
|
|
// FIXME: use a wrapper class to automate the put()
|
|
|
|
chunk const &c = stream.get();
|
|
|
|
examine(c);
|
|
|
|
stream.put(c);
|
|
|
|
|
|
|
|
total_seen += c.len_;
|
|
|
|
pbar->update_percent((total_seen * 100) / stream.size());
|
|
|
|
|
|
|
|
} while (stream.next());
|
|
|
|
|
|
|
|
pbar->update_percent(100);
|
|
|
|
results_.display_results(stream);
|
|
|
|
}
|
|
|
|
|
2015-08-25 13:44:40 +05:30
|
|
|
void examine(chunk const &c) {
|
2015-09-03 17:32:29 +05:30
|
|
|
if (all_zeroes(c))
|
|
|
|
results_.add_zero_duplicate(c.len_);
|
2015-08-25 13:44:40 +05:30
|
|
|
|
2015-09-03 17:32:29 +05:30
|
|
|
else {
|
|
|
|
digestor_.reset();
|
2015-09-04 18:18:02 +05:30
|
|
|
digestor_.process_bytes(c.mem_.begin, c.mem_.end - c.mem_.begin);
|
2015-08-19 17:02:57 +05:30
|
|
|
|
2015-09-03 17:32:29 +05:30
|
|
|
unsigned int digest[5];
|
|
|
|
digestor_.get_digest(digest);
|
2015-08-25 13:44:40 +05:30
|
|
|
|
2015-09-03 17:32:29 +05:30
|
|
|
// hack
|
|
|
|
vector<unsigned int> v(5);
|
|
|
|
for (unsigned i = 0; i < 5; i++)
|
|
|
|
v[i] = digest[i];
|
2015-08-25 13:44:40 +05:30
|
|
|
|
|
|
|
fingerprint_map::const_iterator it = fm_.find(v);
|
|
|
|
if (it != fm_.end()) {
|
2015-09-03 17:32:29 +05:30
|
|
|
results_.add_duplicate(c.len_);
|
2015-08-25 13:44:40 +05:30
|
|
|
} else
|
2015-09-03 17:32:29 +05:30
|
|
|
fm_.insert(make_pair(v, c.offset_));
|
2015-08-25 13:44:40 +05:30
|
|
|
}
|
2015-08-19 17:02:57 +05:30
|
|
|
}
|
|
|
|
|
2015-09-03 17:32:29 +05:30
|
|
|
bool all_zeroes(chunk const &c) const {
|
2015-09-04 18:18:02 +05:30
|
|
|
for (uint8_t *ptr = c.mem_.begin; ptr != c.mem_.end; ptr++) {
|
|
|
|
if (*ptr != 0)
|
|
|
|
return false;
|
2015-09-03 17:32:29 +05:30
|
|
|
}
|
2015-08-24 18:59:06 +05:30
|
|
|
|
2015-09-03 17:32:29 +05:30
|
|
|
return true;
|
2015-08-19 17:02:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
typedef map<vector<unsigned int>, block_address> fingerprint_map;
|
|
|
|
|
|
|
|
unsigned block_size_;
|
|
|
|
boost::uuids::detail::sha1 digestor_;
|
|
|
|
fingerprint_map fm_;
|
|
|
|
duplicate_counter results_;
|
|
|
|
};
|
|
|
|
|
2015-08-21 17:40:49 +05:30
|
|
|
int show_dups_pool(flags const &fs) {
|
2020-05-01 18:41:33 +05:30
|
|
|
block_manager::ptr bm = open_bm(*fs.metadata_dev);
|
2017-10-05 16:23:40 +05:30
|
|
|
transaction_manager::ptr tm =
|
|
|
|
open_tm(bm, superblock_detail::SUPERBLOCK_LOCATION);
|
2015-08-21 17:40:49 +05:30
|
|
|
superblock_detail::superblock sb = read_superblock(bm);
|
|
|
|
block_address block_size = sb.data_block_size_ * 512;
|
2015-08-24 18:59:06 +05:30
|
|
|
block_address nr_blocks = get_nr_blocks(fs.data_dev, block_size);
|
2015-08-24 15:48:31 +05:30
|
|
|
|
2015-08-24 18:59:06 +05:30
|
|
|
cache_stream stream(fs.data_dev, block_size, fs.cache_mem);
|
|
|
|
pool_stream pstream(stream, tm, sb, nr_blocks);
|
2015-08-21 17:40:49 +05:30
|
|
|
|
2015-09-07 20:10:35 +05:30
|
|
|
duplicate_detector detector;
|
|
|
|
|
2015-09-04 19:57:48 +05:30
|
|
|
if (fs.content_based_chunks)
|
2015-09-08 17:47:52 +05:30
|
|
|
detector.scan_with_variable_sized_chunks(pstream);
|
2015-09-08 21:39:41 +05:30
|
|
|
else {
|
2021-06-01 22:53:13 +05:30
|
|
|
if (!!fs.block_size) {
|
2015-09-08 21:39:41 +05:30
|
|
|
if (factor_of(*fs.block_size, block_size))
|
|
|
|
block_size = *fs.block_size;
|
|
|
|
else
|
|
|
|
throw runtime_error("specified block size is not a factor of the pool chunk size\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
detector.scan_with_fixed_sized_chunks(pstream, block_size);
|
|
|
|
}
|
2015-08-21 17:40:49 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int show_dups_linear(flags const &fs) {
|
|
|
|
if (!fs.block_size)
|
|
|
|
// FIXME: this check should be moved to the switch parsing
|
|
|
|
throw runtime_error("--block-sectors or --metadata-dev must be supplied");
|
|
|
|
|
2015-08-25 13:08:01 +05:30
|
|
|
block_address block_size = *fs.block_size;
|
2015-08-21 17:40:49 +05:30
|
|
|
block_address nr_blocks = get_nr_blocks(fs.data_dev, *fs.block_size);
|
2015-08-25 13:08:01 +05:30
|
|
|
|
|
|
|
cerr << "path = " << fs.data_dev << "\n";
|
2015-08-19 17:02:57 +05:30
|
|
|
cerr << "nr_blocks = " << nr_blocks << "\n";
|
2015-08-25 13:08:01 +05:30
|
|
|
cerr << "block size = " << block_size << "\n";
|
2015-08-19 17:02:57 +05:30
|
|
|
|
2015-09-04 19:57:48 +05:30
|
|
|
cache_stream stream(fs.data_dev, block_size, fs.cache_mem);
|
2015-09-07 20:10:35 +05:30
|
|
|
duplicate_detector dd;
|
2015-09-04 15:06:39 +05:30
|
|
|
|
2015-09-04 19:57:48 +05:30
|
|
|
if (fs.content_based_chunks)
|
2015-09-08 17:47:52 +05:30
|
|
|
dd.scan_with_variable_sized_chunks(stream);
|
2015-09-04 19:57:48 +05:30
|
|
|
else
|
2015-09-08 21:39:41 +05:30
|
|
|
dd.scan_with_fixed_sized_chunks(stream, block_size);
|
2015-08-19 17:02:57 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-21 17:40:49 +05:30
|
|
|
int show_dups(flags const &fs) {
|
|
|
|
if (fs.metadata_dev)
|
|
|
|
return show_dups_pool(fs);
|
|
|
|
else {
|
|
|
|
cerr << "No metadata device provided, so treating data device as a linear device\n";
|
2015-08-25 12:52:16 +05:30
|
|
|
return show_dups_linear(fs);
|
2015-08-21 17:40:49 +05:30
|
|
|
}
|
|
|
|
}
|
2016-02-24 20:01:51 +05:30
|
|
|
}
|
2015-08-21 17:40:49 +05:30
|
|
|
|
2016-02-24 20:01:51 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
thin_show_duplicates_cmd::thin_show_duplicates_cmd()
|
|
|
|
: command("thin_show_duplicates")
|
|
|
|
{
|
2015-08-19 14:11:14 +05:30
|
|
|
}
|
|
|
|
|
2016-02-24 20:01:51 +05:30
|
|
|
void
|
|
|
|
thin_show_duplicates_cmd::usage(std::ostream &out) const
|
|
|
|
{
|
|
|
|
out << "Usage: " << get_name() << " [options] {device|file}\n"
|
|
|
|
<< "Options:\n"
|
|
|
|
<< " {--block-sectors} <integer>\n"
|
|
|
|
<< " {--content-based-chunks}\n"
|
|
|
|
<< " {--metadata-dev} <path>\n"
|
|
|
|
<< " {-h|--help}\n"
|
|
|
|
<< " {-V|--version}" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thin_show_duplicates_cmd::run(int argc, char **argv)
|
2015-08-19 14:11:14 +05:30
|
|
|
{
|
|
|
|
int c;
|
|
|
|
flags fs;
|
|
|
|
|
|
|
|
char const shortopts[] = "qhV";
|
|
|
|
option const longopts[] = {
|
2015-08-19 17:37:56 +05:30
|
|
|
{ "block-sectors", required_argument, NULL, 1},
|
2015-09-04 19:57:48 +05:30
|
|
|
{ "content-based-chunks", no_argument, NULL, 2},
|
|
|
|
{ "metadata-dev", required_argument, NULL, 3},
|
2015-08-19 14:11:14 +05:30
|
|
|
{ "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':
|
2016-02-24 20:01:51 +05:30
|
|
|
usage(cout);
|
2015-08-19 14:11:14 +05:30
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'V':
|
|
|
|
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
|
|
|
|
return 0;
|
|
|
|
|
2015-08-19 17:37:56 +05:30
|
|
|
case 1:
|
|
|
|
fs.block_size = 512 * parse_int(optarg, "block sectors");
|
|
|
|
break;
|
|
|
|
|
2015-08-21 17:40:49 +05:30
|
|
|
case 2:
|
2015-09-04 19:57:48 +05:30
|
|
|
fs.content_based_chunks = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2015-08-21 17:40:49 +05:30
|
|
|
fs.metadata_dev = optarg;
|
|
|
|
break;
|
|
|
|
|
2015-08-19 14:11:14 +05:30
|
|
|
default:
|
2016-02-24 20:01:51 +05:30
|
|
|
usage(cerr);
|
2015-08-19 14:11:14 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == optind) {
|
2015-08-21 17:40:49 +05:30
|
|
|
cerr << "No data device/file provided." << endl;
|
2016-02-24 20:01:51 +05:30
|
|
|
usage(cerr);
|
2015-08-19 14:11:14 +05:30
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-08-21 17:40:49 +05:30
|
|
|
fs.data_dev = argv[optind];
|
|
|
|
|
|
|
|
return show_dups(fs);
|
2015-08-19 14:11:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|