blob: 3cdd4a4d785952aeba47e465a39396431657f550 [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 Zhanga37aafc2021-06-14 13:21:37 -040033#include "update_engine/payload_generator/extent_ranges.h"
34#include "update_engine/payload_generator/extent_utils.h"
35#include "update_engine/update_metadata.pb.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040036
37namespace chromeos_update_engine {
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040038// Expected layout of COW file:
39// === Beginning of Cow Image ===
40// All Source Copy Operations
41// ========== Label 0 ==========
42// Operation 0 in PartitionUpdate
43// ========== Label 1 ==========
44// Operation 1 in PartitionUpdate
45// ========== label 2 ==========
46// Operation 2 in PartitionUpdate
47// ========== label 3 ==========
48// .
49// .
50// .
51
52// When resuming, pass |next_op_index_| as label to
53// |InitializeWithAppend|.
54// For example, suppose we finished writing SOURCE_COPY, and we finished writing
55// operation 2 completely. Update is suspended when we are half way through
56// operation 3.
57// |cnext_op_index_| would be 3, so we pass 3 as
58// label to |InitializeWithAppend|. The CowWriter will retain all data before
59// label 3, Which contains all operation 2's data, but none of operation 3's
60// data.
61
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040062using android::snapshot::ICowWriter;
63using ::google::protobuf::RepeatedPtrField;
64
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050065VABCPartitionWriter::VABCPartitionWriter(
66 const PartitionUpdate& partition_update,
67 const InstallPlan::Partition& install_part,
68 DynamicPartitionControlInterface* dynamic_control,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040069 size_t block_size)
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050070 : partition_update_(partition_update),
71 install_part_(install_part),
72 dynamic_control_(dynamic_control),
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050073 block_size_(block_size),
74 executor_(block_size),
75 verified_source_fd_(block_size, install_part.source_path) {}
76
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040077bool VABCPartitionWriter::Init(const InstallPlan* install_plan,
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040078 bool source_may_exist,
79 size_t next_op_index) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040080 TEST_AND_RETURN_FALSE(install_plan != nullptr);
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050081 if (source_may_exist) {
82 TEST_AND_RETURN_FALSE(verified_source_fd_.Open());
83 }
Kelvin Zhang3f60d532020-11-09 13:33:17 -050084 std::optional<std::string> source_path;
85 if (!install_part_.source_path.empty()) {
86 // TODO(zhangkelvin) Make |source_path| a std::optional<std::string>
87 source_path = install_part_.source_path;
88 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040089 cow_writer_ = dynamic_control_->OpenCowWriter(
Kelvin Zhang3f60d532020-11-09 13:33:17 -050090 install_part_.name, source_path, install_plan->is_resume);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040091 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040092
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040093 // ===== Resume case handling code goes here ====
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040094 // It is possible that the SOURCE_COPY are already written but
95 // |next_op_index_| is still 0. In this case we discard previously written
96 // SOURCE_COPY, and start over.
97 if (install_plan->is_resume && next_op_index > 0) {
98 LOG(INFO) << "Resuming update on partition `"
99 << partition_update_.partition_name() << "` op index "
100 << next_op_index;
101 TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op_index));
102 return true;
103 } else {
104 TEST_AND_RETURN_FALSE(cow_writer_->Initialize());
105 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400106
107 // ==============================================
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400108 if (!partition_update_.merge_operations().empty()) {
109 TEST_AND_RETURN_FALSE(WriteMergeSequence(
110 partition_update_.merge_operations(), cow_writer_.get()));
111 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400112
113 // TODO(zhangkelvin) Rewrite this in C++20 coroutine once that's available.
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400114 // TODO(177104308) Don't write all COPY ops up-front if merge sequence is
115 // written
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400116 auto converted = ConvertToCowOperations(partition_update_.operations(),
117 partition_update_.merge_operations());
Kelvin Zhang7a265752020-10-29 15:51:35 -0400118
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500119 if (!converted.empty()) {
120 // Use source fd directly. Ideally we want to verify all extents used in
121 // source copy, but then what do we do if some extents contain correct
122 // hashes and some don't?
123 auto source_fd = std::make_shared<EintrSafeFileDescriptor>();
124 TEST_AND_RETURN_FALSE_ERRNO(
125 source_fd->Open(install_part_.source_path.c_str(), O_RDONLY));
126 WriteAllCowOps(block_size_, converted, cow_writer_.get(), source_fd);
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400127 cow_writer_->AddLabel(0);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500128 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400129 return true;
130}
131
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400132bool VABCPartitionWriter::WriteMergeSequence(
133 const RepeatedPtrField<CowMergeOperation>& merge_sequence,
134 ICowWriter* cow_writer) {
135 std::vector<uint32_t> blocks_merge_order;
136 for (const auto& merge_op : merge_sequence) {
137 const auto& dst_extent = merge_op.dst_extent();
138 // In place copy are basically noops, they do not need to be "merged" at
139 // all, don't include them in merge sequence.
140 if (merge_op.type() == CowMergeOperation::COW_COPY &&
141 merge_op.src_extent() == merge_op.dst_extent()) {
142 continue;
143 }
144 // libsnapshot doesn't like us include REPLACE blocks in merge sequence,
145 // since we don't support XOR ops at this CL, skip them! Remove once we
146 // write XOR ops.
147 if (merge_op.type() == CowMergeOperation::COW_XOR) {
148 continue;
149 }
150 // libsnapshot prefers blocks in reverse order
151 for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) {
152 blocks_merge_order.push_back(dst_extent.start_block() + i);
153 }
154 }
155 return cow_writer->AddSequenceData(blocks_merge_order.size(),
156 blocks_merge_order.data());
157}
158
Kelvin Zhang7a265752020-10-29 15:51:35 -0400159bool VABCPartitionWriter::WriteAllCowOps(
160 size_t block_size,
161 const std::vector<CowOperation>& converted,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400162 ICowWriter* cow_writer,
Kelvin Zhang7a265752020-10-29 15:51:35 -0400163 FileDescriptorPtr source_fd) {
164 std::vector<uint8_t> buffer(block_size);
Kelvin Zhang24599af2020-10-27 13:44:25 -0400165
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400166 for (const auto& cow_op : converted) {
167 switch (cow_op.op) {
168 case CowOperation::CowCopy:
Kelvin Zhang4430ea52021-02-26 13:35:34 -0500169 if (cow_op.src_block == cow_op.dst_block) {
170 continue;
171 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400172 TEST_AND_RETURN_FALSE(
Kelvin Zhang7a265752020-10-29 15:51:35 -0400173 cow_writer->AddCopy(cow_op.dst_block, cow_op.src_block));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400174 break;
175 case CowOperation::CowReplace:
176 ssize_t bytes_read = 0;
Kelvin Zhang4b280242020-11-06 16:07:45 -0500177 TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd,
178 buffer.data(),
179 block_size,
180 cow_op.src_block * block_size,
181 &bytes_read));
Kelvin Zhang7a265752020-10-29 15:51:35 -0400182 if (bytes_read <= 0 || static_cast<size_t>(bytes_read) != block_size) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400183 LOG(ERROR) << "source_fd->Read failed: " << bytes_read;
184 return false;
185 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400186 TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks(
187 cow_op.dst_block, buffer.data(), block_size));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400188 break;
189 }
190 }
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400191
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400192 return true;
193}
194
195std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400196 return std::make_unique<SnapshotExtentWriter>(cow_writer_.get());
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400197}
198
199[[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation(
200 const InstallOperation& operation) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400201 for (const auto& extent : operation.dst_extents()) {
202 TEST_AND_RETURN_FALSE(
203 cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks()));
204 }
205 return true;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400206}
207
208[[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation(
209 const InstallOperation& operation, ErrorCode* error) {
210 // TODO(zhangkelvin) Probably just ignore SOURCE_COPY? They should be taken
211 // care of during Init();
212 return true;
213}
214
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500215bool VABCPartitionWriter::PerformReplaceOperation(const InstallOperation& op,
216 const void* data,
217 size_t count) {
218 // Setup the ExtentWriter stack based on the operation type.
219 std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter();
220
221 return executor_.ExecuteReplaceOperation(op, std::move(writer), data, count);
222}
223
Tianjie8e0090d2021-08-30 22:35:21 -0700224bool VABCPartitionWriter::PerformDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500225 const InstallOperation& operation,
226 ErrorCode* error,
227 const void* data,
228 size_t count) {
229 FileDescriptorPtr source_fd =
230 verified_source_fd_.ChooseSourceFD(operation, error);
231 TEST_AND_RETURN_FALSE(source_fd != nullptr);
232
233 auto writer = CreateBaseExtentWriter();
Tianjie8e0090d2021-08-30 22:35:21 -0700234 return executor_.ExecuteDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500235 operation, std::move(writer), source_fd, data, count);
236}
237
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400238void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) {
239 // No need to call fsync/sync, as CowWriter flushes after a label is added
240 // added.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500241 // if cow_writer_ failed, that means Init() failed. This function shouldn't be
242 // called if Init() fails.
243 TEST_AND_RETURN(cow_writer_ != nullptr);
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400244 cow_writer_->AddLabel(next_op_index);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400245}
246
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400247[[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() {
248 // Add a hardcoded magic label to indicate end of all install ops. This label
249 // is needed by filesystem verification, don't remove.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500250 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400251 return cow_writer_->AddLabel(kEndOfInstallLabel);
252}
253
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400254VABCPartitionWriter::~VABCPartitionWriter() {
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500255 Close();
256}
257
258int VABCPartitionWriter::Close() {
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500259 if (cow_writer_) {
260 cow_writer_->Finalize();
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500261 cow_writer_ = nullptr;
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500262 }
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500263 return 0;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400264}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400265
266} // namespace chromeos_update_engine