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 | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 21 | #include <vector> |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 22 | |
| 23 | #include <libsnapshot/cow_writer.h> |
| 24 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 25 | #include "update_engine/common/cow_operation_convert.h" |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 26 | #include "update_engine/common/utils.h" |
| 27 | #include "update_engine/payload_consumer/extent_writer.h" |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 28 | #include "update_engine/payload_consumer/file_descriptor.h" |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 29 | #include "update_engine/payload_consumer/install_plan.h" |
| 30 | #include "update_engine/payload_consumer/partition_writer.h" |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 31 | #include "update_engine/payload_consumer/snapshot_extent_writer.h" |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 32 | |
| 33 | namespace chromeos_update_engine { |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 34 | // Expected layout of COW file: |
| 35 | // === Beginning of Cow Image === |
| 36 | // All Source Copy Operations |
| 37 | // ========== Label 0 ========== |
| 38 | // Operation 0 in PartitionUpdate |
| 39 | // ========== Label 1 ========== |
| 40 | // Operation 1 in PartitionUpdate |
| 41 | // ========== label 2 ========== |
| 42 | // Operation 2 in PartitionUpdate |
| 43 | // ========== label 3 ========== |
| 44 | // . |
| 45 | // . |
| 46 | // . |
| 47 | |
| 48 | // When resuming, pass |next_op_index_| as label to |
| 49 | // |InitializeWithAppend|. |
| 50 | // For example, suppose we finished writing SOURCE_COPY, and we finished writing |
| 51 | // operation 2 completely. Update is suspended when we are half way through |
| 52 | // operation 3. |
| 53 | // |cnext_op_index_| would be 3, so we pass 3 as |
| 54 | // label to |InitializeWithAppend|. The CowWriter will retain all data before |
| 55 | // label 3, Which contains all operation 2's data, but none of operation 3's |
| 56 | // data. |
| 57 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 58 | bool VABCPartitionWriter::Init(const InstallPlan* install_plan, |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 59 | bool source_may_exist, |
| 60 | size_t next_op_index) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 61 | TEST_AND_RETURN_FALSE(install_plan != nullptr); |
Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 62 | TEST_AND_RETURN_FALSE( |
| 63 | OpenSourcePartition(install_plan->source_slot, source_may_exist)); |
| 64 | std::optional<std::string> source_path; |
| 65 | if (!install_part_.source_path.empty()) { |
| 66 | // TODO(zhangkelvin) Make |source_path| a std::optional<std::string> |
| 67 | source_path = install_part_.source_path; |
| 68 | } |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 69 | cow_writer_ = dynamic_control_->OpenCowWriter( |
Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 70 | install_part_.name, source_path, install_plan->is_resume); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 71 | TEST_AND_RETURN_FALSE(cow_writer_ != nullptr); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 72 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 73 | // ===== Resume case handling code goes here ==== |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 74 | // It is possible that the SOURCE_COPY are already written but |
| 75 | // |next_op_index_| is still 0. In this case we discard previously written |
| 76 | // SOURCE_COPY, and start over. |
| 77 | if (install_plan->is_resume && next_op_index > 0) { |
| 78 | LOG(INFO) << "Resuming update on partition `" |
| 79 | << partition_update_.partition_name() << "` op index " |
| 80 | << next_op_index; |
| 81 | TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op_index)); |
| 82 | return true; |
| 83 | } else { |
| 84 | TEST_AND_RETURN_FALSE(cow_writer_->Initialize()); |
| 85 | } |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 86 | |
| 87 | // ============================================== |
| 88 | |
| 89 | // TODO(zhangkelvin) Rewrite this in C++20 coroutine once that's available. |
| 90 | auto converted = ConvertToCowOperations(partition_update_.operations(), |
| 91 | partition_update_.merge_operations()); |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 92 | |
Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame^] | 93 | if (!converted.empty()) { |
| 94 | // Use source fd directly. Ideally we want to verify all extents used in |
| 95 | // source copy, but then what do we do if some extents contain correct |
| 96 | // hashes and some don't? |
| 97 | auto source_fd = std::make_shared<EintrSafeFileDescriptor>(); |
| 98 | TEST_AND_RETURN_FALSE_ERRNO( |
| 99 | source_fd->Open(install_part_.source_path.c_str(), O_RDONLY)); |
| 100 | WriteAllCowOps(block_size_, converted, cow_writer_.get(), source_fd); |
| 101 | } |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 102 | return true; |
| 103 | } |
| 104 | |
| 105 | bool VABCPartitionWriter::WriteAllCowOps( |
| 106 | size_t block_size, |
| 107 | const std::vector<CowOperation>& converted, |
| 108 | android::snapshot::ICowWriter* cow_writer, |
| 109 | FileDescriptorPtr source_fd) { |
| 110 | std::vector<uint8_t> buffer(block_size); |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 111 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 112 | for (const auto& cow_op : converted) { |
| 113 | switch (cow_op.op) { |
| 114 | case CowOperation::CowCopy: |
Kelvin Zhang | 4430ea5 | 2021-02-26 13:35:34 -0500 | [diff] [blame] | 115 | if (cow_op.src_block == cow_op.dst_block) { |
| 116 | continue; |
| 117 | } |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 118 | TEST_AND_RETURN_FALSE( |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 119 | cow_writer->AddCopy(cow_op.dst_block, cow_op.src_block)); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 120 | break; |
| 121 | case CowOperation::CowReplace: |
| 122 | ssize_t bytes_read = 0; |
Kelvin Zhang | 4b28024 | 2020-11-06 16:07:45 -0500 | [diff] [blame] | 123 | TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd, |
| 124 | buffer.data(), |
| 125 | block_size, |
| 126 | cow_op.src_block * block_size, |
| 127 | &bytes_read)); |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 128 | if (bytes_read <= 0 || static_cast<size_t>(bytes_read) != block_size) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 129 | LOG(ERROR) << "source_fd->Read failed: " << bytes_read; |
| 130 | return false; |
| 131 | } |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 132 | TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks( |
| 133 | cow_op.dst_block, buffer.data(), block_size)); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | } |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 137 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
| 141 | std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 142 | return std::make_unique<SnapshotExtentWriter>(cow_writer_.get()); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | [[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation( |
| 146 | const InstallOperation& operation) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 147 | for (const auto& extent : operation.dst_extents()) { |
| 148 | TEST_AND_RETURN_FALSE( |
| 149 | cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks())); |
| 150 | } |
| 151 | return true; |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | [[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation( |
| 155 | const InstallOperation& operation, ErrorCode* error) { |
| 156 | // TODO(zhangkelvin) Probably just ignore SOURCE_COPY? They should be taken |
| 157 | // care of during Init(); |
| 158 | return true; |
| 159 | } |
| 160 | |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 161 | void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) { |
| 162 | // No need to call fsync/sync, as CowWriter flushes after a label is added |
| 163 | // added. |
Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 164 | // if cow_writer_ failed, that means Init() failed. This function shouldn't be |
| 165 | // called if Init() fails. |
| 166 | TEST_AND_RETURN(cow_writer_ != nullptr); |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 167 | cow_writer_->AddLabel(next_op_index); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 168 | } |
| 169 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 170 | [[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() { |
| 171 | // Add a hardcoded magic label to indicate end of all install ops. This label |
| 172 | // is needed by filesystem verification, don't remove. |
Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 173 | TEST_AND_RETURN_FALSE(cow_writer_ != nullptr); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 174 | return cow_writer_->AddLabel(kEndOfInstallLabel); |
| 175 | } |
| 176 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 177 | VABCPartitionWriter::~VABCPartitionWriter() { |
Kelvin Zhang | 6a4d1ec | 2021-02-04 16:28:48 -0500 | [diff] [blame] | 178 | if (cow_writer_) { |
| 179 | cow_writer_->Finalize(); |
| 180 | } |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 181 | } |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 182 | |
| 183 | } // namespace chromeos_update_engine |