• 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::set;
31 using std::vector;
32 
33 namespace chromeos_update_engine {
34 
ExtentsOverlapOrTouch(const Extent & a,const Extent & b)35 bool ExtentRanges::ExtentsOverlapOrTouch(const Extent& a, const Extent& b) {
36   if (a.start_block() == b.start_block())
37     return true;
38   if (a.start_block() == kSparseHole || b.start_block() == kSparseHole)
39     return false;
40   if (a.start_block() < b.start_block()) {
41     return a.start_block() + a.num_blocks() >= b.start_block();
42   } else {
43     return b.start_block() + b.num_blocks() >= a.start_block();
44   }
45 }
46 
ExtentsOverlap(const Extent & a,const Extent & b)47 bool ExtentRanges::ExtentsOverlap(const Extent& a, const Extent& b) {
48   if (a.start_block() == b.start_block())
49     return true;
50   if (a.start_block() == kSparseHole || b.start_block() == kSparseHole)
51     return false;
52   if (a.start_block() < b.start_block()) {
53     return a.start_block() + a.num_blocks() > b.start_block();
54   } else {
55     return b.start_block() + b.num_blocks() > a.start_block();
56   }
57 }
58 
AddBlock(uint64_t block)59 void ExtentRanges::AddBlock(uint64_t block) {
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     if (ExtentsOverlapOrTouch(*it, extent)) {
91       end_del = it;
92       ++end_del;
93       del_blocks += it->num_blocks();
94       if (begin_del == extent_set_.end())
95         begin_del = it;
96 
97       extent = UnionOverlappingExtents(extent, *it);
98     }
99   }
100   extent_set_.erase(begin_del, end_del);
101   extent_set_.insert(extent);
102   blocks_ -= del_blocks;
103   blocks_ += extent.num_blocks();
104 }
105 
106 namespace {
107 // Returns base - subtractee (set subtraction).
SubtractOverlappingExtents(const Extent & base,const Extent & subtractee)108 ExtentRanges::ExtentSet SubtractOverlappingExtents(const Extent& base,
109                                                    const Extent& subtractee) {
110   ExtentRanges::ExtentSet ret;
111   if (subtractee.start_block() > base.start_block()) {
112     ret.insert(ExtentForRange(base.start_block(),
113                               subtractee.start_block() - base.start_block()));
114   }
115   uint64_t base_end = base.start_block() + base.num_blocks();
116   uint64_t subtractee_end = subtractee.start_block() + subtractee.num_blocks();
117   if (base_end > subtractee_end) {
118     ret.insert(ExtentForRange(subtractee_end, base_end - subtractee_end));
119   }
120   return ret;
121 }
122 }  // namespace
123 
SubtractExtent(const Extent & extent)124 void ExtentRanges::SubtractExtent(const Extent& extent) {
125   if (extent.start_block() == kSparseHole || extent.num_blocks() == 0)
126     return;
127 
128   ExtentSet::iterator begin_del = extent_set_.end();
129   ExtentSet::iterator end_del = extent_set_.end();
130   uint64_t del_blocks = 0;
131   ExtentSet new_extents;
132   for (ExtentSet::iterator it = extent_set_.begin(), e = extent_set_.end();
133        it != e;
134        ++it) {
135     if (!ExtentsOverlap(*it, extent))
136       continue;
137 
138     if (begin_del == extent_set_.end())
139       begin_del = it;
140     end_del = it;
141     ++end_del;
142 
143     del_blocks += it->num_blocks();
144 
145     ExtentSet subtraction = SubtractOverlappingExtents(*it, extent);
146     for (ExtentSet::iterator jt = subtraction.begin(), je = subtraction.end();
147          jt != je;
148          ++jt) {
149       new_extents.insert(*jt);
150       del_blocks -= jt->num_blocks();
151     }
152   }
153   extent_set_.erase(begin_del, end_del);
154   extent_set_.insert(new_extents.begin(), new_extents.end());
155   blocks_ -= del_blocks;
156 }
157 
AddRanges(const ExtentRanges & ranges)158 void ExtentRanges::AddRanges(const ExtentRanges& ranges) {
159   for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
160                                  e = ranges.extent_set_.end();
161        it != e;
162        ++it) {
163     AddExtent(*it);
164   }
165 }
166 
SubtractRanges(const ExtentRanges & ranges)167 void ExtentRanges::SubtractRanges(const ExtentRanges& ranges) {
168   for (ExtentSet::const_iterator it = ranges.extent_set_.begin(),
169                                  e = ranges.extent_set_.end();
170        it != e;
171        ++it) {
172     SubtractExtent(*it);
173   }
174 }
175 
AddExtents(const vector<Extent> & extents)176 void ExtentRanges::AddExtents(const vector<Extent>& extents) {
177   for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
178        it != e;
179        ++it) {
180     AddExtent(*it);
181   }
182 }
183 
SubtractExtents(const vector<Extent> & extents)184 void ExtentRanges::SubtractExtents(const vector<Extent>& extents) {
185   for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
186        it != e;
187        ++it) {
188     SubtractExtent(*it);
189   }
190 }
191 
AddRepeatedExtents(const::google::protobuf::RepeatedPtrField<Extent> & exts)192 void ExtentRanges::AddRepeatedExtents(
193     const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
194   for (int i = 0, e = exts.size(); i != e; ++i) {
195     AddExtent(exts.Get(i));
196   }
197 }
198 
SubtractRepeatedExtents(const::google::protobuf::RepeatedPtrField<Extent> & exts)199 void ExtentRanges::SubtractRepeatedExtents(
200     const ::google::protobuf::RepeatedPtrField<Extent>& exts) {
201   for (int i = 0, e = exts.size(); i != e; ++i) {
202     SubtractExtent(exts.Get(i));
203   }
204 }
205 
ContainsBlock(uint64_t block) const206 bool ExtentRanges::ContainsBlock(uint64_t block) const {
207   auto lower = extent_set_.lower_bound(ExtentForRange(block, 1));
208   // The block could be on the extent before the one in |lower|.
209   if (lower != extent_set_.begin())
210     lower--;
211   // Any extent starting at block+1 or later is not interesting, so this is the
212   // upper limit.
213   auto upper = extent_set_.lower_bound(ExtentForRange(block + 1, 0));
214   for (auto iter = lower; iter != upper; ++iter) {
215     if (iter->start_block() <= block &&
216         block < iter->start_block() + iter->num_blocks()) {
217       return true;
218     }
219   }
220   return false;
221 }
222 
Dump() const223 void ExtentRanges::Dump() const {
224   LOG(INFO) << "ExtentRanges Dump. blocks: " << blocks_;
225   for (ExtentSet::const_iterator it = extent_set_.begin(),
226                                  e = extent_set_.end();
227        it != e;
228        ++it) {
229     LOG(INFO) << "{" << it->start_block() << ", " << it->num_blocks() << "}";
230   }
231 }
232 
ExtentForRange(uint64_t start_block,uint64_t num_blocks)233 Extent ExtentForRange(uint64_t start_block, uint64_t num_blocks) {
234   Extent ret;
235   ret.set_start_block(start_block);
236   ret.set_num_blocks(num_blocks);
237   return ret;
238 }
239 
ExtentForBytes(uint64_t block_size,uint64_t start_bytes,uint64_t size_bytes)240 Extent ExtentForBytes(uint64_t block_size,
241                       uint64_t start_bytes,
242                       uint64_t size_bytes) {
243   uint64_t start_block = start_bytes / block_size;
244   uint64_t end_block = utils::DivRoundUp(start_bytes + size_bytes, block_size);
245   return ExtentForRange(start_block, end_block - start_block);
246 }
247 
GetExtentsForBlockCount(uint64_t count) const248 vector<Extent> ExtentRanges::GetExtentsForBlockCount(uint64_t count) const {
249   vector<Extent> out;
250   if (count == 0)
251     return out;
252   uint64_t out_blocks = 0;
253   CHECK(count <= blocks_);
254   for (ExtentSet::const_iterator it = extent_set_.begin(),
255                                  e = extent_set_.end();
256        it != e;
257        ++it) {
258     const uint64_t blocks_needed = count - out_blocks;
259     const Extent& extent = *it;
260     out.push_back(extent);
261     out_blocks += extent.num_blocks();
262     if (extent.num_blocks() < blocks_needed)
263       continue;
264     if (extent.num_blocks() == blocks_needed)
265       break;
266     // If we get here, we just added the last extent needed, but it's too big
267     out_blocks -= extent.num_blocks();
268     out_blocks += blocks_needed;
269     out.back().set_num_blocks(blocks_needed);
270     break;
271   }
272   CHECK(out_blocks == utils::BlocksInExtents(out));
273   return out;
274 }
275 
FilterExtentRanges(const vector<Extent> & extents,const ExtentRanges & ranges)276 vector<Extent> FilterExtentRanges(const vector<Extent>& extents,
277                                   const ExtentRanges& ranges) {
278   vector<Extent> result;
279   const ExtentRanges::ExtentSet& extent_set = ranges.extent_set();
280   for (Extent extent : extents) {
281     // The extents are sorted by the start_block. We want to iterate all the
282     // Extents in the ExtentSet possibly overlapping the current |extent|. This
283     // is achieved by looking from the extent whose start_block is *lower* than
284     // the extent.start_block() up to the greatest extent whose start_block is
285     // lower than extent.start_block() + extent.num_blocks().
286     auto lower = extent_set.lower_bound(extent);
287     // We need to decrement the lower_bound to look at the extent that could
288     // overlap the beginning of the current |extent|.
289     if (lower != extent_set.begin())
290       lower--;
291     auto upper = extent_set.lower_bound(
292         ExtentForRange(extent.start_block() + extent.num_blocks(), 0));
293     for (auto iter = lower; iter != upper; ++iter) {
294       if (!ExtentRanges::ExtentsOverlap(extent, *iter))
295         continue;
296       if (iter->start_block() <= extent.start_block()) {
297         // We need to cut blocks from the beginning of the |extent|.
298         uint64_t cut_blocks =
299             iter->start_block() + iter->num_blocks() - extent.start_block();
300         if (cut_blocks >= extent.num_blocks()) {
301           extent.set_num_blocks(0);
302           break;
303         }
304         extent = ExtentForRange(extent.start_block() + cut_blocks,
305                                 extent.num_blocks() - cut_blocks);
306       } else {
307         // We need to cut blocks on the middle of the extent, possible up to the
308         // end of it.
309         result.push_back(ExtentForRange(
310             extent.start_block(), iter->start_block() - extent.start_block()));
311         uint64_t new_start = iter->start_block() + iter->num_blocks();
312         uint64_t old_end = extent.start_block() + extent.num_blocks();
313         if (new_start >= old_end) {
314           extent.set_num_blocks(0);
315           break;
316         }
317         extent = ExtentForRange(new_start, old_end - new_start);
318       }
319     }
320     if (extent.num_blocks() > 0)
321       result.push_back(extent);
322   }
323   return result;
324 }
325 
326 }  // namespace chromeos_update_engine
327