blob: 32580b912613809081a147e1d57b2de6c6e39781 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Reyes5fdae4a2010-10-05 10:47:42 -070016
Alex Deymo1beda782015-06-07 23:01:25 +020017#include "update_engine/payload_generator/extent_ranges.h"
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070018
Alex Vakulenkod2779df2014-06-16 13:19:00 -070019#include <algorithm>
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070020#include <set>
21#include <utility>
22#include <vector>
23
24#include <base/logging.h>
25
Amin Hassanid8b67f42017-12-06 13:47:52 -080026#include "update_engine/common/utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/payload_consumer/payload_constants.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070028
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070029using std::vector;
30
31namespace chromeos_update_engine {
32
33bool ExtentRanges::ExtentsOverlapOrTouch(const Extent& a, const Extent& b) {
34 if (a.start_block() == b.start_block())
35 return true;
Darin Petkov94817cb2013-05-08 14:33:24 +020036 if (a.start_block() == kSparseHole || b.start_block() == kSparseHole)
37 return false;
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070038 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
45bool ExtentRanges::ExtentsOverlap(const Extent& a, const Extent& b) {
46 if (a.start_block() == b.start_block())
Kelvin Zhangd567c8b2021-07-08 14:10:23 -040047 return a.num_blocks() != 0;
Darin Petkov94817cb2013-05-08 14:33:24 +020048 if (a.start_block() == kSparseHole || b.start_block() == kSparseHole)
49 return false;
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070050 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
57void ExtentRanges::AddBlock(uint64_t block) {
Kelvin Zhangbef99c32021-08-18 09:57:02 -070058 // Remember to respect |merge_touching_extents_| setting
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070059 AddExtent(ExtentForRange(block, 1));
60}
61
62void ExtentRanges::SubtractBlock(uint64_t block) {
63 SubtractExtent(ExtentForRange(block, 1));
64}
65
66namespace {
67
68Extent UnionOverlappingExtents(const Extent& first, const Extent& second) {
Darin Petkov94817cb2013-05-08 14:33:24 +020069 CHECK_NE(kSparseHole, first.start_block());
70 CHECK_NE(kSparseHole, second.start_block());
Alex Deymof329b932014-10-30 01:37:48 -070071 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 Reyes5fdae4a2010-10-05 10:47:42 -070074 return ExtentForRange(start, end - start);
75}
Darin Petkov94817cb2013-05-08 14:33:24 +020076
Alex Vakulenkod2779df2014-06-16 13:19:00 -070077} // namespace
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070078
79void ExtentRanges::AddExtent(Extent extent) {
Darin Petkov94817cb2013-05-08 14:33:24 +020080 if (extent.start_block() == kSparseHole || extent.num_blocks() == 0)
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070081 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 Zhange532af32024-01-10 15:29:02 -080086 const auto range = GetCandidateRange(extent);
87 for (ExtentSet::iterator it = range.begin(), e = range.end(); it != e; ++it) {
Kelvin Zhangbef99c32021-08-18 09:57:02 -070088 const bool should_merge = merge_touching_extents_
89 ? ExtentsOverlapOrTouch(*it, extent)
90 : ExtentsOverlap(*it, extent);
91 if (should_merge) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070092 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
107namespace {
108// Returns base - subtractee (set subtraction).
109ExtentRanges::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 Vakulenkod2779df2014-06-16 13:19:00 -0700123} // namespace
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700124
125void ExtentRanges::SubtractExtent(const Extent& extent) {
Darin Petkov94817cb2013-05-08 14:33:24 +0200126 if (extent.start_block() == kSparseHole || extent.num_blocks() == 0)
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700127 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 Hassani232f8f92019-01-14 16:15:31 -0800134 it != e;
135 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700136 if (!ExtentsOverlap(*it, extent))
137 continue;
Darin Petkov94817cb2013-05-08 14:33:24 +0200138
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700139 if (begin_del == extent_set_.end())
140 begin_del = it;
141 end_del = it;
142 ++end_del;
Darin Petkov94817cb2013-05-08 14:33:24 +0200143
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700144 del_blocks += it->num_blocks();
145
146 ExtentSet subtraction = SubtractOverlappingExtents(*it, extent);
147 for (ExtentSet::iterator jt = subtraction.begin(), je = subtraction.end();
Amin Hassani232f8f92019-01-14 16:15:31 -0800148 jt != je;
149 ++jt) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700150 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
159void ExtentRanges::AddRanges(const ExtentRanges& ranges) {
Kelvin Zhangbef99c32021-08-18 09:57:02 -0700160 // Remember to respect |merge_touching_extents_| setting
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700161 for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800162 e = ranges.extent_set_.end();
163 it != e;
164 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700165 AddExtent(*it);
166 }
167}
168
169void ExtentRanges::SubtractRanges(const ExtentRanges& ranges) {
170 for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800171 e = ranges.extent_set_.end();
172 it != e;
173 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700174 SubtractExtent(*it);
175 }
176}
177
178void ExtentRanges::AddExtents(const vector<Extent>& extents) {
Kelvin Zhangbef99c32021-08-18 09:57:02 -0700179 // Remember to respect |merge_touching_extents_| setting
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700180 for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
Amin Hassani232f8f92019-01-14 16:15:31 -0800181 it != e;
182 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700183 AddExtent(*it);
184 }
185}
186
187void ExtentRanges::SubtractExtents(const vector<Extent>& extents) {
188 for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
Amin Hassani232f8f92019-01-14 16:15:31 -0800189 it != e;
190 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700191 SubtractExtent(*it);
192 }
193}
194
195void ExtentRanges::AddRepeatedExtents(
Amin Hassani232f8f92019-01-14 16:15:31 -0800196 const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
Kelvin Zhangbef99c32021-08-18 09:57:02 -0700197 // Remember to respect |merge_touching_extents_| setting
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700198 for (int i = 0, e = exts.size(); i != e; ++i) {
199 AddExtent(exts.Get(i));
200 }
201}
202
203void ExtentRanges::SubtractRepeatedExtents(
Amin Hassani232f8f92019-01-14 16:15:31 -0800204 const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700205 for (int i = 0, e = exts.size(); i != e; ++i) {
206 SubtractExtent(exts.Get(i));
207 }
208}
209
Tianjie87af6c02020-08-11 15:06:26 -0700210bool ExtentRanges::OverlapsWithExtent(const Extent& extent) const {
Kelvin Zhangbf357c62021-08-09 18:31:12 -0400211 for (const auto& entry : GetCandidateRange(extent)) {
Tianjie87af6c02020-08-11 15:06:26 -0700212 if (ExtentsOverlap(entry, extent)) {
213 return true;
214 }
215 }
216 return false;
217}
218
Alex Deymof0061352015-07-01 14:59:15 -0700219bool 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 Reyes5fdae4a2010-10-05 10:47:42 -0700236void ExtentRanges::Dump() const {
237 LOG(INFO) << "ExtentRanges Dump. blocks: " << blocks_;
238 for (ExtentSet::const_iterator it = extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800239 e = extent_set_.end();
240 it != e;
241 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700242 LOG(INFO) << "{" << it->start_block() << ", " << it->num_blocks() << "}";
243 }
244}
245
246Extent 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 Jiang0a582fb2018-06-26 19:27:21 -0700253Extent 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 Hassani232f8f92019-01-14 16:15:31 -0800261vector<Extent> ExtentRanges::GetExtentsForBlockCount(uint64_t count) const {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700262 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 Hassani232f8f92019-01-14 16:15:31 -0800268 e = extent_set_.end();
269 it != e;
270 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700271 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 Hassanid8b67f42017-12-06 13:47:52 -0800285 CHECK(out_blocks == utils::BlocksInExtents(out));
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700286 return out;
287}
288
Kelvin Zhangd567c8b2021-07-08 14:10:23 -0400289Range<ExtentRanges::ExtentSet::const_iterator> ExtentRanges::GetCandidateRange(
290 const Extent& extent) const {
291 auto lower_it = extent_set_.lower_bound(extent);
Kelvin Zhange532af32024-01-10 15:29:02 -0800292 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 Zhangd567c8b2021-07-08 14:10:23 -0400296 --lower_it;
297 }
298
Kelvin Zhange532af32024-01-10 15:29:02 -0800299 auto upper_it = extent_set_.upper_bound(
Kelvin Zhangd567c8b2021-07-08 14:10:23 -0400300 ExtentForRange(extent.start_block() + extent.num_blocks(), 1));
Kelvin Zhange532af32024-01-10 15:29:02 -0800301 while (upper_it != extent_set_.end() &&
302 upper_it->start_block() <=
303 extent.start_block() + extent.num_blocks()) {
304 ++upper_it;
305 }
Kelvin Zhangd567c8b2021-07-08 14:10:23 -0400306 return {lower_it, upper_it};
307}
308
309std::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 Deymof0061352015-07-01 14:59:15 -0700322vector<Extent> FilterExtentRanges(const vector<Extent>& extents,
Alex Deymoa376a6e2015-06-05 00:31:34 +0200323 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 Hassani232f8f92019-01-14 16:15:31 -0800344 uint64_t cut_blocks =
345 iter->start_block() + iter->num_blocks() - extent.start_block();
Alex Deymoa376a6e2015-06-05 00:31:34 +0200346 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 Hassani232f8f92019-01-14 16:15:31 -0800355 result.push_back(ExtentForRange(
356 extent.start_block(), iter->start_block() - extent.start_block()));
Alex Deymoa376a6e2015-06-05 00:31:34 +0200357 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 Zhangd567c8b2021-07-08 14:10:23 -0400372Extent 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 Reyes5fdae4a2010-10-05 10:47:42 -0700383} // namespace chromeos_update_engine