• 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 true;
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   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   for (ExtentSet::iterator it = extent_set_.begin(), e = extent_set_.end();
87        it != e;
88        ++it) {
89     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 
105 namespace {
106 // Returns base - subtractee (set subtraction).
SubtractOverlappingExtents(const Extent & base,const Extent & subtractee)107 ExtentRanges::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 }
121 }  // namespace
122 
SubtractExtent(const Extent & extent)123 void ExtentRanges::SubtractExtent(const Extent& extent) {
124   if (extent.start_block() == kSparseHole || extent.num_blocks() == 0)
125     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();
132        it != e;
133        ++it) {
134     if (!ExtentsOverlap(*it, extent))
135       continue;
136 
137     if (begin_del == extent_set_.end())
138       begin_del = it;
139     end_del = it;
140     ++end_del;
141 
142     del_blocks += it->num_blocks();
143 
144     ExtentSet subtraction = SubtractOverlappingExtents(*it, extent);
145     for (ExtentSet::iterator jt = subtraction.begin(), je = subtraction.end();
146          jt != je;
147          ++jt) {
148       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 
AddRanges(const ExtentRanges & ranges)157 void ExtentRanges::AddRanges(const ExtentRanges& ranges) {
158   for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
159                                  e = ranges.extent_set_.end();
160        it != e;
161        ++it) {
162     AddExtent(*it);
163   }
164 }
165 
SubtractRanges(const ExtentRanges & ranges)166 void ExtentRanges::SubtractRanges(const ExtentRanges& ranges) {
167   for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
168                                  e = ranges.extent_set_.end();
169        it != e;
170        ++it) {
171     SubtractExtent(*it);
172   }
173 }
174 
AddExtents(const vector<Extent> & extents)175 void ExtentRanges::AddExtents(const vector<Extent>& extents) {
176   for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
177        it != e;
178        ++it) {
179     AddExtent(*it);
180   }
181 }
182 
SubtractExtents(const vector<Extent> & extents)183 void ExtentRanges::SubtractExtents(const vector<Extent>& extents) {
184   for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
185        it != e;
186        ++it) {
187     SubtractExtent(*it);
188   }
189 }
190 
AddRepeatedExtents(const::google::protobuf::RepeatedPtrField<Extent> & exts)191 void ExtentRanges::AddRepeatedExtents(
192     const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
193   for (int i = 0, e = exts.size(); i != e; ++i) {
194     AddExtent(exts.Get(i));
195   }
196 }
197 
SubtractRepeatedExtents(const::google::protobuf::RepeatedPtrField<Extent> & exts)198 void ExtentRanges::SubtractRepeatedExtents(
199     const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
200   for (int i = 0, e = exts.size(); i != e; ++i) {
201     SubtractExtent(exts.Get(i));
202   }
203 }
204 
OverlapsWithExtent(const Extent & extent) const205 bool 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 
ContainsBlock(uint64_t block) const214 bool 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 
Dump() const231 void ExtentRanges::Dump() const {
232   LOG(INFO) << "ExtentRanges Dump. blocks: " << blocks_;
233   for (ExtentSet::const_iterator it = extent_set_.begin(),
234                                  e = extent_set_.end();
235        it != e;
236        ++it) {
237     LOG(INFO) << "{" << it->start_block() << ", " << it->num_blocks() << "}";
238   }
239 }
240 
ExtentForRange(uint64_t start_block,uint64_t num_blocks)241 Extent 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 
ExtentForBytes(uint64_t block_size,uint64_t start_bytes,uint64_t size_bytes)248 Extent 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 
GetExtentsForBlockCount(uint64_t count) const256 vector<Extent> ExtentRanges::GetExtentsForBlockCount(uint64_t count) const {
257   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(),
263                                  e = extent_set_.end();
264        it != e;
265        ++it) {
266     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   }
280   CHECK(out_blocks == utils::BlocksInExtents(out));
281   return out;
282 }
283 
FilterExtentRanges(const vector<Extent> & extents,const ExtentRanges & ranges)284 vector<Extent> FilterExtentRanges(const vector<Extent>& extents,
285                                   const ExtentRanges& ranges) {
286   vector<Extent> result;
287   const ExtentRanges::ExtentSet& extent_set = ranges.extent_set();
288   for (Extent extent : extents) {
289     // The extents are sorted by the start_block. We want to iterate all the
290     // Extents in the ExtentSet possibly overlapping the current |extent|. This
291     // is achieved by looking from the extent whose start_block is *lower* than
292     // the extent.start_block() up to the greatest extent whose start_block is
293     // lower than extent.start_block() + extent.num_blocks().
294     auto lower = extent_set.lower_bound(extent);
295     // We need to decrement the lower_bound to look at the extent that could
296     // overlap the beginning of the current |extent|.
297     if (lower != extent_set.begin())
298       lower--;
299     auto upper = extent_set.lower_bound(
300         ExtentForRange(extent.start_block() + extent.num_blocks(), 0));
301     for (auto iter = lower; iter != upper; ++iter) {
302       if (!ExtentRanges::ExtentsOverlap(extent, *iter))
303         continue;
304       if (iter->start_block() <= extent.start_block()) {
305         // We need to cut blocks from the beginning of the |extent|.
306         uint64_t cut_blocks =
307             iter->start_block() + iter->num_blocks() - extent.start_block();
308         if (cut_blocks >= extent.num_blocks()) {
309           extent.set_num_blocks(0);
310           break;
311         }
312         extent = ExtentForRange(extent.start_block() + cut_blocks,
313                                 extent.num_blocks() - cut_blocks);
314       } else {
315         // We need to cut blocks on the middle of the extent, possible up to the
316         // end of it.
317         result.push_back(ExtentForRange(
318             extent.start_block(), iter->start_block() - extent.start_block()));
319         uint64_t new_start = iter->start_block() + iter->num_blocks();
320         uint64_t old_end = extent.start_block() + extent.num_blocks();
321         if (new_start >= old_end) {
322           extent.set_num_blocks(0);
323           break;
324         }
325         extent = ExtentForRange(new_start, old_end - new_start);
326       }
327     }
328     if (extent.num_blocks() > 0)
329       result.push_back(extent);
330   }
331   return result;
332 }
333 
334 }  // namespace chromeos_update_engine
335