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