Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 1 | // |
| 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 Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 19 | #include <base/logging.h> |
| 20 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 21 | #include "update_engine/payload_generator/extent_ranges.h" |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 22 | #include "update_engine/payload_generator/extent_utils.h" |
Kelvin Zhang | 59724d8 | 2021-02-09 16:40:01 -0500 | [diff] [blame] | 23 | #include "update_engine/update_metadata.pb.h" |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 24 | |
| 25 | namespace chromeos_update_engine { |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 26 | |
Kelvin Zhang | c14676a | 2021-10-28 16:38:20 -0700 | [diff] [blame] | 27 | void push_back(std::vector<CowOperation>* converted, const CowOperation& op) { |
| 28 | if (!converted->empty() && IsConsecutive(converted->back(), op)) { |
Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 29 | converted->back().block_count += op.block_count; |
Kelvin Zhang | c14676a | 2021-10-28 16:38:20 -0700 | [diff] [blame] | 30 | } else { |
| 31 | converted->push_back(op); |
| 32 | } |
| 33 | } |
| 34 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 35 | std::vector<CowOperation> ConvertToCowOperations( |
| 36 | const ::google::protobuf::RepeatedPtrField< |
| 37 | ::chromeos_update_engine::InstallOperation>& operations, |
| 38 | const ::google::protobuf::RepeatedPtrField<CowMergeOperation>& |
| 39 | merge_operations) { |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 40 | 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 Zhang | 59724d8 | 2021-02-09 16:40:01 -0500 | [diff] [blame] | 51 | if (merge_op.type() != CowMergeOperation::COW_COPY) { |
| 52 | continue; |
| 53 | } |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 54 | 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 Zhang | eb8703b | 2020-12-10 14:17:21 -0500 | [diff] [blame] | 57 | // 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 Zhang | c14676a | 2021-10-28 16:38:20 -0700 | [diff] [blame] | 63 | converted.push_back({CowOperation::CowCopy, src_block, dst_block, 1}); |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 64 | } |
| 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 Zhang | c14676a | 2021-10-28 16:38:20 -0700 | [diff] [blame] | 77 | const auto src_block = *it1; |
| 78 | const auto dst_block = *it2; |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 79 | if (!merge_extents.ContainsBlock(dst_block)) { |
Kelvin Zhang | c14676a | 2021-10-28 16:38:20 -0700 | [diff] [blame] | 80 | push_back(&converted, |
| 81 | {CowOperation::CowReplace, src_block, dst_block, 1}); |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 82 | } |
| 83 | ++it1; |
| 84 | ++it2; |
| 85 | } |
| 86 | } |
| 87 | return converted; |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 88 | } |
| 89 | } // namespace chromeos_update_engine |