Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/extent_ranges.h" |
| 6 | |
| 7 | #include <set> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/logging.h> |
| 12 | |
| 13 | using std::max; |
| 14 | using std::min; |
| 15 | using std::set; |
| 16 | using std::vector; |
| 17 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
| 20 | bool ExtentRanges::ExtentsOverlapOrTouch(const Extent& a, const Extent& b) { |
| 21 | if (a.start_block() == b.start_block()) |
| 22 | return true; |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 23 | if (a.start_block() == kSparseHole || b.start_block() == kSparseHole) |
| 24 | return false; |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 25 | if (a.start_block() < b.start_block()) { |
| 26 | return a.start_block() + a.num_blocks() >= b.start_block(); |
| 27 | } else { |
| 28 | return b.start_block() + b.num_blocks() >= a.start_block(); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | bool ExtentRanges::ExtentsOverlap(const Extent& a, const Extent& b) { |
| 33 | if (a.start_block() == b.start_block()) |
| 34 | return true; |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 35 | if (a.start_block() == kSparseHole || b.start_block() == kSparseHole) |
| 36 | return false; |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 37 | if (a.start_block() < b.start_block()) { |
| 38 | return a.start_block() + a.num_blocks() > b.start_block(); |
| 39 | } else { |
| 40 | return b.start_block() + b.num_blocks() > a.start_block(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void ExtentRanges::AddBlock(uint64_t block) { |
| 45 | AddExtent(ExtentForRange(block, 1)); |
| 46 | } |
| 47 | |
| 48 | void ExtentRanges::SubtractBlock(uint64_t block) { |
| 49 | SubtractExtent(ExtentForRange(block, 1)); |
| 50 | } |
| 51 | |
| 52 | namespace { |
| 53 | |
| 54 | Extent UnionOverlappingExtents(const Extent& first, const Extent& second) { |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 55 | CHECK_NE(kSparseHole, first.start_block()); |
| 56 | CHECK_NE(kSparseHole, second.start_block()); |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 57 | uint64_t start = min(first.start_block(), second.start_block()); |
| 58 | uint64_t end = max(first.start_block() + first.num_blocks(), |
| 59 | second.start_block() + second.num_blocks()); |
| 60 | return ExtentForRange(start, end - start); |
| 61 | } |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 62 | |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 63 | } // namespace {} |
| 64 | |
| 65 | void ExtentRanges::AddExtent(Extent extent) { |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 66 | if (extent.start_block() == kSparseHole || extent.num_blocks() == 0) |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 67 | return; |
| 68 | |
| 69 | ExtentSet::iterator begin_del = extent_set_.end(); |
| 70 | ExtentSet::iterator end_del = extent_set_.end(); |
| 71 | uint64_t del_blocks = 0; |
| 72 | for (ExtentSet::iterator it = extent_set_.begin(), e = extent_set_.end(); |
| 73 | it != e; ++it) { |
| 74 | if (ExtentsOverlapOrTouch(*it, extent)) { |
| 75 | end_del = it; |
| 76 | ++end_del; |
| 77 | del_blocks += it->num_blocks(); |
| 78 | if (begin_del == extent_set_.end()) |
| 79 | begin_del = it; |
| 80 | |
| 81 | extent = UnionOverlappingExtents(extent, *it); |
| 82 | } |
| 83 | } |
| 84 | extent_set_.erase(begin_del, end_del); |
| 85 | extent_set_.insert(extent); |
| 86 | blocks_ -= del_blocks; |
| 87 | blocks_ += extent.num_blocks(); |
| 88 | } |
| 89 | |
| 90 | namespace { |
| 91 | // Returns base - subtractee (set subtraction). |
| 92 | ExtentRanges::ExtentSet SubtractOverlappingExtents(const Extent& base, |
| 93 | const Extent& subtractee) { |
| 94 | ExtentRanges::ExtentSet ret; |
| 95 | if (subtractee.start_block() > base.start_block()) { |
| 96 | ret.insert(ExtentForRange(base.start_block(), |
| 97 | subtractee.start_block() - base.start_block())); |
| 98 | } |
| 99 | uint64_t base_end = base.start_block() + base.num_blocks(); |
| 100 | uint64_t subtractee_end = subtractee.start_block() + subtractee.num_blocks(); |
| 101 | if (base_end > subtractee_end) { |
| 102 | ret.insert(ExtentForRange(subtractee_end, base_end - subtractee_end)); |
| 103 | } |
| 104 | return ret; |
| 105 | } |
| 106 | } // namespace {} |
| 107 | |
| 108 | void ExtentRanges::SubtractExtent(const Extent& extent) { |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 109 | if (extent.start_block() == kSparseHole || extent.num_blocks() == 0) |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 110 | return; |
| 111 | |
| 112 | ExtentSet::iterator begin_del = extent_set_.end(); |
| 113 | ExtentSet::iterator end_del = extent_set_.end(); |
| 114 | uint64_t del_blocks = 0; |
| 115 | ExtentSet new_extents; |
| 116 | for (ExtentSet::iterator it = extent_set_.begin(), e = extent_set_.end(); |
| 117 | it != e; ++it) { |
| 118 | if (!ExtentsOverlap(*it, extent)) |
| 119 | continue; |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 120 | |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 121 | if (begin_del == extent_set_.end()) |
| 122 | begin_del = it; |
| 123 | end_del = it; |
| 124 | ++end_del; |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 125 | |
Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 126 | del_blocks += it->num_blocks(); |
| 127 | |
| 128 | ExtentSet subtraction = SubtractOverlappingExtents(*it, extent); |
| 129 | for (ExtentSet::iterator jt = subtraction.begin(), je = subtraction.end(); |
| 130 | jt != je; ++jt) { |
| 131 | new_extents.insert(*jt); |
| 132 | del_blocks -= jt->num_blocks(); |
| 133 | } |
| 134 | } |
| 135 | extent_set_.erase(begin_del, end_del); |
| 136 | extent_set_.insert(new_extents.begin(), new_extents.end()); |
| 137 | blocks_ -= del_blocks; |
| 138 | } |
| 139 | |
| 140 | void ExtentRanges::AddRanges(const ExtentRanges& ranges) { |
| 141 | for (ExtentSet::const_iterator it = ranges.extent_set_.begin(), |
| 142 | e = ranges.extent_set_.end(); it != e; ++it) { |
| 143 | AddExtent(*it); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void ExtentRanges::SubtractRanges(const ExtentRanges& ranges) { |
| 148 | for (ExtentSet::const_iterator it = ranges.extent_set_.begin(), |
| 149 | e = ranges.extent_set_.end(); it != e; ++it) { |
| 150 | SubtractExtent(*it); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void ExtentRanges::AddExtents(const vector<Extent>& extents) { |
| 155 | for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end(); |
| 156 | it != e; ++it) { |
| 157 | AddExtent(*it); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void ExtentRanges::SubtractExtents(const vector<Extent>& extents) { |
| 162 | for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end(); |
| 163 | it != e; ++it) { |
| 164 | SubtractExtent(*it); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void ExtentRanges::AddRepeatedExtents( |
| 169 | const ::google::protobuf::RepeatedPtrField<Extent> &exts) { |
| 170 | for (int i = 0, e = exts.size(); i != e; ++i) { |
| 171 | AddExtent(exts.Get(i)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void ExtentRanges::SubtractRepeatedExtents( |
| 176 | const ::google::protobuf::RepeatedPtrField<Extent> &exts) { |
| 177 | for (int i = 0, e = exts.size(); i != e; ++i) { |
| 178 | SubtractExtent(exts.Get(i)); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void ExtentRanges::Dump() const { |
| 183 | LOG(INFO) << "ExtentRanges Dump. blocks: " << blocks_; |
| 184 | for (ExtentSet::const_iterator it = extent_set_.begin(), |
| 185 | e = extent_set_.end(); |
| 186 | it != e; ++it) { |
| 187 | LOG(INFO) << "{" << it->start_block() << ", " << it->num_blocks() << "}"; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks) { |
| 192 | Extent ret; |
| 193 | ret.set_start_block(start_block); |
| 194 | ret.set_num_blocks(num_blocks); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | std::vector<Extent> ExtentRanges::GetExtentsForBlockCount( |
| 199 | uint64_t count) const { |
| 200 | vector<Extent> out; |
| 201 | if (count == 0) |
| 202 | return out; |
| 203 | uint64_t out_blocks = 0; |
| 204 | CHECK(count <= blocks_); |
| 205 | for (ExtentSet::const_iterator it = extent_set_.begin(), |
| 206 | e = extent_set_.end(); |
| 207 | it != e; ++it) { |
| 208 | const uint64_t blocks_needed = count - out_blocks; |
| 209 | const Extent& extent = *it; |
| 210 | out.push_back(extent); |
| 211 | out_blocks += extent.num_blocks(); |
| 212 | if (extent.num_blocks() < blocks_needed) |
| 213 | continue; |
| 214 | if (extent.num_blocks() == blocks_needed) |
| 215 | break; |
| 216 | // If we get here, we just added the last extent needed, but it's too big |
| 217 | out_blocks -= extent.num_blocks(); |
| 218 | out_blocks += blocks_needed; |
| 219 | out.back().set_num_blocks(blocks_needed); |
| 220 | break; |
| 221 | } |
| 222 | return out; |
| 223 | } |
| 224 | |
| 225 | } // namespace chromeos_update_engine |