blob: a8f7541f40dbbe8ef21ffb3366c124f003bc7bb7 [file] [log] [blame]
Kelvin Zhang9b10dba2020-09-25 17:09:11 -04001//
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
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040019#include <base/logging.h>
20
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040021#include "update_engine/payload_generator/extent_ranges.h"
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040022#include "update_engine/payload_generator/extent_utils.h"
Kelvin Zhang59724d82021-02-09 16:40:01 -050023#include "update_engine/update_metadata.pb.h"
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040024
25namespace chromeos_update_engine {
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040026
Kelvin Zhangc14676a2021-10-28 16:38:20 -070027namespace {
28
29bool 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
34void 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
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040044std::vector<CowOperation> ConvertToCowOperations(
45 const ::google::protobuf::RepeatedPtrField<
46 ::chromeos_update_engine::InstallOperation>& operations,
47 const ::google::protobuf::RepeatedPtrField<CowMergeOperation>&
48 merge_operations) {
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040049 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) {
Kelvin Zhang59724d82021-02-09 16:40:01 -050060 if (merge_op.type() != CowMergeOperation::COW_COPY) {
61 continue;
62 }
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040063 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();
Kelvin Zhangeb8703b2020-12-10 14:17:21 -050066 // 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;
Kelvin Zhangc14676a2021-10-28 16:38:20 -070072 converted.push_back({CowOperation::CowCopy, src_block, dst_block, 1});
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040073 }
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()) {
Kelvin Zhangc14676a2021-10-28 16:38:20 -070086 const auto src_block = *it1;
87 const auto dst_block = *it2;
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040088 if (!merge_extents.ContainsBlock(dst_block)) {
Kelvin Zhangc14676a2021-10-28 16:38:20 -070089 push_back(&converted,
90 {CowOperation::CowReplace, src_block, dst_block, 1});
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040091 }
92 ++it1;
93 ++it2;
94 }
95 }
96 return converted;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040097}
98} // namespace chromeos_update_engine