[thin_trim] FInally get thin_trim emitting the right discards
This commit is contained in:
parent
2288fc4565
commit
866986b883
@ -22,17 +22,6 @@ using namespace thin_provisioning;
|
|||||||
//----------------------------------------------------------------
|
//----------------------------------------------------------------
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
void confirm_pool_is_not_active() {
|
|
||||||
cout << "The pool must *not* be active when running this tool.\n"
|
|
||||||
<< "Do you wish to continue? [Y/N]\n"
|
|
||||||
<< endl;
|
|
||||||
|
|
||||||
string input;
|
|
||||||
cin >> input;
|
|
||||||
if (input != "Y")
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
class discard_emitter {
|
class discard_emitter {
|
||||||
public:
|
public:
|
||||||
discard_emitter(string const &data_dev, unsigned block_size, uint64_t nr_blocks)
|
discard_emitter(string const &data_dev, unsigned block_size, uint64_t nr_blocks)
|
||||||
@ -50,6 +39,8 @@ namespace {
|
|||||||
range[0] = block_to_byte(b);
|
range[0] = block_to_byte(b);
|
||||||
range[1] = block_to_byte(e) - range[0];
|
range[1] = block_to_byte(e) - range[0];
|
||||||
|
|
||||||
|
cerr << "emitting discard for blocks (" << b << ", " << e << "]\n";
|
||||||
|
|
||||||
if (ioctl(fd_, BLKDISCARD, &range))
|
if (ioctl(fd_, BLKDISCARD, &range))
|
||||||
throw runtime_error("discard ioctl failed");
|
throw runtime_error("discard ioctl failed");
|
||||||
}
|
}
|
||||||
@ -98,43 +89,57 @@ namespace {
|
|||||||
unsigned block_size_;
|
unsigned block_size_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class trim_visitor : public space_map_detail::visitor {
|
class trim_iterator : public space_map::iterator {
|
||||||
public:
|
public:
|
||||||
trim_visitor(discard_emitter &e)
|
trim_iterator(discard_emitter &e)
|
||||||
: emitter_(e) {
|
: emitter_(e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void visit(space_map_detail::missing_counts const &mc) {
|
virtual void operator() (block_address b, ref_t count) {
|
||||||
throw std::runtime_error("corrupt metadata, please use thin_check for details");
|
highest_ = b;
|
||||||
|
|
||||||
|
if (count) {
|
||||||
|
if (last_referenced_ && (b > *last_referenced_ + 1))
|
||||||
|
emitter_.emit(*last_referenced_ + 1, b);
|
||||||
|
|
||||||
|
last_referenced_ = b;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void visit(block_address b, uint32_t count) {
|
void complete() {
|
||||||
if (last_visited_ && (b > *last_visited_ + 1))
|
if (last_referenced_) {
|
||||||
emitter_.emit(*last_visited_ + 1, b);
|
if (*last_referenced_ != *highest_)
|
||||||
|
emitter_.emit(*last_referenced_, *highest_ + 1ull);
|
||||||
|
|
||||||
last_visited_ = b;
|
} else if (highest_)
|
||||||
|
emitter_.emit(0ull, *highest_ + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
discard_emitter &emitter_;
|
discard_emitter &emitter_;
|
||||||
boost::optional<block_address> last_visited_;
|
boost::optional<block_address> last_referenced_;
|
||||||
|
boost::optional<block_address> highest_;
|
||||||
};
|
};
|
||||||
|
|
||||||
int trim(string const &metadata_dev, string const &data_dev) {
|
int trim(string const &metadata_dev, string const &data_dev) {
|
||||||
|
cerr << "in trim\n";
|
||||||
|
|
||||||
// We can trim any block that has zero count in the data
|
// We can trim any block that has zero count in the data
|
||||||
// space map.
|
// space map.
|
||||||
block_manager<>::ptr bm = open_bm(metadata_dev, block_manager<>::READ_ONLY);
|
block_manager<>::ptr bm = open_bm(metadata_dev, block_manager<>::READ_ONLY);
|
||||||
metadata md(bm);
|
metadata md(bm);
|
||||||
|
|
||||||
if (!md.data_sm_->get_nr_free())
|
if (!md.data_sm_->get_nr_free()) {
|
||||||
|
cerr << "All data blocks allocated, nothing to discard\n";
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
discard_emitter de(data_dev, md.sb_.data_block_size_,
|
discard_emitter de(data_dev, md.sb_.data_block_size_,
|
||||||
md.data_sm_->get_nr_blocks());
|
md.data_sm_->get_nr_blocks());
|
||||||
trim_visitor tv(de);
|
trim_iterator ti(de);
|
||||||
|
|
||||||
confirm_pool_is_not_active();
|
md.data_sm_->iterate(ti);
|
||||||
md.data_sm_->visit(tv);
|
ti.complete();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -155,9 +160,8 @@ thin_trim_cmd::thin_trim_cmd()
|
|||||||
void
|
void
|
||||||
thin_trim_cmd::usage(std::ostream &out) const
|
thin_trim_cmd::usage(std::ostream &out) const
|
||||||
{
|
{
|
||||||
out << "Usage: " << get_name() << " [options] {device|file}\n"
|
out << "Usage: " << get_name() << " [options] --metadata-dev {device|file} --data-dev {device|file}\n"
|
||||||
<< "Options:\n"
|
<< "Options:\n"
|
||||||
<< " {--pool-inactive}\n"
|
|
||||||
<< " {-h|--help}\n"
|
<< " {-h|--help}\n"
|
||||||
<< " {-V|--version}" << endl;
|
<< " {-V|--version}" << endl;
|
||||||
}
|
}
|
||||||
@ -188,6 +192,10 @@ thin_trim_cmd::run(int argc, char **argv)
|
|||||||
fs.data_dev = optarg;
|
fs.data_dev = optarg;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
cerr << "--pool-inactive no longer required since we ensure the metadata device is opened exclusively.\n";
|
||||||
|
break;
|
||||||
|
|
||||||
case 'h':
|
case 'h':
|
||||||
usage(cout);
|
usage(cout);
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user