blob: 6b64a9c7ecbfedbad050edc60f5cc5fd1c65bce3 [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 Zhang9b10dba2020-09-25 17:09:11 -040023
24namespace chromeos_update_engine {
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040025
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040026std::vector<CowOperation> ConvertToCowOperations(
27 const ::google::protobuf::RepeatedPtrField<
28 ::chromeos_update_engine::InstallOperation>& operations,
29 const ::google::protobuf::RepeatedPtrField<CowMergeOperation>&
30 merge_operations) {
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040031 ExtentRanges merge_extents;
32 std::vector<CowOperation> converted;
Kelvin Zhang4796ea82020-11-02 10:54:07 -050033 ExtentRanges modified_extents;
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040034
35 // We want all CowCopy ops to be done first, before any COW_REPLACE happen.
36 // Therefore we add these ops in 2 separate loops. This is because during
37 // merge, a CowReplace might modify a block needed by CowCopy, so we always
38 // perform CowCopy first.
39
40 // This loop handles CowCopy blocks within SOURCE_COPY, and the next loop
41 // converts the leftover blocks to CowReplace?
42 for (const auto& merge_op : merge_operations) {
43 merge_extents.AddExtent(merge_op.dst_extent());
44 const auto& src_extent = merge_op.src_extent();
45 const auto& dst_extent = merge_op.dst_extent();
Kelvin Zhang4796ea82020-11-02 10:54:07 -050046 // Add blocks in reverse order to avoid merge conflicts on self-overlapping
47 // Ops.
48 // For example: SOURCE_COPY [20 - 30] -> [25 - 35] If blocks are added in
49 // forward order, then 20->25 is performed first, destroying block 25, which
50 // is neede by a later operation.
51 if (src_extent.start_block() < dst_extent.start_block()) {
52 for (uint64_t i = src_extent.num_blocks(); i > 0; i--) {
53 auto src_block = src_extent.start_block() + i - 1;
54 auto dst_block = dst_extent.start_block() + i - 1;
55 CHECK(!modified_extents.ContainsBlock(src_block))
56 << "block " << src_block << " is modified by previous CowCopy";
57 converted.push_back({CowOperation::CowCopy, src_block, dst_block});
58 modified_extents.AddBlock(dst_block);
59 }
60 } else {
61 for (uint64_t i = 0; i < src_extent.num_blocks(); i++) {
62 auto src_block = src_extent.start_block() + i;
63 auto dst_block = dst_extent.start_block() + i;
64 CHECK(!modified_extents.ContainsBlock(src_block))
65 << "block " << src_block << " is modified by previous CowCopy";
66 converted.push_back({CowOperation::CowCopy, src_block, dst_block});
67 modified_extents.AddBlock(dst_block);
68 }
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040069 }
70 }
71 // COW_REPLACE are added after COW_COPY, because replace might modify blocks
72 // needed by COW_COPY. Please don't merge this loop with the previous one.
73 for (const auto& operation : operations) {
74 if (operation.type() != InstallOperation::SOURCE_COPY) {
75 continue;
76 }
77 const auto& src_extents = operation.src_extents();
78 const auto& dst_extents = operation.dst_extents();
79 BlockIterator it1{src_extents};
80 BlockIterator it2{dst_extents};
81 while (!it1.is_end() && !it2.is_end()) {
82 auto src_block = *it1;
83 auto dst_block = *it2;
84 if (!merge_extents.ContainsBlock(dst_block)) {
85 converted.push_back({CowOperation::CowReplace, src_block, dst_block});
86 }
87 ++it1;
88 ++it2;
89 }
90 }
91 return converted;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040092}
93} // namespace chromeos_update_engine