blob: a11bdf4190bc294b3b507eb0fc3b2701c3c7a485 [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 Zhang94f51cc2020-09-25 11:34:49 -040036#include "update_engine/payload_consumer/install_plan.h"
37#include "update_engine/payload_consumer/partition_writer.h"
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040038#include "update_engine/payload_consumer/snapshot_extent_writer.h"
Kelvin Zhang76f10b82021-06-25 18:45:46 -040039#include "update_engine/payload_consumer/xor_extent_writer.h"
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040040#include "update_engine/payload_generator/extent_ranges.h"
41#include "update_engine/payload_generator/extent_utils.h"
42#include "update_engine/update_metadata.pb.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040043
44namespace chromeos_update_engine {
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040045// Expected layout of COW file:
46// === Beginning of Cow Image ===
47// All Source Copy Operations
48// ========== Label 0 ==========
49// Operation 0 in PartitionUpdate
50// ========== Label 1 ==========
51// Operation 1 in PartitionUpdate
52// ========== label 2 ==========
53// Operation 2 in PartitionUpdate
54// ========== label 3 ==========
55// .
56// .
57// .
58
59// When resuming, pass |next_op_index_| as label to
60// |InitializeWithAppend|.
61// For example, suppose we finished writing SOURCE_COPY, and we finished writing
62// operation 2 completely. Update is suspended when we are half way through
63// operation 3.
64// |cnext_op_index_| would be 3, so we pass 3 as
65// label to |InitializeWithAppend|. The CowWriter will retain all data before
66// label 3, Which contains all operation 2's data, but none of operation 3's
67// data.
68
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040069using android::snapshot::ICowWriter;
70using ::google::protobuf::RepeatedPtrField;
71
Kelvin Zhang76f10b82021-06-25 18:45:46 -040072// Compute XOR map, a map from dst extent to corresponding merge operation
73static ExtentMap<const CowMergeOperation*, ExtentLess> ComputeXorMap(
74 const RepeatedPtrField<CowMergeOperation>& merge_ops) {
75 ExtentMap<const CowMergeOperation*, ExtentLess> xor_map;
76 for (const auto& merge_op : merge_ops) {
77 if (merge_op.type() == CowMergeOperation::COW_XOR) {
78 xor_map.AddExtent(merge_op.dst_extent(), &merge_op);
79 }
80 }
81 return xor_map;
82}
83
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050084VABCPartitionWriter::VABCPartitionWriter(
85 const PartitionUpdate& partition_update,
86 const InstallPlan::Partition& install_part,
87 DynamicPartitionControlInterface* dynamic_control,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040088 size_t block_size)
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050089 : partition_update_(partition_update),
90 install_part_(install_part),
91 dynamic_control_(dynamic_control),
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050092 block_size_(block_size),
93 executor_(block_size),
94 verified_source_fd_(block_size, install_part.source_path) {}
95
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040096bool VABCPartitionWriter::Init(const InstallPlan* install_plan,
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040097 bool source_may_exist,
98 size_t next_op_index) {
Kelvin Zhang76f10b82021-06-25 18:45:46 -040099 xor_map_ = ComputeXorMap(partition_update_.merge_operations());
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400100 TEST_AND_RETURN_FALSE(install_plan != nullptr);
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700101 if (source_may_exist && install_part_.source_size > 0) {
102 TEST_AND_RETURN_FALSE(!install_part_.source_path.empty());
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500103 TEST_AND_RETURN_FALSE(verified_source_fd_.Open());
104 }
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500105 std::optional<std::string> source_path;
106 if (!install_part_.source_path.empty()) {
107 // TODO(zhangkelvin) Make |source_path| a std::optional<std::string>
108 source_path = install_part_.source_path;
109 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400110 cow_writer_ = dynamic_control_->OpenCowWriter(
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500111 install_part_.name, source_path, install_plan->is_resume);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400112 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400113
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400114 // ===== Resume case handling code goes here ====
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400115 // It is possible that the SOURCE_COPY are already written but
116 // |next_op_index_| is still 0. In this case we discard previously written
117 // SOURCE_COPY, and start over.
118 if (install_plan->is_resume && next_op_index > 0) {
119 LOG(INFO) << "Resuming update on partition `"
120 << partition_update_.partition_name() << "` op index "
121 << next_op_index;
122 TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op_index));
123 return true;
124 } else {
125 TEST_AND_RETURN_FALSE(cow_writer_->Initialize());
126 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400127
128 // ==============================================
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400129 if (!partition_update_.merge_operations().empty()) {
Kelvin Zhang5d74b722021-09-29 15:24:26 -0700130 if (IsXorEnabled()) {
131 LOG(INFO) << "VABC XOR enabled for partition "
132 << partition_update_.partition_name();
133 TEST_AND_RETURN_FALSE(WriteMergeSequence(
134 partition_update_.merge_operations(), cow_writer_.get()));
135 }
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400136 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400137
138 // TODO(zhangkelvin) Rewrite this in C++20 coroutine once that's available.
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400139 // TODO(177104308) Don't write all COPY ops up-front if merge sequence is
140 // written
Kelvin Zhang5d74b722021-09-29 15:24:26 -0700141 const auto converted = ConvertToCowOperations(
142 partition_update_.operations(), partition_update_.merge_operations());
Kelvin Zhang7a265752020-10-29 15:51:35 -0400143
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500144 if (!converted.empty()) {
145 // Use source fd directly. Ideally we want to verify all extents used in
146 // source copy, but then what do we do if some extents contain correct
147 // hashes and some don't?
148 auto source_fd = std::make_shared<EintrSafeFileDescriptor>();
149 TEST_AND_RETURN_FALSE_ERRNO(
150 source_fd->Open(install_part_.source_path.c_str(), O_RDONLY));
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400151 TEST_AND_RETURN_FALSE(WriteSourceCopyCowOps(
152 block_size_, converted, cow_writer_.get(), source_fd));
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400153 cow_writer_->AddLabel(0);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500154 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400155 return true;
156}
157
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400158bool VABCPartitionWriter::WriteMergeSequence(
159 const RepeatedPtrField<CowMergeOperation>& merge_sequence,
160 ICowWriter* cow_writer) {
161 std::vector<uint32_t> blocks_merge_order;
162 for (const auto& merge_op : merge_sequence) {
163 const auto& dst_extent = merge_op.dst_extent();
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700164 const auto& src_extent = merge_op.src_extent();
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400165 // In place copy are basically noops, they do not need to be "merged" at
166 // all, don't include them in merge sequence.
167 if (merge_op.type() == CowMergeOperation::COW_COPY &&
168 merge_op.src_extent() == merge_op.dst_extent()) {
169 continue;
170 }
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000171
172 const bool extent_overlap =
173 ExtentRanges::ExtentsOverlap(src_extent, dst_extent);
174 // TODO(193863443) Remove this check once this feature
175 // lands on all pixel devices.
176 const bool is_ascending = android::base::GetBoolProperty(
177 "ro.virtual_ab.userspace.snapshots.enabled", false);
178
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700179 // If this is a self-overlapping op and |dst_extent| comes after
180 // |src_extent|, we must write in reverse order for correctness.
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000181 //
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700182 // If this is self-overlapping op and |dst_extent| comes before
183 // |src_extent|, we must write in ascending order for correctness.
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000184 //
185 // If this isn't a self overlapping op, write block in ascending order
186 // if userspace snapshots are enabled
187 if (extent_overlap) {
188 if (dst_extent.start_block() <= src_extent.start_block()) {
189 for (size_t i = 0; i < dst_extent.num_blocks(); i++) {
190 blocks_merge_order.push_back(dst_extent.start_block() + i);
191 }
192 } else {
193 for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) {
194 blocks_merge_order.push_back(dst_extent.start_block() + i);
195 }
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700196 }
197 } else {
Akilesh Kailash3e6e7df2021-11-18 23:29:15 +0000198 if (is_ascending) {
199 for (size_t i = 0; i < dst_extent.num_blocks(); i++) {
200 blocks_merge_order.push_back(dst_extent.start_block() + i);
201 }
202 } else {
203 for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) {
204 blocks_merge_order.push_back(dst_extent.start_block() + i);
205 }
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700206 }
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400207 }
208 }
209 return cow_writer->AddSequenceData(blocks_merge_order.size(),
210 blocks_merge_order.data());
211}
212
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400213bool VABCPartitionWriter::WriteSourceCopyCowOps(
Kelvin Zhang7a265752020-10-29 15:51:35 -0400214 size_t block_size,
215 const std::vector<CowOperation>& converted,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400216 ICowWriter* cow_writer,
Kelvin Zhang7a265752020-10-29 15:51:35 -0400217 FileDescriptorPtr source_fd) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400218 for (const auto& cow_op : converted) {
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700219 std::vector<uint8_t> buffer;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400220 switch (cow_op.op) {
221 case CowOperation::CowCopy:
Kelvin Zhang4430ea52021-02-26 13:35:34 -0500222 if (cow_op.src_block == cow_op.dst_block) {
223 continue;
224 }
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700225 // Add blocks in reverse order, because snapused specifically prefers
226 // this ordering. Since we already eliminated all self-overlapping
227 // SOURCE_COPY during delta generation, this should be safe to do.
228 for (size_t i = cow_op.block_count; i > 0; i--) {
229 TEST_AND_RETURN_FALSE(cow_writer->AddCopy(cow_op.dst_block + i - 1,
230 cow_op.src_block + i - 1));
231 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400232 break;
233 case CowOperation::CowReplace:
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700234 buffer.resize(block_size * cow_op.block_count);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400235 ssize_t bytes_read = 0;
Kelvin Zhang4b280242020-11-06 16:07:45 -0500236 TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd,
237 buffer.data(),
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700238 block_size * cow_op.block_count,
Kelvin Zhang4b280242020-11-06 16:07:45 -0500239 cow_op.src_block * block_size,
240 &bytes_read));
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700241 if (bytes_read <= 0 ||
242 static_cast<size_t>(bytes_read) != buffer.size()) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400243 LOG(ERROR) << "source_fd->Read failed: " << bytes_read;
244 return false;
245 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400246 TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks(
Kelvin Zhangc14676a2021-10-28 16:38:20 -0700247 cow_op.dst_block, buffer.data(), buffer.size()));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400248 break;
249 }
250 }
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400251
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400252 return true;
253}
254
255std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400256 return std::make_unique<SnapshotExtentWriter>(cow_writer_.get());
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400257}
258
259[[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation(
260 const InstallOperation& operation) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400261 for (const auto& extent : operation.dst_extents()) {
262 TEST_AND_RETURN_FALSE(
263 cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks()));
264 }
265 return true;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400266}
267
268[[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation(
269 const InstallOperation& operation, ErrorCode* error) {
270 // TODO(zhangkelvin) Probably just ignore SOURCE_COPY? They should be taken
271 // care of during Init();
272 return true;
273}
274
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500275bool VABCPartitionWriter::PerformReplaceOperation(const InstallOperation& op,
276 const void* data,
277 size_t count) {
278 // Setup the ExtentWriter stack based on the operation type.
279 std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter();
280
281 return executor_.ExecuteReplaceOperation(op, std::move(writer), data, count);
282}
283
Tianjie8e0090d2021-08-30 22:35:21 -0700284bool VABCPartitionWriter::PerformDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500285 const InstallOperation& operation,
286 ErrorCode* error,
287 const void* data,
288 size_t count) {
289 FileDescriptorPtr source_fd =
290 verified_source_fd_.ChooseSourceFD(operation, error);
291 TEST_AND_RETURN_FALSE(source_fd != nullptr);
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400292 TEST_AND_RETURN_FALSE(source_fd->IsOpen());
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500293
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400294 std::unique_ptr<ExtentWriter> writer =
295 IsXorEnabled() ? std::make_unique<XORExtentWriter>(
296 operation, source_fd, cow_writer_.get(), xor_map_)
297 : CreateBaseExtentWriter();
Tianjie8e0090d2021-08-30 22:35:21 -0700298 return executor_.ExecuteDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500299 operation, std::move(writer), source_fd, data, count);
300}
301
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400302void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) {
303 // No need to call fsync/sync, as CowWriter flushes after a label is added
304 // added.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500305 // if cow_writer_ failed, that means Init() failed. This function shouldn't be
306 // called if Init() fails.
307 TEST_AND_RETURN(cow_writer_ != nullptr);
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400308 cow_writer_->AddLabel(next_op_index);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400309}
310
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400311[[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() {
312 // Add a hardcoded magic label to indicate end of all install ops. This label
313 // is needed by filesystem verification, don't remove.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500314 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang9e5e1ed2021-09-28 14:19:16 -0700315 TEST_AND_RETURN_FALSE(cow_writer_->AddLabel(kEndOfInstallLabel));
316 TEST_AND_RETURN_FALSE(cow_writer_->Finalize());
317 return cow_writer_->VerifyMergeOps();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400318}
319
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400320VABCPartitionWriter::~VABCPartitionWriter() {
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500321 Close();
322}
323
324int VABCPartitionWriter::Close() {
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500325 if (cow_writer_) {
326 cow_writer_->Finalize();
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500327 cow_writer_ = nullptr;
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500328 }
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500329 return 0;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400330}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400331
332} // namespace chromeos_update_engine