• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2020 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/common/cow_operation_convert.h"
18 
19 #include <base/logging.h>
20 
21 #include "update_engine/payload_generator/extent_ranges.h"
22 #include "update_engine/payload_generator/extent_utils.h"
23 #include "update_engine/update_metadata.pb.h"
24 
25 namespace chromeos_update_engine {
26 
27 namespace {
28 
IsConsecutive(const CowOperation & op1,const CowOperation & op2)29 bool IsConsecutive(const CowOperation& op1, const CowOperation& op2) {
30   return op1.op == op2.op && op1.dst_block + op1.block_count == op2.dst_block &&
31          op1.src_block + op1.block_count == op2.src_block;
32 }
33 
push_back(std::vector<CowOperation> * converted,const CowOperation & op)34 void push_back(std::vector<CowOperation>* converted, const CowOperation& op) {
35   if (!converted->empty() && IsConsecutive(converted->back(), op)) {
36     converted->back().block_count++;
37   } else {
38     converted->push_back(op);
39   }
40 }
41 
42 }  // namespace
43 
ConvertToCowOperations(const::google::protobuf::RepeatedPtrField<::chromeos_update_engine::InstallOperation> & operations,const::google::protobuf::RepeatedPtrField<CowMergeOperation> & merge_operations)44 std::vector<CowOperation> ConvertToCowOperations(
45     const ::google::protobuf::RepeatedPtrField<
46         ::chromeos_update_engine::InstallOperation>& operations,
47     const ::google::protobuf::RepeatedPtrField<CowMergeOperation>&
48         merge_operations) {
49   ExtentRanges merge_extents;
50   std::vector<CowOperation> converted;
51 
52   // We want all CowCopy ops to be done first, before any COW_REPLACE happen.
53   // Therefore we add these ops in 2 separate loops. This is because during
54   // merge, a CowReplace might modify a block needed by CowCopy, so we always
55   // perform CowCopy first.
56 
57   // This loop handles CowCopy blocks within SOURCE_COPY, and the next loop
58   // converts the leftover blocks to CowReplace?
59   for (const auto& merge_op : merge_operations) {
60     if (merge_op.type() != CowMergeOperation::COW_COPY) {
61       continue;
62     }
63     merge_extents.AddExtent(merge_op.dst_extent());
64     const auto& src_extent = merge_op.src_extent();
65     const auto& dst_extent = merge_op.dst_extent();
66     // Add blocks in reverse order, because snapused specifically prefers this
67     // ordering. Since we already eliminated all self-overlapping SOURCE_COPY
68     // during delta generation, this should be safe to do.
69     for (uint64_t i = src_extent.num_blocks(); i > 0; i--) {
70       auto src_block = src_extent.start_block() + i - 1;
71       auto dst_block = dst_extent.start_block() + i - 1;
72       converted.push_back({CowOperation::CowCopy, src_block, dst_block, 1});
73     }
74   }
75   // COW_REPLACE are added after COW_COPY, because replace might modify blocks
76   // needed by COW_COPY. Please don't merge this loop with the previous one.
77   for (const auto& operation : operations) {
78     if (operation.type() != InstallOperation::SOURCE_COPY) {
79       continue;
80     }
81     const auto& src_extents = operation.src_extents();
82     const auto& dst_extents = operation.dst_extents();
83     BlockIterator it1{src_extents};
84     BlockIterator it2{dst_extents};
85     while (!it1.is_end() && !it2.is_end()) {
86       const auto src_block = *it1;
87       const auto dst_block = *it2;
88       if (!merge_extents.ContainsBlock(dst_block)) {
89         push_back(&converted,
90                   {CowOperation::CowReplace, src_block, dst_block, 1});
91       }
92       ++it1;
93       ++it2;
94     }
95   }
96   return converted;
97 }
98 }  // namespace chromeos_update_engine
99