blob: 1ae82b5c042e3311f0acc5936be4404c7605df23 [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
Kelvin Zhang76f10b82021-06-25 18:45:46 -040026#include <brillo/secure_blob.h>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040027#include <libsnapshot/cow_writer.h>
28
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040029#include "update_engine/common/cow_operation_convert.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040030#include "update_engine/common/utils.h"
Kelvin Zhang76f10b82021-06-25 18:45:46 -040031#include "update_engine/payload_consumer/block_extent_writer.h"
32#include "update_engine/payload_consumer/extent_map.h"
33#include "update_engine/payload_consumer/extent_reader.h"
Kelvin Zhang7a265752020-10-29 15:51:35 -040034#include "update_engine/payload_consumer/file_descriptor.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040035#include "update_engine/payload_consumer/install_plan.h"
36#include "update_engine/payload_consumer/partition_writer.h"
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040037#include "update_engine/payload_consumer/snapshot_extent_writer.h"
Kelvin Zhang76f10b82021-06-25 18:45:46 -040038#include "update_engine/payload_consumer/xor_extent_writer.h"
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040039#include "update_engine/payload_generator/extent_ranges.h"
40#include "update_engine/payload_generator/extent_utils.h"
41#include "update_engine/update_metadata.pb.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040042
43namespace chromeos_update_engine {
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040044// Expected layout of COW file:
45// === Beginning of Cow Image ===
46// All Source Copy Operations
47// ========== Label 0 ==========
48// Operation 0 in PartitionUpdate
49// ========== Label 1 ==========
50// Operation 1 in PartitionUpdate
51// ========== label 2 ==========
52// Operation 2 in PartitionUpdate
53// ========== label 3 ==========
54// .
55// .
56// .
57
58// When resuming, pass |next_op_index_| as label to
59// |InitializeWithAppend|.
60// For example, suppose we finished writing SOURCE_COPY, and we finished writing
61// operation 2 completely. Update is suspended when we are half way through
62// operation 3.
63// |cnext_op_index_| would be 3, so we pass 3 as
64// label to |InitializeWithAppend|. The CowWriter will retain all data before
65// label 3, Which contains all operation 2's data, but none of operation 3's
66// data.
67
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040068using android::snapshot::ICowWriter;
69using ::google::protobuf::RepeatedPtrField;
70
Kelvin Zhang76f10b82021-06-25 18:45:46 -040071// Compute XOR map, a map from dst extent to corresponding merge operation
72static ExtentMap<const CowMergeOperation*, ExtentLess> ComputeXorMap(
73 const RepeatedPtrField<CowMergeOperation>& merge_ops) {
74 ExtentMap<const CowMergeOperation*, ExtentLess> xor_map;
75 for (const auto& merge_op : merge_ops) {
76 if (merge_op.type() == CowMergeOperation::COW_XOR) {
77 xor_map.AddExtent(merge_op.dst_extent(), &merge_op);
78 }
79 }
80 return xor_map;
81}
82
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050083VABCPartitionWriter::VABCPartitionWriter(
84 const PartitionUpdate& partition_update,
85 const InstallPlan::Partition& install_part,
86 DynamicPartitionControlInterface* dynamic_control,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040087 size_t block_size)
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050088 : partition_update_(partition_update),
89 install_part_(install_part),
90 dynamic_control_(dynamic_control),
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050091 block_size_(block_size),
92 executor_(block_size),
93 verified_source_fd_(block_size, install_part.source_path) {}
94
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040095bool VABCPartitionWriter::Init(const InstallPlan* install_plan,
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040096 bool source_may_exist,
97 size_t next_op_index) {
Kelvin Zhang76f10b82021-06-25 18:45:46 -040098 xor_map_ = ComputeXorMap(partition_update_.merge_operations());
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040099 TEST_AND_RETURN_FALSE(install_plan != nullptr);
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700100 if (source_may_exist && install_part_.source_size > 0) {
101 TEST_AND_RETURN_FALSE(!install_part_.source_path.empty());
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500102 TEST_AND_RETURN_FALSE(verified_source_fd_.Open());
103 }
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500104 std::optional<std::string> source_path;
105 if (!install_part_.source_path.empty()) {
106 // TODO(zhangkelvin) Make |source_path| a std::optional<std::string>
107 source_path = install_part_.source_path;
108 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400109 cow_writer_ = dynamic_control_->OpenCowWriter(
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500110 install_part_.name, source_path, install_plan->is_resume);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400111 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400112
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400113 // ===== Resume case handling code goes here ====
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400114 // It is possible that the SOURCE_COPY are already written but
115 // |next_op_index_| is still 0. In this case we discard previously written
116 // SOURCE_COPY, and start over.
117 if (install_plan->is_resume && next_op_index > 0) {
118 LOG(INFO) << "Resuming update on partition `"
119 << partition_update_.partition_name() << "` op index "
120 << next_op_index;
121 TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op_index));
122 return true;
123 } else {
124 TEST_AND_RETURN_FALSE(cow_writer_->Initialize());
125 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400126
127 // ==============================================
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400128 if (!partition_update_.merge_operations().empty()) {
129 TEST_AND_RETURN_FALSE(WriteMergeSequence(
130 partition_update_.merge_operations(), cow_writer_.get()));
131 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400132
133 // TODO(zhangkelvin) Rewrite this in C++20 coroutine once that's available.
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400134 // TODO(177104308) Don't write all COPY ops up-front if merge sequence is
135 // written
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400136 auto converted = ConvertToCowOperations(partition_update_.operations(),
137 partition_update_.merge_operations());
Kelvin Zhang7a265752020-10-29 15:51:35 -0400138
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500139 if (!converted.empty()) {
140 // Use source fd directly. Ideally we want to verify all extents used in
141 // source copy, but then what do we do if some extents contain correct
142 // hashes and some don't?
143 auto source_fd = std::make_shared<EintrSafeFileDescriptor>();
144 TEST_AND_RETURN_FALSE_ERRNO(
145 source_fd->Open(install_part_.source_path.c_str(), O_RDONLY));
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400146 TEST_AND_RETURN_FALSE(WriteSourceCopyCowOps(
147 block_size_, converted, cow_writer_.get(), source_fd));
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400148 cow_writer_->AddLabel(0);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500149 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400150 return true;
151}
152
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400153bool VABCPartitionWriter::WriteMergeSequence(
154 const RepeatedPtrField<CowMergeOperation>& merge_sequence,
155 ICowWriter* cow_writer) {
156 std::vector<uint32_t> blocks_merge_order;
157 for (const auto& merge_op : merge_sequence) {
158 const auto& dst_extent = merge_op.dst_extent();
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700159 const auto& src_extent = merge_op.src_extent();
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400160 // In place copy are basically noops, they do not need to be "merged" at
161 // all, don't include them in merge sequence.
162 if (merge_op.type() == CowMergeOperation::COW_COPY &&
163 merge_op.src_extent() == merge_op.dst_extent()) {
164 continue;
165 }
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700166 // libsnapshot prefers blocks in reverse order, so if this isn't a self
167 // overlapping OP, writing block in reverser order
168 // If this is a self-overlapping op and |dst_extent| comes after
169 // |src_extent|, we must write in reverse order for correctness.
170 // If this is self-overlapping op and |dst_extent| comes before
171 // |src_extent|, we must write in ascending order for correctness.
172 if (ExtentRanges::ExtentsOverlap(src_extent, dst_extent) &&
Kelvin Zhang82161a62021-09-20 17:54:16 -0700173 dst_extent.start_block() <= src_extent.start_block()) {
Kelvin Zhangd1f90bc2021-09-15 21:12:34 -0700174 for (size_t i = 0; i < dst_extent.num_blocks(); i++) {
175 blocks_merge_order.push_back(dst_extent.start_block() + i);
176 }
177 } else {
178 for (int i = dst_extent.num_blocks() - 1; i >= 0; i--) {
179 blocks_merge_order.push_back(dst_extent.start_block() + i);
180 }
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400181 }
182 }
183 return cow_writer->AddSequenceData(blocks_merge_order.size(),
184 blocks_merge_order.data());
185}
186
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400187bool VABCPartitionWriter::WriteSourceCopyCowOps(
Kelvin Zhang7a265752020-10-29 15:51:35 -0400188 size_t block_size,
189 const std::vector<CowOperation>& converted,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -0400190 ICowWriter* cow_writer,
Kelvin Zhang7a265752020-10-29 15:51:35 -0400191 FileDescriptorPtr source_fd) {
192 std::vector<uint8_t> buffer(block_size);
Kelvin Zhang24599af2020-10-27 13:44:25 -0400193
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400194 for (const auto& cow_op : converted) {
195 switch (cow_op.op) {
196 case CowOperation::CowCopy:
Kelvin Zhang4430ea52021-02-26 13:35:34 -0500197 if (cow_op.src_block == cow_op.dst_block) {
198 continue;
199 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400200 TEST_AND_RETURN_FALSE(
Kelvin Zhang7a265752020-10-29 15:51:35 -0400201 cow_writer->AddCopy(cow_op.dst_block, cow_op.src_block));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400202 break;
203 case CowOperation::CowReplace:
204 ssize_t bytes_read = 0;
Kelvin Zhang4b280242020-11-06 16:07:45 -0500205 TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd,
206 buffer.data(),
207 block_size,
208 cow_op.src_block * block_size,
209 &bytes_read));
Kelvin Zhang7a265752020-10-29 15:51:35 -0400210 if (bytes_read <= 0 || static_cast<size_t>(bytes_read) != block_size) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400211 LOG(ERROR) << "source_fd->Read failed: " << bytes_read;
212 return false;
213 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400214 TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks(
215 cow_op.dst_block, buffer.data(), block_size));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400216 break;
217 }
218 }
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400219
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400220 return true;
221}
222
223std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400224 return std::make_unique<SnapshotExtentWriter>(cow_writer_.get());
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400225}
226
227[[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation(
228 const InstallOperation& operation) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400229 for (const auto& extent : operation.dst_extents()) {
230 TEST_AND_RETURN_FALSE(
231 cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks()));
232 }
233 return true;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400234}
235
236[[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation(
237 const InstallOperation& operation, ErrorCode* error) {
238 // TODO(zhangkelvin) Probably just ignore SOURCE_COPY? They should be taken
239 // care of during Init();
240 return true;
241}
242
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500243bool VABCPartitionWriter::PerformReplaceOperation(const InstallOperation& op,
244 const void* data,
245 size_t count) {
246 // Setup the ExtentWriter stack based on the operation type.
247 std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter();
248
249 return executor_.ExecuteReplaceOperation(op, std::move(writer), data, count);
250}
251
Tianjie8e0090d2021-08-30 22:35:21 -0700252bool VABCPartitionWriter::PerformDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500253 const InstallOperation& operation,
254 ErrorCode* error,
255 const void* data,
256 size_t count) {
257 FileDescriptorPtr source_fd =
258 verified_source_fd_.ChooseSourceFD(operation, error);
259 TEST_AND_RETURN_FALSE(source_fd != nullptr);
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400260 TEST_AND_RETURN_FALSE(source_fd->IsOpen());
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500261
Kelvin Zhang76f10b82021-06-25 18:45:46 -0400262 std::unique_ptr<ExtentWriter> writer =
263 IsXorEnabled() ? std::make_unique<XORExtentWriter>(
264 operation, source_fd, cow_writer_.get(), xor_map_)
265 : CreateBaseExtentWriter();
Tianjie8e0090d2021-08-30 22:35:21 -0700266 return executor_.ExecuteDiffOperation(
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500267 operation, std::move(writer), source_fd, data, count);
268}
269
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400270void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) {
271 // No need to call fsync/sync, as CowWriter flushes after a label is added
272 // added.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500273 // if cow_writer_ failed, that means Init() failed. This function shouldn't be
274 // called if Init() fails.
275 TEST_AND_RETURN(cow_writer_ != nullptr);
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400276 cow_writer_->AddLabel(next_op_index);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400277}
278
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400279[[nodiscard]] bool VABCPartitionWriter::FinishedInstallOps() {
280 // Add a hardcoded magic label to indicate end of all install ops. This label
281 // is needed by filesystem verification, don't remove.
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500282 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang9e5e1ed2021-09-28 14:19:16 -0700283 TEST_AND_RETURN_FALSE(cow_writer_->AddLabel(kEndOfInstallLabel));
284 TEST_AND_RETURN_FALSE(cow_writer_->Finalize());
285 return cow_writer_->VerifyMergeOps();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400286}
287
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400288VABCPartitionWriter::~VABCPartitionWriter() {
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500289 Close();
290}
291
292int VABCPartitionWriter::Close() {
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500293 if (cow_writer_) {
294 cow_writer_->Finalize();
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500295 cow_writer_ = nullptr;
Kelvin Zhang6a4d1ec2021-02-04 16:28:48 -0500296 }
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500297 return 0;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400298}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400299
300} // namespace chromeos_update_engine