blob: 4600efe75a564a06828ce49a02597481a7facf8e [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())
48 return true;
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
Alex Deymof0061352015-07-01 14:59:15 -0700205bool ExtentRanges::ContainsBlock(uint64_t block) const {
206 auto lower = extent_set_.lower_bound(ExtentForRange(block, 1));
207 // The block could be on the extent before the one in |lower|.
208 if (lower != extent_set_.begin())
209 lower--;
210 // Any extent starting at block+1 or later is not interesting, so this is the
211 // upper limit.
212 auto upper = extent_set_.lower_bound(ExtentForRange(block + 1, 0));
213 for (auto iter = lower; iter != upper; ++iter) {
214 if (iter->start_block() <= block &&
215 block < iter->start_block() + iter->num_blocks()) {
216 return true;
217 }
218 }
219 return false;
220}
221
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700222void ExtentRanges::Dump() const {
223 LOG(INFO) << "ExtentRanges Dump. blocks: " << blocks_;
224 for (ExtentSet::const_iterator it = extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800225 e = extent_set_.end();
226 it != e;
227 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700228 LOG(INFO) << "{" << it->start_block() << ", " << it->num_blocks() << "}";
229 }
230}
231
232Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks) {
233 Extent ret;
234 ret.set_start_block(start_block);
235 ret.set_num_blocks(num_blocks);
236 return ret;
237}
238
Sen Jiang0a582fb2018-06-26 19:27:21 -0700239Extent ExtentForBytes(uint64_t block_size,
240 uint64_t start_bytes,
241 uint64_t size_bytes) {
242 uint64_t start_block = start_bytes / block_size;
243 uint64_t end_block = utils::DivRoundUp(start_bytes + size_bytes, block_size);
244 return ExtentForRange(start_block, end_block - start_block);
245}
246
Amin Hassani232f8f92019-01-14 16:15:31 -0800247vector<Extent> ExtentRanges::GetExtentsForBlockCount(uint64_t count) const {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700248 vector<Extent> out;
249 if (count == 0)
250 return out;
251 uint64_t out_blocks = 0;
252 CHECK(count <= blocks_);
253 for (ExtentSet::const_iterator it = extent_set_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800254 e = extent_set_.end();
255 it != e;
256 ++it) {
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700257 const uint64_t blocks_needed = count - out_blocks;
258 const Extent& extent = *it;
259 out.push_back(extent);
260 out_blocks += extent.num_blocks();
261 if (extent.num_blocks() < blocks_needed)
262 continue;
263 if (extent.num_blocks() == blocks_needed)
264 break;
265 // If we get here, we just added the last extent needed, but it's too big
266 out_blocks -= extent.num_blocks();
267 out_blocks += blocks_needed;
268 out.back().set_num_blocks(blocks_needed);
269 break;
270 }
Amin Hassanid8b67f42017-12-06 13:47:52 -0800271 CHECK(out_blocks == utils::BlocksInExtents(out));
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700272 return out;
273}
274
Alex Deymof0061352015-07-01 14:59:15 -0700275vector<Extent> FilterExtentRanges(const vector<Extent>& extents,
Alex Deymoa376a6e2015-06-05 00:31:34 +0200276 const ExtentRanges& ranges) {
277 vector<Extent> result;
278 const ExtentRanges::ExtentSet& extent_set = ranges.extent_set();
279 for (Extent extent : extents) {
280 // The extents are sorted by the start_block. We want to iterate all the
281 // Extents in the ExtentSet possibly overlapping the current |extent|. This
282 // is achieved by looking from the extent whose start_block is *lower* than
283 // the extent.start_block() up to the greatest extent whose start_block is
284 // lower than extent.start_block() + extent.num_blocks().
285 auto lower = extent_set.lower_bound(extent);
286 // We need to decrement the lower_bound to look at the extent that could
287 // overlap the beginning of the current |extent|.
288 if (lower != extent_set.begin())
289 lower--;
290 auto upper = extent_set.lower_bound(
291 ExtentForRange(extent.start_block() + extent.num_blocks(), 0));
292 for (auto iter = lower; iter != upper; ++iter) {
293 if (!ExtentRanges::ExtentsOverlap(extent, *iter))
294 continue;
295 if (iter->start_block() <= extent.start_block()) {
296 // We need to cut blocks from the beginning of the |extent|.
Amin Hassani232f8f92019-01-14 16:15:31 -0800297 uint64_t cut_blocks =
298 iter->start_block() + iter->num_blocks() - extent.start_block();
Alex Deymoa376a6e2015-06-05 00:31:34 +0200299 if (cut_blocks >= extent.num_blocks()) {
300 extent.set_num_blocks(0);
301 break;
302 }
303 extent = ExtentForRange(extent.start_block() + cut_blocks,
304 extent.num_blocks() - cut_blocks);
305 } else {
306 // We need to cut blocks on the middle of the extent, possible up to the
307 // end of it.
Amin Hassani232f8f92019-01-14 16:15:31 -0800308 result.push_back(ExtentForRange(
309 extent.start_block(), iter->start_block() - extent.start_block()));
Alex Deymoa376a6e2015-06-05 00:31:34 +0200310 uint64_t new_start = iter->start_block() + iter->num_blocks();
311 uint64_t old_end = extent.start_block() + extent.num_blocks();
312 if (new_start >= old_end) {
313 extent.set_num_blocks(0);
314 break;
315 }
316 extent = ExtentForRange(new_start, old_end - new_start);
317 }
318 }
319 if (extent.num_blocks() > 0)
320 result.push_back(extent);
321 }
322 return result;
323}
324
Andrew de los Reyes5fdae4a2010-10-05 10:47:42 -0700325} // namespace chromeos_update_engine