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 | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 20 | #include <vector> |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 21 | |
| 22 | #include <libsnapshot/cow_writer.h> |
| 23 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 24 | #include "update_engine/common/cow_operation_convert.h" |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 25 | #include "update_engine/common/utils.h" |
| 26 | #include "update_engine/payload_consumer/extent_writer.h" |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 27 | #include "update_engine/payload_consumer/file_descriptor.h" |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 28 | #include "update_engine/payload_consumer/install_plan.h" |
| 29 | #include "update_engine/payload_consumer/partition_writer.h" |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 30 | #include "update_engine/payload_consumer/snapshot_extent_writer.h" |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 31 | |
| 32 | namespace chromeos_update_engine { |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 33 | // Expected layout of COW file: |
| 34 | // === Beginning of Cow Image === |
| 35 | // All Source Copy Operations |
| 36 | // ========== Label 0 ========== |
| 37 | // Operation 0 in PartitionUpdate |
| 38 | // ========== Label 1 ========== |
| 39 | // Operation 1 in PartitionUpdate |
| 40 | // ========== label 2 ========== |
| 41 | // Operation 2 in PartitionUpdate |
| 42 | // ========== label 3 ========== |
| 43 | // . |
| 44 | // . |
| 45 | // . |
| 46 | |
| 47 | // When resuming, pass |kPrefsUpdateStatePartitionNextOperation| as label to |
| 48 | // |InitializeWithAppend|. |
| 49 | // For example, suppose we finished writing SOURCE_COPY, and we finished writing |
| 50 | // operation 2 completely. Update is suspended when we are half way through |
| 51 | // operation 3. |
| 52 | // |kPrefsUpdateStatePartitionNextOperation| would be 3, so we pass 3 as |
| 53 | // label to |InitializeWithAppend|. The CowWriter will retain all data before |
| 54 | // label 3, Which contains all operation 2's data, but none of operation 3's |
| 55 | // data. |
| 56 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 57 | bool VABCPartitionWriter::Init(const InstallPlan* install_plan, |
| 58 | bool source_may_exist) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 59 | TEST_AND_RETURN_FALSE(install_plan != nullptr); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 60 | TEST_AND_RETURN_FALSE(PartitionWriter::Init(install_plan, source_may_exist)); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 61 | cow_writer_ = dynamic_control_->OpenCowWriter( |
| 62 | install_part_.name, install_part_.source_path, install_plan->is_resume); |
| 63 | TEST_AND_RETURN_FALSE(cow_writer_ != nullptr); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 64 | |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 65 | // Emit a label before writing SOURCE_COPY. When resuming, |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 66 | // use pref or CowWriter::GetLastLabel to determine if the SOURCE_COPY ops are |
| 67 | // written. No need to handle SOURCE_COPY operations when resuming. |
| 68 | |
| 69 | // ===== Resume case handling code goes here ==== |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 70 | if (install_plan->is_resume) { |
| 71 | int64_t next_op = 0; |
| 72 | if (!prefs_->GetInt64(kPrefsUpdateStatePartitionNextOperation, &next_op)) { |
| 73 | LOG(ERROR) |
| 74 | << "Resuming an update but can't fetch |next_op| from saved prefs."; |
| 75 | return false; |
| 76 | } |
| 77 | if (next_op < 0) { |
| 78 | TEST_AND_RETURN_FALSE(cow_writer_->Initialize()); |
| 79 | } else { |
| 80 | TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op)); |
| 81 | return true; |
| 82 | } |
| 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 | // ============================================== |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 88 | TEST_AND_RETURN_FALSE( |
| 89 | prefs_->SetInt64(kPrefsUpdateStatePartitionNextOperation, -1)); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 90 | |
| 91 | // TODO(zhangkelvin) Rewrite this in C++20 coroutine once that's available. |
| 92 | auto converted = ConvertToCowOperations(partition_update_.operations(), |
| 93 | partition_update_.merge_operations()); |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 94 | |
| 95 | WriteAllCowOps(block_size_, converted, cow_writer_.get(), source_fd_); |
| 96 | // Emit label 0 to mark end of all SOURCE_COPY operations |
| 97 | cow_writer_->AddLabel(0); |
| 98 | TEST_AND_RETURN_FALSE( |
| 99 | prefs_->SetInt64(kPrefsUpdateStatePartitionNextOperation, 0)); |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | bool VABCPartitionWriter::WriteAllCowOps( |
| 104 | size_t block_size, |
| 105 | const std::vector<CowOperation>& converted, |
| 106 | android::snapshot::ICowWriter* cow_writer, |
| 107 | FileDescriptorPtr source_fd) { |
| 108 | std::vector<uint8_t> buffer(block_size); |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 109 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 110 | for (const auto& cow_op : converted) { |
| 111 | switch (cow_op.op) { |
| 112 | case CowOperation::CowCopy: |
| 113 | TEST_AND_RETURN_FALSE( |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 114 | cow_writer->AddCopy(cow_op.dst_block, cow_op.src_block)); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 115 | break; |
| 116 | case CowOperation::CowReplace: |
| 117 | ssize_t bytes_read = 0; |
Kelvin Zhang | 4b28024 | 2020-11-06 16:07:45 -0500 | [diff] [blame] | 118 | TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd, |
| 119 | buffer.data(), |
| 120 | block_size, |
| 121 | cow_op.src_block * block_size, |
| 122 | &bytes_read)); |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 123 | if (bytes_read <= 0 || static_cast<size_t>(bytes_read) != block_size) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 124 | LOG(ERROR) << "source_fd->Read failed: " << bytes_read; |
| 125 | return false; |
| 126 | } |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 127 | TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks( |
| 128 | cow_op.dst_block, buffer.data(), block_size)); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 129 | break; |
| 130 | } |
| 131 | } |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
| 135 | std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 136 | return std::make_unique<SnapshotExtentWriter>(cow_writer_.get()); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | [[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation( |
| 140 | const InstallOperation& operation) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 141 | for (const auto& extent : operation.dst_extents()) { |
| 142 | TEST_AND_RETURN_FALSE( |
| 143 | cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks())); |
| 144 | } |
| 145 | return true; |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | [[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation( |
| 149 | const InstallOperation& operation, ErrorCode* error) { |
| 150 | // TODO(zhangkelvin) Probably just ignore SOURCE_COPY? They should be taken |
| 151 | // care of during Init(); |
| 152 | return true; |
| 153 | } |
| 154 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 155 | bool VABCPartitionWriter::Flush() { |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 156 | // No need to call fsync/sync, as CowWriter flushes after a label is added |
| 157 | // added. |
| 158 | int64_t next_op = 0; |
| 159 | // |kPrefsUpdateStatePartitionNextOperation| will be maintained and set by |
| 160 | // CheckpointUpdateProgress() |
| 161 | TEST_AND_RETURN_FALSE( |
| 162 | prefs_->GetInt64(kPrefsUpdateStatePartitionNextOperation, &next_op)); |
| 163 | // +1 because label 0 is reserved for SOURCE_COPY. See beginning of this |
| 164 | // file for explanation for cow format. |
| 165 | cow_writer_->AddLabel(next_op + 1); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 169 | void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) { |
| 170 | prefs_->SetInt64(kPrefsUpdateStatePartitionNextOperation, next_op_index); |
| 171 | } |
| 172 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 173 | VABCPartitionWriter::~VABCPartitionWriter() { |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 174 | // Reset |kPrefsUpdateStatePartitionNextOperation| once we finished a |
| 175 | // partition. |
| 176 | prefs_->SetInt64(kPrefsUpdateStatePartitionNextOperation, -1); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 177 | cow_writer_->Finalize(); |
| 178 | } |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 179 | |
| 180 | } // namespace chromeos_update_engine |