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 
ContainsBlock(uint64_t block) const205 bool 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 
Dump() const222 void ExtentRanges::Dump() const {
223   LOG(INFO) << "ExtentRanges Dump. blocks: " << blocks_;
224   for (ExtentSet::const_iterator it = extent_set_.begin(),
225                                  e = extent_set_.end();
226        it != e;
227        ++it) {
228     LOG(INFO) << "{" << it->start_block() << ", " << it->num_blocks() << "}";
229   }
230 }
231 
ExtentForRange(uint64_t start_block,uint64_t num_blocks)232 Extent 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 
ExtentForBytes(uint64_t block_size,uint64_t start_bytes,uint64_t size_bytes)239 Extent 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 
GetExtentsForBlockCount(uint64_t count) const247 vector<Extent> ExtentRanges::GetExtentsForBlockCount(uint64_t count) const {
248   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(),
254                                  e = extent_set_.end();
255        it != e;
256        ++it) {
257     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   }
271   CHECK(out_blocks == utils::BlocksInExtents(out));
272   return out;
273 }
274 
FilterExtentRanges(const vector<Extent> & extents,const ExtentRanges & ranges)275 vector<Extent> FilterExtentRanges(const vector<Extent>& extents,
276                                   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|.
297         uint64_t cut_blocks =
298             iter->start_block() + iter->num_blocks() - extent.start_block();
299         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.
308         result.push_back(ExtentForRange(
309             extent.start_block(), iter->start_block() - extent.start_block()));
310         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 
325 }  // namespace chromeos_update_engine
326