blob: 2564abf04bb68c43578eddac3fd9d0b69f002529 [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 Zhang9b10dba2020-09-25 17:09:11 -040027std::vector<CowOperation> ConvertToCowOperations(
28 const ::google::protobuf::RepeatedPtrField<
29 ::chromeos_update_engine::InstallOperation>& operations,
30 const ::google::protobuf::RepeatedPtrField<CowMergeOperation>&
31 merge_operations) {
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040032 ExtentRanges merge_extents;
33 std::vector<CowOperation> converted;
Kelvin Zhang4796ea82020-11-02 10:54:07 -050034 ExtentRanges modified_extents;
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040035
36 // We want all CowCopy ops to be done first, before any COW_REPLACE happen.
37 // Therefore we add these ops in 2 separate loops. This is because during
38 // merge, a CowReplace might modify a block needed by CowCopy, so we always
39 // perform CowCopy first.
40
41 // This loop handles CowCopy blocks within SOURCE_COPY, and the next loop
42 // converts the leftover blocks to CowReplace?
43 for (const auto& merge_op : merge_operations) {
Kelvin Zhang59724d82021-02-09 16:40:01 -050044 if (merge_op.type() != CowMergeOperation::COW_COPY) {
45 continue;
46 }
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040047 merge_extents.AddExtent(merge_op.dst_extent());
48 const auto& src_extent = merge_op.src_extent();
49 const auto& dst_extent = merge_op.dst_extent();
Kelvin Zhangeb8703b2020-12-10 14:17:21 -050050 // Add blocks in reverse order, because snapused specifically prefers this
51 // ordering. Since we already eliminated all self-overlapping SOURCE_COPY
52 // during delta generation, this should be safe to do.
53 for (uint64_t i = src_extent.num_blocks(); i > 0; i--) {
54 auto src_block = src_extent.start_block() + i - 1;
55 auto dst_block = dst_extent.start_block() + i - 1;
56 converted.push_back({CowOperation::CowCopy, src_block, dst_block});
57 modified_extents.AddBlock(dst_block);
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040058 }
59 }
60 // COW_REPLACE are added after COW_COPY, because replace might modify blocks
61 // needed by COW_COPY. Please don't merge this loop with the previous one.
62 for (const auto& operation : operations) {
63 if (operation.type() != InstallOperation::SOURCE_COPY) {
64 continue;
65 }
66 const auto& src_extents = operation.src_extents();
67 const auto& dst_extents = operation.dst_extents();
68 BlockIterator it1{src_extents};
69 BlockIterator it2{dst_extents};
70 while (!it1.is_end() && !it2.is_end()) {
71 auto src_block = *it1;
72 auto dst_block = *it2;
73 if (!merge_extents.ContainsBlock(dst_block)) {
74 converted.push_back({CowOperation::CowReplace, src_block, dst_block});
75 }
76 ++it1;
77 ++it2;
78 }
79 }
80 return converted;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040081}
82} // namespace chromeos_update_engine