From 8e921580554ed91e84bb68ea32a8c2ea47ad6ff3 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Tue, 18 Nov 2014 16:03:03 +0000 Subject: [PATCH] [thin_trim] first code drop. No testing done as yet. --- Makefile.in | 3 + thin-provisioning/commands.h | 1 + thin-provisioning/thin_trim.cc | 198 +++++++++++++++++++++++++++++++++ 3 files changed, 202 insertions(+) create mode 100644 thin-provisioning/thin_trim.cc diff --git a/Makefile.in b/Makefile.in index ff2a2a3..fe666f5 100644 --- a/Makefile.in +++ b/Makefile.in @@ -86,6 +86,7 @@ SOURCE=\ thin-provisioning/thin_repair.cc \ thin-provisioning/thin_restore.cc \ thin-provisioning/thin_rmap.cc \ + thin-provisioning/thin_trim.cc \ thin-provisioning/xml_format.cc CC:=@CC@ @@ -171,6 +172,7 @@ install: bin/pdata_tools ln -s -f pdata_tools $(BINDIR)/thin_repair ln -s -f pdata_tools $(BINDIR)/thin_restore ln -s -f pdata_tools $(BINDIR)/thin_rmap + ln -s -f pdata_tools $(BINDIR)/thin_trim ln -s -f pdata_tools $(BINDIR)/thin_metadata_size ln -s -f pdata_tools $(BINDIR)/era_check ln -s -f pdata_tools $(BINDIR)/era_dump @@ -186,6 +188,7 @@ install: bin/pdata_tools $(INSTALL_DATA) man8/thin_repair.8 $(MANPATH)/man8 $(INSTALL_DATA) man8/thin_restore.8 $(MANPATH)/man8 $(INSTALL_DATA) man8/thin_rmap.8 $(MANPATH)/man8 + $(INSTALL_DATA) man8/thin_trim.8 $(MANPATH)/man8 $(INSTALL_DATA) man8/thin_metadata_size.8 $(MANPATH)/man8 $(INSTALL_DATA) man8/era_check.8 $(MANPATH)/man8 $(INSTALL_DATA) man8/era_dump.8 $(MANPATH)/man8 diff --git a/thin-provisioning/commands.h b/thin-provisioning/commands.h index 5635665..de63e53 100644 --- a/thin-provisioning/commands.h +++ b/thin-provisioning/commands.h @@ -13,6 +13,7 @@ namespace thin_provisioning { extern base::command thin_restore_cmd; extern base::command thin_repair_cmd; extern base::command thin_rmap_cmd; + extern base::command thin_trim_cmd; extern base::command thin_metadata_size_cmd; } diff --git a/thin-provisioning/thin_trim.cc b/thin-provisioning/thin_trim.cc new file mode 100644 index 0000000..ae57092 --- /dev/null +++ b/thin-provisioning/thin_trim.cc @@ -0,0 +1,198 @@ +#include +#include +#include +#include + +#undef BLOCK_SIZE + +#include "thin-provisioning/commands.h" +#include "metadata.h" +#include "version.h" + +using namespace persistent_data; +using namespace std; +using namespace thin_provisioning; + +//---------------------------------------------------------------- + +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 { + public: + discard_emitter(string const &data_dev, unsigned block_size, uint64_t nr_blocks) + : fd_(open_dev(data_dev, block_size * nr_blocks)), + block_size_(block_size) { + } + + ~discard_emitter() { + ::close(fd_); + } + + void emit(block_address b, block_address e) { + uint64_t range[2]; + + range[0] = block_to_byte(b); + range[1] = block_to_byte(e) - range[0]; + + if (ioctl(fd_, BLKDISCARD, &range)) + throw runtime_error("discard ioctl failed"); + } + + private: + static int open_dev(string const &data_dev, uint64_t expected_size) { + int r, fd; + uint64_t blksize; + struct stat info; + + fd = ::open(data_dev.c_str(), O_WRONLY); + if (fd < 0) { + ostringstream out; + out << "Couldn't open data device '" << data_dev << "'"; + throw runtime_error(out.str()); + } + + try { + r = fstat(fd, &info); + if (r) + throw runtime_error("Couldn't stat data device"); + + if (!S_ISBLK(info.st_mode)) + throw runtime_error("Data device is not a block device"); + + r = ioctl(fd, BLKGETSIZE64, &blksize); + if (r) + throw runtime_error("Couldn't get data device size"); + + if (blksize != (expected_size << 9)) + throw runtime_error("Data device is not the expected size"); + + } catch (...) { + ::close(fd); + throw; + } + + return fd; + } + + uint64_t block_to_byte(block_address b) { + return (b * block_size_) << 9; + } + + int fd_; + unsigned block_size_; + }; + + class trim_visitor : public space_map_detail::visitor { + public: + trim_visitor(discard_emitter &e) + : emitter_(e) { + } + + virtual void visit(space_map_detail::missing_counts const &mc) { + throw std::runtime_error("corrupt metadata, please use thin_check for details"); + } + + virtual void visit(block_address b, uint32_t count) { + if (last_visited_ && (b > *last_visited_ + 1)) + emitter_.emit(*last_visited_ + 1, b); + + last_visited_ = b; + } + + private: + discard_emitter &emitter_; + boost::optional last_visited_; + }; + + int trim(string const &metadata_dev, string const &data_dev) { + // We can trim any block that has zero count in the data + // space map. + metadata md(metadata_dev, 0); + + if (!md.data_sm_->get_nr_free()) + return 0; + + discard_emitter de(data_dev, md.sb_.data_block_size_, + md.data_sm_->get_nr_blocks()); + trim_visitor tv(de); + + confirm_pool_is_not_active(); + md.data_sm_->visit(tv); + + return 0; + } + + void usage(ostream &out, string const &cmd) { + out << "Usage: " << cmd << " [options] {device|file}\n" + << "Options:\n" + << " {--pool-inactive}\n" + << " {-h|--help}\n" + << " {-V|--version}" << endl; + } + + struct flags { + boost::optional metadata_dev; + boost::optional data_dev; + }; +} + +//---------------------------------------------------------------- + +int thin_trim_main(int argc, char **argv) +{ + int c; + flags fs; + const char shortopts[] = "hV"; + + const struct option longopts[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { "metadata-dev", required_argument, NULL, 0 }, + { "data-dev", required_argument, NULL, 1 }, + { "pool-inactive", no_argument, NULL, 2 }, + { NULL, no_argument, NULL, 0 } + }; + + while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { + switch(c) { + case 0: + fs.metadata_dev = optarg; + break; + + case 1: + fs.data_dev = optarg; + break; + + case 'h': + usage(cout, basename(argv[0])); + return 0; + + case 'V': + cout << THIN_PROVISIONING_TOOLS_VERSION << endl; + return 0; + + default: + usage(cerr, basename(argv[0])); + return 1; + } + } + + if (!fs.metadata_dev || !fs.data_dev) { + usage(cerr, basename(argv[0])); + return 1; + } + + return trim(*fs.metadata_dev, *fs.data_dev); +} + +//----------------------------------------------------------------