blob: b74261108f782b012f3b7ec29850a8c3120e7400 [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"
Rahul Chaudhryefbeecf2016-11-15 15:46:55 -080028#include "update_engine/payload_generator/extent_utils.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070029
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070030using std::vector;
31
32namespace chromeos_update_engine {
33
34bool ExtentRanges::ExtentsOverlapOrTouch(const Extent& a, const Extent& b) {
35 if (a.start_block() == b.start_block())
36 return true;
Darin Petkov94817cb2013-05-08 14:33:24 +020037 if (a.start_block() == kSparseHole || b.start_block() == kSparseHole)
38 return false;
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070039 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
46bool ExtentRanges::ExtentsOverlap(const Extent& a, const Extent& b) {
47 if (a.start_block() == b.start_block())
Kelvin Zhangd567c8b2021-07-08 14:10:23 -040048 return a.num_blocks() != 0;
Darin Petkov94817cb2013-05-08 14:33:24 +020049 if (a.start_block() == kSparseHole || b.start_block() == kSparseHole)
50 return false;
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070051 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
58void ExtentRanges::AddBlock(uint64_t block) {
Kelvin Zhangbef99c32021-08-18 09:57:02 -070059 // Remember to respect |merge_touching_extents_| setting
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070060 AddExtent(ExtentForRange(block, 1));
61}
62
63void ExtentRanges::SubtractBlock(uint64_t block) {
64 SubtractExtent(ExtentForRange(block, 1));
65}
66
67namespace {
68
69Extent UnionOverlappingExtents(const Extent& first, const Extent& second) {
Darin Petkov94817cb2013-05-08 14:33:24 +020070 CHECK_NE(kSparseHole, first.start_block());
71 CHECK_NE(kSparseHole, second.start_block());
Alex Deymof329b932014-10-30 01:37:48 -070072 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 Reyes5fdae4a2010-10-05 10:47:42 -070075 return ExtentForRange(start, end - start);
76}
Darin Petkov94817cb2013-05-08 14:33:24 +020077
Alex Vakulenkod2779df2014-06-16 13:19:00 -070078} // namespace
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070079
80void ExtentRanges::AddExtent(Extent extent) {
Darin Petkov94817cb2013-05-08 14:33:24 +020081 if (extent.start_block() == kSparseHole || extent.num_blocks() == 0)
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070082 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 Hassani232f8f92019-01-14 16:15:31 -080088 it != e;
89 ++it) {
Kelvin Zhangbef99c32021-08-18 09:57:02 -070090 const bool should_merge = merge_touching_extents_
91 ? ExtentsOverlapOrTouch(*it, extent)
92 : ExtentsOverlap(*it, extent);
93 if (should_merge) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070094 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
109namespace {
110// Returns base - subtractee (set subtraction).
111ExtentRanges::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 Vakulenkod2779df2014-06-16 13:19:00 -0700125} // namespace
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700126
127void ExtentRanges::SubtractExtent(const Extent& extent) {
Darin Petkov94817cb2013-05-08 14:33:24 +0200128 if (extent.start_block() == kSparseHole || extent.num_blocks() == 0)
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700129 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 Hassani232f8f92019-01-14 16:15:31 -0800136 it != e;
137 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700138 if (!ExtentsOverlap(*it, extent))
139 continue;
Darin Petkov94817cb2013-05-08 14:33:24 +0200140
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700141 if (begin_del == extent_set_.end())
142 begin_del = it;
143 end_del = it;
144 ++end_del;
Darin Petkov94817cb2013-05-08 14:33:24 +0200145
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700146 del_blocks += it->num_blocks();
147
148 ExtentSet subtraction = SubtractOverlappingExtents(*it, extent);
149 for (ExtentSet::iterator jt = subtraction.begin(), je = subtraction.end();
Amin Hassani232f8f92019-01-14 16:15:31 -0800150 jt != je;
151 ++jt) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700152 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
161void ExtentRanges::AddRanges(const ExtentRanges& ranges) {
Kelvin Zhangbef99c32021-08-18 09:57:02 -0700162 // Remember to respect |merge_touching_extents_| setting
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700163 for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800164 e = ranges.extent_set_.end();
165 it != e;
166 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700167 AddExtent(*it);
168 }
169}
170
171void ExtentRanges::SubtractRanges(const ExtentRanges& ranges) {
172 for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800173 e = ranges.extent_set_.end();
174 it != e;
175 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700176 SubtractExtent(*it);
177 }
178}
179
180void ExtentRanges::AddExtents(const vector<Extent>& extents) {
Kelvin Zhangbef99c32021-08-18 09:57:02 -0700181 // Remember to respect |merge_touching_extents_| setting
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700182 for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
Amin Hassani232f8f92019-01-14 16:15:31 -0800183 it != e;
184 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700185 AddExtent(*it);
186 }
187}
188
189void ExtentRanges::SubtractExtents(const vector<Extent>& extents) {
190 for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
Amin Hassani232f8f92019-01-14 16:15:31 -0800191 it != e;
192 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700193 SubtractExtent(*it);
194 }
195}
196
197void ExtentRanges::AddRepeatedExtents(
Amin Hassani232f8f92019-01-14 16:15:31 -0800198 const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
Kelvin Zhangbef99c32021-08-18 09:57:02 -0700199 // Remember to respect |merge_touching_extents_| setting
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700200 for (int i = 0, e = exts.size(); i != e; ++i) {
201 AddExtent(exts.Get(i));
202 }
203}
204
205void ExtentRanges::SubtractRepeatedExtents(
Amin Hassani232f8f92019-01-14 16:15:31 -0800206 const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700207 for (int i = 0, e = exts.size(); i != e; ++i) {
208 SubtractExtent(exts.Get(i));
209 }
210}
211
Tianjie87af6c02020-08-11 15:06:26 -0700212bool ExtentRanges::OverlapsWithExtent(const Extent& extent) const {
213 for (const auto& entry : extent_set_) {
214 if (ExtentsOverlap(entry, extent)) {
215 return true;
216 }
217 }
218 return false;
219}
220
Alex Deymof0061352015-07-01 14:59:15 -0700221bool 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 Reyes5fdae4a2010-10-05 10:47:42 -0700238void ExtentRanges::Dump() const {
239 LOG(INFO) << "ExtentRanges Dump. blocks: " << blocks_;
240 for (ExtentSet::const_iterator it = extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800241 e = extent_set_.end();
242 it != e;
243 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700244 LOG(INFO) << "{" << it->start_block() << ", " << it->num_blocks() << "}";
245 }
246}
247
248Extent 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 Jiang0a582fb2018-06-26 19:27:21 -0700255Extent 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 Hassani232f8f92019-01-14 16:15:31 -0800263vector<Extent> ExtentRanges::GetExtentsForBlockCount(uint64_t count) const {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700264 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 Hassani232f8f92019-01-14 16:15:31 -0800270 e = extent_set_.end();
271 it != e;
272 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700273 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 Hassanid8b67f42017-12-06 13:47:52 -0800287 CHECK(out_blocks == utils::BlocksInExtents(out));
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700288 return out;
289}
290
Kelvin Zhangd567c8b2021-07-08 14:10:23 -0400291Range<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
303std::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 Deymof0061352015-07-01 14:59:15 -0700316vector<Extent> FilterExtentRanges(const vector<Extent>& extents,
Alex Deymoa376a6e2015-06-05 00:31:34 +0200317 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 Hassani232f8f92019-01-14 16:15:31 -0800338 uint64_t cut_blocks =
339 iter->start_block() + iter->num_blocks() - extent.start_block();
Alex Deymoa376a6e2015-06-05 00:31:34 +0200340 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 Hassani232f8f92019-01-14 16:15:31 -0800349 result.push_back(ExtentForRange(
350 extent.start_block(), iter->start_block() - extent.start_block()));
Alex Deymoa376a6e2015-06-05 00:31:34 +0200351 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 Zhangd567c8b2021-07-08 14:10:23 -0400366Extent 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 Reyes5fdae4a2010-10-05 10:47:42 -0700377} // namespace chromeos_update_engine