blob: adbfb7d37355ee82b0601959cc334b4f955484fb [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 -070027void push_back(std::vector<CowOperation>* converted, const CowOperation& op) {
28 if (!converted->empty() && IsConsecutive(converted->back(), op)) {
Kelvin Zhangd11e2fc2022-10-24 15:40:30 -070029 converted->back().block_count += op.block_count;
Kelvin Zhangc14676a2021-10-28 16:38:20 -070030 } else {
31 converted->push_back(op);
32 }
33}
34
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040035std::vector<CowOperation> ConvertToCowOperations(
36 const ::google::protobuf::RepeatedPtrField<
37 ::chromeos_update_engine::InstallOperation>& operations,
38 const ::google::protobuf::RepeatedPtrField<CowMergeOperation>&
39 merge_operations) {
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040040 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) {
Kelvin Zhang59724d82021-02-09 16:40:01 -050051 if (merge_op.type() != CowMergeOperation::COW_COPY) {
52 continue;
53 }
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040054 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();
Kelvin Zhangeb8703b2020-12-10 14:17:21 -050057 // 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;
Kelvin Zhangc14676a2021-10-28 16:38:20 -070063 converted.push_back({CowOperation::CowCopy, src_block, dst_block, 1});
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040064 }
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()) {
Kelvin Zhangc14676a2021-10-28 16:38:20 -070077 const auto src_block = *it1;
78 const auto dst_block = *it2;
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040079 if (!merge_extents.ContainsBlock(dst_block)) {
Kelvin Zhangc14676a2021-10-28 16:38:20 -070080 push_back(&converted,
81 {CowOperation::CowReplace, src_block, dst_block, 1});
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040082 }
83 ++it1;
84 ++it2;
85 }
86 }
87 return converted;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040088}
89} // namespace chromeos_update_engine