blob: 980f2ca0bc0a2c4b5061456652eea53da24211a1 [file] [log] [blame]
Kelvin Zhang94f51cc2020-09-25 11:34:49 -04001//
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 Zhang9b10dba2020-09-25 17:09:11 -040020#include <vector>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040021
22#include <libsnapshot/cow_writer.h>
23
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040024#include "update_engine/common/cow_operation_convert.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040025#include "update_engine/common/utils.h"
26#include "update_engine/payload_consumer/extent_writer.h"
Kelvin Zhang7a265752020-10-29 15:51:35 -040027#include "update_engine/payload_consumer/file_descriptor.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040028#include "update_engine/payload_consumer/install_plan.h"
29#include "update_engine/payload_consumer/partition_writer.h"
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040030#include "update_engine/payload_consumer/snapshot_extent_writer.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040031
32namespace chromeos_update_engine {
33bool VABCPartitionWriter::Init(const InstallPlan* install_plan,
34 bool source_may_exist) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040035 TEST_AND_RETURN_FALSE(install_plan != nullptr);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040036 TEST_AND_RETURN_FALSE(PartitionWriter::Init(install_plan, source_may_exist));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040037 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 Zhang94f51cc2020-09-25 11:34:49 -040040
Kelvin Zhang59928f12020-11-11 21:21:27 +000041 // TODO(zhangkelvin) Emit a label before writing SOURCE_COPY. When resuming,
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040042 // 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 Zhang7a265752020-10-29 15:51:35 -040052
53 WriteAllCowOps(block_size_, converted, cow_writer_.get(), source_fd_);
Kelvin Zhang7a265752020-10-29 15:51:35 -040054 return true;
55}
56
57bool 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 Zhang24599af2020-10-27 13:44:25 -040063
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040064 for (const auto& cow_op : converted) {
65 switch (cow_op.op) {
66 case CowOperation::CowCopy:
67 TEST_AND_RETURN_FALSE(
Kelvin Zhang7a265752020-10-29 15:51:35 -040068 cow_writer->AddCopy(cow_op.dst_block, cow_op.src_block));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040069 break;
70 case CowOperation::CowReplace:
71 ssize_t bytes_read = 0;
Kelvin Zhang4b280242020-11-06 16:07:45 -050072 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 Zhang7a265752020-10-29 15:51:35 -040077 if (bytes_read <= 0 || static_cast<size_t>(bytes_read) != block_size) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040078 LOG(ERROR) << "source_fd->Read failed: " << bytes_read;
79 return false;
80 }
Kelvin Zhang7a265752020-10-29 15:51:35 -040081 TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks(
82 cow_op.dst_block, buffer.data(), block_size));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040083 break;
84 }
85 }
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040086 return true;
87}
88
89std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040090 return std::make_unique<SnapshotExtentWriter>(cow_writer_.get());
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040091}
92
93[[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation(
94 const InstallOperation& operation) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040095 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 Zhang94f51cc2020-09-25 11:34:49 -0400100}
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 Zhang9b10dba2020-09-25 17:09:11 -0400109bool VABCPartitionWriter::Flush() {
Kelvin Zhang59928f12020-11-11 21:21:27 +0000110 // No need to do anything, as CowWriter automatically flushes every OP added.
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400111 return true;
112}
113
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400114[[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() {
115 // Add a hardcoded magic label to indicate end of all install ops. This label
116 // is needed by filesystem verification, don't remove.
117 return cow_writer_->AddLabel(kEndOfInstallLabel);
118}
119
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400120VABCPartitionWriter::~VABCPartitionWriter() {
121 cow_writer_->Finalize();
122}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400123
124} // namespace chromeos_update_engine