blob: c3b2e41e9fda8046e81ecc6c97be2830c5002248 [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 Zhang1c4b9812022-04-06 17:29:00 -0700100 if (dynamic_control_->GetVirtualAbCompressionXorFeatureFlag().IsEnabled()) {
101 xor_map_ = ComputeXorMap(partition_update_.merge_operations());
102 if (xor_map_.size() > 0) {
103 LOG(INFO) << "Virtual AB Compression with XOR is enabled";
104 } else {
105 LOG(INFO) << "Device supports Virtual AB compression with XOR, but OTA "
106 "package does not.";
107 }
108 } else {
109 LOG(INFO) << "Virtual AB Compression with XOR is disabled.";
110 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400111 TEST_AND_RETURN_FALSE(install_plan != nullptr);
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700112 if (source_may_exist && install_part_.source_size > 0) {
113 TEST_AND_RETURN_FALSE(!install_part_.source_path.empty());
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500114 TEST_AND_RETURN_FALSE(verified_source_fd_.Open());
115 }
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500116 std::optional<std::string> source_path;
117 if (!install_part_.source_path.empty()) {
118 // TODO(zhangkelvin) Make |source_path| a std::optional<std::string>
119 source_path = install_part_.source_path;
120 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400121 cow_writer_ = dynamic_control_->OpenCowWriter(
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500122 install_part_.name, source_path, install_plan->is_resume);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400123 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400124
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400125 // ===== Resume case handling code goes here ====
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400126 // It is possible that the SOURCE_COPY are already written but
127 // |next_op_index_| is still 0. In this case we discard previously written
128 // SOURCE_COPY, and start over.
129 if (install_plan->is_resume && next_op_index > 0) {
130 LOG(INFO) << "Resuming update on partition `"
131 << partition_update_.partition_name() << "` op index "
132 << next_op_index;
133 TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op_index));
134 return true;
135 } else {
136 TEST_AND_RETURN_FALSE(cow_writer_->Initialize());
137 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400138
139 // ==============================================
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400140 if (!partition_update_.merge_operations().empty()) {
Kelvin Zhang5d74b722021-09-29 15:24:26 -0700141 if (IsXorEnabled()) {
142 LOG(INFO) << "VABC XOR enabled for partition "
143 << partition_update_.partition_name();
144 TEST_AND_RETURN_FALSE(WriteMergeSequence(
145 partition_update_.merge_operations(), cow_writer_.get()));
146 }
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400147 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400148
149 // TODO(zhangkelvin) Rewrite this in C++20 coroutine once that's available.
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400150 // TODO(177104308) Don't write all COPY ops up-front if merge sequence is
151 // written
Kelvin Zhang5d74b722021-09-29 15:24:26 -0700152 const auto converted = ConvertToCowOperations(
153 partition_update_.operations(), partition_update_.merge_operations());
Kelvin Zhang7a265752020-10-29 15:51:35 -0400154
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500155 if (!converted.empty()) {
156 // Use source fd directly. Ideally we want to verify all extents used in
157 // source copy, but then what do we do if some extents contain correct
158 // hashes and some don't?
159 auto source_fd = std::make_shared<EintrSafeFileDescriptor>();
160 TEST_AND_RETURN_FALSE_ERRNO(
161 source_fd->Open(install_part_.source_path.c_str(), O_RDONLY));
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400162 TEST_AND_RETURN_FALSE(WriteSourceCopyCowOps(
163 block_size_, converted, cow_writer_.get(), source_fd));
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400164 cow_writer_->AddLabel(0);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500165 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400166 return true;
167}
168
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400169bool VABCPartitionWriter::WriteMergeSequence(
170 const RepeatedPtrField<CowMergeOperation>& merge_sequence,
171 ICowWriter* cow_writer) {
172 std::vector<uint32_t> blocks_merge_order;
173 for (const auto& merge_op : merge_sequence) {
174 const auto& dst_extent = merge_op.dst_extent();
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700175 const auto& src_extent = merge_op.src_extent();
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400176 // In place copy are basically noops, they do not need to be "merged" at
177 // all, don't include them in merge sequence.
178 if (merge_op.type() == CowMergeOperation::COW_COPY &&
179 merge_op.src_extent() == merge_op.dst_extent()) {
180 continue;
181 }
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000182
183 const bool extent_overlap =
184 ExtentRanges::ExtentsOverlap(src_extent, dst_extent);
185 // TODO(193863443) Remove this check once this feature
186 // lands on all pixel devices.
187 const bool is_ascending = android::base::GetBoolProperty(
188 "ro.virtual_ab.userspace.snapshots.enabled", false);
189
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700190 // If this is a self-overlapping op and |dst_extent| comes after
191 // |src_extent|, we must write in reverse order for correctness.
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000192 //
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700193 // If this is self-overlapping op and |dst_extent| comes before
194 // |src_extent|, we must write in ascending order for correctness.
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000195 //
196 // If this isn't a self overlapping op, write block in ascending order
197 // if userspace snapshots are enabled
198 if (extent_overlap) {
199 if (dst_extent.start_block() <= src_extent.start_block()) {
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 }
208 } else {
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000209 if (is_ascending) {
210 for (size_t i = 0; i < dst_extent.num_blocks(); i++) {
211 blocks_merge_order.push_back(dst_extent.start_block() + i);
212 }
213 } else {
214 for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) {
215 blocks_merge_order.push_back(dst_extent.start_block() + i);
216 }
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700217 }
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400218 }
219 }
220 return cow_writer->AddSequenceData(blocks_merge_order.size(),
221 blocks_merge_order.data());
222}
223
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400224bool VABCPartitionWriter::WriteSourceCopyCowOps(
Kelvin Zhang7a265752020-10-29 15:51:35 -0400225 size_t block_size,
226 const std::vector<CowOperation>& converted,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400227 ICowWriter* cow_writer,
Kelvin Zhang7a265752020-10-29 15:51:35 -0400228 FileDescriptorPtr source_fd) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400229 for (const auto& cow_op : converted) {
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700230 std::vector<uint8_t> buffer;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400231 switch (cow_op.op) {
Akilesh Kailash1528f922022-10-19 07:20:46 +0000232 case CowOperation::CowCopy: {
Kelvin Zhang4430ea52021-02-26 13:35:34 -0500233 if (cow_op.src_block == cow_op.dst_block) {
234 continue;
235 }
Akilesh Kailash1528f922022-10-19 07:20:46 +0000236
237 const bool userSnapshots = android::base::GetBoolProperty(
238 "ro.virtual_ab.userspace.snapshots.enabled", false);
239
240 if (userSnapshots) {
241 TEST_AND_RETURN_FALSE(cow_op.block_count != 0);
242 TEST_AND_RETURN_FALSE(cow_writer->AddCopy(
243 cow_op.dst_block, cow_op.src_block, cow_op.block_count));
244 } else {
245 // Add blocks in reverse order, because snapused specifically prefers
246 // this ordering. Since we already eliminated all self-overlapping
247 // SOURCE_COPY during delta generation, this should be safe to do.
248 for (size_t i = cow_op.block_count; i > 0; i--) {
249 TEST_AND_RETURN_FALSE(cow_writer->AddCopy(
250 cow_op.dst_block + i - 1, cow_op.src_block + i - 1));
251 }
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700252 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400253 break;
Akilesh Kailash1528f922022-10-19 07:20:46 +0000254 }
255 case CowOperation::CowReplace: {
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700256 buffer.resize(block_size * cow_op.block_count);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400257 ssize_t bytes_read = 0;
Kelvin Zhang4b280242020-11-06 16:07:45 -0500258 TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd,
259 buffer.data(),
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700260 block_size * cow_op.block_count,
Kelvin Zhang4b280242020-11-06 16:07:45 -0500261 cow_op.src_block * block_size,
262 &bytes_read));
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700263 if (bytes_read <= 0 ||
264 static_cast<size_t>(bytes_read) != buffer.size()) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400265 LOG(ERROR) << "source_fd->Read failed: " << bytes_read;
266 return false;
267 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400268 TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks(
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700269 cow_op.dst_block, buffer.data(), buffer.size()));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400270 break;
Akilesh Kailash1528f922022-10-19 07:20:46 +0000271 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400272 }
273 }
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400274
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400275 return true;
276}
277
278std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400279 return std::make_unique<SnapshotExtentWriter>(cow_writer_.get());
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400280}
281
282[[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation(
283 const InstallOperation& operation) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400284 for (const auto& extent : operation.dst_extents()) {
285 TEST_AND_RETURN_FALSE(
286 cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks()));
287 }
288 return true;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400289}
290
291[[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation(
292 const InstallOperation& operation, ErrorCode* error) {
Kelvin Zhangb5bd0752022-03-10 15:45:25 -0800293 // COPY ops are already handled during Init(), no need to do actual work, but
294 // we still want to verify that all blocks contain expected data.
295 auto source_fd = std::make_shared<EintrSafeFileDescriptor>();
296 TEST_AND_RETURN_FALSE_ERRNO(
297 source_fd->Open(install_part_.source_path.c_str(), O_RDONLY));
Kelvin Zhangdc122bc2022-03-15 14:19:04 -0700298 if (!operation.has_src_sha256_hash()) {
299 return true;
300 }
Kelvin Zhangb5bd0752022-03-10 15:45:25 -0800301 return PartitionWriter::ValidateSourceHash(
302 operation, source_fd, block_size_, error);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400303}
304
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500305bool VABCPartitionWriter::PerformReplaceOperation(const InstallOperation& op,
306 const void* data,
307 size_t count) {
308 // Setup the ExtentWriter stack based on the operation type.
309 std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter();
310
311 return executor_.ExecuteReplaceOperation(op, std::move(writer), data, count);
312}
313
Tianjie8e0090d2021-08-30 22:35:21 -0700314bool VABCPartitionWriter::PerformDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500315 const InstallOperation& operation,
316 ErrorCode* error,
317 const void* data,
318 size_t count) {
319 FileDescriptorPtr source_fd =
320 verified_source_fd_.ChooseSourceFD(operation, error);
321 TEST_AND_RETURN_FALSE(source_fd != nullptr);
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400322 TEST_AND_RETURN_FALSE(source_fd->IsOpen());
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500323
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400324 std::unique_ptr<ExtentWriter> writer =
325 IsXorEnabled() ? std::make_unique<XORExtentWriter>(
326 operation, source_fd, cow_writer_.get(), xor_map_)
327 : CreateBaseExtentWriter();
Tianjie8e0090d2021-08-30 22:35:21 -0700328 return executor_.ExecuteDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500329 operation, std::move(writer), source_fd, data, count);
330}
331
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400332void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) {
333 // No need to call fsync/sync, as CowWriter flushes after a label is added
334 // added.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500335 // if cow_writer_ failed, that means Init() failed. This function shouldn't be
336 // called if Init() fails.
337 TEST_AND_RETURN(cow_writer_ != nullptr);
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400338 cow_writer_->AddLabel(next_op_index);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400339}
340
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400341[[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() {
342 // Add a hardcoded magic label to indicate end of all install ops. This label
343 // is needed by filesystem verification, don't remove.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500344 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang9e5e1ed2021-09-28 14:19:16 -0700345 TEST_AND_RETURN_FALSE(cow_writer_->AddLabel(kEndOfInstallLabel));
346 TEST_AND_RETURN_FALSE(cow_writer_->Finalize());
Kelvin Zhang02fe6622021-11-01 16:37:58 -0700347 TEST_AND_RETURN_FALSE(cow_writer_->VerifyMergeOps());
348 return true;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400349}
350
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400351VABCPartitionWriter::~VABCPartitionWriter() {
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500352 Close();
353}
354
355int VABCPartitionWriter::Close() {
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500356 if (cow_writer_) {
357 cow_writer_->Finalize();
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500358 cow_writer_ = nullptr;
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500359 }
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500360 return 0;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400361}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400362
363} // namespace chromeos_update_engine