1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2020 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker
17*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/cow_operation_convert.h"
18*5a923131SAndroid Build Coastguard Worker
19*5a923131SAndroid Build Coastguard Worker #include <base/logging.h>
20*5a923131SAndroid Build Coastguard Worker
21*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_generator/extent_ranges.h"
22*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_generator/extent_utils.h"
23*5a923131SAndroid Build Coastguard Worker #include "update_engine/update_metadata.pb.h"
24*5a923131SAndroid Build Coastguard Worker
25*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
26*5a923131SAndroid Build Coastguard Worker
push_back(std::vector<CowOperation> * converted,const CowOperation & op)27*5a923131SAndroid Build Coastguard Worker void push_back(std::vector<CowOperation>* converted, const CowOperation& op) {
28*5a923131SAndroid Build Coastguard Worker if (!converted->empty() && IsConsecutive(converted->back(), op)) {
29*5a923131SAndroid Build Coastguard Worker converted->back().block_count += op.block_count;
30*5a923131SAndroid Build Coastguard Worker } else {
31*5a923131SAndroid Build Coastguard Worker converted->push_back(op);
32*5a923131SAndroid Build Coastguard Worker }
33*5a923131SAndroid Build Coastguard Worker }
34*5a923131SAndroid Build Coastguard Worker
ConvertToCowOperations(const::google::protobuf::RepeatedPtrField<::chromeos_update_engine::InstallOperation> & operations,const::google::protobuf::RepeatedPtrField<CowMergeOperation> & merge_operations)35*5a923131SAndroid Build Coastguard Worker std::vector<CowOperation> ConvertToCowOperations(
36*5a923131SAndroid Build Coastguard Worker const ::google::protobuf::RepeatedPtrField<
37*5a923131SAndroid Build Coastguard Worker ::chromeos_update_engine::InstallOperation>& operations,
38*5a923131SAndroid Build Coastguard Worker const ::google::protobuf::RepeatedPtrField<CowMergeOperation>&
39*5a923131SAndroid Build Coastguard Worker merge_operations) {
40*5a923131SAndroid Build Coastguard Worker ExtentRanges merge_extents;
41*5a923131SAndroid Build Coastguard Worker std::vector<CowOperation> converted;
42*5a923131SAndroid Build Coastguard Worker
43*5a923131SAndroid Build Coastguard Worker // We want all CowCopy ops to be done first, before any COW_REPLACE happen.
44*5a923131SAndroid Build Coastguard Worker // Therefore we add these ops in 2 separate loops. This is because during
45*5a923131SAndroid Build Coastguard Worker // merge, a CowReplace might modify a block needed by CowCopy, so we always
46*5a923131SAndroid Build Coastguard Worker // perform CowCopy first.
47*5a923131SAndroid Build Coastguard Worker
48*5a923131SAndroid Build Coastguard Worker // This loop handles CowCopy blocks within SOURCE_COPY, and the next loop
49*5a923131SAndroid Build Coastguard Worker // converts the leftover blocks to CowReplace?
50*5a923131SAndroid Build Coastguard Worker for (const auto& merge_op : merge_operations) {
51*5a923131SAndroid Build Coastguard Worker if (merge_op.type() != CowMergeOperation::COW_COPY) {
52*5a923131SAndroid Build Coastguard Worker continue;
53*5a923131SAndroid Build Coastguard Worker }
54*5a923131SAndroid Build Coastguard Worker merge_extents.AddExtent(merge_op.dst_extent());
55*5a923131SAndroid Build Coastguard Worker const auto& src_extent = merge_op.src_extent();
56*5a923131SAndroid Build Coastguard Worker const auto& dst_extent = merge_op.dst_extent();
57*5a923131SAndroid Build Coastguard Worker // Add blocks in reverse order, because snapused specifically prefers this
58*5a923131SAndroid Build Coastguard Worker // ordering. Since we already eliminated all self-overlapping SOURCE_COPY
59*5a923131SAndroid Build Coastguard Worker // during delta generation, this should be safe to do.
60*5a923131SAndroid Build Coastguard Worker for (uint64_t i = src_extent.num_blocks(); i > 0; i--) {
61*5a923131SAndroid Build Coastguard Worker auto src_block = src_extent.start_block() + i - 1;
62*5a923131SAndroid Build Coastguard Worker auto dst_block = dst_extent.start_block() + i - 1;
63*5a923131SAndroid Build Coastguard Worker converted.push_back({CowOperation::CowCopy, src_block, dst_block, 1});
64*5a923131SAndroid Build Coastguard Worker }
65*5a923131SAndroid Build Coastguard Worker }
66*5a923131SAndroid Build Coastguard Worker // COW_REPLACE are added after COW_COPY, because replace might modify blocks
67*5a923131SAndroid Build Coastguard Worker // needed by COW_COPY. Please don't merge this loop with the previous one.
68*5a923131SAndroid Build Coastguard Worker for (const auto& operation : operations) {
69*5a923131SAndroid Build Coastguard Worker if (operation.type() != InstallOperation::SOURCE_COPY) {
70*5a923131SAndroid Build Coastguard Worker continue;
71*5a923131SAndroid Build Coastguard Worker }
72*5a923131SAndroid Build Coastguard Worker const auto& src_extents = operation.src_extents();
73*5a923131SAndroid Build Coastguard Worker const auto& dst_extents = operation.dst_extents();
74*5a923131SAndroid Build Coastguard Worker BlockIterator it1{src_extents};
75*5a923131SAndroid Build Coastguard Worker BlockIterator it2{dst_extents};
76*5a923131SAndroid Build Coastguard Worker while (!it1.is_end() && !it2.is_end()) {
77*5a923131SAndroid Build Coastguard Worker const auto src_block = *it1;
78*5a923131SAndroid Build Coastguard Worker const auto dst_block = *it2;
79*5a923131SAndroid Build Coastguard Worker if (!merge_extents.ContainsBlock(dst_block)) {
80*5a923131SAndroid Build Coastguard Worker push_back(&converted,
81*5a923131SAndroid Build Coastguard Worker {CowOperation::CowReplace, src_block, dst_block, 1});
82*5a923131SAndroid Build Coastguard Worker }
83*5a923131SAndroid Build Coastguard Worker ++it1;
84*5a923131SAndroid Build Coastguard Worker ++it2;
85*5a923131SAndroid Build Coastguard Worker }
86*5a923131SAndroid Build Coastguard Worker }
87*5a923131SAndroid Build Coastguard Worker return converted;
88*5a923131SAndroid Build Coastguard Worker }
89*5a923131SAndroid Build Coastguard Worker } // namespace chromeos_update_engine
90