blob: 583ba4cd0ca508760c5a1748bc048e2e9205d89f [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
Kelvin Zhang76f10b82021-06-25 18:45:46 -040019#include <algorithm>
20#include <map>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040021#include <memory>
Kelvin Zhang3f60d532020-11-09 13:33:17 -050022#include <string>
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050023#include <utility>
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040024#include <vector>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040025
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +000026#include <android-base/properties.h>
Kelvin Zhang76f10b82021-06-25 18:45:46 -040027#include <brillo/secure_blob.h>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040028#include <libsnapshot/cow_writer.h>
29
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040030#include "update_engine/common/cow_operation_convert.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040031#include "update_engine/common/utils.h"
Kelvin Zhang76f10b82021-06-25 18:45:46 -040032#include "update_engine/payload_consumer/block_extent_writer.h"
33#include "update_engine/payload_consumer/extent_map.h"
34#include "update_engine/payload_consumer/extent_reader.h"
Kelvin Zhang7a265752020-10-29 15:51:35 -040035#include "update_engine/payload_consumer/file_descriptor.h"
Kelvin Zhangb5bd0752022-03-10 15:45:25 -080036#include "update_engine/payload_consumer/file_descriptor_utils.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040037#include "update_engine/payload_consumer/install_plan.h"
38#include "update_engine/payload_consumer/partition_writer.h"
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040039#include "update_engine/payload_consumer/snapshot_extent_writer.h"
Kelvin Zhang76f10b82021-06-25 18:45:46 -040040#include "update_engine/payload_consumer/xor_extent_writer.h"
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040041#include "update_engine/payload_generator/extent_ranges.h"
42#include "update_engine/payload_generator/extent_utils.h"
43#include "update_engine/update_metadata.pb.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040044
45namespace chromeos_update_engine {
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040046// Expected layout of COW file:
47// === Beginning of Cow Image ===
48// All Source Copy Operations
49// ========== Label 0 ==========
50// Operation 0 in PartitionUpdate
51// ========== Label 1 ==========
52// Operation 1 in PartitionUpdate
53// ========== label 2 ==========
54// Operation 2 in PartitionUpdate
55// ========== label 3 ==========
56// .
57// .
58// .
59
60// When resuming, pass |next_op_index_| as label to
61// |InitializeWithAppend|.
62// For example, suppose we finished writing SOURCE_COPY, and we finished writing
63// operation 2 completely. Update is suspended when we are half way through
64// operation 3.
65// |cnext_op_index_| would be 3, so we pass 3 as
66// label to |InitializeWithAppend|. The CowWriter will retain all data before
67// label 3, Which contains all operation 2's data, but none of operation 3's
68// data.
69
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040070using android::snapshot::ICowWriter;
71using ::google::protobuf::RepeatedPtrField;
72
Kelvin Zhang76f10b82021-06-25 18:45:46 -040073// Compute XOR map, a map from dst extent to corresponding merge operation
74static ExtentMap<const CowMergeOperation*, ExtentLess> ComputeXorMap(
75 const RepeatedPtrField<CowMergeOperation>& merge_ops) {
76 ExtentMap<const CowMergeOperation*, ExtentLess> xor_map;
77 for (const auto& merge_op : merge_ops) {
78 if (merge_op.type() == CowMergeOperation::COW_XOR) {
79 xor_map.AddExtent(merge_op.dst_extent(), &merge_op);
80 }
81 }
82 return xor_map;
83}
84
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050085VABCPartitionWriter::VABCPartitionWriter(
86 const PartitionUpdate& partition_update,
87 const InstallPlan::Partition& install_part,
88 DynamicPartitionControlInterface* dynamic_control,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040089 size_t block_size)
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050090 : partition_update_(partition_update),
91 install_part_(install_part),
92 dynamic_control_(dynamic_control),
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050093 block_size_(block_size),
94 executor_(block_size),
95 verified_source_fd_(block_size, install_part.source_path) {}
96
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040097bool VABCPartitionWriter::Init(const InstallPlan* install_plan,
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040098 bool source_may_exist,
99 size_t next_op_index) {
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400100 xor_map_ = ComputeXorMap(partition_update_.merge_operations());
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400101 TEST_AND_RETURN_FALSE(install_plan != nullptr);
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700102 if (source_may_exist && install_part_.source_size > 0) {
103 TEST_AND_RETURN_FALSE(!install_part_.source_path.empty());
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500104 TEST_AND_RETURN_FALSE(verified_source_fd_.Open());
105 }
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500106 std::optional<std::string> source_path;
107 if (!install_part_.source_path.empty()) {
108 // TODO(zhangkelvin) Make |source_path| a std::optional<std::string>
109 source_path = install_part_.source_path;
110 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400111 cow_writer_ = dynamic_control_->OpenCowWriter(
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500112 install_part_.name, source_path, install_plan->is_resume);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400113 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400114
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400115 // ===== Resume case handling code goes here ====
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400116 // It is possible that the SOURCE_COPY are already written but
117 // |next_op_index_| is still 0. In this case we discard previously written
118 // SOURCE_COPY, and start over.
119 if (install_plan->is_resume && next_op_index > 0) {
120 LOG(INFO) << "Resuming update on partition `"
121 << partition_update_.partition_name() << "` op index "
122 << next_op_index;
123 TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op_index));
124 return true;
125 } else {
126 TEST_AND_RETURN_FALSE(cow_writer_->Initialize());
127 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400128
129 // ==============================================
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400130 if (!partition_update_.merge_operations().empty()) {
Kelvin Zhang5d74b722021-09-29 15:24:26 -0700131 if (IsXorEnabled()) {
132 LOG(INFO) << "VABC XOR enabled for partition "
133 << partition_update_.partition_name();
134 TEST_AND_RETURN_FALSE(WriteMergeSequence(
135 partition_update_.merge_operations(), cow_writer_.get()));
136 }
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400137 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400138
139 // TODO(zhangkelvin) Rewrite this in C++20 coroutine once that's available.
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400140 // TODO(177104308) Don't write all COPY ops up-front if merge sequence is
141 // written
Kelvin Zhang5d74b722021-09-29 15:24:26 -0700142 const auto converted = ConvertToCowOperations(
143 partition_update_.operations(), partition_update_.merge_operations());
Kelvin Zhang7a265752020-10-29 15:51:35 -0400144
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500145 if (!converted.empty()) {
146 // Use source fd directly. Ideally we want to verify all extents used in
147 // source copy, but then what do we do if some extents contain correct
148 // hashes and some don't?
149 auto source_fd = std::make_shared<EintrSafeFileDescriptor>();
150 TEST_AND_RETURN_FALSE_ERRNO(
151 source_fd->Open(install_part_.source_path.c_str(), O_RDONLY));
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400152 TEST_AND_RETURN_FALSE(WriteSourceCopyCowOps(
153 block_size_, converted, cow_writer_.get(), source_fd));
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400154 cow_writer_->AddLabel(0);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500155 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400156 return true;
157}
158
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400159bool VABCPartitionWriter::WriteMergeSequence(
160 const RepeatedPtrField<CowMergeOperation>& merge_sequence,
161 ICowWriter* cow_writer) {
162 std::vector<uint32_t> blocks_merge_order;
163 for (const auto& merge_op : merge_sequence) {
164 const auto& dst_extent = merge_op.dst_extent();
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700165 const auto& src_extent = merge_op.src_extent();
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400166 // In place copy are basically noops, they do not need to be "merged" at
167 // all, don't include them in merge sequence.
168 if (merge_op.type() == CowMergeOperation::COW_COPY &&
169 merge_op.src_extent() == merge_op.dst_extent()) {
170 continue;
171 }
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000172
173 const bool extent_overlap =
174 ExtentRanges::ExtentsOverlap(src_extent, dst_extent);
175 // TODO(193863443) Remove this check once this feature
176 // lands on all pixel devices.
177 const bool is_ascending = android::base::GetBoolProperty(
178 "ro.virtual_ab.userspace.snapshots.enabled", false);
179
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700180 // If this is a self-overlapping op and |dst_extent| comes after
181 // |src_extent|, we must write in reverse order for correctness.
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000182 //
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700183 // If this is self-overlapping op and |dst_extent| comes before
184 // |src_extent|, we must write in ascending order for correctness.
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000185 //
186 // If this isn't a self overlapping op, write block in ascending order
187 // if userspace snapshots are enabled
188 if (extent_overlap) {
189 if (dst_extent.start_block() <= src_extent.start_block()) {
190 for (size_t i = 0; i < dst_extent.num_blocks(); i++) {
191 blocks_merge_order.push_back(dst_extent.start_block() + i);
192 }
193 } else {
194 for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) {
195 blocks_merge_order.push_back(dst_extent.start_block() + i);
196 }
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700197 }
198 } else {
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000199 if (is_ascending) {
200 for (size_t i = 0; i < dst_extent.num_blocks(); i++) {
201 blocks_merge_order.push_back(dst_extent.start_block() + i);
202 }
203 } else {
204 for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) {
205 blocks_merge_order.push_back(dst_extent.start_block() + i);
206 }
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700207 }
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400208 }
209 }
210 return cow_writer->AddSequenceData(blocks_merge_order.size(),
211 blocks_merge_order.data());
212}
213
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400214bool VABCPartitionWriter::WriteSourceCopyCowOps(
Kelvin Zhang7a265752020-10-29 15:51:35 -0400215 size_t block_size,
216 const std::vector<CowOperation>& converted,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400217 ICowWriter* cow_writer,
Kelvin Zhang7a265752020-10-29 15:51:35 -0400218 FileDescriptorPtr source_fd) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400219 for (const auto& cow_op : converted) {
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700220 std::vector<uint8_t> buffer;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400221 switch (cow_op.op) {
222 case CowOperation::CowCopy:
Kelvin Zhang4430ea52021-02-26 13:35:34 -0500223 if (cow_op.src_block == cow_op.dst_block) {
224 continue;
225 }
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700226 // Add blocks in reverse order, because snapused specifically prefers
227 // this ordering. Since we already eliminated all self-overlapping
228 // SOURCE_COPY during delta generation, this should be safe to do.
229 for (size_t i = cow_op.block_count; i > 0; i--) {
230 TEST_AND_RETURN_FALSE(cow_writer->AddCopy(cow_op.dst_block + i - 1,
231 cow_op.src_block + i - 1));
232 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400233 break;
234 case CowOperation::CowReplace:
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700235 buffer.resize(block_size * cow_op.block_count);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400236 ssize_t bytes_read = 0;
Kelvin Zhang4b280242020-11-06 16:07:45 -0500237 TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd,
238 buffer.data(),
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700239 block_size * cow_op.block_count,
Kelvin Zhang4b280242020-11-06 16:07:45 -0500240 cow_op.src_block * block_size,
241 &bytes_read));
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700242 if (bytes_read <= 0 ||
243 static_cast<size_t>(bytes_read) != buffer.size()) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400244 LOG(ERROR) << "source_fd->Read failed: " << bytes_read;
245 return false;
246 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400247 TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks(
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700248 cow_op.dst_block, buffer.data(), buffer.size()));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400249 break;
250 }
251 }
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400252
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400253 return true;
254}
255
256std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400257 return std::make_unique<SnapshotExtentWriter>(cow_writer_.get());
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400258}
259
260[[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation(
261 const InstallOperation& operation) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400262 for (const auto& extent : operation.dst_extents()) {
263 TEST_AND_RETURN_FALSE(
264 cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks()));
265 }
266 return true;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400267}
268
269[[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation(
270 const InstallOperation& operation, ErrorCode* error) {
Kelvin Zhangb5bd0752022-03-10 15:45:25 -0800271 // COPY ops are already handled during Init(), no need to do actual work, but
272 // we still want to verify that all blocks contain expected data.
273 auto source_fd = std::make_shared<EintrSafeFileDescriptor>();
274 TEST_AND_RETURN_FALSE_ERRNO(
275 source_fd->Open(install_part_.source_path.c_str(), O_RDONLY));
276 return PartitionWriter::ValidateSourceHash(
277 operation, source_fd, block_size_, error);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400278}
279
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500280bool VABCPartitionWriter::PerformReplaceOperation(const InstallOperation& op,
281 const void* data,
282 size_t count) {
283 // Setup the ExtentWriter stack based on the operation type.
284 std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter();
285
286 return executor_.ExecuteReplaceOperation(op, std::move(writer), data, count);
287}
288
Tianjie8e0090d2021-08-30 22:35:21 -0700289bool VABCPartitionWriter::PerformDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500290 const InstallOperation& operation,
291 ErrorCode* error,
292 const void* data,
293 size_t count) {
294 FileDescriptorPtr source_fd =
295 verified_source_fd_.ChooseSourceFD(operation, error);
296 TEST_AND_RETURN_FALSE(source_fd != nullptr);
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400297 TEST_AND_RETURN_FALSE(source_fd->IsOpen());
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500298
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400299 std::unique_ptr<ExtentWriter> writer =
300 IsXorEnabled() ? std::make_unique<XORExtentWriter>(
301 operation, source_fd, cow_writer_.get(), xor_map_)
302 : CreateBaseExtentWriter();
Tianjie8e0090d2021-08-30 22:35:21 -0700303 return executor_.ExecuteDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500304 operation, std::move(writer), source_fd, data, count);
305}
306
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400307void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) {
308 // No need to call fsync/sync, as CowWriter flushes after a label is added
309 // added.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500310 // if cow_writer_ failed, that means Init() failed. This function shouldn't be
311 // called if Init() fails.
312 TEST_AND_RETURN(cow_writer_ != nullptr);
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400313 cow_writer_->AddLabel(next_op_index);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400314}
315
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400316[[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() {
317 // Add a hardcoded magic label to indicate end of all install ops. This label
318 // is needed by filesystem verification, don't remove.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500319 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang9e5e1ed2021-09-28 14:19:16 -0700320 TEST_AND_RETURN_FALSE(cow_writer_->AddLabel(kEndOfInstallLabel));
321 TEST_AND_RETURN_FALSE(cow_writer_->Finalize());
Kelvin Zhang02fe6622021-11-01 16:37:58 -0700322 TEST_AND_RETURN_FALSE(cow_writer_->VerifyMergeOps());
323 return true;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400324}
325
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400326VABCPartitionWriter::~VABCPartitionWriter() {
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500327 Close();
328}
329
330int VABCPartitionWriter::Close() {
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500331 if (cow_writer_) {
332 cow_writer_->Finalize();
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500333 cow_writer_ = nullptr;
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500334 }
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500335 return 0;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400336}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400337
338} // namespace chromeos_update_engine