diff --git a/thin-provisioning/device_tree.cc b/thin-provisioning/device_tree.cc index 223d816..4837cb7 100644 --- a/thin-provisioning/device_tree.cc +++ b/thin-provisioning/device_tree.cc @@ -47,6 +47,13 @@ namespace { namespace thin_provisioning { namespace device_tree_detail { + device_details::device_details() + : mapped_blocks_(0), + transaction_id_(0), + creation_time_(0), + snapshotted_time_(0) { + } + void device_details_traits::unpack(device_details_disk const &disk, device_details &value) { diff --git a/thin-provisioning/device_tree.h b/thin-provisioning/device_tree.h index 23ae924..ef32b45 100644 --- a/thin-provisioning/device_tree.h +++ b/thin-provisioning/device_tree.h @@ -20,6 +20,8 @@ namespace thin_provisioning { uint64_t transaction_id_; /* when created */ uint32_t creation_time_; uint32_t snapshotted_time_; + + device_details(); }; struct device_details_traits { diff --git a/thin-provisioning/thin_ll_restore.cc b/thin-provisioning/thin_ll_restore.cc index 1168579..27ab184 100644 --- a/thin-provisioning/thin_ll_restore.cc +++ b/thin-provisioning/thin_ll_restore.cc @@ -85,9 +85,6 @@ namespace { void parse_device(metadata::ptr md, emitter::ptr e, attributes const &attr) { uint32_t dev_id = get_attr(attr, "dev_id"); device_tree_detail::device_details details; - details.transaction_id_ = 0; - details.creation_time_ = 0; - details.snapshotted_time_ = 0; device_tree::ptr details_tree; boost::optional details_root = get_opt_attr(attr, "blocknr"); diff --git a/thin-provisioning/thin_scan.cc b/thin-provisioning/thin_scan.cc index c5afbd9..94edd1d 100644 --- a/thin-provisioning/thin_scan.cc +++ b/thin-provisioning/thin_scan.cc @@ -151,10 +151,43 @@ namespace { value_size_(rhs.value_size_), is_valid_(rhs.is_valid_) { } + + uint64_t size() const { + return (end_ > begin_) ? (end_ - begin_) : 0; + } + + // returns true if r is left or right-adjacent + bool is_adjacent_to(block_range const &r) const { + block_range const &lhs = begin_ < r.begin_ ? *this : r; + block_range const &rhs = begin_ < r.begin_ ? r : *this; + uint64_t common_end = std::min(end_, r.end_); + + if (size() && r.size() && + rhs.begin_ == common_end && + ((!blocknr_begin_ && !r.blocknr_begin_) || + (blocknr_begin_ && r.blocknr_begin_ && + *rhs.blocknr_begin_ > *lhs.blocknr_begin_ && + (*rhs.blocknr_begin_ - *lhs.blocknr_begin_ == rhs.begin_ - lhs.begin_))) && + type_ == r.type_ && + ref_count_ == r.ref_count_ && + value_size_ == r.value_size_ && + is_valid_ == r.is_valid_) + return true; + + return false; + } + + bool concat(block_range const &r) { + if (!is_adjacent_to(r)) + return false; + begin_ = std::min(begin_, r.begin_); + end_ = std::max(end_, r.end_); + return true; + } }; void output_block_range(block_range const &r, std::ostream &out) { - if (r.end_ <= r.begin_) + if (!r.size()) return; if (r.end_ - r.begin_ > 1) { @@ -288,18 +321,8 @@ namespace { curr_range.ref_count_ = -1; } - // output the current block - if (run_range.end_ == 0) - run_range = curr_range; - else if (((!curr_range.blocknr_begin_ && !run_range.blocknr_begin_) || - (curr_range.blocknr_begin_ && run_range.blocknr_begin_ && - *curr_range.blocknr_begin_ == *run_range.blocknr_begin_ + (run_range.end_ - run_range.begin_))) && - curr_range.type_ == run_range.type_ && - curr_range.ref_count_ == run_range.ref_count_ && - curr_range.value_size_ == run_range.value_size_ && - curr_range.is_valid_ == run_range.is_valid_) { - ++run_range.end_; - } else { + // store the current block + if (!run_range.concat(curr_range)) { output_block_range(run_range, out); run_range = curr_range; }