blob: df9c0a66dd72db647b55e0d6cd7b9449f2f1ce2b [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/extent_map.h"
Kelvin Zhang7a265752020-10-29 15:51:35 -040033#include "update_engine/payload_consumer/file_descriptor.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040034#include "update_engine/payload_consumer/install_plan.h"
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040035#include "update_engine/payload_consumer/snapshot_extent_writer.h"
Kelvin Zhang76f10b82021-06-25 18:45:46 -040036#include "update_engine/payload_consumer/xor_extent_writer.h"
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040037#include "update_engine/payload_generator/extent_ranges.h"
38#include "update_engine/payload_generator/extent_utils.h"
39#include "update_engine/update_metadata.pb.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040040
41namespace chromeos_update_engine {
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040042// Expected layout of COW file:
43// === Beginning of Cow Image ===
44// All Source Copy Operations
45// ========== Label 0 ==========
46// Operation 0 in PartitionUpdate
47// ========== Label 1 ==========
48// Operation 1 in PartitionUpdate
49// ========== label 2 ==========
50// Operation 2 in PartitionUpdate
51// ========== label 3 ==========
52// .
53// .
54// .
55
56// When resuming, pass |next_op_index_| as label to
57// |InitializeWithAppend|.
58// For example, suppose we finished writing SOURCE_COPY, and we finished writing
59// operation 2 completely. Update is suspended when we are half way through
60// operation 3.
61// |cnext_op_index_| would be 3, so we pass 3 as
62// label to |InitializeWithAppend|. The CowWriter will retain all data before
63// label 3, Which contains all operation 2's data, but none of operation 3's
64// data.
65
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040066using android::snapshot::ICowWriter;
67using ::google::protobuf::RepeatedPtrField;
68
Kelvin Zhang76f10b82021-06-25 18:45:46 -040069// Compute XOR map, a map from dst extent to corresponding merge operation
70static ExtentMap<const CowMergeOperation*, ExtentLess> ComputeXorMap(
71 const RepeatedPtrField<CowMergeOperation>& merge_ops) {
72 ExtentMap<const CowMergeOperation*, ExtentLess> xor_map;
73 for (const auto& merge_op : merge_ops) {
74 if (merge_op.type() == CowMergeOperation::COW_XOR) {
75 xor_map.AddExtent(merge_op.dst_extent(), &merge_op);
76 }
77 }
78 return xor_map;
79}
80
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050081VABCPartitionWriter::VABCPartitionWriter(
82 const PartitionUpdate& partition_update,
83 const InstallPlan::Partition& install_part,
84 DynamicPartitionControlInterface* dynamic_control,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040085 size_t block_size)
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050086 : partition_update_(partition_update),
87 install_part_(install_part),
88 dynamic_control_(dynamic_control),
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050089 block_size_(block_size),
90 executor_(block_size),
Kelvin Zhangd11e2fc2022-10-24 15:40:30 -070091 verified_source_fd_(block_size, install_part.source_path) {
92 for (const auto& cow_op : partition_update_.merge_operations()) {
93 if (cow_op.type() != CowMergeOperation::COW_COPY) {
94 continue;
95 }
96 copy_blocks_.AddExtent(cow_op.dst_extent());
97 }
98}
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050099
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400100bool VABCPartitionWriter::Init(const InstallPlan* install_plan,
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400101 bool source_may_exist,
102 size_t next_op_index) {
Kelvin Zhang1c4b9812022-04-06 17:29:00 -0700103 if (dynamic_control_->GetVirtualAbCompressionXorFeatureFlag().IsEnabled()) {
104 xor_map_ = ComputeXorMap(partition_update_.merge_operations());
105 if (xor_map_.size() > 0) {
106 LOG(INFO) << "Virtual AB Compression with XOR is enabled";
107 } else {
108 LOG(INFO) << "Device supports Virtual AB compression with XOR, but OTA "
109 "package does not.";
110 }
111 } else {
112 LOG(INFO) << "Virtual AB Compression with XOR is disabled.";
113 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400114 TEST_AND_RETURN_FALSE(install_plan != nullptr);
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700115 if (source_may_exist && install_part_.source_size > 0) {
116 TEST_AND_RETURN_FALSE(!install_part_.source_path.empty());
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500117 TEST_AND_RETURN_FALSE(verified_source_fd_.Open());
118 }
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500119 std::optional<std::string> source_path;
120 if (!install_part_.source_path.empty()) {
121 // TODO(zhangkelvin) Make |source_path| a std::optional<std::string>
122 source_path = install_part_.source_path;
123 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400124 cow_writer_ = dynamic_control_->OpenCowWriter(
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500125 install_part_.name, source_path, install_plan->is_resume);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400126 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400127
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400128 // ===== Resume case handling code goes here ====
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400129 // It is possible that the SOURCE_COPY are already written but
130 // |next_op_index_| is still 0. In this case we discard previously written
131 // SOURCE_COPY, and start over.
132 if (install_plan->is_resume && next_op_index > 0) {
133 LOG(INFO) << "Resuming update on partition `"
134 << partition_update_.partition_name() << "` op index "
135 << next_op_index;
136 TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op_index));
137 return true;
138 } else {
139 TEST_AND_RETURN_FALSE(cow_writer_->Initialize());
140 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400141
142 // ==============================================
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400143 if (!partition_update_.merge_operations().empty()) {
Kelvin Zhang5d74b722021-09-29 15:24:26 -0700144 if (IsXorEnabled()) {
145 LOG(INFO) << "VABC XOR enabled for partition "
146 << partition_update_.partition_name();
147 TEST_AND_RETURN_FALSE(WriteMergeSequence(
148 partition_update_.merge_operations(), cow_writer_.get()));
149 }
Kelvin Zhangd11e2fc2022-10-24 15:40:30 -0700150 const bool userSnapshots = android::base::GetBoolProperty(
151 "ro.virtual_ab.userspace.snapshots.enabled", false);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400152
Kelvin Zhangd11e2fc2022-10-24 15:40:30 -0700153 for (const auto& cow_op : partition_update_.merge_operations()) {
154 if (cow_op.type() != CowMergeOperation::COW_COPY) {
155 continue;
156 }
157 if (cow_op.dst_extent() == cow_op.src_extent()) {
158 continue;
159 }
160 if (userSnapshots) {
161 TEST_AND_RETURN_FALSE(cow_op.src_extent().num_blocks() != 0);
162 TEST_AND_RETURN_FALSE(
163 cow_writer_->AddCopy(cow_op.dst_extent().start_block(),
164 cow_op.src_extent().start_block(),
165 cow_op.src_extent().num_blocks()));
166 } else {
167 // Add blocks in reverse order, because snapused specifically prefers
168 // this ordering. Since we already eliminated all self-overlapping
169 // SOURCE_COPY during delta generation, this should be safe to do.
170 for (size_t i = cow_op.src_extent().num_blocks(); i > 0; i--) {
171 TEST_AND_RETURN_FALSE(
172 cow_writer_->AddCopy(cow_op.dst_extent().start_block() + i - 1,
173 cow_op.src_extent().start_block() + i - 1));
174 }
175 }
176 }
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400177 cow_writer_->AddLabel(0);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500178 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400179 return true;
180}
181
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400182bool VABCPartitionWriter::WriteMergeSequence(
183 const RepeatedPtrField<CowMergeOperation>& merge_sequence,
184 ICowWriter* cow_writer) {
185 std::vector<uint32_t> blocks_merge_order;
186 for (const auto& merge_op : merge_sequence) {
187 const auto& dst_extent = merge_op.dst_extent();
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700188 const auto& src_extent = merge_op.src_extent();
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400189 // In place copy are basically noops, they do not need to be "merged" at
190 // all, don't include them in merge sequence.
191 if (merge_op.type() == CowMergeOperation::COW_COPY &&
192 merge_op.src_extent() == merge_op.dst_extent()) {
193 continue;
194 }
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000195
196 const bool extent_overlap =
197 ExtentRanges::ExtentsOverlap(src_extent, dst_extent);
198 // TODO(193863443) Remove this check once this feature
199 // lands on all pixel devices.
200 const bool is_ascending = android::base::GetBoolProperty(
201 "ro.virtual_ab.userspace.snapshots.enabled", false);
202
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700203 // If this is a self-overlapping op and |dst_extent| comes after
204 // |src_extent|, we must write in reverse order for correctness.
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000205 //
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700206 // If this is self-overlapping op and |dst_extent| comes before
207 // |src_extent|, we must write in ascending order for correctness.
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000208 //
209 // If this isn't a self overlapping op, write block in ascending order
210 // if userspace snapshots are enabled
211 if (extent_overlap) {
212 if (dst_extent.start_block() <= src_extent.start_block()) {
213 for (size_t i = 0; i < dst_extent.num_blocks(); i++) {
214 blocks_merge_order.push_back(dst_extent.start_block() + i);
215 }
216 } else {
217 for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) {
218 blocks_merge_order.push_back(dst_extent.start_block() + i);
219 }
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700220 }
221 } else {
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000222 if (is_ascending) {
223 for (size_t i = 0; i < dst_extent.num_blocks(); i++) {
224 blocks_merge_order.push_back(dst_extent.start_block() + i);
225 }
226 } else {
227 for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) {
228 blocks_merge_order.push_back(dst_extent.start_block() + i);
229 }
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700230 }
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400231 }
232 }
233 return cow_writer->AddSequenceData(blocks_merge_order.size(),
234 blocks_merge_order.data());
235}
236
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400237std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400238 return std::make_unique<SnapshotExtentWriter>(cow_writer_.get());
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400239}
240
241[[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation(
242 const InstallOperation& operation) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400243 for (const auto& extent : operation.dst_extents()) {
244 TEST_AND_RETURN_FALSE(
245 cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks()));
246 }
247 return true;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400248}
249
250[[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation(
251 const InstallOperation& operation, ErrorCode* error) {
Kelvin Zhangb5bd0752022-03-10 15:45:25 -0800252 // COPY ops are already handled during Init(), no need to do actual work, but
253 // we still want to verify that all blocks contain expected data.
Kelvin Zhangd11e2fc2022-10-24 15:40:30 -0700254 auto source_fd = verified_source_fd_.ChooseSourceFD(operation, error);
255 TEST_AND_RETURN_FALSE(source_fd != nullptr);
256 std::vector<CowOperation> converted;
257
258 const auto& src_extents = operation.src_extents();
259 const auto& dst_extents = operation.dst_extents();
260 BlockIterator it1{src_extents};
261 BlockIterator it2{dst_extents};
262 while (!it1.is_end() && !it2.is_end()) {
263 const auto src_block = *it1;
264 const auto dst_block = *it2;
265 ++it1;
266 ++it2;
267 if (src_block == dst_block) {
268 continue;
269 }
270 if (!copy_blocks_.ContainsBlock(dst_block)) {
271 push_back(&converted,
272 {CowOperation::CowReplace, src_block, dst_block, 1});
273 }
Kelvin Zhangdc122bc2022-03-15 14:19:04 -0700274 }
Kelvin Zhangd11e2fc2022-10-24 15:40:30 -0700275 std::vector<uint8_t> buffer;
276 for (const auto& cow_op : converted) {
277 buffer.resize(block_size_ * cow_op.block_count);
278 ssize_t bytes_read = 0;
279 TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd,
280 buffer.data(),
281 block_size_ * cow_op.block_count,
282 cow_op.src_block * block_size_,
283 &bytes_read));
284 if (bytes_read <= 0 || static_cast<size_t>(bytes_read) != buffer.size()) {
285 LOG(ERROR) << "source_fd->Read failed: " << bytes_read;
286 return false;
287 }
288 TEST_AND_RETURN_FALSE(cow_writer_->AddRawBlocks(
289 cow_op.dst_block, buffer.data(), buffer.size()));
290 }
291 return true;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400292}
293
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500294bool VABCPartitionWriter::PerformReplaceOperation(const InstallOperation& op,
295 const void* data,
296 size_t count) {
297 // Setup the ExtentWriter stack based on the operation type.
298 std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter();
299
300 return executor_.ExecuteReplaceOperation(op, std::move(writer), data, count);
301}
302
Tianjie8e0090d2021-08-30 22:35:21 -0700303bool VABCPartitionWriter::PerformDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500304 const InstallOperation& operation,
305 ErrorCode* error,
306 const void* data,
307 size_t count) {
308 FileDescriptorPtr source_fd =
309 verified_source_fd_.ChooseSourceFD(operation, error);
310 TEST_AND_RETURN_FALSE(source_fd != nullptr);
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400311 TEST_AND_RETURN_FALSE(source_fd->IsOpen());
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500312
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400313 std::unique_ptr<ExtentWriter> writer =
314 IsXorEnabled() ? std::make_unique<XORExtentWriter>(
315 operation, source_fd, cow_writer_.get(), xor_map_)
316 : CreateBaseExtentWriter();
Tianjie8e0090d2021-08-30 22:35:21 -0700317 return executor_.ExecuteDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500318 operation, std::move(writer), source_fd, data, count);
319}
320
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400321void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) {
322 // No need to call fsync/sync, as CowWriter flushes after a label is added
323 // added.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500324 // if cow_writer_ failed, that means Init() failed. This function shouldn't be
325 // called if Init() fails.
326 TEST_AND_RETURN(cow_writer_ != nullptr);
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400327 cow_writer_->AddLabel(next_op_index);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400328}
329
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400330[[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() {
331 // Add a hardcoded magic label to indicate end of all install ops. This label
332 // is needed by filesystem verification, don't remove.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500333 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang9e5e1ed2021-09-28 14:19:16 -0700334 TEST_AND_RETURN_FALSE(cow_writer_->AddLabel(kEndOfInstallLabel));
335 TEST_AND_RETURN_FALSE(cow_writer_->Finalize());
Kelvin Zhang02fe6622021-11-01 16:37:58 -0700336 TEST_AND_RETURN_FALSE(cow_writer_->VerifyMergeOps());
337 return true;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400338}
339
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400340VABCPartitionWriter::~VABCPartitionWriter() {
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500341 Close();
342}
343
344int VABCPartitionWriter::Close() {
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500345 if (cow_writer_) {
Kelvin Zhangd11e2fc2022-10-24 15:40:30 -0700346 LOG(INFO) << "Finalizing " << partition_update_.partition_name()
347 << " COW image";
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500348 cow_writer_->Finalize();
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500349 cow_writer_ = nullptr;
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500350 }
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500351 return 0;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400352}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400353
354} // namespace chromeos_update_engine