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