blob: 4cc2d6f94d10e145fb2ee4317b0719c31547572e [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) {
59 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;
86 for (ExtentSet::iterator it = extent_set_.begin(), e = extent_set_.end();
Amin Hassani232f8f92019-01-14 16:15:31 -080087 it != e;
88 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -070089 if (ExtentsOverlapOrTouch(*it, extent)) {
90 end_del = it;
91 ++end_del;
92 del_blocks += it->num_blocks();
93 if (begin_del == extent_set_.end())
94 begin_del = it;
95
96 extent = UnionOverlappingExtents(extent, *it);
97 }
98 }
99 extent_set_.erase(begin_del, end_del);
100 extent_set_.insert(extent);
101 blocks_ -= del_blocks;
102 blocks_ += extent.num_blocks();
103}
104
105namespace {
106// Returns base - subtractee (set subtraction).
107ExtentRanges::ExtentSet SubtractOverlappingExtents(const Extent& base,
108 const Extent& subtractee) {
109 ExtentRanges::ExtentSet ret;
110 if (subtractee.start_block() > base.start_block()) {
111 ret.insert(ExtentForRange(base.start_block(),
112 subtractee.start_block() - base.start_block()));
113 }
114 uint64_t base_end = base.start_block() + base.num_blocks();
115 uint64_t subtractee_end = subtractee.start_block() + subtractee.num_blocks();
116 if (base_end > subtractee_end) {
117 ret.insert(ExtentForRange(subtractee_end, base_end - subtractee_end));
118 }
119 return ret;
120}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700121} // namespace
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700122
123void ExtentRanges::SubtractExtent(const Extent& extent) {
Darin Petkov94817cb2013-05-08 14:33:24 +0200124 if (extent.start_block() == kSparseHole || extent.num_blocks() == 0)
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700125 return;
126
127 ExtentSet::iterator begin_del = extent_set_.end();
128 ExtentSet::iterator end_del = extent_set_.end();
129 uint64_t del_blocks = 0;
130 ExtentSet new_extents;
131 for (ExtentSet::iterator it = extent_set_.begin(), e = extent_set_.end();
Amin Hassani232f8f92019-01-14 16:15:31 -0800132 it != e;
133 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700134 if (!ExtentsOverlap(*it, extent))
135 continue;
Darin Petkov94817cb2013-05-08 14:33:24 +0200136
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700137 if (begin_del == extent_set_.end())
138 begin_del = it;
139 end_del = it;
140 ++end_del;
Darin Petkov94817cb2013-05-08 14:33:24 +0200141
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700142 del_blocks += it->num_blocks();
143
144 ExtentSet subtraction = SubtractOverlappingExtents(*it, extent);
145 for (ExtentSet::iterator jt = subtraction.begin(), je = subtraction.end();
Amin Hassani232f8f92019-01-14 16:15:31 -0800146 jt != je;
147 ++jt) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700148 new_extents.insert(*jt);
149 del_blocks -= jt->num_blocks();
150 }
151 }
152 extent_set_.erase(begin_del, end_del);
153 extent_set_.insert(new_extents.begin(), new_extents.end());
154 blocks_ -= del_blocks;
155}
156
157void ExtentRanges::AddRanges(const ExtentRanges& ranges) {
158 for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800159 e = ranges.extent_set_.end();
160 it != e;
161 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700162 AddExtent(*it);
163 }
164}
165
166void ExtentRanges::SubtractRanges(const ExtentRanges& ranges) {
167 for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800168 e = ranges.extent_set_.end();
169 it != e;
170 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700171 SubtractExtent(*it);
172 }
173}
174
175void ExtentRanges::AddExtents(const vector<Extent>& extents) {
176 for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
Amin Hassani232f8f92019-01-14 16:15:31 -0800177 it != e;
178 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700179 AddExtent(*it);
180 }
181}
182
183void ExtentRanges::SubtractExtents(const vector<Extent>& extents) {
184 for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
Amin Hassani232f8f92019-01-14 16:15:31 -0800185 it != e;
186 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700187 SubtractExtent(*it);
188 }
189}
190
191void ExtentRanges::AddRepeatedExtents(
Amin Hassani232f8f92019-01-14 16:15:31 -0800192 const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700193 for (int i = 0, e = exts.size(); i != e; ++i) {
194 AddExtent(exts.Get(i));
195 }
196}
197
198void ExtentRanges::SubtractRepeatedExtents(
Amin Hassani232f8f92019-01-14 16:15:31 -0800199 const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700200 for (int i = 0, e = exts.size(); i != e; ++i) {
201 SubtractExtent(exts.Get(i));
202 }
203}
204
Tianjie87af6c02020-08-11 15:06:26 -0700205bool ExtentRanges::OverlapsWithExtent(const Extent& extent) const {
206 for (const auto& entry : extent_set_) {
207 if (ExtentsOverlap(entry, extent)) {
208 return true;
209 }
210 }
211 return false;
212}
213
Alex Deymof0061352015-07-01 14:59:15 -0700214bool ExtentRanges::ContainsBlock(uint64_t block) const {
215 auto lower = extent_set_.lower_bound(ExtentForRange(block, 1));
216 // The block could be on the extent before the one in |lower|.
217 if (lower != extent_set_.begin())
218 lower--;
219 // Any extent starting at block+1 or later is not interesting, so this is the
220 // upper limit.
221 auto upper = extent_set_.lower_bound(ExtentForRange(block + 1, 0));
222 for (auto iter = lower; iter != upper; ++iter) {
223 if (iter->start_block() <= block &&
224 block < iter->start_block() + iter->num_blocks()) {
225 return true;
226 }
227 }
228 return false;
229}
230
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700231void ExtentRanges::Dump() const {
232 LOG(INFO) << "ExtentRanges Dump. blocks: " << blocks_;
233 for (ExtentSet::const_iterator it = extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800234 e = extent_set_.end();
235 it != e;
236 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700237 LOG(INFO) << "{" << it->start_block() << ", " << it->num_blocks() << "}";
238 }
239}
240
241Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks) {
242 Extent ret;
243 ret.set_start_block(start_block);
244 ret.set_num_blocks(num_blocks);
245 return ret;
246}
247
Sen Jiang0a582fb2018-06-26 19:27:21 -0700248Extent ExtentForBytes(uint64_t block_size,
249 uint64_t start_bytes,
250 uint64_t size_bytes) {
251 uint64_t start_block = start_bytes / block_size;
252 uint64_t end_block = utils::DivRoundUp(start_bytes + size_bytes, block_size);
253 return ExtentForRange(start_block, end_block - start_block);
254}
255
Amin Hassani232f8f92019-01-14 16:15:31 -0800256vector<Extent> ExtentRanges::GetExtentsForBlockCount(uint64_t count) const {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700257 vector<Extent> out;
258 if (count == 0)
259 return out;
260 uint64_t out_blocks = 0;
261 CHECK(count <= blocks_);
262 for (ExtentSet::const_iterator it = extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800263 e = extent_set_.end();
264 it != e;
265 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700266 const uint64_t blocks_needed = count - out_blocks;
267 const Extent& extent = *it;
268 out.push_back(extent);
269 out_blocks += extent.num_blocks();
270 if (extent.num_blocks() < blocks_needed)
271 continue;
272 if (extent.num_blocks() == blocks_needed)
273 break;
274 // If we get here, we just added the last extent needed, but it's too big
275 out_blocks -= extent.num_blocks();
276 out_blocks += blocks_needed;
277 out.back().set_num_blocks(blocks_needed);
278 break;
279 }
Amin Hassanid8b67f42017-12-06 13:47:52 -0800280 CHECK(out_blocks == utils::BlocksInExtents(out));
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700281 return out;
282}
283
Kelvin Zhangd567c8b2021-07-08 14:10:23 -0400284Range<ExtentRanges::ExtentSet::const_iterator> ExtentRanges::GetCandidateRange(
285 const Extent& extent) const {
286 auto lower_it = extent_set_.lower_bound(extent);
287 if (lower_it != extent_set_.begin()) {
288 --lower_it;
289 }
290
291 const auto upper_it = extent_set_.upper_bound(
292 ExtentForRange(extent.start_block() + extent.num_blocks(), 1));
293 return {lower_it, upper_it};
294}
295
296std::vector<Extent> ExtentRanges::GetIntersectingExtents(
297 const Extent& extent) const {
298 const auto candidates = GetCandidateRange(extent);
299 std::vector<Extent> result;
300 for (const auto& ext : candidates) {
301 if (auto intersection = GetOverlapExtent(ext, extent);
302 intersection.num_blocks() != 0) {
303 result.emplace_back(std::move(intersection));
304 }
305 }
306 return result;
307}
308
Alex Deymof0061352015-07-01 14:59:15 -0700309vector<Extent> FilterExtentRanges(const vector<Extent>& extents,
Alex Deymoa376a6e2015-06-05 00:31:34 +0200310 const ExtentRanges& ranges) {
311 vector<Extent> result;
312 const ExtentRanges::ExtentSet& extent_set = ranges.extent_set();
313 for (Extent extent : extents) {
314 // The extents are sorted by the start_block. We want to iterate all the
315 // Extents in the ExtentSet possibly overlapping the current |extent|. This
316 // is achieved by looking from the extent whose start_block is *lower* than
317 // the extent.start_block() up to the greatest extent whose start_block is
318 // lower than extent.start_block() + extent.num_blocks().
319 auto lower = extent_set.lower_bound(extent);
320 // We need to decrement the lower_bound to look at the extent that could
321 // overlap the beginning of the current |extent|.
322 if (lower != extent_set.begin())
323 lower--;
324 auto upper = extent_set.lower_bound(
325 ExtentForRange(extent.start_block() + extent.num_blocks(), 0));
326 for (auto iter = lower; iter != upper; ++iter) {
327 if (!ExtentRanges::ExtentsOverlap(extent, *iter))
328 continue;
329 if (iter->start_block() <= extent.start_block()) {
330 // We need to cut blocks from the beginning of the |extent|.
Amin Hassani232f8f92019-01-14 16:15:31 -0800331 uint64_t cut_blocks =
332 iter->start_block() + iter->num_blocks() - extent.start_block();
Alex Deymoa376a6e2015-06-05 00:31:34 +0200333 if (cut_blocks >= extent.num_blocks()) {
334 extent.set_num_blocks(0);
335 break;
336 }
337 extent = ExtentForRange(extent.start_block() + cut_blocks,
338 extent.num_blocks() - cut_blocks);
339 } else {
340 // We need to cut blocks on the middle of the extent, possible up to the
341 // end of it.
Amin Hassani232f8f92019-01-14 16:15:31 -0800342 result.push_back(ExtentForRange(
343 extent.start_block(), iter->start_block() - extent.start_block()));
Alex Deymoa376a6e2015-06-05 00:31:34 +0200344 uint64_t new_start = iter->start_block() + iter->num_blocks();
345 uint64_t old_end = extent.start_block() + extent.num_blocks();
346 if (new_start >= old_end) {
347 extent.set_num_blocks(0);
348 break;
349 }
350 extent = ExtentForRange(new_start, old_end - new_start);
351 }
352 }
353 if (extent.num_blocks() > 0)
354 result.push_back(extent);
355 }
356 return result;
357}
358
Kelvin Zhangd567c8b2021-07-08 14:10:23 -0400359Extent GetOverlapExtent(const Extent& extent1, const Extent& extent2) {
360 if (!ExtentRanges::ExtentsOverlap(extent1, extent2)) {
361 return {};
362 }
363 const auto start_block =
364 std::max(extent1.start_block(), extent2.start_block());
365 const auto end_block = std::min(extent1.start_block() + extent1.num_blocks(),
366 extent2.start_block() + extent2.num_blocks());
367 return ExtentForRange(start_block, end_block - start_block);
368}
369
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700370} // namespace chromeos_update_engine