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/block_extent_writer.h" |
| 33 | #include "update_engine/payload_consumer/extent_map.h" |
| 34 | #include "update_engine/payload_consumer/extent_reader.h" |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 35 | #include "update_engine/payload_consumer/file_descriptor.h" |
Kelvin Zhang | b5bd075 | 2022-03-10 15:45:25 -0800 | [diff] [blame^] | 36 | #include "update_engine/payload_consumer/file_descriptor_utils.h" |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 37 | #include "update_engine/payload_consumer/install_plan.h" |
| 38 | #include "update_engine/payload_consumer/partition_writer.h" |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 39 | #include "update_engine/payload_consumer/snapshot_extent_writer.h" |
Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 40 | #include "update_engine/payload_consumer/xor_extent_writer.h" |
Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 41 | #include "update_engine/payload_generator/extent_ranges.h" |
| 42 | #include "update_engine/payload_generator/extent_utils.h" |
| 43 | #include "update_engine/update_metadata.pb.h" |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 44 | |
| 45 | namespace chromeos_update_engine { |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 46 | // Expected layout of COW file: |
| 47 | // === Beginning of Cow Image === |
| 48 | // All Source Copy Operations |
| 49 | // ========== Label 0 ========== |
| 50 | // Operation 0 in PartitionUpdate |
| 51 | // ========== Label 1 ========== |
| 52 | // Operation 1 in PartitionUpdate |
| 53 | // ========== label 2 ========== |
| 54 | // Operation 2 in PartitionUpdate |
| 55 | // ========== label 3 ========== |
| 56 | // . |
| 57 | // . |
| 58 | // . |
| 59 | |
| 60 | // When resuming, pass |next_op_index_| as label to |
| 61 | // |InitializeWithAppend|. |
| 62 | // For example, suppose we finished writing SOURCE_COPY, and we finished writing |
| 63 | // operation 2 completely. Update is suspended when we are half way through |
| 64 | // operation 3. |
| 65 | // |cnext_op_index_| would be 3, so we pass 3 as |
| 66 | // label to |InitializeWithAppend|. The CowWriter will retain all data before |
| 67 | // label 3, Which contains all operation 2's data, but none of operation 3's |
| 68 | // data. |
| 69 | |
Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 70 | using android::snapshot::ICowWriter; |
| 71 | using ::google::protobuf::RepeatedPtrField; |
| 72 | |
Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 73 | // Compute XOR map, a map from dst extent to corresponding merge operation |
| 74 | static ExtentMap<const CowMergeOperation*, ExtentLess> ComputeXorMap( |
| 75 | const RepeatedPtrField<CowMergeOperation>& merge_ops) { |
| 76 | ExtentMap<const CowMergeOperation*, ExtentLess> xor_map; |
| 77 | for (const auto& merge_op : merge_ops) { |
| 78 | if (merge_op.type() == CowMergeOperation::COW_XOR) { |
| 79 | xor_map.AddExtent(merge_op.dst_extent(), &merge_op); |
| 80 | } |
| 81 | } |
| 82 | return xor_map; |
| 83 | } |
| 84 | |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 85 | VABCPartitionWriter::VABCPartitionWriter( |
| 86 | const PartitionUpdate& partition_update, |
| 87 | const InstallPlan::Partition& install_part, |
| 88 | DynamicPartitionControlInterface* dynamic_control, |
Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 89 | size_t block_size) |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 90 | : partition_update_(partition_update), |
| 91 | install_part_(install_part), |
| 92 | dynamic_control_(dynamic_control), |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 93 | block_size_(block_size), |
| 94 | executor_(block_size), |
| 95 | verified_source_fd_(block_size, install_part.source_path) {} |
| 96 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 97 | bool VABCPartitionWriter::Init(const InstallPlan* install_plan, |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 98 | bool source_may_exist, |
| 99 | size_t next_op_index) { |
Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 100 | xor_map_ = ComputeXorMap(partition_update_.merge_operations()); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 101 | TEST_AND_RETURN_FALSE(install_plan != nullptr); |
Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 102 | if (source_may_exist && install_part_.source_size > 0) { |
| 103 | TEST_AND_RETURN_FALSE(!install_part_.source_path.empty()); |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 104 | TEST_AND_RETURN_FALSE(verified_source_fd_.Open()); |
| 105 | } |
Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 106 | std::optional<std::string> source_path; |
| 107 | if (!install_part_.source_path.empty()) { |
| 108 | // TODO(zhangkelvin) Make |source_path| a std::optional<std::string> |
| 109 | source_path = install_part_.source_path; |
| 110 | } |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 111 | cow_writer_ = dynamic_control_->OpenCowWriter( |
Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 112 | install_part_.name, source_path, install_plan->is_resume); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 113 | TEST_AND_RETURN_FALSE(cow_writer_ != nullptr); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 114 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 115 | // ===== Resume case handling code goes here ==== |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 116 | // It is possible that the SOURCE_COPY are already written but |
| 117 | // |next_op_index_| is still 0. In this case we discard previously written |
| 118 | // SOURCE_COPY, and start over. |
| 119 | if (install_plan->is_resume && next_op_index > 0) { |
| 120 | LOG(INFO) << "Resuming update on partition `" |
| 121 | << partition_update_.partition_name() << "` op index " |
| 122 | << next_op_index; |
| 123 | TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op_index)); |
| 124 | return true; |
| 125 | } else { |
| 126 | TEST_AND_RETURN_FALSE(cow_writer_->Initialize()); |
| 127 | } |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 128 | |
| 129 | // ============================================== |
Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 130 | if (!partition_update_.merge_operations().empty()) { |
Kelvin Zhang | 5d74b72 | 2021-09-29 15:24:26 -0700 | [diff] [blame] | 131 | if (IsXorEnabled()) { |
| 132 | LOG(INFO) << "VABC XOR enabled for partition " |
| 133 | << partition_update_.partition_name(); |
| 134 | TEST_AND_RETURN_FALSE(WriteMergeSequence( |
| 135 | partition_update_.merge_operations(), cow_writer_.get())); |
| 136 | } |
Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 137 | } |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 138 | |
| 139 | // TODO(zhangkelvin) Rewrite this in C++20 coroutine once that's available. |
Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 140 | // TODO(177104308) Don't write all COPY ops up-front if merge sequence is |
| 141 | // written |
Kelvin Zhang | 5d74b72 | 2021-09-29 15:24:26 -0700 | [diff] [blame] | 142 | const auto converted = ConvertToCowOperations( |
| 143 | partition_update_.operations(), partition_update_.merge_operations()); |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 144 | |
Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 145 | if (!converted.empty()) { |
| 146 | // Use source fd directly. Ideally we want to verify all extents used in |
| 147 | // source copy, but then what do we do if some extents contain correct |
| 148 | // hashes and some don't? |
| 149 | auto source_fd = std::make_shared<EintrSafeFileDescriptor>(); |
| 150 | TEST_AND_RETURN_FALSE_ERRNO( |
| 151 | source_fd->Open(install_part_.source_path.c_str(), O_RDONLY)); |
Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 152 | TEST_AND_RETURN_FALSE(WriteSourceCopyCowOps( |
| 153 | block_size_, converted, cow_writer_.get(), source_fd)); |
Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 154 | cow_writer_->AddLabel(0); |
Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 155 | } |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 156 | return true; |
| 157 | } |
| 158 | |
Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 159 | bool VABCPartitionWriter::WriteMergeSequence( |
| 160 | const RepeatedPtrField<CowMergeOperation>& merge_sequence, |
| 161 | ICowWriter* cow_writer) { |
| 162 | std::vector<uint32_t> blocks_merge_order; |
| 163 | for (const auto& merge_op : merge_sequence) { |
| 164 | const auto& dst_extent = merge_op.dst_extent(); |
Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 165 | const auto& src_extent = merge_op.src_extent(); |
Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 166 | // In place copy are basically noops, they do not need to be "merged" at |
| 167 | // all, don't include them in merge sequence. |
| 168 | if (merge_op.type() == CowMergeOperation::COW_COPY && |
| 169 | merge_op.src_extent() == merge_op.dst_extent()) { |
| 170 | continue; |
| 171 | } |
Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 172 | |
| 173 | const bool extent_overlap = |
| 174 | ExtentRanges::ExtentsOverlap(src_extent, dst_extent); |
| 175 | // TODO(193863443) Remove this check once this feature |
| 176 | // lands on all pixel devices. |
| 177 | const bool is_ascending = android::base::GetBoolProperty( |
| 178 | "ro.virtual_ab.userspace.snapshots.enabled", false); |
| 179 | |
Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 180 | // If this is a self-overlapping op and |dst_extent| comes after |
| 181 | // |src_extent|, we must write in reverse order for correctness. |
Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 182 | // |
Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 183 | // If this is self-overlapping op and |dst_extent| comes before |
| 184 | // |src_extent|, we must write in ascending order for correctness. |
Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 185 | // |
| 186 | // If this isn't a self overlapping op, write block in ascending order |
| 187 | // if userspace snapshots are enabled |
| 188 | if (extent_overlap) { |
| 189 | if (dst_extent.start_block() <= src_extent.start_block()) { |
| 190 | for (size_t i = 0; i < dst_extent.num_blocks(); i++) { |
| 191 | blocks_merge_order.push_back(dst_extent.start_block() + i); |
| 192 | } |
| 193 | } else { |
| 194 | for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) { |
| 195 | blocks_merge_order.push_back(dst_extent.start_block() + i); |
| 196 | } |
Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 197 | } |
| 198 | } else { |
Akilesh Kailash | 3e6e7df | 2021-11-18 23:29:15 +0000 | [diff] [blame] | 199 | if (is_ascending) { |
| 200 | for (size_t i = 0; i < dst_extent.num_blocks(); i++) { |
| 201 | blocks_merge_order.push_back(dst_extent.start_block() + i); |
| 202 | } |
| 203 | } else { |
| 204 | for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) { |
| 205 | blocks_merge_order.push_back(dst_extent.start_block() + i); |
| 206 | } |
Kelvin Zhang | d1f90bc | 2021-09-15 21:12:34 -0700 | [diff] [blame] | 207 | } |
Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | return cow_writer->AddSequenceData(blocks_merge_order.size(), |
| 211 | blocks_merge_order.data()); |
| 212 | } |
| 213 | |
Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 214 | bool VABCPartitionWriter::WriteSourceCopyCowOps( |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 215 | size_t block_size, |
| 216 | const std::vector<CowOperation>& converted, |
Kelvin Zhang | a37aafc | 2021-06-14 13:21:37 -0400 | [diff] [blame] | 217 | ICowWriter* cow_writer, |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 218 | FileDescriptorPtr source_fd) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 219 | for (const auto& cow_op : converted) { |
Kelvin Zhang | c14676a | 2021-10-28 16:38:20 -0700 | [diff] [blame] | 220 | std::vector<uint8_t> buffer; |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 221 | switch (cow_op.op) { |
| 222 | case CowOperation::CowCopy: |
Kelvin Zhang | 4430ea5 | 2021-02-26 13:35:34 -0500 | [diff] [blame] | 223 | if (cow_op.src_block == cow_op.dst_block) { |
| 224 | continue; |
| 225 | } |
Kelvin Zhang | c14676a | 2021-10-28 16:38:20 -0700 | [diff] [blame] | 226 | // Add blocks in reverse order, because snapused specifically prefers |
| 227 | // this ordering. Since we already eliminated all self-overlapping |
| 228 | // SOURCE_COPY during delta generation, this should be safe to do. |
| 229 | for (size_t i = cow_op.block_count; i > 0; i--) { |
| 230 | TEST_AND_RETURN_FALSE(cow_writer->AddCopy(cow_op.dst_block + i - 1, |
| 231 | cow_op.src_block + i - 1)); |
| 232 | } |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 233 | break; |
| 234 | case CowOperation::CowReplace: |
Kelvin Zhang | c14676a | 2021-10-28 16:38:20 -0700 | [diff] [blame] | 235 | buffer.resize(block_size * cow_op.block_count); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 236 | ssize_t bytes_read = 0; |
Kelvin Zhang | 4b28024 | 2020-11-06 16:07:45 -0500 | [diff] [blame] | 237 | TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd, |
| 238 | buffer.data(), |
Kelvin Zhang | c14676a | 2021-10-28 16:38:20 -0700 | [diff] [blame] | 239 | block_size * cow_op.block_count, |
Kelvin Zhang | 4b28024 | 2020-11-06 16:07:45 -0500 | [diff] [blame] | 240 | cow_op.src_block * block_size, |
| 241 | &bytes_read)); |
Kelvin Zhang | c14676a | 2021-10-28 16:38:20 -0700 | [diff] [blame] | 242 | if (bytes_read <= 0 || |
| 243 | static_cast<size_t>(bytes_read) != buffer.size()) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 244 | LOG(ERROR) << "source_fd->Read failed: " << bytes_read; |
| 245 | return false; |
| 246 | } |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 247 | TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks( |
Kelvin Zhang | c14676a | 2021-10-28 16:38:20 -0700 | [diff] [blame] | 248 | cow_op.dst_block, buffer.data(), buffer.size())); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 249 | break; |
| 250 | } |
| 251 | } |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 252 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | |
| 256 | std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 257 | return std::make_unique<SnapshotExtentWriter>(cow_writer_.get()); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | [[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation( |
| 261 | const InstallOperation& operation) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 262 | for (const auto& extent : operation.dst_extents()) { |
| 263 | TEST_AND_RETURN_FALSE( |
| 264 | cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks())); |
| 265 | } |
| 266 | return true; |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | [[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation( |
| 270 | const InstallOperation& operation, ErrorCode* error) { |
Kelvin Zhang | b5bd075 | 2022-03-10 15:45:25 -0800 | [diff] [blame^] | 271 | // COPY ops are already handled during Init(), no need to do actual work, but |
| 272 | // we still want to verify that all blocks contain expected data. |
| 273 | auto source_fd = std::make_shared<EintrSafeFileDescriptor>(); |
| 274 | TEST_AND_RETURN_FALSE_ERRNO( |
| 275 | source_fd->Open(install_part_.source_path.c_str(), O_RDONLY)); |
| 276 | return PartitionWriter::ValidateSourceHash( |
| 277 | operation, source_fd, block_size_, error); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 278 | } |
| 279 | |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 280 | bool VABCPartitionWriter::PerformReplaceOperation(const InstallOperation& op, |
| 281 | const void* data, |
| 282 | size_t count) { |
| 283 | // Setup the ExtentWriter stack based on the operation type. |
| 284 | std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter(); |
| 285 | |
| 286 | return executor_.ExecuteReplaceOperation(op, std::move(writer), data, count); |
| 287 | } |
| 288 | |
Tianjie | 8e0090d | 2021-08-30 22:35:21 -0700 | [diff] [blame] | 289 | bool VABCPartitionWriter::PerformDiffOperation( |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 290 | const InstallOperation& operation, |
| 291 | ErrorCode* error, |
| 292 | const void* data, |
| 293 | size_t count) { |
| 294 | FileDescriptorPtr source_fd = |
| 295 | verified_source_fd_.ChooseSourceFD(operation, error); |
| 296 | TEST_AND_RETURN_FALSE(source_fd != nullptr); |
Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 297 | TEST_AND_RETURN_FALSE(source_fd->IsOpen()); |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 298 | |
Kelvin Zhang | 76f10b8 | 2021-06-25 18:45:46 -0400 | [diff] [blame] | 299 | std::unique_ptr<ExtentWriter> writer = |
| 300 | IsXorEnabled() ? std::make_unique<XORExtentWriter>( |
| 301 | operation, source_fd, cow_writer_.get(), xor_map_) |
| 302 | : CreateBaseExtentWriter(); |
Tianjie | 8e0090d | 2021-08-30 22:35:21 -0700 | [diff] [blame] | 303 | return executor_.ExecuteDiffOperation( |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 304 | operation, std::move(writer), source_fd, data, count); |
| 305 | } |
| 306 | |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 307 | void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) { |
| 308 | // No need to call fsync/sync, as CowWriter flushes after a label is added |
| 309 | // added. |
Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 310 | // if cow_writer_ failed, that means Init() failed. This function shouldn't be |
| 311 | // called if Init() fails. |
| 312 | TEST_AND_RETURN(cow_writer_ != nullptr); |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 313 | cow_writer_->AddLabel(next_op_index); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 316 | [[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() { |
| 317 | // Add a hardcoded magic label to indicate end of all install ops. This label |
| 318 | // is needed by filesystem verification, don't remove. |
Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 319 | TEST_AND_RETURN_FALSE(cow_writer_ != nullptr); |
Kelvin Zhang | 9e5e1ed | 2021-09-28 14:19:16 -0700 | [diff] [blame] | 320 | TEST_AND_RETURN_FALSE(cow_writer_->AddLabel(kEndOfInstallLabel)); |
| 321 | TEST_AND_RETURN_FALSE(cow_writer_->Finalize()); |
Kelvin Zhang | 02fe662 | 2021-11-01 16:37:58 -0700 | [diff] [blame] | 322 | TEST_AND_RETURN_FALSE(cow_writer_->VerifyMergeOps()); |
| 323 | return true; |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 324 | } |
| 325 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 326 | VABCPartitionWriter::~VABCPartitionWriter() { |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 327 | Close(); |
| 328 | } |
| 329 | |
| 330 | int VABCPartitionWriter::Close() { |
Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 331 | if (cow_writer_) { |
| 332 | cow_writer_->Finalize(); |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 333 | cow_writer_ = nullptr; |
Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 334 | } |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 335 | return 0; |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 336 | } |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 337 | |
| 338 | } // namespace chromeos_update_engine |