| 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 | } | 
|  | 98 | } | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 99 |  | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 100 | bool VABCPartitionWriter::Init(const InstallPlan* install_plan, | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 101 | bool source_may_exist, | 
|  | 102 | size_t next_op_index) { | 
| Kelvin Zhang | 1c4b981 | 2022-04-06 17:29:00 -0700 | [diff] [blame] | 103 | if (dynamic_control_->GetVirtualAbCompressionXorFeatureFlag().IsEnabled()) { | 
|  | 104 | xor_map_ = ComputeXorMap(partition_update_.merge_operations()); | 
|  | 105 | if (xor_map_.size() > 0) { | 
|  | 106 | LOG(INFO) << "Virtual AB Compression with XOR is enabled"; | 
|  | 107 | } else { | 
|  | 108 | LOG(INFO) << "Device supports Virtual AB compression with XOR, but OTA " | 
|  | 109 | "package does not."; | 
|  | 110 | } | 
|  | 111 | } else { | 
|  | 112 | LOG(INFO) << "Virtual AB Compression with XOR is disabled."; | 
|  | 113 | } | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 114 | TEST_AND_RETURN_FALSE(install_plan != nullptr); | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 115 | if (source_may_exist && install_part_.source_size > 0) { | 
|  | 116 | TEST_AND_RETURN_FALSE(!install_part_.source_path.empty()); | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 117 | TEST_AND_RETURN_FALSE(verified_source_fd_.Open()); | 
|  | 118 | } | 
| Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 119 | std::optional<std::string> source_path; | 
|  | 120 | if (!install_part_.source_path.empty()) { | 
|  | 121 | // TODO(zhangkelvin) Make |source_path| a std::optional<std::string> | 
|  | 122 | source_path = install_part_.source_path; | 
|  | 123 | } | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 124 | cow_writer_ = dynamic_control_->OpenCowWriter( | 
| Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 125 | install_part_.name, source_path, install_plan->is_resume); | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 126 | TEST_AND_RETURN_FALSE(cow_writer_ != nullptr); | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 127 |  | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 128 | // ===== Resume case handling code goes here ==== | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 129 | // It is possible that the SOURCE_COPY are already written but | 
|  | 130 | // |next_op_index_| is still 0. In this case we discard previously written | 
|  | 131 | // SOURCE_COPY, and start over. | 
|  | 132 | if (install_plan->is_resume && next_op_index > 0) { | 
|  | 133 | LOG(INFO) << "Resuming update on partition `" | 
|  | 134 | << partition_update_.partition_name() << "` op index " | 
|  | 135 | << next_op_index; | 
|  | 136 | TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op_index)); | 
|  | 137 | return true; | 
|  | 138 | } else { | 
|  | 139 | TEST_AND_RETURN_FALSE(cow_writer_->Initialize()); | 
|  | 140 | } | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 141 |  | 
|  | 142 | // ============================================== | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 143 | if (!partition_update_.merge_operations().empty()) { | 
| Kelvin Zhang | 5d74b72 | 2021-09-29 15:24:26 -0700 | [diff] [blame] | 144 | if (IsXorEnabled()) { | 
|  | 145 | LOG(INFO) << "VABC XOR enabled for partition " | 
|  | 146 | << partition_update_.partition_name(); | 
|  | 147 | TEST_AND_RETURN_FALSE(WriteMergeSequence( | 
|  | 148 | partition_update_.merge_operations(), cow_writer_.get())); | 
|  | 149 | } | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 150 | const bool userSnapshots = android::base::GetBoolProperty( | 
|  | 151 | "ro.virtual_ab.userspace.snapshots.enabled", false); | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 152 |  | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 153 | for (const auto& cow_op : partition_update_.merge_operations()) { | 
|  | 154 | if (cow_op.type() != CowMergeOperation::COW_COPY) { | 
|  | 155 | continue; | 
|  | 156 | } | 
|  | 157 | if (cow_op.dst_extent() == cow_op.src_extent()) { | 
|  | 158 | continue; | 
|  | 159 | } | 
|  | 160 | if (userSnapshots) { | 
|  | 161 | TEST_AND_RETURN_FALSE(cow_op.src_extent().num_blocks() != 0); | 
|  | 162 | TEST_AND_RETURN_FALSE( | 
|  | 163 | cow_writer_->AddCopy(cow_op.dst_extent().start_block(), | 
|  | 164 | cow_op.src_extent().start_block(), | 
|  | 165 | cow_op.src_extent().num_blocks())); | 
|  | 166 | } else { | 
|  | 167 | // Add blocks in reverse order, because snapused specifically prefers | 
|  | 168 | // this ordering. Since we already eliminated all self-overlapping | 
|  | 169 | // SOURCE_COPY during delta generation, this should be safe to do. | 
|  | 170 | for (size_t i = cow_op.src_extent().num_blocks(); i > 0; i--) { | 
|  | 171 | TEST_AND_RETURN_FALSE( | 
|  | 172 | cow_writer_->AddCopy(cow_op.dst_extent().start_block() + i - 1, | 
|  | 173 | cow_op.src_extent().start_block() + i - 1)); | 
|  | 174 | } | 
|  | 175 | } | 
|  | 176 | } | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 177 | cow_writer_->AddLabel(0); | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 178 | } | 
| Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 179 | return true; | 
|  | 180 | } | 
|  | 181 |  | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 182 | bool VABCPartitionWriter::WriteMergeSequence( | 
|  | 183 | const RepeatedPtrField<CowMergeOperation>& merge_sequence, | 
|  | 184 | ICowWriter* cow_writer) { | 
|  | 185 | std::vector<uint32_t> blocks_merge_order; | 
|  | 186 | for (const auto& merge_op : merge_sequence) { | 
|  | 187 | const auto& dst_extent = merge_op.dst_extent(); | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 188 | const auto& src_extent = merge_op.src_extent(); | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 189 | // In place copy are basically noops, they do not need to be "merged" at | 
|  | 190 | // all, don't include them in merge sequence. | 
|  | 191 | if (merge_op.type() == CowMergeOperation::COW_COPY && | 
|  | 192 | merge_op.src_extent() == merge_op.dst_extent()) { | 
|  | 193 | continue; | 
|  | 194 | } | 
| Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 195 |  | 
|  | 196 | const bool extent_overlap = | 
|  | 197 | ExtentRanges::ExtentsOverlap(src_extent, dst_extent); | 
|  | 198 | // TODO(193863443) Remove this check once this feature | 
|  | 199 | // lands on all pixel devices. | 
|  | 200 | const bool is_ascending = android::base::GetBoolProperty( | 
|  | 201 | "ro.virtual_ab.userspace.snapshots.enabled", false); | 
|  | 202 |  | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 203 | // If this is a self-overlapping op and |dst_extent| comes after | 
|  | 204 | // |src_extent|, we must write in reverse order for correctness. | 
| Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 205 | // | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 206 | // If this is self-overlapping op and |dst_extent| comes before | 
|  | 207 | // |src_extent|, we must write in ascending order for correctness. | 
| Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 208 | // | 
|  | 209 | // If this isn't a self overlapping op, write block in ascending order | 
|  | 210 | // if userspace snapshots are enabled | 
|  | 211 | if (extent_overlap) { | 
|  | 212 | if (dst_extent.start_block() <= src_extent.start_block()) { | 
|  | 213 | for (size_t i = 0; i < dst_extent.num_blocks(); i++) { | 
|  | 214 | blocks_merge_order.push_back(dst_extent.start_block() + i); | 
|  | 215 | } | 
|  | 216 | } else { | 
|  | 217 | for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) { | 
|  | 218 | blocks_merge_order.push_back(dst_extent.start_block() + i); | 
|  | 219 | } | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 220 | } | 
|  | 221 | } else { | 
| Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 222 | if (is_ascending) { | 
|  | 223 | for (size_t i = 0; i < dst_extent.num_blocks(); i++) { | 
|  | 224 | blocks_merge_order.push_back(dst_extent.start_block() + i); | 
|  | 225 | } | 
|  | 226 | } else { | 
|  | 227 | for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) { | 
|  | 228 | blocks_merge_order.push_back(dst_extent.start_block() + i); | 
|  | 229 | } | 
| Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 230 | } | 
| Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 231 | } | 
|  | 232 | } | 
|  | 233 | return cow_writer->AddSequenceData(blocks_merge_order.size(), | 
|  | 234 | blocks_merge_order.data()); | 
|  | 235 | } | 
|  | 236 |  | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 237 | std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() { | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 238 | return std::make_unique<SnapshotExtentWriter>(cow_writer_.get()); | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
|  | 241 | [[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation( | 
|  | 242 | const InstallOperation& operation) { | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 243 | for (const auto& extent : operation.dst_extents()) { | 
|  | 244 | TEST_AND_RETURN_FALSE( | 
|  | 245 | cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks())); | 
|  | 246 | } | 
|  | 247 | return true; | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 248 | } | 
|  | 249 |  | 
|  | 250 | [[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation( | 
|  | 251 | const InstallOperation& operation, ErrorCode* error) { | 
| Kelvin Zhang | b5bd075 | 2022-03-10 15:45:25 -0800 | [diff] [blame] | 252 | // COPY ops are already handled during Init(), no need to do actual work, but | 
|  | 253 | // we still want to verify that all blocks contain expected data. | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 254 | auto source_fd = verified_source_fd_.ChooseSourceFD(operation, error); | 
|  | 255 | TEST_AND_RETURN_FALSE(source_fd != nullptr); | 
|  | 256 | std::vector<CowOperation> converted; | 
|  | 257 |  | 
|  | 258 | const auto& src_extents = operation.src_extents(); | 
|  | 259 | const auto& dst_extents = operation.dst_extents(); | 
|  | 260 | BlockIterator it1{src_extents}; | 
|  | 261 | BlockIterator it2{dst_extents}; | 
|  | 262 | while (!it1.is_end() && !it2.is_end()) { | 
|  | 263 | const auto src_block = *it1; | 
|  | 264 | const auto dst_block = *it2; | 
|  | 265 | ++it1; | 
|  | 266 | ++it2; | 
|  | 267 | if (src_block == dst_block) { | 
|  | 268 | continue; | 
|  | 269 | } | 
|  | 270 | if (!copy_blocks_.ContainsBlock(dst_block)) { | 
|  | 271 | push_back(&converted, | 
|  | 272 | {CowOperation::CowReplace, src_block, dst_block, 1}); | 
|  | 273 | } | 
| Kelvin Zhang | dc122bc | 2022-03-15 14:19:04 -0700 | [diff] [blame] | 274 | } | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 275 | std::vector<uint8_t> buffer; | 
|  | 276 | for (const auto& cow_op : converted) { | 
|  | 277 | buffer.resize(block_size_ * cow_op.block_count); | 
|  | 278 | ssize_t bytes_read = 0; | 
|  | 279 | TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd, | 
|  | 280 | buffer.data(), | 
|  | 281 | block_size_ * cow_op.block_count, | 
|  | 282 | cow_op.src_block * block_size_, | 
|  | 283 | &bytes_read)); | 
|  | 284 | if (bytes_read <= 0 || static_cast<size_t>(bytes_read) != buffer.size()) { | 
|  | 285 | LOG(ERROR) << "source_fd->Read failed: " << bytes_read; | 
|  | 286 | return false; | 
|  | 287 | } | 
|  | 288 | TEST_AND_RETURN_FALSE(cow_writer_->AddRawBlocks( | 
|  | 289 | cow_op.dst_block, buffer.data(), buffer.size())); | 
|  | 290 | } | 
|  | 291 | return true; | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 292 | } | 
|  | 293 |  | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 294 | bool VABCPartitionWriter::PerformReplaceOperation(const InstallOperation& op, | 
|  | 295 | const void* data, | 
|  | 296 | size_t count) { | 
|  | 297 | // Setup the ExtentWriter stack based on the operation type. | 
|  | 298 | std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter(); | 
|  | 299 |  | 
|  | 300 | return executor_.ExecuteReplaceOperation(op, std::move(writer), data, count); | 
|  | 301 | } | 
|  | 302 |  | 
| Tianjie | 8e0090d | 2021-08-30 22:35:21 -0700 | [diff] [blame] | 303 | bool VABCPartitionWriter::PerformDiffOperation( | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 304 | const InstallOperation& operation, | 
|  | 305 | ErrorCode* error, | 
|  | 306 | const void* data, | 
|  | 307 | size_t count) { | 
|  | 308 | FileDescriptorPtr source_fd = | 
|  | 309 | verified_source_fd_.ChooseSourceFD(operation, error); | 
|  | 310 | TEST_AND_RETURN_FALSE(source_fd != nullptr); | 
| Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 311 | TEST_AND_RETURN_FALSE(source_fd->IsOpen()); | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 312 |  | 
| Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 313 | std::unique_ptr<ExtentWriter> writer = | 
|  | 314 | IsXorEnabled() ? std::make_unique<XORExtentWriter>( | 
|  | 315 | operation, source_fd, cow_writer_.get(), xor_map_) | 
|  | 316 | : CreateBaseExtentWriter(); | 
| Tianjie | 8e0090d | 2021-08-30 22:35:21 -0700 | [diff] [blame] | 317 | return executor_.ExecuteDiffOperation( | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 318 | operation, std::move(writer), source_fd, data, count); | 
|  | 319 | } | 
|  | 320 |  | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 321 | void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) { | 
|  | 322 | // No need to call fsync/sync, as CowWriter flushes after a label is added | 
|  | 323 | // added. | 
| Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 324 | // if cow_writer_ failed, that means Init() failed. This function shouldn't be | 
|  | 325 | // called if Init() fails. | 
|  | 326 | TEST_AND_RETURN(cow_writer_ != nullptr); | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 327 | cow_writer_->AddLabel(next_op_index); | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 328 | } | 
|  | 329 |  | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 330 | [[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() { | 
|  | 331 | // Add a hardcoded magic label to indicate end of all install ops. This label | 
|  | 332 | // is needed by filesystem verification, don't remove. | 
| Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 333 | TEST_AND_RETURN_FALSE(cow_writer_ != nullptr); | 
| Kelvin Zhang | 9e5e1ed | 2021-09-28 14:19:16 -0700 | [diff] [blame] | 334 | TEST_AND_RETURN_FALSE(cow_writer_->AddLabel(kEndOfInstallLabel)); | 
|  | 335 | TEST_AND_RETURN_FALSE(cow_writer_->Finalize()); | 
| Kelvin Zhang | 02fe662 | 2021-11-01 16:37:58 -0700 | [diff] [blame] | 336 | TEST_AND_RETURN_FALSE(cow_writer_->VerifyMergeOps()); | 
|  | 337 | return true; | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 338 | } | 
|  | 339 |  | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 340 | VABCPartitionWriter::~VABCPartitionWriter() { | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 341 | Close(); | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | int VABCPartitionWriter::Close() { | 
| Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 345 | if (cow_writer_) { | 
| Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 346 | LOG(INFO) << "Finalizing " << partition_update_.partition_name() | 
|  | 347 | << " COW image"; | 
| Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 348 | cow_writer_->Finalize(); | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 349 | cow_writer_ = nullptr; | 
| Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 350 | } | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 351 | return 0; | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 352 | } | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 353 |  | 
|  | 354 | }  // namespace chromeos_update_engine |