blob: e8994b4006f9050d0857a8b3d6c50d4ff979ddf9 [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
19#include <memory>
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040020#include <vector>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040021
22#include <libsnapshot/cow_writer.h>
23
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040024#include "update_engine/common/cow_operation_convert.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040025#include "update_engine/common/utils.h"
26#include "update_engine/payload_consumer/extent_writer.h"
Kelvin Zhang7a265752020-10-29 15:51:35 -040027#include "update_engine/payload_consumer/file_descriptor.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040028#include "update_engine/payload_consumer/install_plan.h"
29#include "update_engine/payload_consumer/partition_writer.h"
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040030#include "update_engine/payload_consumer/snapshot_extent_writer.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040031
32namespace chromeos_update_engine {
Kelvin Zhang24599af2020-10-27 13:44:25 -040033// Expected layout of COW file:
34// === Beginning of Cow Image ===
35// All Source Copy Operations
36// ========== Label 0 ==========
37// Operation 0 in PartitionUpdate
38// ========== Label 1 ==========
39// Operation 1 in PartitionUpdate
40// ========== label 2 ==========
41// Operation 2 in PartitionUpdate
42// ========== label 3 ==========
43// .
44// .
45// .
46
47// When resuming, pass |kPrefsUpdateStatePartitionNextOperation| as label to
48// |InitializeWithAppend|.
49// For example, suppose we finished writing SOURCE_COPY, and we finished writing
50// operation 2 completely. Update is suspended when we are half way through
51// operation 3.
52// |kPrefsUpdateStatePartitionNextOperation| would be 3, so we pass 3 as
53// label to |InitializeWithAppend|. The CowWriter will retain all data before
54// label 3, Which contains all operation 2's data, but none of operation 3's
55// data.
56
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040057bool VABCPartitionWriter::Init(const InstallPlan* install_plan,
58 bool source_may_exist) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040059 TEST_AND_RETURN_FALSE(install_plan != nullptr);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040060 TEST_AND_RETURN_FALSE(PartitionWriter::Init(install_plan, source_may_exist));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040061 cow_writer_ = dynamic_control_->OpenCowWriter(
62 install_part_.name, install_part_.source_path, install_plan->is_resume);
63 TEST_AND_RETURN_FALSE(cow_writer_ != nullptr);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040064
Kelvin Zhang24599af2020-10-27 13:44:25 -040065 // Emit a label before writing SOURCE_COPY. When resuming,
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040066 // use pref or CowWriter::GetLastLabel to determine if the SOURCE_COPY ops are
67 // written. No need to handle SOURCE_COPY operations when resuming.
68
69 // ===== Resume case handling code goes here ====
Kelvin Zhang24599af2020-10-27 13:44:25 -040070 if (install_plan->is_resume) {
71 int64_t next_op = 0;
72 if (!prefs_->GetInt64(kPrefsUpdateStatePartitionNextOperation, &next_op)) {
73 LOG(ERROR)
74 << "Resuming an update but can't fetch |next_op| from saved prefs.";
75 return false;
76 }
77 if (next_op < 0) {
78 TEST_AND_RETURN_FALSE(cow_writer_->Initialize());
79 } else {
80 TEST_AND_RETURN_FALSE(cow_writer_->InitializeAppend(next_op));
81 return true;
82 }
83 } else {
84 TEST_AND_RETURN_FALSE(cow_writer_->Initialize());
85 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040086
87 // ==============================================
Kelvin Zhang24599af2020-10-27 13:44:25 -040088 TEST_AND_RETURN_FALSE(
89 prefs_->SetInt64(kPrefsUpdateStatePartitionNextOperation, -1));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040090
91 // TODO(zhangkelvin) Rewrite this in C++20 coroutine once that's available.
92 auto converted = ConvertToCowOperations(partition_update_.operations(),
93 partition_update_.merge_operations());
Kelvin Zhang7a265752020-10-29 15:51:35 -040094
95 WriteAllCowOps(block_size_, converted, cow_writer_.get(), source_fd_);
96 // Emit label 0 to mark end of all SOURCE_COPY operations
97 cow_writer_->AddLabel(0);
98 TEST_AND_RETURN_FALSE(
99 prefs_->SetInt64(kPrefsUpdateStatePartitionNextOperation, 0));
100 return true;
101}
102
103bool VABCPartitionWriter::WriteAllCowOps(
104 size_t block_size,
105 const std::vector<CowOperation>& converted,
106 android::snapshot::ICowWriter* cow_writer,
107 FileDescriptorPtr source_fd) {
108 std::vector<uint8_t> buffer(block_size);
Kelvin Zhang24599af2020-10-27 13:44:25 -0400109
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400110 for (const auto& cow_op : converted) {
111 switch (cow_op.op) {
112 case CowOperation::CowCopy:
113 TEST_AND_RETURN_FALSE(
Kelvin Zhang7a265752020-10-29 15:51:35 -0400114 cow_writer->AddCopy(cow_op.dst_block, cow_op.src_block));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400115 break;
116 case CowOperation::CowReplace:
117 ssize_t bytes_read = 0;
Kelvin Zhang4b280242020-11-06 16:07:45 -0500118 TEST_AND_RETURN_FALSE(utils::ReadAll(source_fd,
119 buffer.data(),
120 block_size,
121 cow_op.src_block * block_size,
122 &bytes_read));
Kelvin Zhang7a265752020-10-29 15:51:35 -0400123 if (bytes_read <= 0 || static_cast<size_t>(bytes_read) != block_size) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400124 LOG(ERROR) << "source_fd->Read failed: " << bytes_read;
125 return false;
126 }
Kelvin Zhang7a265752020-10-29 15:51:35 -0400127 TEST_AND_RETURN_FALSE(cow_writer->AddRawBlocks(
128 cow_op.dst_block, buffer.data(), block_size));
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400129 break;
130 }
131 }
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400132 return true;
133}
134
135std::unique_ptr<ExtentWriter> VABCPartitionWriter::CreateBaseExtentWriter() {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400136 return std::make_unique<SnapshotExtentWriter>(cow_writer_.get());
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400137}
138
139[[nodiscard]] bool VABCPartitionWriter::PerformZeroOrDiscardOperation(
140 const InstallOperation& operation) {
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400141 for (const auto& extent : operation.dst_extents()) {
142 TEST_AND_RETURN_FALSE(
143 cow_writer_->AddZeroBlocks(extent.start_block(), extent.num_blocks()));
144 }
145 return true;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400146}
147
148[[nodiscard]] bool VABCPartitionWriter::PerformSourceCopyOperation(
149 const InstallOperation& operation, ErrorCode* error) {
150 // TODO(zhangkelvin) Probably just ignore SOURCE_COPY? They should be taken
151 // care of during Init();
152 return true;
153}
154
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400155bool VABCPartitionWriter::Flush() {
Kelvin Zhang24599af2020-10-27 13:44:25 -0400156 // No need to call fsync/sync, as CowWriter flushes after a label is added
157 // added.
158 int64_t next_op = 0;
159 // |kPrefsUpdateStatePartitionNextOperation| will be maintained and set by
160 // CheckpointUpdateProgress()
161 TEST_AND_RETURN_FALSE(
162 prefs_->GetInt64(kPrefsUpdateStatePartitionNextOperation, &next_op));
163 // +1 because label 0 is reserved for SOURCE_COPY. See beginning of this
164 // file for explanation for cow format.
165 cow_writer_->AddLabel(next_op + 1);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400166 return true;
167}
168
Kelvin Zhang24599af2020-10-27 13:44:25 -0400169void VABCPartitionWriter::CheckpointUpdateProgress(size_t next_op_index) {
170 prefs_->SetInt64(kPrefsUpdateStatePartitionNextOperation, next_op_index);
171}
172
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400173VABCPartitionWriter::~VABCPartitionWriter() {
Kelvin Zhang24599af2020-10-27 13:44:25 -0400174 // Reset |kPrefsUpdateStatePartitionNextOperation| once we finished a
175 // partition.
176 prefs_->SetInt64(kPrefsUpdateStatePartitionNextOperation, -1);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400177 cow_writer_->Finalize();
178}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400179
180} // namespace chromeos_update_engine