1 //
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 #include "update_engine/aosp/cleanup_previous_update_action.h"
17
18 #include <chrono> // NOLINT(build/c++11) -- for merge times
19 #include <functional>
20 #include <string>
21 #include <type_traits>
22
23 #include <android-base/chrono_utils.h>
24 #include <android-base/properties.h>
25 #include <base/bind.h>
26
27 #ifndef __ANDROID_RECOVERY__
28 #include <statslog.h>
29 #endif
30
31 #include "update_engine/common/utils.h"
32 #include "update_engine/payload_consumer/delta_performer.h"
33
34 using android::base::GetBoolProperty;
35 using android::snapshot::ISnapshotManager;
36 using android::snapshot::SnapshotMergeStats;
37 using android::snapshot::UpdateState;
38 using brillo::MessageLoop;
39
40 constexpr char kBootCompletedProp[] = "sys.boot_completed";
41 // Interval to check sys.boot_completed.
42 constexpr auto kCheckBootCompletedInterval = base::TimeDelta::FromSeconds(2);
43 // Interval to check IBootControl::isSlotMarkedSuccessful
44 constexpr auto kCheckSlotMarkedSuccessfulInterval =
45 base::TimeDelta::FromSeconds(2);
46 // Interval to call SnapshotManager::ProcessUpdateState
47 constexpr auto kWaitForMergeInterval = base::TimeDelta::FromSeconds(2);
48
49 #ifdef __ANDROID_RECOVERY__
50 static constexpr bool kIsRecovery = true;
51 #else
52 static constexpr bool kIsRecovery = false;
53 #endif
54
55 namespace chromeos_update_engine {
56
CleanupPreviousUpdateAction(PrefsInterface * prefs,BootControlInterface * boot_control,android::snapshot::ISnapshotManager * snapshot,CleanupPreviousUpdateActionDelegateInterface * delegate)57 CleanupPreviousUpdateAction::CleanupPreviousUpdateAction(
58 PrefsInterface* prefs,
59 BootControlInterface* boot_control,
60 android::snapshot::ISnapshotManager* snapshot,
61 CleanupPreviousUpdateActionDelegateInterface* delegate)
62 : prefs_(prefs),
63 boot_control_(boot_control),
64 snapshot_(snapshot),
65 delegate_(delegate),
66 running_(false),
67 cancel_failed_(false),
68 last_percentage_(0),
69 merge_stats_(nullptr) {}
70
~CleanupPreviousUpdateAction()71 CleanupPreviousUpdateAction::~CleanupPreviousUpdateAction() {
72 StopActionInternal();
73 }
74
PerformAction()75 void CleanupPreviousUpdateAction::PerformAction() {
76 StartActionInternal();
77 }
78
TerminateProcessing()79 void CleanupPreviousUpdateAction::TerminateProcessing() {
80 StopActionInternal();
81 }
82
ResumeAction()83 void CleanupPreviousUpdateAction::ResumeAction() {
84 StartActionInternal();
85 }
86
SuspendAction()87 void CleanupPreviousUpdateAction::SuspendAction() {
88 StopActionInternal();
89 }
90
ActionCompleted(ErrorCode error_code)91 void CleanupPreviousUpdateAction::ActionCompleted(ErrorCode error_code) {
92 StopActionInternal();
93 ReportMergeStats();
94 metadata_device_ = nullptr;
95 }
96
Type() const97 std::string CleanupPreviousUpdateAction::Type() const {
98 return StaticType();
99 }
100
StaticType()101 std::string CleanupPreviousUpdateAction::StaticType() {
102 return "CleanupPreviousUpdateAction";
103 }
104
105 // This function is called at the beginning of all delayed functions. By
106 // resetting |scheduled_task_|, the delayed function acknowledges that the task
107 // has already been executed, therefore there's no need to cancel it in the
108 // future. This avoids StopActionInternal() from resetting task IDs in an
109 // unexpected way because task IDs could be reused.
AcknowledgeTaskExecuted()110 void CleanupPreviousUpdateAction::AcknowledgeTaskExecuted() {
111 if (scheduled_task_ != MessageLoop::kTaskIdNull) {
112 LOG(INFO) << "Executing task " << scheduled_task_;
113 }
114 scheduled_task_ = MessageLoop::kTaskIdNull;
115 }
116
117 // Check that scheduled_task_ is a valid task ID. Otherwise, terminate the
118 // action.
CheckTaskScheduled(std::string_view name)119 void CleanupPreviousUpdateAction::CheckTaskScheduled(std::string_view name) {
120 if (scheduled_task_ == MessageLoop::kTaskIdNull) {
121 LOG(ERROR) << "Unable to schedule " << name;
122 processor_->ActionComplete(this, ErrorCode::kError);
123 } else {
124 LOG(INFO) << "CleanupPreviousUpdateAction scheduled task ID "
125 << scheduled_task_ << " for " << name;
126 }
127 }
128
StopActionInternal()129 void CleanupPreviousUpdateAction::StopActionInternal() {
130 LOG(INFO) << "Stopping/suspending/completing CleanupPreviousUpdateAction";
131 running_ = false;
132
133 if (scheduled_task_ != MessageLoop::kTaskIdNull) {
134 if (MessageLoop::current()->CancelTask(scheduled_task_)) {
135 LOG(INFO) << "CleanupPreviousUpdateAction cancelled pending task ID "
136 << scheduled_task_;
137 } else {
138 LOG(ERROR) << "CleanupPreviousUpdateAction unable to cancel task ID "
139 << scheduled_task_;
140 }
141 }
142 scheduled_task_ = MessageLoop::kTaskIdNull;
143 }
144
StartActionInternal()145 void CleanupPreviousUpdateAction::StartActionInternal() {
146 CHECK(prefs_);
147 CHECK(boot_control_);
148
149 LOG(INFO) << "Starting/resuming CleanupPreviousUpdateAction";
150 running_ = true;
151 // Do nothing on non-VAB device.
152 if (!boot_control_->GetDynamicPartitionControl()
153 ->GetVirtualAbFeatureFlag()
154 .IsEnabled()) {
155 processor_->ActionComplete(this, ErrorCode::kSuccess);
156 return;
157 }
158 // SnapshotManager must be available on VAB devices.
159 CHECK(snapshot_ != nullptr);
160 merge_stats_ = snapshot_->GetSnapshotMergeStatsInstance();
161 CHECK(merge_stats_ != nullptr);
162 WaitBootCompletedOrSchedule();
163 }
164
ScheduleWaitBootCompleted()165 void CleanupPreviousUpdateAction::ScheduleWaitBootCompleted() {
166 TEST_AND_RETURN(running_);
167 scheduled_task_ = MessageLoop::current()->PostDelayedTask(
168 FROM_HERE,
169 base::Bind(&CleanupPreviousUpdateAction::WaitBootCompletedOrSchedule,
170 base::Unretained(this)),
171 kCheckBootCompletedInterval);
172 CheckTaskScheduled("WaitBootCompleted");
173 }
174
WaitBootCompletedOrSchedule()175 void CleanupPreviousUpdateAction::WaitBootCompletedOrSchedule() {
176 AcknowledgeTaskExecuted();
177 TEST_AND_RETURN(running_);
178 if (!kIsRecovery &&
179 !android::base::GetBoolProperty(kBootCompletedProp, false)) {
180 // repeat
181 ScheduleWaitBootCompleted();
182 return;
183 }
184
185 auto boot_time = std::chrono::duration_cast<std::chrono::milliseconds>(
186 android::base::boot_clock::now().time_since_epoch());
187 merge_stats_->set_boot_complete_time_ms(boot_time.count());
188
189 LOG(INFO) << "Boot completed, waiting on markBootSuccessful()";
190 CheckSlotMarkedSuccessfulOrSchedule();
191 }
192
ScheduleWaitMarkBootSuccessful()193 void CleanupPreviousUpdateAction::ScheduleWaitMarkBootSuccessful() {
194 TEST_AND_RETURN(running_);
195 scheduled_task_ = MessageLoop::current()->PostDelayedTask(
196 FROM_HERE,
197 base::Bind(
198 &CleanupPreviousUpdateAction::CheckSlotMarkedSuccessfulOrSchedule,
199 base::Unretained(this)),
200 kCheckSlotMarkedSuccessfulInterval);
201 CheckTaskScheduled("WaitMarkBootSuccessful");
202 }
203
CheckSlotMarkedSuccessfulOrSchedule()204 void CleanupPreviousUpdateAction::CheckSlotMarkedSuccessfulOrSchedule() {
205 AcknowledgeTaskExecuted();
206 TEST_AND_RETURN(running_);
207 if (!kIsRecovery &&
208 !boot_control_->IsSlotMarkedSuccessful(boot_control_->GetCurrentSlot())) {
209 ScheduleWaitMarkBootSuccessful();
210 return;
211 }
212
213 if (metadata_device_ == nullptr) {
214 metadata_device_ = snapshot_->EnsureMetadataMounted();
215 }
216
217 if (metadata_device_ == nullptr) {
218 LOG(ERROR) << "Failed to mount /metadata.";
219 // If metadata is erased but not formatted, it is possible to not mount
220 // it in recovery. It is safe to skip CleanupPreviousUpdateAction.
221 processor_->ActionComplete(
222 this, kIsRecovery ? ErrorCode::kSuccess : ErrorCode::kError);
223 return;
224 }
225
226 if (kIsRecovery) {
227 auto snapshots_created =
228 snapshot_->RecoveryCreateSnapshotDevices(metadata_device_);
229 switch (snapshots_created) {
230 case android::snapshot::CreateResult::CREATED: {
231 // If previous update has not finished merging, snapshots exists and are
232 // created here so that ProcessUpdateState can proceed.
233 LOG(INFO) << "Snapshot devices are created";
234 break;
235 }
236 case android::snapshot::CreateResult::NOT_CREATED: {
237 // If there is no previous update, no snapshot devices are created and
238 // ProcessUpdateState will return immediately. Hence, NOT_CREATED is not
239 // considered an error.
240 LOG(INFO) << "Snapshot devices are not created";
241 break;
242 }
243 case android::snapshot::CreateResult::ERROR:
244 default: {
245 LOG(ERROR)
246 << "Failed to create snapshot devices (CreateResult = "
247 << static_cast<
248 std::underlying_type_t<android::snapshot::CreateResult>>(
249 snapshots_created);
250 processor_->ActionComplete(this, ErrorCode::kError);
251 return;
252 }
253 }
254 }
255
256 if (!merge_stats_->Start()) {
257 // Not an error because CleanupPreviousUpdateAction may be paused and
258 // resumed while kernel continues merging snapshots in the background.
259 LOG(WARNING) << "SnapshotMergeStats::Start failed.";
260 }
261 LOG(INFO) << "Waiting for any previous merge request to complete. "
262 << "This can take up to several minutes.";
263 WaitForMergeOrSchedule();
264 }
265
ScheduleWaitForMerge()266 void CleanupPreviousUpdateAction::ScheduleWaitForMerge() {
267 TEST_AND_RETURN(running_);
268 scheduled_task_ = MessageLoop::current()->PostDelayedTask(
269 FROM_HERE,
270 base::Bind(&CleanupPreviousUpdateAction::WaitForMergeOrSchedule,
271 base::Unretained(this)),
272 kWaitForMergeInterval);
273 CheckTaskScheduled("WaitForMerge");
274 }
275
WaitForMergeOrSchedule()276 void CleanupPreviousUpdateAction::WaitForMergeOrSchedule() {
277 AcknowledgeTaskExecuted();
278 TEST_AND_RETURN(running_);
279 auto update_uses_compression = snapshot_->UpdateUsesCompression();
280 auto state = snapshot_->ProcessUpdateState(
281 std::bind(&CleanupPreviousUpdateAction::OnMergePercentageUpdate, this),
282 std::bind(&CleanupPreviousUpdateAction::BeforeCancel, this));
283 merge_stats_->set_state(state, update_uses_compression);
284
285 switch (state) {
286 case UpdateState::None: {
287 LOG(INFO) << "Can't find any snapshot to merge.";
288 ErrorCode error_code = ErrorCode::kSuccess;
289 if (!snapshot_->CancelUpdate()) {
290 error_code = ErrorCode::kError;
291 LOG(INFO) << "Failed to call SnapshotManager::CancelUpdate().";
292 }
293 processor_->ActionComplete(this, error_code);
294 return;
295 }
296
297 case UpdateState::Initiated: {
298 LOG(ERROR) << "Previous update has not been completed, not cleaning up";
299 processor_->ActionComplete(this, ErrorCode::kSuccess);
300 return;
301 }
302
303 case UpdateState::Unverified: {
304 InitiateMergeAndWait();
305 return;
306 }
307
308 case UpdateState::Merging: {
309 ScheduleWaitForMerge();
310 return;
311 }
312
313 case UpdateState::MergeNeedsReboot: {
314 LOG(ERROR) << "Need reboot to finish merging.";
315 processor_->ActionComplete(this, ErrorCode::kError);
316 return;
317 }
318
319 case UpdateState::MergeCompleted: {
320 LOG(INFO) << "Merge finished with state MergeCompleted.";
321 processor_->ActionComplete(this, ErrorCode::kSuccess);
322 return;
323 }
324
325 case UpdateState::MergeFailed: {
326 LOG(ERROR) << "Merge failed. Device may be corrupted.";
327 merge_stats_->set_merge_failure_code(snapshot_->ReadMergeFailureCode());
328 processor_->ActionComplete(this, ErrorCode::kDeviceCorrupted);
329 return;
330 }
331
332 case UpdateState::Cancelled: {
333 // DeltaPerformer::ResetUpdateProgress failed, hence snapshots are
334 // not deleted to avoid inconsistency.
335 // Nothing can be done here; just try next time.
336 ErrorCode error_code =
337 cancel_failed_ ? ErrorCode::kError : ErrorCode::kSuccess;
338 processor_->ActionComplete(this, error_code);
339 return;
340 }
341
342 default: {
343 // Protobuf has some reserved enum values, so a default case is needed.
344 LOG(FATAL) << "SnapshotManager::ProcessUpdateState returns "
345 << static_cast<int32_t>(state);
346 }
347 }
348 }
349
OnMergePercentageUpdate()350 bool CleanupPreviousUpdateAction::OnMergePercentageUpdate() {
351 double percentage = 0.0;
352 snapshot_->GetUpdateState(&percentage);
353 if (delegate_) {
354 // libsnapshot uses [0, 100] percentage but update_engine uses [0, 1].
355 delegate_->OnCleanupProgressUpdate(percentage / 100);
356 }
357
358 // Log if percentage increments by at least 1.
359 if (last_percentage_ < static_cast<unsigned int>(percentage)) {
360 last_percentage_ = percentage;
361 LOG(INFO) << "Waiting for merge to complete: " << last_percentage_ << "%.";
362 }
363
364 // Do not continue to wait for merge. Instead, let ProcessUpdateState
365 // return Merging directly so that we can ScheduleWaitForMerge() in
366 // MessageLoop.
367 return false;
368 }
369
BeforeCancel()370 bool CleanupPreviousUpdateAction::BeforeCancel() {
371 if (DeltaPerformer::ResetUpdateProgress(
372 prefs_,
373 false /* quick */,
374 false /* skip dynamic partitions metadata*/)) {
375 return true;
376 }
377
378 // ResetUpdateProgress might not work on stub prefs. Do additional checks.
379 LOG(WARNING) << "ProcessUpdateState returns Cancelled but cleanup failed.";
380
381 std::string val;
382 ignore_result(prefs_->GetString(kPrefsDynamicPartitionMetadataUpdated, &val));
383 if (val.empty()) {
384 LOG(INFO) << kPrefsDynamicPartitionMetadataUpdated
385 << " is empty, assuming successful cleanup";
386 return true;
387 }
388 LOG(WARNING)
389 << kPrefsDynamicPartitionMetadataUpdated << " is " << val
390 << ", not deleting snapshots even though UpdateState is Cancelled.";
391 cancel_failed_ = true;
392 return false;
393 }
394
InitiateMergeAndWait()395 void CleanupPreviousUpdateAction::InitiateMergeAndWait() {
396 TEST_AND_RETURN(running_);
397 LOG(INFO) << "Attempting to initiate merge.";
398 // suspend the VAB merge when running a DSU
399 if (GetBoolProperty("ro.gsid.image_running", false)) {
400 LOG(WARNING) << "Suspend the VAB merge when running a DSU.";
401 processor_->ActionComplete(this, ErrorCode::kError);
402 return;
403 }
404
405 snapshot_->UpdateCowStats(merge_stats_);
406
407 auto merge_start_time = std::chrono::duration_cast<std::chrono::milliseconds>(
408 android::base::boot_clock::now().time_since_epoch());
409 merge_stats_->set_boot_complete_to_merge_start_time_ms(
410 merge_start_time.count() - merge_stats_->boot_complete_time_ms());
411
412 auto source_build_fingerprint = snapshot_->ReadSourceBuildFingerprint();
413 merge_stats_->set_source_build_fingerprint(source_build_fingerprint);
414
415 if (!merge_stats_->WriteState()) {
416 LOG(ERROR) << "Failed to write merge stats; record may be unreliable if "
417 "merge is interrupted.";
418 }
419
420 if (snapshot_->InitiateMerge()) {
421 WaitForMergeOrSchedule();
422 return;
423 }
424
425 LOG(WARNING) << "InitiateMerge failed.";
426 auto state = snapshot_->GetUpdateState();
427 merge_stats_->set_state(state, snapshot_->UpdateUsesCompression());
428 if (state == UpdateState::Unverified) {
429 // We are stuck at unverified state. This can happen if the update has
430 // been applied, but it has not even been attempted yet (in libsnapshot,
431 // rollback indicator does not exist); for example, if update_engine
432 // restarts before the device reboots, then this state may be reached.
433 // Nothing should be done here.
434 LOG(WARNING) << "InitiateMerge leaves the device at "
435 << "UpdateState::Unverified. (Did update_engine "
436 << "restarted?)";
437 processor_->ActionComplete(this, ErrorCode::kSuccess);
438 return;
439 }
440
441 // State does seems to be advanced.
442 // It is possibly racy. For example, on a userdebug build, the user may
443 // manually initiate a merge with snapshotctl between last time
444 // update_engine checks UpdateState. Hence, just call
445 // WaitForMergeOrSchedule one more time.
446 LOG(WARNING) << "IniitateMerge failed but GetUpdateState returned "
447 << android::snapshot::UpdateState_Name(state)
448 << ", try to wait for merge again.";
449 WaitForMergeOrSchedule();
450 return;
451 }
452
ReportMergeStats()453 void CleanupPreviousUpdateAction::ReportMergeStats() {
454 auto result = merge_stats_->Finish();
455 if (result == nullptr) {
456 LOG(WARNING) << "Not reporting merge stats because "
457 "SnapshotMergeStats::Finish failed.";
458 return;
459 }
460
461 #ifdef __ANDROID_RECOVERY__
462 LOG(INFO) << "Skip reporting merge stats in recovery.";
463 #else
464 const auto& report = result->report();
465
466 if (report.state() == UpdateState::None ||
467 report.state() == UpdateState::Initiated ||
468 report.state() == UpdateState::Unverified) {
469 LOG(INFO) << "Not reporting merge stats because state is "
470 << android::snapshot::UpdateState_Name(report.state());
471 return;
472 }
473
474 auto passed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
475 result->merge_time());
476
477 bool vab_retrofit = boot_control_->GetDynamicPartitionControl()
478 ->GetVirtualAbFeatureFlag()
479 .IsRetrofit();
480 bool vab_compression_enabled = boot_control_->GetDynamicPartitionControl()
481 ->GetVirtualAbCompressionFeatureFlag()
482 .IsEnabled();
483 // The snapshot has been merged, so we can no longer call
484 // DynamicPartitionControlInterface::UpdateUsesSnapshotCompression.
485 // However, we have saved the flag in the snapshot report.
486 bool vab_compression_used = report.compression_enabled();
487
488 auto target_build_fingerprint =
489 android::base::GetProperty("ro.build.fingerprint", "");
490
491 LOG(INFO) << "Reporting merge stats: "
492 << android::snapshot::UpdateState_Name(report.state()) << " in "
493 << passed_ms.count() << "ms (resumed " << report.resume_count()
494 << " times), using " << report.cow_file_size()
495 << " bytes of COW image.";
496 android::util::stats_write(android::util::SNAPSHOT_MERGE_REPORTED,
497 static_cast<int32_t>(report.state()),
498 static_cast<int64_t>(passed_ms.count()),
499 static_cast<int32_t>(report.resume_count()),
500 vab_retrofit,
501 static_cast<int64_t>(report.cow_file_size()),
502 vab_compression_enabled,
503 vab_compression_used,
504 report.total_cow_size_bytes(),
505 report.estimated_cow_size_bytes(),
506 report.boot_complete_time_ms(),
507 report.boot_complete_to_merge_start_time_ms(),
508 static_cast<int32_t>(report.merge_failure_code()),
509 report.source_build_fingerprint().c_str(),
510 target_build_fingerprint.c_str());
511 #endif
512 }
513
514 } // namespace chromeos_update_engine
515