| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2010 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | 1beda78 | 2015-06-07 23:01:25 +0200 | [diff] [blame] | 17 | #include "update_engine/payload_generator/extent_ranges.h" | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 18 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 19 | #include <algorithm> | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 20 | #include <set> | 
|  | 21 | #include <utility> | 
|  | 22 | #include <vector> | 
|  | 23 |  | 
|  | 24 | #include <base/logging.h> | 
|  | 25 |  | 
| Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 26 | #include "update_engine/common/utils.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/payload_consumer/payload_constants.h" | 
| Rahul Chaudhry | efbeecf | 2016-11-15 15:46:55 -0800 | [diff] [blame] | 28 | #include "update_engine/payload_generator/extent_utils.h" | 
| Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 29 |  | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 30 | using std::vector; | 
|  | 31 |  | 
|  | 32 | namespace chromeos_update_engine { | 
|  | 33 |  | 
|  | 34 | bool ExtentRanges::ExtentsOverlapOrTouch(const Extent& a, const Extent& b) { | 
|  | 35 | if (a.start_block() == b.start_block()) | 
|  | 36 | return true; | 
| Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 37 | if (a.start_block() == kSparseHole || b.start_block() == kSparseHole) | 
|  | 38 | return false; | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 39 | if (a.start_block() < b.start_block()) { | 
|  | 40 | return a.start_block() + a.num_blocks() >= b.start_block(); | 
|  | 41 | } else { | 
|  | 42 | return b.start_block() + b.num_blocks() >= a.start_block(); | 
|  | 43 | } | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | bool ExtentRanges::ExtentsOverlap(const Extent& a, const Extent& b) { | 
|  | 47 | if (a.start_block() == b.start_block()) | 
| Kelvin Zhang | d567c8b | 2021-07-08 14:10:23 -0400 | [diff] [blame] | 48 | return a.num_blocks() != 0; | 
| Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 49 | if (a.start_block() == kSparseHole || b.start_block() == kSparseHole) | 
|  | 50 | return false; | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 51 | if (a.start_block() < b.start_block()) { | 
|  | 52 | return a.start_block() + a.num_blocks() > b.start_block(); | 
|  | 53 | } else { | 
|  | 54 | return b.start_block() + b.num_blocks() > a.start_block(); | 
|  | 55 | } | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | void ExtentRanges::AddBlock(uint64_t block) { | 
| Kelvin Zhang | bef99c3 | 2021-08-18 09:57:02 -0700 | [diff] [blame] | 59 | // Remember to respect |merge_touching_extents_| setting | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 60 | AddExtent(ExtentForRange(block, 1)); | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | void ExtentRanges::SubtractBlock(uint64_t block) { | 
|  | 64 | SubtractExtent(ExtentForRange(block, 1)); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | namespace { | 
|  | 68 |  | 
|  | 69 | Extent UnionOverlappingExtents(const Extent& first, const Extent& second) { | 
| Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 70 | CHECK_NE(kSparseHole, first.start_block()); | 
|  | 71 | CHECK_NE(kSparseHole, second.start_block()); | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 72 | uint64_t start = std::min(first.start_block(), second.start_block()); | 
|  | 73 | uint64_t end = std::max(first.start_block() + first.num_blocks(), | 
|  | 74 | second.start_block() + second.num_blocks()); | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 75 | return ExtentForRange(start, end - start); | 
|  | 76 | } | 
| Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 77 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 78 | }  // namespace | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 79 |  | 
|  | 80 | void ExtentRanges::AddExtent(Extent extent) { | 
| Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 81 | if (extent.start_block() == kSparseHole || extent.num_blocks() == 0) | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 82 | return; | 
|  | 83 |  | 
|  | 84 | ExtentSet::iterator begin_del = extent_set_.end(); | 
|  | 85 | ExtentSet::iterator end_del = extent_set_.end(); | 
|  | 86 | uint64_t del_blocks = 0; | 
|  | 87 | for (ExtentSet::iterator it = extent_set_.begin(), e = extent_set_.end(); | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 88 | it != e; | 
|  | 89 | ++it) { | 
| Kelvin Zhang | bef99c3 | 2021-08-18 09:57:02 -0700 | [diff] [blame] | 90 | const bool should_merge = merge_touching_extents_ | 
|  | 91 | ? ExtentsOverlapOrTouch(*it, extent) | 
|  | 92 | : ExtentsOverlap(*it, extent); | 
|  | 93 | if (should_merge) { | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 94 | end_del = it; | 
|  | 95 | ++end_del; | 
|  | 96 | del_blocks += it->num_blocks(); | 
|  | 97 | if (begin_del == extent_set_.end()) | 
|  | 98 | begin_del = it; | 
|  | 99 |  | 
|  | 100 | extent = UnionOverlappingExtents(extent, *it); | 
|  | 101 | } | 
|  | 102 | } | 
|  | 103 | extent_set_.erase(begin_del, end_del); | 
|  | 104 | extent_set_.insert(extent); | 
|  | 105 | blocks_ -= del_blocks; | 
|  | 106 | blocks_ += extent.num_blocks(); | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | namespace { | 
|  | 110 | // Returns base - subtractee (set subtraction). | 
|  | 111 | ExtentRanges::ExtentSet SubtractOverlappingExtents(const Extent& base, | 
|  | 112 | const Extent& subtractee) { | 
|  | 113 | ExtentRanges::ExtentSet ret; | 
|  | 114 | if (subtractee.start_block() > base.start_block()) { | 
|  | 115 | ret.insert(ExtentForRange(base.start_block(), | 
|  | 116 | subtractee.start_block() - base.start_block())); | 
|  | 117 | } | 
|  | 118 | uint64_t base_end = base.start_block() + base.num_blocks(); | 
|  | 119 | uint64_t subtractee_end = subtractee.start_block() + subtractee.num_blocks(); | 
|  | 120 | if (base_end > subtractee_end) { | 
|  | 121 | ret.insert(ExtentForRange(subtractee_end, base_end - subtractee_end)); | 
|  | 122 | } | 
|  | 123 | return ret; | 
|  | 124 | } | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 125 | }  // namespace | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 126 |  | 
|  | 127 | void ExtentRanges::SubtractExtent(const Extent& extent) { | 
| Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 128 | if (extent.start_block() == kSparseHole || extent.num_blocks() == 0) | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 129 | return; | 
|  | 130 |  | 
|  | 131 | ExtentSet::iterator begin_del = extent_set_.end(); | 
|  | 132 | ExtentSet::iterator end_del = extent_set_.end(); | 
|  | 133 | uint64_t del_blocks = 0; | 
|  | 134 | ExtentSet new_extents; | 
|  | 135 | for (ExtentSet::iterator it = extent_set_.begin(), e = extent_set_.end(); | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 136 | it != e; | 
|  | 137 | ++it) { | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 138 | if (!ExtentsOverlap(*it, extent)) | 
|  | 139 | continue; | 
| Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 140 |  | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 141 | if (begin_del == extent_set_.end()) | 
|  | 142 | begin_del = it; | 
|  | 143 | end_del = it; | 
|  | 144 | ++end_del; | 
| Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 145 |  | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 146 | del_blocks += it->num_blocks(); | 
|  | 147 |  | 
|  | 148 | ExtentSet subtraction = SubtractOverlappingExtents(*it, extent); | 
|  | 149 | for (ExtentSet::iterator jt = subtraction.begin(), je = subtraction.end(); | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 150 | jt != je; | 
|  | 151 | ++jt) { | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 152 | new_extents.insert(*jt); | 
|  | 153 | del_blocks -= jt->num_blocks(); | 
|  | 154 | } | 
|  | 155 | } | 
|  | 156 | extent_set_.erase(begin_del, end_del); | 
|  | 157 | extent_set_.insert(new_extents.begin(), new_extents.end()); | 
|  | 158 | blocks_ -= del_blocks; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | void ExtentRanges::AddRanges(const ExtentRanges& ranges) { | 
| Kelvin Zhang | bef99c3 | 2021-08-18 09:57:02 -0700 | [diff] [blame] | 162 | // Remember to respect |merge_touching_extents_| setting | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 163 | for (ExtentSet::const_iterator it = ranges.extent_set_.begin(), | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 164 | e = ranges.extent_set_.end(); | 
|  | 165 | it != e; | 
|  | 166 | ++it) { | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 167 | AddExtent(*it); | 
|  | 168 | } | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | void ExtentRanges::SubtractRanges(const ExtentRanges& ranges) { | 
|  | 172 | for (ExtentSet::const_iterator it = ranges.extent_set_.begin(), | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 173 | e = ranges.extent_set_.end(); | 
|  | 174 | it != e; | 
|  | 175 | ++it) { | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 176 | SubtractExtent(*it); | 
|  | 177 | } | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | void ExtentRanges::AddExtents(const vector<Extent>& extents) { | 
| Kelvin Zhang | bef99c3 | 2021-08-18 09:57:02 -0700 | [diff] [blame] | 181 | // Remember to respect |merge_touching_extents_| setting | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 182 | for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end(); | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 183 | it != e; | 
|  | 184 | ++it) { | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 185 | AddExtent(*it); | 
|  | 186 | } | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | void ExtentRanges::SubtractExtents(const vector<Extent>& extents) { | 
|  | 190 | for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end(); | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 191 | it != e; | 
|  | 192 | ++it) { | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 193 | SubtractExtent(*it); | 
|  | 194 | } | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | void ExtentRanges::AddRepeatedExtents( | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 198 | const ::google::protobuf::RepeatedPtrField<Extent>& exts) { | 
| Kelvin Zhang | bef99c3 | 2021-08-18 09:57:02 -0700 | [diff] [blame] | 199 | // Remember to respect |merge_touching_extents_| setting | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 200 | for (int i = 0, e = exts.size(); i != e; ++i) { | 
|  | 201 | AddExtent(exts.Get(i)); | 
|  | 202 | } | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | void ExtentRanges::SubtractRepeatedExtents( | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 206 | const ::google::protobuf::RepeatedPtrField<Extent>& exts) { | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 207 | for (int i = 0, e = exts.size(); i != e; ++i) { | 
|  | 208 | SubtractExtent(exts.Get(i)); | 
|  | 209 | } | 
|  | 210 | } | 
|  | 211 |  | 
| Tianjie | 87af6c0 | 2020-08-11 15:06:26 -0700 | [diff] [blame] | 212 | bool ExtentRanges::OverlapsWithExtent(const Extent& extent) const { | 
| Kelvin Zhang | bf357c6 | 2021-08-09 18:31:12 -0400 | [diff] [blame] | 213 | for (const auto& entry : GetCandidateRange(extent)) { | 
| Tianjie | 87af6c0 | 2020-08-11 15:06:26 -0700 | [diff] [blame] | 214 | if (ExtentsOverlap(entry, extent)) { | 
|  | 215 | return true; | 
|  | 216 | } | 
|  | 217 | } | 
|  | 218 | return false; | 
|  | 219 | } | 
|  | 220 |  | 
| Alex Deymo | f006135 | 2015-07-01 14:59:15 -0700 | [diff] [blame] | 221 | bool ExtentRanges::ContainsBlock(uint64_t block) const { | 
|  | 222 | auto lower = extent_set_.lower_bound(ExtentForRange(block, 1)); | 
|  | 223 | // The block could be on the extent before the one in |lower|. | 
|  | 224 | if (lower != extent_set_.begin()) | 
|  | 225 | lower--; | 
|  | 226 | // Any extent starting at block+1 or later is not interesting, so this is the | 
|  | 227 | // upper limit. | 
|  | 228 | auto upper = extent_set_.lower_bound(ExtentForRange(block + 1, 0)); | 
|  | 229 | for (auto iter = lower; iter != upper; ++iter) { | 
|  | 230 | if (iter->start_block() <= block && | 
|  | 231 | block < iter->start_block() + iter->num_blocks()) { | 
|  | 232 | return true; | 
|  | 233 | } | 
|  | 234 | } | 
|  | 235 | return false; | 
|  | 236 | } | 
|  | 237 |  | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 238 | void ExtentRanges::Dump() const { | 
|  | 239 | LOG(INFO) << "ExtentRanges Dump. blocks: " << blocks_; | 
|  | 240 | for (ExtentSet::const_iterator it = extent_set_.begin(), | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 241 | e = extent_set_.end(); | 
|  | 242 | it != e; | 
|  | 243 | ++it) { | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 244 | LOG(INFO) << "{" << it->start_block() << ", " << it->num_blocks() << "}"; | 
|  | 245 | } | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks) { | 
|  | 249 | Extent ret; | 
|  | 250 | ret.set_start_block(start_block); | 
|  | 251 | ret.set_num_blocks(num_blocks); | 
|  | 252 | return ret; | 
|  | 253 | } | 
|  | 254 |  | 
| Sen Jiang | 0a582fb | 2018-06-26 19:27:21 -0700 | [diff] [blame] | 255 | Extent ExtentForBytes(uint64_t block_size, | 
|  | 256 | uint64_t start_bytes, | 
|  | 257 | uint64_t size_bytes) { | 
|  | 258 | uint64_t start_block = start_bytes / block_size; | 
|  | 259 | uint64_t end_block = utils::DivRoundUp(start_bytes + size_bytes, block_size); | 
|  | 260 | return ExtentForRange(start_block, end_block - start_block); | 
|  | 261 | } | 
|  | 262 |  | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 263 | vector<Extent> ExtentRanges::GetExtentsForBlockCount(uint64_t count) const { | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 264 | vector<Extent> out; | 
|  | 265 | if (count == 0) | 
|  | 266 | return out; | 
|  | 267 | uint64_t out_blocks = 0; | 
|  | 268 | CHECK(count <= blocks_); | 
|  | 269 | for (ExtentSet::const_iterator it = extent_set_.begin(), | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 270 | e = extent_set_.end(); | 
|  | 271 | it != e; | 
|  | 272 | ++it) { | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 273 | const uint64_t blocks_needed = count - out_blocks; | 
|  | 274 | const Extent& extent = *it; | 
|  | 275 | out.push_back(extent); | 
|  | 276 | out_blocks += extent.num_blocks(); | 
|  | 277 | if (extent.num_blocks() < blocks_needed) | 
|  | 278 | continue; | 
|  | 279 | if (extent.num_blocks() == blocks_needed) | 
|  | 280 | break; | 
|  | 281 | // If we get here, we just added the last extent needed, but it's too big | 
|  | 282 | out_blocks -= extent.num_blocks(); | 
|  | 283 | out_blocks += blocks_needed; | 
|  | 284 | out.back().set_num_blocks(blocks_needed); | 
|  | 285 | break; | 
|  | 286 | } | 
| Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 287 | CHECK(out_blocks == utils::BlocksInExtents(out)); | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 288 | return out; | 
|  | 289 | } | 
|  | 290 |  | 
| Kelvin Zhang | d567c8b | 2021-07-08 14:10:23 -0400 | [diff] [blame] | 291 | Range<ExtentRanges::ExtentSet::const_iterator> ExtentRanges::GetCandidateRange( | 
|  | 292 | const Extent& extent) const { | 
|  | 293 | auto lower_it = extent_set_.lower_bound(extent); | 
|  | 294 | if (lower_it != extent_set_.begin()) { | 
|  | 295 | --lower_it; | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | const auto upper_it = extent_set_.upper_bound( | 
|  | 299 | ExtentForRange(extent.start_block() + extent.num_blocks(), 1)); | 
|  | 300 | return {lower_it, upper_it}; | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | std::vector<Extent> ExtentRanges::GetIntersectingExtents( | 
|  | 304 | const Extent& extent) const { | 
|  | 305 | const auto candidates = GetCandidateRange(extent); | 
|  | 306 | std::vector<Extent> result; | 
|  | 307 | for (const auto& ext : candidates) { | 
|  | 308 | if (auto intersection = GetOverlapExtent(ext, extent); | 
|  | 309 | intersection.num_blocks() != 0) { | 
|  | 310 | result.emplace_back(std::move(intersection)); | 
|  | 311 | } | 
|  | 312 | } | 
|  | 313 | return result; | 
|  | 314 | } | 
|  | 315 |  | 
| Alex Deymo | f006135 | 2015-07-01 14:59:15 -0700 | [diff] [blame] | 316 | vector<Extent> FilterExtentRanges(const vector<Extent>& extents, | 
| Alex Deymo | a376a6e | 2015-06-05 00:31:34 +0200 | [diff] [blame] | 317 | const ExtentRanges& ranges) { | 
|  | 318 | vector<Extent> result; | 
|  | 319 | const ExtentRanges::ExtentSet& extent_set = ranges.extent_set(); | 
|  | 320 | for (Extent extent : extents) { | 
|  | 321 | // The extents are sorted by the start_block. We want to iterate all the | 
|  | 322 | // Extents in the ExtentSet possibly overlapping the current |extent|. This | 
|  | 323 | // is achieved by looking from the extent whose start_block is *lower* than | 
|  | 324 | // the extent.start_block() up to the greatest extent whose start_block is | 
|  | 325 | // lower than extent.start_block() + extent.num_blocks(). | 
|  | 326 | auto lower = extent_set.lower_bound(extent); | 
|  | 327 | // We need to decrement the lower_bound to look at the extent that could | 
|  | 328 | // overlap the beginning of the current |extent|. | 
|  | 329 | if (lower != extent_set.begin()) | 
|  | 330 | lower--; | 
|  | 331 | auto upper = extent_set.lower_bound( | 
|  | 332 | ExtentForRange(extent.start_block() + extent.num_blocks(), 0)); | 
|  | 333 | for (auto iter = lower; iter != upper; ++iter) { | 
|  | 334 | if (!ExtentRanges::ExtentsOverlap(extent, *iter)) | 
|  | 335 | continue; | 
|  | 336 | if (iter->start_block() <= extent.start_block()) { | 
|  | 337 | // We need to cut blocks from the beginning of the |extent|. | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 338 | uint64_t cut_blocks = | 
|  | 339 | iter->start_block() + iter->num_blocks() - extent.start_block(); | 
| Alex Deymo | a376a6e | 2015-06-05 00:31:34 +0200 | [diff] [blame] | 340 | if (cut_blocks >= extent.num_blocks()) { | 
|  | 341 | extent.set_num_blocks(0); | 
|  | 342 | break; | 
|  | 343 | } | 
|  | 344 | extent = ExtentForRange(extent.start_block() + cut_blocks, | 
|  | 345 | extent.num_blocks() - cut_blocks); | 
|  | 346 | } else { | 
|  | 347 | // We need to cut blocks on the middle of the extent, possible up to the | 
|  | 348 | // end of it. | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 349 | result.push_back(ExtentForRange( | 
|  | 350 | extent.start_block(), iter->start_block() - extent.start_block())); | 
| Alex Deymo | a376a6e | 2015-06-05 00:31:34 +0200 | [diff] [blame] | 351 | uint64_t new_start = iter->start_block() + iter->num_blocks(); | 
|  | 352 | uint64_t old_end = extent.start_block() + extent.num_blocks(); | 
|  | 353 | if (new_start >= old_end) { | 
|  | 354 | extent.set_num_blocks(0); | 
|  | 355 | break; | 
|  | 356 | } | 
|  | 357 | extent = ExtentForRange(new_start, old_end - new_start); | 
|  | 358 | } | 
|  | 359 | } | 
|  | 360 | if (extent.num_blocks() > 0) | 
|  | 361 | result.push_back(extent); | 
|  | 362 | } | 
|  | 363 | return result; | 
|  | 364 | } | 
|  | 365 |  | 
| Kelvin Zhang | d567c8b | 2021-07-08 14:10:23 -0400 | [diff] [blame] | 366 | Extent GetOverlapExtent(const Extent& extent1, const Extent& extent2) { | 
|  | 367 | if (!ExtentRanges::ExtentsOverlap(extent1, extent2)) { | 
|  | 368 | return {}; | 
|  | 369 | } | 
|  | 370 | const auto start_block = | 
|  | 371 | std::max(extent1.start_block(), extent2.start_block()); | 
|  | 372 | const auto end_block = std::min(extent1.start_block() + extent1.num_blocks(), | 
|  | 373 | extent2.start_block() + extent2.num_blocks()); | 
|  | 374 | return ExtentForRange(start_block, end_block - start_block); | 
|  | 375 | } | 
|  | 376 |  | 
| Andrew de los Reyes | 5fdae4a | 2010-10-05 10:47:42 -0700 | [diff] [blame] | 377 | }  // namespace chromeos_update_engine |