• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "update_engine/payload_generator/extent_utils.h"
29 
30 using std::vector;
31 
32 namespace chromeos_update_engine {
33 
ExtentsOverlapOrTouch(const Extent & a,const Extent & b)34 bool ExtentRanges::ExtentsOverlapOrTouch(const Extent& a, const Extent& b) {
35   if (a.start_block() == b.start_block())
36     return true;
37   if (a.start_block() == kSparseHole || b.start_block() == kSparseHole)
38     return false;
39   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 
ExtentsOverlap(const Extent & a,const Extent & b)46 bool ExtentRanges::ExtentsOverlap(const Extent& a, const Extent& b) {
47   if (a.start_block() == b.start_block())
48     return a.num_blocks() != 0;
49   if (a.start_block() == kSparseHole || b.start_block() == kSparseHole)
50     return false;
51   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 
AddBlock(uint64_t block)58 void ExtentRanges::AddBlock(uint64_t block) {
59   // Remember to respect |merge_touching_extents_| setting
60   AddExtent(ExtentForRange(block, 1));
61 }
62 
SubtractBlock(uint64_t block)63 void ExtentRanges::SubtractBlock(uint64_t block) {
64   SubtractExtent(ExtentForRange(block, 1));
65 }
66 
67 namespace {
68 
UnionOverlappingExtents(const Extent & first,const Extent & second)69 Extent UnionOverlappingExtents(const Extent& first, const Extent& second) {
70   CHECK_NE(kSparseHole, first.start_block());
71   CHECK_NE(kSparseHole, second.start_block());
72   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());
75   return ExtentForRange(start, end - start);
76 }
77 
78 }  // namespace
79 
AddExtent(Extent extent)80 void ExtentRanges::AddExtent(Extent extent) {
81   if (extent.start_block() == kSparseHole || extent.num_blocks() == 0)
82     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();
88        it != e;
89        ++it) {
90     const bool should_merge = merge_touching_extents_
91                                   ? ExtentsOverlapOrTouch(*it, extent)
92                                   : ExtentsOverlap(*it, extent);
93     if (should_merge) {
94       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 
109 namespace {
110 // Returns base - subtractee (set subtraction).
SubtractOverlappingExtents(const Extent & base,const Extent & subtractee)111 ExtentRanges::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 }
125 }  // namespace
126 
SubtractExtent(const Extent & extent)127 void ExtentRanges::SubtractExtent(const Extent& extent) {
128   if (extent.start_block() == kSparseHole || extent.num_blocks() == 0)
129     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();
136        it != e;
137        ++it) {
138     if (!ExtentsOverlap(*it, extent))
139       continue;
140 
141     if (begin_del == extent_set_.end())
142       begin_del = it;
143     end_del = it;
144     ++end_del;
145 
146     del_blocks += it->num_blocks();
147 
148     ExtentSet subtraction = SubtractOverlappingExtents(*it, extent);
149     for (ExtentSet::iterator jt = subtraction.begin(), je = subtraction.end();
150          jt != je;
151          ++jt) {
152       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 
AddRanges(const ExtentRanges & ranges)161 void ExtentRanges::AddRanges(const ExtentRanges& ranges) {
162   // Remember to respect |merge_touching_extents_| setting
163   for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
164                                  e = ranges.extent_set_.end();
165        it != e;
166        ++it) {
167     AddExtent(*it);
168   }
169 }
170 
SubtractRanges(const ExtentRanges & ranges)171 void ExtentRanges::SubtractRanges(const ExtentRanges& ranges) {
172   for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
173                                  e = ranges.extent_set_.end();
174        it != e;
175        ++it) {
176     SubtractExtent(*it);
177   }
178 }
179 
AddExtents(const vector<Extent> & extents)180 void ExtentRanges::AddExtents(const vector<Extent>& extents) {
181   // Remember to respect |merge_touching_extents_| setting
182   for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
183        it != e;
184        ++it) {
185     AddExtent(*it);
186   }
187 }
188 
SubtractExtents(const vector<Extent> & extents)189 void ExtentRanges::SubtractExtents(const vector<Extent>& extents) {
190   for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
191        it != e;
192        ++it) {
193     SubtractExtent(*it);
194   }
195 }
196 
AddRepeatedExtents(const::google::protobuf::RepeatedPtrField<Extent> & exts)197 void ExtentRanges::AddRepeatedExtents(
198     const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
199   // Remember to respect |merge_touching_extents_| setting
200   for (int i = 0, e = exts.size(); i != e; ++i) {
201     AddExtent(exts.Get(i));
202   }
203 }
204 
SubtractRepeatedExtents(const::google::protobuf::RepeatedPtrField<Extent> & exts)205 void ExtentRanges::SubtractRepeatedExtents(
206     const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
207   for (int i = 0, e = exts.size(); i != e; ++i) {
208     SubtractExtent(exts.Get(i));
209   }
210 }
211 
OverlapsWithExtent(const Extent & extent) const212 bool ExtentRanges::OverlapsWithExtent(const Extent& extent) const {
213   for (const auto& entry : GetCandidateRange(extent)) {
214     if (ExtentsOverlap(entry, extent)) {
215       return true;
216     }
217   }
218   return false;
219 }
220 
ContainsBlock(uint64_t block) const221 bool 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 
Dump() const238 void ExtentRanges::Dump() const {
239   LOG(INFO) << "ExtentRanges Dump. blocks: " << blocks_;
240   for (ExtentSet::const_iterator it = extent_set_.begin(),
241                                  e = extent_set_.end();
242        it != e;
243        ++it) {
244     LOG(INFO) << "{" << it->start_block() << ", " << it->num_blocks() << "}";
245   }
246 }
247 
ExtentForRange(uint64_t start_block,uint64_t num_blocks)248 Extent 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 
ExtentForBytes(uint64_t block_size,uint64_t start_bytes,uint64_t size_bytes)255 Extent 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 
GetExtentsForBlockCount(uint64_t count) const263 vector<Extent> ExtentRanges::GetExtentsForBlockCount(uint64_t count) const {
264   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(),
270                                  e = extent_set_.end();
271        it != e;
272        ++it) {
273     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   }
287   CHECK(out_blocks == utils::BlocksInExtents(out));
288   return out;
289 }
290 
GetCandidateRange(const Extent & extent) const291 Range<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 
GetIntersectingExtents(const Extent & extent) const303 std::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 
FilterExtentRanges(const vector<Extent> & extents,const ExtentRanges & ranges)316 vector<Extent> FilterExtentRanges(const vector<Extent>& extents,
317                                   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|.
338         uint64_t cut_blocks =
339             iter->start_block() + iter->num_blocks() - extent.start_block();
340         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.
349         result.push_back(ExtentForRange(
350             extent.start_block(), iter->start_block() - extent.start_block()));
351         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 
GetOverlapExtent(const Extent & extent1,const Extent & extent2)366 Extent 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 
377 }  // namespace chromeos_update_engine
378