| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -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/payload_consumer/vabc_partition_writer.h" | 
|  | 18 |  | 
| Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 19 | #include <algorithm> | 
|  | 20 | #include <map> | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 21 | #include <memory> | 
| Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 22 | #include <string> | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 23 | #include <utility> | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 24 | #include <vector> | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 25 |  | 
| Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 26 | #include <android-base/properties.h> | 
| Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 27 | #include <brillo/secure_blob.h> | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 28 | #include <libsnapshot/cow_writer.h> | 
|  | 29 |  | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 30 | #include "update_engine/common/cow_operation_convert.h" | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 31 | #include "update_engine/common/utils.h" | 
| Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 32 | #include "update_engine/payload_consumer/extent_map.h" | 
| Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 33 | #include "update_engine/payload_consumer/file_descriptor.h" | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 34 | #include "update_engine/payload_consumer/install_plan.h" | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 35 | #include "update_engine/payload_consumer/snapshot_extent_writer.h" | 
| Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 36 | #include "update_engine/payload_consumer/xor_extent_writer.h" | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 37 | #include "update_engine/payload_generator/extent_ranges.h" | 
|  | 38 | #include "update_engine/payload_generator/extent_utils.h" | 
|  | 39 | #include "update_engine/update_metadata.pb.h" | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 40 |  | 
|  | 41 | namespace chromeos_update_engine { | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 42 | // Expected layout of COW file: | 
|  | 43 | // === Beginning of Cow Image === | 
|  | 44 | // All Source Copy Operations | 
|  | 45 | // ========== Label 0 ========== | 
|  | 46 | // Operation 0 in PartitionUpdate | 
|  | 47 | // ========== Label 1 ========== | 
|  | 48 | // Operation 1 in PartitionUpdate | 
|  | 49 | // ========== label 2 ========== | 
|  | 50 | // Operation 2 in PartitionUpdate | 
|  | 51 | // ========== label 3 ========== | 
|  | 52 | // . | 
|  | 53 | // . | 
|  | 54 | // . | 
|  | 55 |  | 
|  | 56 | // When resuming, pass |next_op_index_| as label to | 
|  | 57 | // |InitializeWithAppend|. | 
|  | 58 | // For example, suppose we finished writing SOURCE_COPY, and we finished writing | 
|  | 59 | // operation 2 completely. Update is suspended when we are half way through | 
|  | 60 | // operation 3. | 
|  | 61 | // |cnext_op_index_| would be 3, so we pass 3 as | 
|  | 62 | // label to |InitializeWithAppend|. The CowWriter will retain all data before | 
|  | 63 | // label 3, Which contains all operation 2's data, but none of operation 3's | 
|  | 64 | // data. | 
|  | 65 |  | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 66 | using android::snapshot::ICowWriter; | 
|  | 67 | using ::google::protobuf::RepeatedPtrField; | 
|  | 68 |  | 
| Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 69 | // Compute XOR map, a map from dst extent to corresponding merge operation | 
|  | 70 | static ExtentMap<const CowMergeOperation*, ExtentLess> ComputeXorMap( | 
|  | 71 | const RepeatedPtrField<CowMergeOperation>& merge_ops) { | 
|  | 72 | ExtentMap<const CowMergeOperation*, ExtentLess> xor_map; | 
|  | 73 | for (const auto& merge_op : merge_ops) { | 
|  | 74 | if (merge_op.type() == CowMergeOperation::COW_XOR) { | 
|  | 75 | xor_map.AddExtent(merge_op.dst_extent(), &merge_op); | 
|  | 76 | } | 
|  | 77 | } | 
|  | 78 | return xor_map; | 
|  | 79 | } | 
|  | 80 |  | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 81 | VABCPartitionWriter::VABCPartitionWriter( | 
|  | 82 | const PartitionUpdate& partition_update, | 
|  | 83 | const InstallPlan::Partition& install_part, | 
|  | 84 | DynamicPartitionControlInterface* dynamic_control, | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 85 | size_t block_size) | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 86 | : partition_update_(partition_update), | 
|  | 87 | install_part_(install_part), | 
|  | 88 | dynamic_control_(dynamic_control), | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 89 | block_size_(block_size), | 
|  | 90 | executor_(block_size), | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 91 | verified_source_fd_(block_size, install_part.source_path) { | 
|  | 92 | for (const auto& cow_op : partition_update_.merge_operations()) { | 
|  | 93 | if (cow_op.type() != CowMergeOperation::COW_COPY) { | 
|  | 94 | continue; | 
|  | 95 | } | 
|  | 96 | copy_blocks_.AddExtent(cow_op.dst_extent()); | 
|  | 97 | } | 
| Kelvin Zhang | 7dd5a5e | 2023-05-11 15:30:38 -0700 | [diff] [blame] | 98 | LOG(INFO) << "Partition `" << partition_update.partition_name() << "` has " | 
| Kelvin Zhang | 67144e5 | 2023-04-03 18:17:07 -0700 | [diff] [blame] | 99 | << copy_blocks_.blocks() << " copy blocks"; | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 100 | } | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 101 |  | 
| Kelvin Zhang | d795875 | 2023-03-23 12:23:47 -0700 | [diff] [blame] | 102 | bool VABCPartitionWriter::DoesDeviceSupportsXor() { | 
|  | 103 | return dynamic_control_->GetVirtualAbCompressionXorFeatureFlag().IsEnabled(); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | bool VABCPartitionWriter::WriteAllCopyOps() { | 
|  | 107 | const bool userSnapshots = android::base::GetBoolProperty( | 
|  | 108 | "ro.virtual_ab.userspace.snapshots.enabled", false); | 
|  | 109 | for (const auto& cow_op : partition_update_.merge_operations()) { | 
|  | 110 | if (cow_op.type() != CowMergeOperation::COW_COPY) { | 
|  | 111 | continue; | 
|  | 112 | } | 
|  | 113 | if (cow_op.dst_extent() == cow_op.src_extent()) { | 
|  | 114 | continue; | 
|  | 115 | } | 
|  | 116 | if (userSnapshots) { | 
|  | 117 | TEST_AND_RETURN_FALSE(cow_op.src_extent().num_blocks() != 0); | 
|  | 118 | TEST_AND_RETURN_FALSE( | 
|  | 119 | cow_writer_->AddCopy(cow_op.dst_extent().start_block(), | 
|  | 120 | cow_op.src_extent().start_block(), | 
|  | 121 | cow_op.src_extent().num_blocks())); | 
|  | 122 | } else { | 
|  | 123 | // Add blocks in reverse order, because snapused specifically prefers | 
|  | 124 | // this ordering. Since we already eliminated all self-overlapping | 
|  | 125 | // SOURCE_COPY during delta generation, this should be safe to do. | 
|  | 126 | for (size_t i = cow_op.src_extent().num_blocks(); i > 0; i--) { | 
|  | 127 | TEST_AND_RETURN_FALSE( | 
|  | 128 | cow_writer_->AddCopy(cow_op.dst_extent().start_block() + i - 1, | 
|  | 129 | cow_op.src_extent().start_block() + i - 1)); | 
|  | 130 | } | 
|  | 131 | } | 
|  | 132 | } | 
|  | 133 | return true; | 
|  | 134 | } | 
|  | 135 |  | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 136 | bool VABCPartitionWriter::Init(const InstallPlan* install_plan, | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 137 | bool source_may_exist, | 
|  | 138 | size_t next_op_index) { | 
| Kelvin Zhang | 1c4b981 | 2022-04-06 17:29:00 -0700 | [diff] [blame] | 139 | if (dynamic_control_->GetVirtualAbCompressionXorFeatureFlag().IsEnabled()) { | 
|  | 140 | xor_map_ = ComputeXorMap(partition_update_.merge_operations()); | 
|  | 141 | if (xor_map_.size() > 0) { | 
|  | 142 | LOG(INFO) << "Virtual AB Compression with XOR is enabled"; | 
|  | 143 | } else { | 
|  | 144 | LOG(INFO) << "Device supports Virtual AB compression with XOR, but OTA " | 
|  | 145 | "package does not."; | 
|  | 146 | } | 
|  | 147 | } else { | 
|  | 148 | LOG(INFO) << "Virtual AB Compression with XOR is disabled."; | 
|  | 149 | } | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 150 | TEST_AND_RETURN_FALSE(install_plan != nullptr); | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 151 | if (source_may_exist && install_part_.source_size > 0) { | 
|  | 152 | TEST_AND_RETURN_FALSE(!install_part_.source_path.empty()); | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 153 | TEST_AND_RETURN_FALSE(verified_source_fd_.Open()); | 
|  | 154 | } | 
| Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 155 | std::optional<std::string> source_path; | 
|  | 156 | if (!install_part_.source_path.empty()) { | 
|  | 157 | // TODO(zhangkelvin) Make |source_path| a std::optional<std::string> | 
|  | 158 | source_path = install_part_.source_path; | 
|  | 159 | } | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 160 |  | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 161 | // ===== Resume case handling code goes here ==== | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 162 | // It is possible that the SOURCE_COPY are already written but | 
|  | 163 | // |next_op_index_| is still 0. In this case we discard previously written | 
|  | 164 | // SOURCE_COPY, and start over. | 
| David Anderson | a4b7ba6 | 2023-05-10 21:41:37 -0700 | [diff] [blame] | 165 | std::optional<uint64_t> label; | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 166 | if (install_plan->is_resume && next_op_index > 0) { | 
|  | 167 | LOG(INFO) << "Resuming update on partition `" | 
|  | 168 | << partition_update_.partition_name() << "` op index " | 
|  | 169 | << next_op_index; | 
| David Anderson | a4b7ba6 | 2023-05-10 21:41:37 -0700 | [diff] [blame] | 170 | label = {next_op_index}; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | cow_writer_ = | 
|  | 174 | dynamic_control_->OpenCowWriter(install_part_.name, source_path, label); | 
|  | 175 | TEST_AND_RETURN_FALSE(cow_writer_ != nullptr); | 
|  | 176 |  | 
|  | 177 | if (label) { | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 178 | return true; | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 179 | } | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 180 |  | 
|  | 181 | // ============================================== | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 182 | if (!partition_update_.merge_operations().empty()) { | 
| Kelvin Zhang | 5d74b72 | 2021-09-29 15:24:26 -0700 | [diff] [blame] | 183 | if (IsXorEnabled()) { | 
|  | 184 | LOG(INFO) << "VABC XOR enabled for partition " | 
|  | 185 | << partition_update_.partition_name(); | 
| Kelvin Zhang | d795875 | 2023-03-23 12:23:47 -0700 | [diff] [blame] | 186 | } | 
|  | 187 | // When merge sequence is present in COW, snapuserd will merge blocks in | 
|  | 188 | // order specified by the merge seuqnece op. Hence we have the freedom of | 
|  | 189 | // writing COPY operations out of order. Delay processing of copy ops so | 
|  | 190 | // that update_engine can be more responsive in progress updates. | 
|  | 191 | if (DoesDeviceSupportsXor()) { | 
|  | 192 | LOG(INFO) << "Snapuserd supports XOR and merge sequence, writing merge " | 
|  | 193 | "sequence and delay writing COPY operations"; | 
| Kelvin Zhang | 5d74b72 | 2021-09-29 15:24:26 -0700 | [diff] [blame] | 194 | TEST_AND_RETURN_FALSE(WriteMergeSequence( | 
|  | 195 | partition_update_.merge_operations(), cow_writer_.get())); | 
| Kelvin Zhang | d795875 | 2023-03-23 12:23:47 -0700 | [diff] [blame] | 196 | } else { | 
|  | 197 | LOG(INFO) << "Snapuserd does not support merge sequence, writing all " | 
|  | 198 | "COPY operations up front, this may take few " | 
|  | 199 | "minutes."; | 
|  | 200 | TEST_AND_RETURN_FALSE(WriteAllCopyOps()); | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 201 | } | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 202 | cow_writer_->AddLabel(0); | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 203 | } | 
| Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 204 | return true; | 
|  | 205 | } | 
|  | 206 |  | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 207 | bool VABCPartitionWriter::WriteMergeSequence( | 
|  | 208 | const RepeatedPtrField<CowMergeOperation>& merge_sequence, | 
|  | 209 | ICowWriter* cow_writer) { | 
|  | 210 | std::vector<uint32_t> blocks_merge_order; | 
|  | 211 | for (const auto& merge_op : merge_sequence) { | 
|  | 212 | const auto& dst_extent = merge_op.dst_extent(); | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 213 | const auto& src_extent = merge_op.src_extent(); | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 214 | // In place copy are basically noops, they do not need to be "merged" at | 
|  | 215 | // all, don't include them in merge sequence. | 
|  | 216 | if (merge_op.type() == CowMergeOperation::COW_COPY && | 
|  | 217 | merge_op.src_extent() == merge_op.dst_extent()) { | 
|  | 218 | continue; | 
|  | 219 | } | 
| Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 220 |  | 
|  | 221 | const bool extent_overlap = | 
|  | 222 | ExtentRanges::ExtentsOverlap(src_extent, dst_extent); | 
|  | 223 | // TODO(193863443) Remove this check once this feature | 
|  | 224 | // lands on all pixel devices. | 
|  | 225 | const bool is_ascending = android::base::GetBoolProperty( | 
|  | 226 | "ro.virtual_ab.userspace.snapshots.enabled", false); | 
|  | 227 |  | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 228 | // If this is a self-overlapping op and |dst_extent| comes after | 
|  | 229 | // |src_extent|, we must write in reverse order for correctness. | 
| Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 230 | // | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 231 | // If this is self-overlapping op and |dst_extent| comes before | 
|  | 232 | // |src_extent|, we must write in ascending order for correctness. | 
| Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 233 | // | 
|  | 234 | // If this isn't a self overlapping op, write block in ascending order | 
|  | 235 | // if userspace snapshots are enabled | 
|  | 236 | if (extent_overlap) { | 
|  | 237 | if (dst_extent.start_block() <= src_extent.start_block()) { | 
|  | 238 | for (size_t i = 0; i < dst_extent.num_blocks(); i++) { | 
|  | 239 | blocks_merge_order.push_back(dst_extent.start_block() + i); | 
|  | 240 | } | 
|  | 241 | } else { | 
|  | 242 | for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) { | 
|  | 243 | blocks_merge_order.push_back(dst_extent.start_block() + i); | 
|  | 244 | } | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 245 | } | 
|  | 246 | } else { | 
| Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 247 | if (is_ascending) { | 
|  | 248 | for (size_t i = 0; i < dst_extent.num_blocks(); i++) { | 
|  | 249 | blocks_merge_order.push_back(dst_extent.start_block() + i); | 
|  | 250 | } | 
|  | 251 | } else { | 
|  | 252 | for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) { | 
|  | 253 | blocks_merge_order.push_back(dst_extent.start_block() + i); | 
|  | 254 | } | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 255 | } | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 256 | } | 
|  | 257 | } | 
|  | 258 | return cow_writer->AddSequenceData(blocks_merge_order.size(), | 
|  | 259 | blocks_merge_order.data()); | 
|  | 260 | } | 
|  | 261 |  | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 262 | std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() { | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 263 | return std::make_unique<SnapshotExtentWriter>(cow_writer_.get()); | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 264 | } | 
|  | 265 |  | 
|  | 266 | [[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation( | 
|  | 267 | const InstallOperation& operation) { | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 268 | for (const auto& extent : operation.dst_extents()) { | 
|  | 269 | TEST_AND_RETURN_FALSE( | 
|  | 270 | cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks())); | 
|  | 271 | } | 
|  | 272 | return true; | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 273 | } | 
|  | 274 |  | 
|  | 275 | [[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation( | 
|  | 276 | const InstallOperation& operation, ErrorCode* error) { | 
| Kelvin Zhang | b5bd075 | 2022-03-10 15:45:25 -0800 | [diff] [blame] | 277 | // COPY ops are already handled during Init(), no need to do actual work, but | 
|  | 278 | // we still want to verify that all blocks contain expected data. | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 279 | auto source_fd = verified_source_fd_.ChooseSourceFD(operation, error); | 
|  | 280 | TEST_AND_RETURN_FALSE(source_fd != nullptr); | 
|  | 281 | std::vector<CowOperation> converted; | 
|  | 282 |  | 
|  | 283 | const auto& src_extents = operation.src_extents(); | 
|  | 284 | const auto& dst_extents = operation.dst_extents(); | 
|  | 285 | BlockIterator it1{src_extents}; | 
|  | 286 | BlockIterator it2{dst_extents}; | 
| Kelvin Zhang | d795875 | 2023-03-23 12:23:47 -0700 | [diff] [blame] | 287 | const bool userSnapshots = android::base::GetBoolProperty( | 
|  | 288 | "ro.virtual_ab.userspace.snapshots.enabled", false); | 
| Kelvin Zhang | 67144e5 | 2023-04-03 18:17:07 -0700 | [diff] [blame] | 289 | // For devices not supporting XOR, sequence op is not supported, so all COPY | 
|  | 290 | // operations are written up front in strict merge order. | 
|  | 291 | const auto sequence_op_supported = DoesDeviceSupportsXor(); | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 292 | while (!it1.is_end() && !it2.is_end()) { | 
|  | 293 | const auto src_block = *it1; | 
|  | 294 | const auto dst_block = *it2; | 
|  | 295 | ++it1; | 
|  | 296 | ++it2; | 
|  | 297 | if (src_block == dst_block) { | 
|  | 298 | continue; | 
|  | 299 | } | 
| Kelvin Zhang | d795875 | 2023-03-23 12:23:47 -0700 | [diff] [blame] | 300 | if (copy_blocks_.ContainsBlock(dst_block)) { | 
| Kelvin Zhang | 67144e5 | 2023-04-03 18:17:07 -0700 | [diff] [blame] | 301 | if (sequence_op_supported) { | 
|  | 302 | push_back(&converted, {CowOperation::CowCopy, src_block, dst_block, 1}); | 
|  | 303 | } | 
| Kelvin Zhang | d795875 | 2023-03-23 12:23:47 -0700 | [diff] [blame] | 304 | } else { | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 305 | push_back(&converted, | 
|  | 306 | {CowOperation::CowReplace, src_block, dst_block, 1}); | 
|  | 307 | } | 
| Kelvin Zhang | dc122bc | 2022-03-15 14:19:04 -0700 | [diff] [blame] | 308 | } | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 309 | std::vector<uint8_t> buffer; | 
|  | 310 | for (const auto& cow_op : converted) { | 
| Kelvin Zhang | d795875 | 2023-03-23 12:23:47 -0700 | [diff] [blame] | 311 | if (cow_op.op == CowOperation::CowCopy) { | 
|  | 312 | if (userSnapshots) { | 
|  | 313 | cow_writer_->AddCopy( | 
|  | 314 | cow_op.dst_block, cow_op.src_block, cow_op.block_count); | 
|  | 315 | } else { | 
|  | 316 | // Add blocks in reverse order, because snapused specifically prefers | 
|  | 317 | // this ordering. Since we already eliminated all self-overlapping | 
|  | 318 | // SOURCE_COPY during delta generation, this should be safe to do. | 
|  | 319 | for (size_t i = cow_op.block_count; i > 0; i--) { | 
|  | 320 | TEST_AND_RETURN_FALSE(cow_writer_->AddCopy(cow_op.dst_block + i - 1, | 
|  | 321 | cow_op.src_block + i - 1)); | 
|  | 322 | } | 
|  | 323 | } | 
|  | 324 | continue; | 
|  | 325 | } | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 326 | buffer.resize(block_size_ * cow_op.block_count); | 
|  | 327 | ssize_t bytes_read = 0; | 
|  | 328 | TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd, | 
|  | 329 | buffer.data(), | 
|  | 330 | block_size_ * cow_op.block_count, | 
|  | 331 | cow_op.src_block * block_size_, | 
|  | 332 | &bytes_read)); | 
|  | 333 | if (bytes_read <= 0 || static_cast<size_t>(bytes_read) != buffer.size()) { | 
|  | 334 | LOG(ERROR) << "source_fd->Read failed: " << bytes_read; | 
|  | 335 | return false; | 
|  | 336 | } | 
|  | 337 | TEST_AND_RETURN_FALSE(cow_writer_->AddRawBlocks( | 
|  | 338 | cow_op.dst_block, buffer.data(), buffer.size())); | 
|  | 339 | } | 
|  | 340 | return true; | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 341 | } | 
|  | 342 |  | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 343 | bool VABCPartitionWriter::PerformReplaceOperation(const InstallOperation& op, | 
|  | 344 | const void* data, | 
|  | 345 | size_t count) { | 
|  | 346 | // Setup the ExtentWriter stack based on the operation type. | 
|  | 347 | std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter(); | 
|  | 348 |  | 
|  | 349 | return executor_.ExecuteReplaceOperation(op, std::move(writer), data, count); | 
|  | 350 | } | 
|  | 351 |  | 
| Tianjie | 8e0090d | 2021-08-30 22:35:21 -0700 | [diff] [blame] | 352 | bool VABCPartitionWriter::PerformDiffOperation( | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 353 | const InstallOperation& operation, | 
|  | 354 | ErrorCode* error, | 
|  | 355 | const void* data, | 
|  | 356 | size_t count) { | 
|  | 357 | FileDescriptorPtr source_fd = | 
|  | 358 | verified_source_fd_.ChooseSourceFD(operation, error); | 
|  | 359 | TEST_AND_RETURN_FALSE(source_fd != nullptr); | 
| Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 360 | TEST_AND_RETURN_FALSE(source_fd->IsOpen()); | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 361 |  | 
| Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 362 | std::unique_ptr<ExtentWriter> writer = | 
|  | 363 | IsXorEnabled() ? std::make_unique<XORExtentWriter>( | 
| Kelvin Zhang | a3a68a9 | 2023-04-05 13:17:18 -0700 | [diff] [blame] | 364 | operation, | 
|  | 365 | source_fd, | 
|  | 366 | cow_writer_.get(), | 
|  | 367 | xor_map_, | 
|  | 368 | partition_update_.old_partition_info().size()) | 
| Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 369 | : CreateBaseExtentWriter(); | 
| Tianjie | 8e0090d | 2021-08-30 22:35:21 -0700 | [diff] [blame] | 370 | return executor_.ExecuteDiffOperation( | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 371 | operation, std::move(writer), source_fd, data, count); | 
|  | 372 | } | 
|  | 373 |  | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 374 | void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) { | 
|  | 375 | // No need to call fsync/sync, as CowWriter flushes after a label is added | 
|  | 376 | // added. | 
| Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 377 | // if cow_writer_ failed, that means Init() failed. This function shouldn't be | 
|  | 378 | // called if Init() fails. | 
|  | 379 | TEST_AND_RETURN(cow_writer_ != nullptr); | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 380 | cow_writer_->AddLabel(next_op_index); | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 381 | } | 
|  | 382 |  | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 383 | [[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() { | 
|  | 384 | // Add a hardcoded magic label to indicate end of all install ops. This label | 
|  | 385 | // is needed by filesystem verification, don't remove. | 
| Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 386 | TEST_AND_RETURN_FALSE(cow_writer_ != nullptr); | 
| Kelvin Zhang | 9e5e1ed | 2021-09-28 14:19:16 -0700 | [diff] [blame] | 387 | TEST_AND_RETURN_FALSE(cow_writer_->AddLabel(kEndOfInstallLabel)); | 
|  | 388 | TEST_AND_RETURN_FALSE(cow_writer_->Finalize()); | 
| David Anderson | a4b7ba6 | 2023-05-10 21:41:37 -0700 | [diff] [blame] | 389 |  | 
|  | 390 | auto cow_reader = cow_writer_->OpenReader(); | 
|  | 391 | TEST_AND_RETURN_FALSE(cow_reader); | 
|  | 392 | TEST_AND_RETURN_FALSE(cow_reader->VerifyMergeOps()); | 
| Kelvin Zhang | 02fe662 | 2021-11-01 16:37:58 -0700 | [diff] [blame] | 393 | return true; | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 394 | } | 
|  | 395 |  | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 396 | VABCPartitionWriter::~VABCPartitionWriter() { | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 397 | Close(); | 
|  | 398 | } | 
|  | 399 |  | 
|  | 400 | int VABCPartitionWriter::Close() { | 
| Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 401 | if (cow_writer_) { | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 402 | LOG(INFO) << "Finalizing " << partition_update_.partition_name() | 
|  | 403 | << " COW image"; | 
| Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 404 | cow_writer_->Finalize(); | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 405 | cow_writer_ = nullptr; | 
| Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 406 | } | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 407 | return 0; | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 408 | } | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 409 |  | 
|  | 410 | }  // namespace chromeos_update_engine |