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 //
16 
17 #include "update_engine/payload_generator/extent_ranges.h"
18 
19 #include <algorithm>
20 #include <set>
21 #include <utility>
22 #include <vector>
23 
24 #include <base/logging.h>
25 
26 #include "update_engine/common/utils.h"
27 #include "update_engine/payload_consumer/payload_constants.h"
28 
29 using std::vector;
30 
31 namespace chromeos_update_engine {
32 
ExtentsOverlapOrTouch(const Extent & a,const Extent & b)33 bool ExtentRanges::ExtentsOverlapOrTouch(const Extent& a, const Extent& b) {
34   if (a.start_block() == b.start_block())
35     return true;
36   if (a.start_block() == kSparseHole || b.start_block() == kSparseHole)
37     return false;
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 
ExtentsOverlap(const Extent & a,const Extent & b)45 bool ExtentRanges::ExtentsOverlap(const Extent& a, const Extent& b) {
46   if (a.start_block() == b.start_block())
47     return a.num_blocks() != 0;
48   if (a.start_block() == kSparseHole || b.start_block() == kSparseHole)
49     return false;
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 
AddBlock(uint64_t block)57 void ExtentRanges::AddBlock(uint64_t block) {
58   // Remember to respect |merge_touching_extents_| setting
59   AddExtent(ExtentForRange(block, 1));
60 }
61 
SubtractBlock(uint64_t block)62 void ExtentRanges::SubtractBlock(uint64_t block) {
63   SubtractExtent(ExtentForRange(block, 1));
64 }
65 
66 namespace {
67 
UnionOverlappingExtents(const Extent & first,const Extent & second)68 Extent UnionOverlappingExtents(const Extent& first, const Extent& second) {
69   CHECK_NE(kSparseHole, first.start_block());
70   CHECK_NE(kSparseHole, second.start_block());
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());
74   return ExtentForRange(start, end - start);
75 }
76 
77 }  // namespace
78 
AddExtent(Extent extent)79 void ExtentRanges::AddExtent(Extent extent) {
80   if (extent.start_block() == kSparseHole || extent.num_blocks() == 0)
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;
86   const auto range = GetCandidateRange(extent);
87   for (ExtentSet::iterator it = range.begin(), e = range.end(); it != e; ++it) {
88     const bool should_merge = merge_touching_extents_
89                                   ? ExtentsOverlapOrTouch(*it, extent)
90                                   : ExtentsOverlap(*it, extent);
91     if (should_merge) {
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).
SubtractOverlappingExtents(const Extent & base,const Extent & subtractee)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 }
123 }  // namespace
124 
SubtractExtent(const Extent & extent)125 void ExtentRanges::SubtractExtent(const Extent& extent) {
126   if (extent.start_block() == kSparseHole || extent.num_blocks() == 0)
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();
134        it != e;
135        ++it) {
136     if (!ExtentsOverlap(*it, extent))
137       continue;
138 
139     if (begin_del == extent_set_.end())
140       begin_del = it;
141     end_del = it;
142     ++end_del;
143 
144     del_blocks += it->num_blocks();
145 
146     ExtentSet subtraction = SubtractOverlappingExtents(*it, extent);
147     for (ExtentSet::iterator jt = subtraction.begin(), je = subtraction.end();
148          jt != je;
149          ++jt) {
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 
AddRanges(const ExtentRanges & ranges)159 void ExtentRanges::AddRanges(const ExtentRanges& ranges) {
160   // Remember to respect |merge_touching_extents_| setting
161   for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
162                                  e = ranges.extent_set_.end();
163        it != e;
164        ++it) {
165     AddExtent(*it);
166   }
167 }
168 
SubtractRanges(const ExtentRanges & ranges)169 void ExtentRanges::SubtractRanges(const ExtentRanges& ranges) {
170   for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
171                                  e = ranges.extent_set_.end();
172        it != e;
173        ++it) {
174     SubtractExtent(*it);
175   }
176 }
177 
AddExtents(const vector<Extent> & extents)178 void ExtentRanges::AddExtents(const vector<Extent>& extents) {
179   // Remember to respect |merge_touching_extents_| setting
180   for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
181        it != e;
182        ++it) {
183     AddExtent(*it);
184   }
185 }
186 
SubtractExtents(const vector<Extent> & extents)187 void ExtentRanges::SubtractExtents(const vector<Extent>& extents) {
188   for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
189        it != e;
190        ++it) {
191     SubtractExtent(*it);
192   }
193 }
194 
AddRepeatedExtents(const::google::protobuf::RepeatedPtrField<Extent> & exts)195 void ExtentRanges::AddRepeatedExtents(
196     const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
197   // Remember to respect |merge_touching_extents_| setting
198   for (int i = 0, e = exts.size(); i != e; ++i) {
199     AddExtent(exts.Get(i));
200   }
201 }
202 
SubtractRepeatedExtents(const::google::protobuf::RepeatedPtrField<Extent> & exts)203 void ExtentRanges::SubtractRepeatedExtents(
204     const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
205   for (int i = 0, e = exts.size(); i != e; ++i) {
206     SubtractExtent(exts.Get(i));
207   }
208 }
209 
OverlapsWithExtent(const Extent & extent) const210 bool ExtentRanges::OverlapsWithExtent(const Extent& extent) const {
211   for (const auto& entry : GetCandidateRange(extent)) {
212     if (ExtentsOverlap(entry, extent)) {
213       return true;
214     }
215   }
216   return false;
217 }
218 
ContainsBlock(uint64_t block) const219 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 
Dump() const236 void ExtentRanges::Dump() const {
237   LOG(INFO) << "ExtentRanges Dump. blocks: " << blocks_;
238   for (ExtentSet::const_iterator it = extent_set_.begin(),
239                                  e = extent_set_.end();
240        it != e;
241        ++it) {
242     LOG(INFO) << "{" << it->start_block() << ", " << it->num_blocks() << "}";
243   }
244 }
245 
ExtentForRange(uint64_t start_block,uint64_t num_blocks)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 
ExtentForBytes(uint64_t block_size,uint64_t start_bytes,uint64_t size_bytes)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 
GetExtentsForBlockCount(uint64_t count) const261 vector<Extent> ExtentRanges::GetExtentsForBlockCount(uint64_t count) const {
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(),
268                                  e = extent_set_.end();
269        it != e;
270        ++it) {
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   }
285   CHECK(out_blocks == utils::BlocksInExtents(out));
286   return out;
287 }
288 
GetCandidateRange(const Extent & extent) const289 Range<ExtentRanges::ExtentSet::const_iterator> ExtentRanges::GetCandidateRange(
290     const Extent& extent) const {
291   auto lower_it = extent_set_.lower_bound(extent);
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())) {
296     --lower_it;
297   }
298 
299   auto upper_it = extent_set_.upper_bound(
300       ExtentForRange(extent.start_block() + extent.num_blocks(), 1));
301   while (upper_it != extent_set_.end() &&
302          upper_it->start_block() <=
303              extent.start_block() + extent.num_blocks()) {
304     ++upper_it;
305   }
306   return {lower_it, upper_it};
307 }
308 
GetIntersectingExtents(const Extent & extent) const309 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 
FilterExtentRanges(const vector<Extent> & extents,const ExtentRanges & ranges)322 vector<Extent> FilterExtentRanges(const vector<Extent>& extents,
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|.
344         uint64_t cut_blocks =
345             iter->start_block() + iter->num_blocks() - extent.start_block();
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.
355         result.push_back(ExtentForRange(
356             extent.start_block(), iter->start_block() - extent.start_block()));
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 
GetOverlapExtent(const Extent & extent1,const Extent & extent2)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 
383 }  // namespace chromeos_update_engine
384