blob: d181c75a2c1e395f66570cf4d745d6258ec3aaf2 [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 Zhang3f60d532020-11-09 13:33:17 -050020#include <string>
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050021#include <utility>
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040022#include <vector>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040023
24#include <libsnapshot/cow_writer.h>
25
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040026#include "update_engine/common/cow_operation_convert.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040027#include "update_engine/common/utils.h"
28#include "update_engine/payload_consumer/extent_writer.h"
Kelvin Zhang7a265752020-10-29 15:51:35 -040029#include "update_engine/payload_consumer/file_descriptor.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040030#include "update_engine/payload_consumer/install_plan.h"
31#include "update_engine/payload_consumer/partition_writer.h"
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040032#include "update_engine/payload_consumer/snapshot_extent_writer.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040033
34namespace chromeos_update_engine {
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040035// Expected layout of COW file:
36// === Beginning of Cow Image ===
37// All Source Copy Operations
38// ========== Label 0 ==========
39// Operation 0 in PartitionUpdate
40// ========== Label 1 ==========
41// Operation 1 in PartitionUpdate
42// ========== label 2 ==========
43// Operation 2 in PartitionUpdate
44// ========== label 3 ==========
45// .
46// .
47// .
48
49// When resuming, pass |next_op_index_| as label to
50// |InitializeWithAppend|.
51// For example, suppose we finished writing SOURCE_COPY, and we finished writing
52// operation 2 completely. Update is suspended when we are half way through
53// operation 3.
54// |cnext_op_index_| would be 3, so we pass 3 as
55// label to |InitializeWithAppend|. The CowWriter will retain all data before
56// label 3, Which contains all operation 2's data, but none of operation 3's
57// data.
58
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050059VABCPartitionWriter::VABCPartitionWriter(
60 const PartitionUpdate& partition_update,
61 const InstallPlan::Partition& install_part,
62 DynamicPartitionControlInterface* dynamic_control,
63 size_t block_size,
64 bool is_interactive)
65 : partition_update_(partition_update),
66 install_part_(install_part),
67 dynamic_control_(dynamic_control),
68 interactive_(is_interactive),
69 block_size_(block_size),
70 executor_(block_size),
71 verified_source_fd_(block_size, install_part.source_path) {}
72
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040073bool VABCPartitionWriter::Init(const InstallPlan* install_plan,
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040074 bool source_may_exist,
75 size_t next_op_index) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040076 TEST_AND_RETURN_FALSE(install_plan != nullptr);
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050077 if (source_may_exist) {
78 TEST_AND_RETURN_FALSE(verified_source_fd_.Open());
79 }
Kelvin Zhang3f60d532020-11-09 13:33:17 -050080 std::optional<std::string> source_path;
81 if (!install_part_.source_path.empty()) {
82 // TODO(zhangkelvin) Make |source_path| a std::optional<std::string>
83 source_path = install_part_.source_path;
84 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040085 cow_writer_ = dynamic_control_->OpenCowWriter(
Kelvin Zhang3f60d532020-11-09 13:33:17 -050086 install_part_.name, source_path, install_plan->is_resume);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040087 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040088
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040089 // ===== Resume case handling code goes here ====
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040090 // It is possible that the SOURCE_COPY are already written but
91 // |next_op_index_| is still 0. In this case we discard previously written
92 // SOURCE_COPY, and start over.
93 if (install_plan->is_resume && next_op_index > 0) {
94 LOG(INFO) << "Resuming update on partition `"
95 << partition_update_.partition_name() << "` op index "
96 << next_op_index;
97 TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op_index));
98 return true;
99 } else {
100 TEST_AND_RETURN_FALSE(cow_writer_->Initialize());
101 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400102
103 // ==============================================
104
105 // TODO(zhangkelvin) Rewrite this in C++20 coroutine once that's available.
106 auto converted = ConvertToCowOperations(partition_update_.operations(),
107 partition_update_.merge_operations());
Kelvin Zhang7a265752020-10-29 15:51:35 -0400108
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500109 if (!converted.empty()) {
110 // Use source fd directly. Ideally we want to verify all extents used in
111 // source copy, but then what do we do if some extents contain correct
112 // hashes and some don't?
113 auto source_fd = std::make_shared<EintrSafeFileDescriptor>();
114 TEST_AND_RETURN_FALSE_ERRNO(
115 source_fd->Open(install_part_.source_path.c_str(), O_RDONLY));
116 WriteAllCowOps(block_size_, converted, cow_writer_.get(), source_fd);
117 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400118 return true;
119}
120
121bool VABCPartitionWriter::WriteAllCowOps(
122 size_t block_size,
123 const std::vector<CowOperation>& converted,
124 android::snapshot::ICowWriter* cow_writer,
125 FileDescriptorPtr source_fd) {
126 std::vector<uint8_t> buffer(block_size);
Kelvin Zhang24599af2020-10-27 13:44:25 -0400127
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400128 for (const auto& cow_op : converted) {
129 switch (cow_op.op) {
130 case CowOperation::CowCopy:
Kelvin Zhang4430ea52021-02-26 13:35:34 -0500131 if (cow_op.src_block == cow_op.dst_block) {
132 continue;
133 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400134 TEST_AND_RETURN_FALSE(
Kelvin Zhang7a265752020-10-29 15:51:35 -0400135 cow_writer->AddCopy(cow_op.dst_block, cow_op.src_block));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400136 break;
137 case CowOperation::CowReplace:
138 ssize_t bytes_read = 0;
Kelvin Zhang4b280242020-11-06 16:07:45 -0500139 TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd,
140 buffer.data(),
141 block_size,
142 cow_op.src_block * block_size,
143 &bytes_read));
Kelvin Zhang7a265752020-10-29 15:51:35 -0400144 if (bytes_read <= 0 || static_cast<size_t>(bytes_read) != block_size) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400145 LOG(ERROR) << "source_fd->Read failed: " << bytes_read;
146 return false;
147 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400148 TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks(
149 cow_op.dst_block, buffer.data(), block_size));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400150 break;
151 }
152 }
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400153
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400154 return true;
155}
156
157std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400158 return std::make_unique<SnapshotExtentWriter>(cow_writer_.get());
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400159}
160
161[[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation(
162 const InstallOperation& operation) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400163 for (const auto& extent : operation.dst_extents()) {
164 TEST_AND_RETURN_FALSE(
165 cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks()));
166 }
167 return true;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400168}
169
170[[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation(
171 const InstallOperation& operation, ErrorCode* error) {
172 // TODO(zhangkelvin) Probably just ignore SOURCE_COPY? They should be taken
173 // care of during Init();
174 return true;
175}
176
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500177bool VABCPartitionWriter::PerformReplaceOperation(const InstallOperation& op,
178 const void* data,
179 size_t count) {
180 // Setup the ExtentWriter stack based on the operation type.
181 std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter();
182
183 return executor_.ExecuteReplaceOperation(op, std::move(writer), data, count);
184}
185
186bool VABCPartitionWriter::PerformSourceBsdiffOperation(
187 const InstallOperation& operation,
188 ErrorCode* error,
189 const void* data,
190 size_t count) {
191 FileDescriptorPtr source_fd =
192 verified_source_fd_.ChooseSourceFD(operation, error);
193 TEST_AND_RETURN_FALSE(source_fd != nullptr);
194
195 auto writer = CreateBaseExtentWriter();
196 return executor_.ExecuteSourceBsdiffOperation(
197 operation, std::move(writer), source_fd, data, count);
198}
199
200bool VABCPartitionWriter::PerformPuffDiffOperation(
201 const InstallOperation& operation,
202 ErrorCode* error,
203 const void* data,
204 size_t count) {
205 FileDescriptorPtr source_fd =
206 verified_source_fd_.ChooseSourceFD(operation, error);
207 TEST_AND_RETURN_FALSE(source_fd != nullptr);
208
209 auto writer = CreateBaseExtentWriter();
210 return executor_.ExecutePuffDiffOperation(
211 operation, std::move(writer), source_fd, data, count);
212}
213
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400214void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) {
215 // No need to call fsync/sync, as CowWriter flushes after a label is added
216 // added.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500217 // if cow_writer_ failed, that means Init() failed. This function shouldn't be
218 // called if Init() fails.
219 TEST_AND_RETURN(cow_writer_ != nullptr);
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400220 cow_writer_->AddLabel(next_op_index);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400221}
222
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400223[[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() {
224 // Add a hardcoded magic label to indicate end of all install ops. This label
225 // is needed by filesystem verification, don't remove.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500226 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400227 return cow_writer_->AddLabel(kEndOfInstallLabel);
228}
229
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400230VABCPartitionWriter::~VABCPartitionWriter() {
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500231 Close();
232}
233
234int VABCPartitionWriter::Close() {
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500235 if (cow_writer_) {
236 cow_writer_->Finalize();
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500237 cow_writer_ = nullptr;
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500238 }
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500239 return 0;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400240}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400241
242} // namespace chromeos_update_engine