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