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