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