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