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 { |
| 34 | bool VABCPartitionWriter::Init(const InstallPlan* install_plan, |
| 35 | bool source_may_exist) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 36 | TEST_AND_RETURN_FALSE(install_plan != nullptr); |
Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 37 | TEST_AND_RETURN_FALSE( |
| 38 | OpenSourcePartition(install_plan->source_slot, source_may_exist)); |
| 39 | std::optional<std::string> source_path; |
| 40 | if (!install_part_.source_path.empty()) { |
| 41 | // TODO(zhangkelvin) Make |source_path| a std::optional<std::string> |
| 42 | source_path = install_part_.source_path; |
| 43 | } |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 44 | cow_writer_ = dynamic_control_->OpenCowWriter( |
Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 45 | install_part_.name, source_path, install_plan->is_resume); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 46 | TEST_AND_RETURN_FALSE(cow_writer_ != nullptr); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 47 | |
Kelvin Zhang | 59928f1 | 2020-11-11 21:21:27 +0000 | [diff] [blame] | 48 | // TODO(zhangkelvin) Emit a label before writing SOURCE_COPY. When resuming, |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 49 | // use pref or CowWriter::GetLastLabel to determine if the SOURCE_COPY ops are |
| 50 | // written. No need to handle SOURCE_COPY operations when resuming. |
| 51 | |
| 52 | // ===== Resume case handling code goes here ==== |
| 53 | |
| 54 | // ============================================== |
| 55 | |
| 56 | // TODO(zhangkelvin) Rewrite this in C++20 coroutine once that's available. |
| 57 | auto converted = ConvertToCowOperations(partition_update_.operations(), |
| 58 | partition_update_.merge_operations()); |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 59 | |
| 60 | WriteAllCowOps(block_size_, converted, cow_writer_.get(), source_fd_); |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | |
| 64 | bool VABCPartitionWriter::WriteAllCowOps( |
| 65 | size_t block_size, |
| 66 | const std::vector<CowOperation>& converted, |
| 67 | android::snapshot::ICowWriter* cow_writer, |
| 68 | FileDescriptorPtr source_fd) { |
| 69 | std::vector<uint8_t> buffer(block_size); |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 70 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 71 | for (const auto& cow_op : converted) { |
| 72 | switch (cow_op.op) { |
| 73 | case CowOperation::CowCopy: |
| 74 | TEST_AND_RETURN_FALSE( |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 75 | cow_writer->AddCopy(cow_op.dst_block, cow_op.src_block)); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 76 | break; |
| 77 | case CowOperation::CowReplace: |
| 78 | ssize_t bytes_read = 0; |
Kelvin Zhang | 4b28024 | 2020-11-06 16:07:45 -0500 | [diff] [blame] | 79 | TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd, |
| 80 | buffer.data(), |
| 81 | block_size, |
| 82 | cow_op.src_block * block_size, |
| 83 | &bytes_read)); |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 84 | if (bytes_read <= 0 || static_cast<size_t>(bytes_read) != block_size) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 85 | LOG(ERROR) << "source_fd->Read failed: " << bytes_read; |
| 86 | return false; |
| 87 | } |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 88 | TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks( |
| 89 | cow_op.dst_block, buffer.data(), block_size)); |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 90 | break; |
| 91 | } |
| 92 | } |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
| 96 | std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 97 | return std::make_unique<SnapshotExtentWriter>(cow_writer_.get()); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | [[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation( |
| 101 | const InstallOperation& operation) { |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 102 | for (const auto& extent : operation.dst_extents()) { |
| 103 | TEST_AND_RETURN_FALSE( |
| 104 | cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks())); |
| 105 | } |
| 106 | return true; |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | [[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation( |
| 110 | const InstallOperation& operation, ErrorCode* error) { |
| 111 | // TODO(zhangkelvin) Probably just ignore SOURCE_COPY? They should be taken |
| 112 | // care of during Init(); |
| 113 | return true; |
| 114 | } |
| 115 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 116 | bool VABCPartitionWriter::Flush() { |
Kelvin Zhang | 59928f1 | 2020-11-11 21:21:27 +0000 | [diff] [blame] | 117 | // No need to do anything, as CowWriter automatically flushes every OP added. |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 121 | [[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() { |
| 122 | // Add a hardcoded magic label to indicate end of all install ops. This label |
| 123 | // is needed by filesystem verification, don't remove. |
| 124 | return cow_writer_->AddLabel(kEndOfInstallLabel); |
| 125 | } |
| 126 | |
Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 127 | VABCPartitionWriter::~VABCPartitionWriter() { |
| 128 | cow_writer_->Finalize(); |
| 129 | } |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 130 | |
| 131 | } // namespace chromeos_update_engine |