1 // Copyright (C) 2019 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 #include <stdint.h> 18 #include <unistd.h> 19 20 #include <chrono> 21 #include <map> 22 #include <memory> 23 #include <optional> 24 #include <ostream> 25 #include <string> 26 #include <string_view> 27 #include <vector> 28 29 #include <android-base/unique_fd.h> 30 #include <android/snapshot/snapshot.pb.h> 31 #include <fs_mgr_dm_linear.h> 32 #include <libdm/dm.h> 33 #include <libfiemap/image_manager.h> 34 #include <liblp/builder.h> 35 #include <liblp/liblp.h> 36 #include <libsnapshot/auto_device.h> 37 #include <libsnapshot/cow_writer.h> 38 #include <libsnapshot/return.h> 39 #include <snapuserd/snapuserd_client.h> 40 #include <update_engine/update_metadata.pb.h> 41 42 #ifndef FRIEND_TEST 43 #define FRIEND_TEST(test_set_name, individual_test) \ 44 friend class test_set_name##_##individual_test##_Test 45 #define DEFINED_FRIEND_TEST 46 #endif 47 48 namespace aidl::android::hardware::boot { 49 enum class MergeStatus; 50 } 51 52 namespace android { 53 54 namespace fiemap { 55 class IImageManager; 56 } // namespace fiemap 57 58 namespace fs_mgr { 59 struct CreateLogicalPartitionParams; 60 class IPartitionOpener; 61 } // namespace fs_mgr 62 63 // Forward declare IBootControl types since we cannot include only the headers 64 // with Soong. Note: keep the enum width in sync. 65 66 namespace snapshot { 67 68 struct AutoDeleteCowImage; 69 struct AutoDeleteSnapshot; 70 struct AutoDeviceList; 71 struct PartitionCowCreator; 72 class ISnapshotMergeStats; 73 class SnapshotMergeStats; 74 class SnapshotStatus; 75 76 using std::chrono::duration_cast; 77 using namespace std::chrono_literals; 78 79 static constexpr const std::string_view kCowGroupName = "cow"; 80 static constexpr char kVirtualAbCompressionProp[] = "ro.virtual_ab.compression.enabled"; 81 82 bool OptimizeSourceCopyOperation(const chromeos_update_engine::InstallOperation& operation, 83 chromeos_update_engine::InstallOperation* optimized); 84 85 enum class CreateResult : unsigned int { 86 ERROR, 87 CREATED, 88 NOT_CREATED, 89 }; 90 91 class ISnapshotManager { 92 public: 93 // Dependency injection for testing. 94 class IDeviceInfo { 95 public: 96 using IImageManager = android::fiemap::IImageManager; 97 using MergeStatus = aidl::android::hardware::boot::MergeStatus; 98 ~IDeviceInfo()99 virtual ~IDeviceInfo() {} 100 virtual std::string GetMetadataDir() const = 0; 101 virtual std::string GetSlotSuffix() const = 0; 102 virtual std::string GetOtherSlotSuffix() const = 0; 103 virtual std::string GetSuperDevice(uint32_t slot) const = 0; 104 virtual const android::fs_mgr::IPartitionOpener& GetPartitionOpener() const = 0; 105 virtual bool IsOverlayfsSetup() const = 0; 106 virtual bool SetBootControlMergeStatus(MergeStatus status) = 0; 107 virtual bool SetSlotAsUnbootable(unsigned int slot) = 0; 108 virtual bool IsRecovery() const = 0; IsTestDevice()109 virtual bool IsTestDevice() const { return false; } 110 virtual bool IsFirstStageInit() const = 0; 111 virtual std::unique_ptr<IImageManager> OpenImageManager() const = 0; 112 virtual android::dm::IDeviceMapper& GetDeviceMapper() = 0; 113 114 // Helper method for implementing OpenImageManager. 115 std::unique_ptr<IImageManager> OpenImageManager(const std::string& gsid_dir) const; 116 }; 117 virtual ~ISnapshotManager() = default; 118 119 // Begin an update. This must be called before creating any snapshots. It 120 // will fail if GetUpdateState() != None. 121 virtual bool BeginUpdate() = 0; 122 123 // Cancel an update; any snapshots will be deleted. This is allowed if the 124 // state == Initiated, None, or Unverified (before rebooting to the new 125 // slot). 126 virtual bool CancelUpdate() = 0; 127 128 // Mark snapshot writes as having completed. After this, new snapshots cannot 129 // be created, and the device must either cancel the OTA (either before 130 // rebooting or after rolling back), or merge the OTA. 131 // Before calling this function, all snapshots must be mapped. 132 // If |wipe| is set to true, wipe is scheduled after reboot, and snapshots 133 // may need to be merged before wiping. 134 virtual bool FinishedSnapshotWrites(bool wipe) = 0; 135 136 // Set feature flags on an ISnapshotMergeStats object. 137 virtual void SetMergeStatsFeatures(ISnapshotMergeStats* stats) = 0; 138 139 // Update an ISnapshotMergeStats object with statistics about COW usage. 140 // This should be called before the merge begins as otherwise snapshots 141 // may be deleted. 142 virtual void UpdateCowStats(ISnapshotMergeStats* stats) = 0; 143 144 // Initiate a merge on all snapshot devices. This should only be used after an 145 // update has been marked successful after booting. 146 virtual bool InitiateMerge() = 0; 147 148 // Perform any necessary post-boot actions. This should be run soon after 149 // /data is mounted. 150 // 151 // If a merge is in progress, this function will block until the merge is 152 // completed. 153 // - Callback is called periodically during the merge. If callback() 154 // returns false during the merge, ProcessUpdateState() will pause 155 // and returns Merging. 156 // If a merge or update was cancelled, this will clean up any 157 // update artifacts and return. 158 // 159 // Note that after calling this, GetUpdateState() may still return that a 160 // merge is in progress: 161 // MergeFailed indicates that a fatal error occurred. WaitForMerge() may 162 // called any number of times again to attempt to make more progress, but 163 // we do not expect it to succeed if a catastrophic error occurred. 164 // 165 // MergeNeedsReboot indicates that the merge has completed, but cleanup 166 // failed. This can happen if for some reason resources were not closed 167 // properly. In this case another reboot is needed before we can take 168 // another OTA. However, WaitForMerge() can be called again without 169 // rebooting, to attempt to finish cleanup anyway. 170 // 171 // MergeCompleted indicates that the update has fully completed. 172 // GetUpdateState will return None, and a new update can begin. 173 // 174 // The optional callback allows the caller to periodically check the 175 // progress with GetUpdateState(). 176 virtual UpdateState ProcessUpdateState(const std::function<bool()>& callback = {}, 177 const std::function<bool()>& before_cancel = {}) = 0; 178 179 // If ProcessUpdateState() returned MergeFailed, this returns the appropriate 180 // code. Otherwise, MergeFailureCode::Ok is returned. 181 virtual MergeFailureCode ReadMergeFailureCode() = 0; 182 183 // If an update is in progress, return the source build fingerprint. 184 virtual std::string ReadSourceBuildFingerprint() = 0; 185 186 // Find the status of the current update, if any. 187 // 188 // |progress| depends on the returned status: 189 // Merging: Value in the range [0, 100] 190 // MergeCompleted: 100 191 // Other: 0 192 virtual UpdateState GetUpdateState(double* progress = nullptr) = 0; 193 194 // Returns true if compression is enabled for the current update. This always returns false if 195 // UpdateState is None, or no snapshots have been created. 196 virtual bool UpdateUsesCompression() = 0; 197 198 // Returns true if userspace snapshots is enabled for the current update. 199 virtual bool UpdateUsesUserSnapshots() = 0; 200 201 // Create necessary COW device / files for OTA clients. New logical partitions will be added to 202 // group "cow" in target_metadata. Regions of partitions of current_metadata will be 203 // "write-protected" and snapshotted. 204 virtual Return CreateUpdateSnapshots( 205 const chromeos_update_engine::DeltaArchiveManifest& manifest) = 0; 206 207 // Map a snapshotted partition for OTA clients to write to. Write-protected regions are 208 // determined previously in CreateSnapshots. 209 // 210 // |snapshot_path| must not be nullptr. 211 // 212 // This method will return false if ro.virtual_ab.compression.enabled is true. 213 virtual bool MapUpdateSnapshot(const android::fs_mgr::CreateLogicalPartitionParams& params, 214 std::string* snapshot_path) = 0; 215 216 // Create an ICowWriter to build a snapshot against a target partition. The partition name 217 // must be suffixed. If a source partition exists, it must be specified as well. The source 218 // partition will only be used if raw bytes are needed. The source partition should be an 219 // absolute path to the device, not a partition name. 220 virtual std::unique_ptr<ICowWriter> OpenSnapshotWriter( 221 const android::fs_mgr::CreateLogicalPartitionParams& params, 222 std::optional<uint64_t> label = {}) = 0; 223 224 // Unmap a snapshot device or CowWriter that was previously opened with MapUpdateSnapshot, 225 // OpenSnapshotWriter. All outstanding open descriptors, writers, or 226 // readers must be deleted before this is called. 227 virtual bool UnmapUpdateSnapshot(const std::string& target_partition_name) = 0; 228 229 // If this returns true, first-stage mount must call 230 // CreateLogicalAndSnapshotPartitions rather than CreateLogicalPartitions. 231 virtual bool NeedSnapshotsInFirstStageMount() = 0; 232 233 // Perform first-stage mapping of snapshot targets. This replaces init's 234 // call to CreateLogicalPartitions when snapshots are present. 235 virtual bool CreateLogicalAndSnapshotPartitions( 236 const std::string& super_device, const std::chrono::milliseconds& timeout_ms = {}) = 0; 237 238 // Map all snapshots. This is analogous to CreateLogicalAndSnapshotPartitions, except it maps 239 // the target slot rather than the current slot. It should only be used immediately after 240 // applying an update, before rebooting to the new slot. 241 virtual bool MapAllSnapshots(const std::chrono::milliseconds& timeout_ms = {}) = 0; 242 243 // Unmap all snapshots. This should be called to undo MapAllSnapshots(). 244 virtual bool UnmapAllSnapshots() = 0; 245 246 // This method should be called preceding any wipe or flash of metadata or 247 // userdata. It is only valid in recovery or fastbootd, and it ensures that 248 // a merge has been completed. 249 // 250 // When userdata will be wiped or flashed, it is necessary to clean up any 251 // snapshot state. If a merge is in progress, the merge must be finished. 252 // If a snapshot is present but not yet merged, the slot must be marked as 253 // unbootable. 254 // 255 // Returns true on success (or nothing to do), false on failure. The 256 // optional callback fires periodically to query progress via GetUpdateState. 257 virtual bool HandleImminentDataWipe(const std::function<void()>& callback = {}) = 0; 258 259 // Force a merge to complete in recovery. This is similar to HandleImminentDataWipe 260 // but does not expect a data wipe after. 261 virtual bool FinishMergeInRecovery() = 0; 262 263 // This method is only allowed in recovery and is used as a helper to 264 // initialize the snapshot devices as a requirement to mount a snapshotted 265 // /system in recovery. 266 // This function returns: 267 // - CreateResult::CREATED if snapshot devices were successfully created; 268 // - CreateResult::NOT_CREATED if it was not necessary to create snapshot 269 // devices; 270 // - CreateResult::ERROR if a fatal error occurred, mounting /system should 271 // be aborted. 272 // This function mounts /metadata when called, and unmounts /metadata upon 273 // return. 274 virtual CreateResult RecoveryCreateSnapshotDevices() = 0; 275 276 // Same as RecoveryCreateSnapshotDevices(), but does not auto mount/umount 277 // /metadata. 278 virtual CreateResult RecoveryCreateSnapshotDevices( 279 const std::unique_ptr<AutoDevice>& metadata_device) = 0; 280 281 // Dump debug information. 282 virtual bool Dump(std::ostream& os) = 0; 283 284 // Ensure metadata directory is mounted in recovery. When the returned 285 // AutoDevice is destroyed, the metadata directory is automatically 286 // unmounted. 287 // Return nullptr if any failure. 288 // In Android mode, Return an AutoDevice that does nothing 289 // In recovery, return an AutoDevice that does nothing if metadata entry 290 // is not found in fstab. 291 // Note: if this function is called the second time before the AutoDevice returned from the 292 // first call is destroyed, the device will be unmounted when any of these AutoDevices is 293 // destroyed. For example: 294 // auto a = mgr->EnsureMetadataMounted(); // mounts 295 // auto b = mgr->EnsureMetadataMounted(); // does nothing 296 // b.reset() // unmounts 297 // a.reset() // does nothing 298 virtual std::unique_ptr<AutoDevice> EnsureMetadataMounted() = 0; 299 300 // Return the associated ISnapshotMergeStats instance. Never null. 301 virtual ISnapshotMergeStats* GetSnapshotMergeStatsInstance() = 0; 302 }; 303 304 class SnapshotManager final : public ISnapshotManager { 305 using CreateLogicalPartitionParams = android::fs_mgr::CreateLogicalPartitionParams; 306 using IPartitionOpener = android::fs_mgr::IPartitionOpener; 307 using LpMetadata = android::fs_mgr::LpMetadata; 308 using MetadataBuilder = android::fs_mgr::MetadataBuilder; 309 using DeltaArchiveManifest = chromeos_update_engine::DeltaArchiveManifest; 310 using MergeStatus = aidl::android::hardware::boot::MergeStatus; 311 using FiemapStatus = android::fiemap::FiemapStatus; 312 313 friend class SnapshotMergeStats; 314 315 public: 316 ~SnapshotManager(); 317 318 // Return a new SnapshotManager instance, or null on error. The device 319 // pointer is owned for the lifetime of SnapshotManager. If null, a default 320 // instance will be created. 321 static std::unique_ptr<SnapshotManager> New(IDeviceInfo* device = nullptr); 322 323 // This is similar to New(), except designed specifically for first-stage 324 // init or recovery. 325 static std::unique_ptr<SnapshotManager> NewForFirstStageMount(IDeviceInfo* device = nullptr); 326 327 // Helper function for first-stage init to check whether a SnapshotManager 328 // might be needed to perform first-stage mounts. 329 static bool IsSnapshotManagerNeeded(); 330 331 // Helper function for second stage init to restorecon on the rollback indicator. 332 static std::string GetGlobalRollbackIndicatorPath(); 333 334 // Populate |snapuserd_argv| with the necessary arguments to restart snapuserd 335 // after loading selinux policy. 336 bool PrepareSnapuserdArgsForSelinux(std::vector<std::string>* snapuserd_argv); 337 338 // Detach dm-user devices from the first stage snapuserd. Load 339 // new dm-user tables after loading selinux policy. 340 bool DetachFirstStageSnapuserdForSelinux(); 341 342 // Perform the transition from the selinux stage of snapuserd into the 343 // second-stage of snapuserd. This process involves re-creating the dm-user 344 // table entries for each device, so that they connect to the new daemon. 345 // Once all new tables have been activated, we ask the first-stage daemon 346 // to cleanly exit. 347 bool PerformSecondStageInitTransition(); 348 349 // ISnapshotManager overrides. 350 bool BeginUpdate() override; 351 bool CancelUpdate() override; 352 bool FinishedSnapshotWrites(bool wipe) override; 353 void UpdateCowStats(ISnapshotMergeStats* stats) override; 354 MergeFailureCode ReadMergeFailureCode() override; 355 bool InitiateMerge() override; 356 UpdateState ProcessUpdateState(const std::function<bool()>& callback = {}, 357 const std::function<bool()>& before_cancel = {}) override; 358 UpdateState GetUpdateState(double* progress = nullptr) override; 359 bool UpdateUsesCompression() override; 360 bool UpdateUsesUserSnapshots() override; 361 Return CreateUpdateSnapshots(const DeltaArchiveManifest& manifest) override; 362 bool MapUpdateSnapshot(const CreateLogicalPartitionParams& params, 363 std::string* snapshot_path) override; 364 std::unique_ptr<ICowWriter> OpenSnapshotWriter( 365 const android::fs_mgr::CreateLogicalPartitionParams& params, 366 std::optional<uint64_t> label) override; 367 bool UnmapUpdateSnapshot(const std::string& target_partition_name) override; 368 bool NeedSnapshotsInFirstStageMount() override; 369 bool CreateLogicalAndSnapshotPartitions( 370 const std::string& super_device, 371 const std::chrono::milliseconds& timeout_ms = {}) override; 372 bool HandleImminentDataWipe(const std::function<void()>& callback = {}) override; 373 bool FinishMergeInRecovery() override; 374 CreateResult RecoveryCreateSnapshotDevices() override; 375 CreateResult RecoveryCreateSnapshotDevices( 376 const std::unique_ptr<AutoDevice>& metadata_device) override; 377 bool Dump(std::ostream& os) override; 378 std::unique_ptr<AutoDevice> EnsureMetadataMounted() override; 379 ISnapshotMergeStats* GetSnapshotMergeStatsInstance() override; 380 bool MapAllSnapshots(const std::chrono::milliseconds& timeout_ms = {}) override; 381 bool UnmapAllSnapshots() override; 382 std::string ReadSourceBuildFingerprint() override; 383 void SetMergeStatsFeatures(ISnapshotMergeStats* stats) override; 384 385 // We can't use WaitForFile during first-stage init, because ueventd is not 386 // running and therefore will not automatically create symlinks. Instead, 387 // we let init provide us with the correct function to use to ensure 388 // uevents have been processed and symlink/mknod calls completed. SetUeventRegenCallback(std::function<bool (const std::string &)> callback)389 void SetUeventRegenCallback(std::function<bool(const std::string&)> callback) { 390 uevent_regen_callback_ = callback; 391 } 392 393 // If true, compression is enabled for this update. This is used by 394 // first-stage to decide whether to launch snapuserd. 395 bool IsSnapuserdRequired(); 396 397 // This is primarily used to device reboot. If OTA update is in progress, 398 // init will avoid killing processes 399 bool IsUserspaceSnapshotUpdateInProgress(); 400 401 enum class SnapshotDriver { 402 DM_SNAPSHOT, 403 DM_USER, 404 }; 405 406 // Add new public entries above this line. 407 408 private: 409 FRIEND_TEST(SnapshotTest, CleanFirstStageMount); 410 FRIEND_TEST(SnapshotTest, CreateSnapshot); 411 FRIEND_TEST(SnapshotTest, FirstStageMountAfterRollback); 412 FRIEND_TEST(SnapshotTest, FirstStageMountAndMerge); 413 FRIEND_TEST(SnapshotTest, FlagCheck); 414 FRIEND_TEST(SnapshotTest, FlashSuperDuringMerge); 415 FRIEND_TEST(SnapshotTest, FlashSuperDuringUpdate); 416 FRIEND_TEST(SnapshotTest, MapPartialSnapshot); 417 FRIEND_TEST(SnapshotTest, MapSnapshot); 418 FRIEND_TEST(SnapshotTest, Merge); 419 FRIEND_TEST(SnapshotTest, MergeFailureCode); 420 FRIEND_TEST(SnapshotTest, NoMergeBeforeReboot); 421 FRIEND_TEST(SnapshotTest, UpdateBootControlHal); 422 FRIEND_TEST(SnapshotTest, BootSnapshotWithoutSlotSwitch); 423 FRIEND_TEST(SnapshotUpdateTest, AddPartition); 424 FRIEND_TEST(SnapshotUpdateTest, ConsistencyCheckResume); 425 FRIEND_TEST(SnapshotUpdateTest, DaemonTransition); 426 FRIEND_TEST(SnapshotUpdateTest, DataWipeAfterRollback); 427 FRIEND_TEST(SnapshotUpdateTest, DataWipeRollbackInRecovery); 428 FRIEND_TEST(SnapshotUpdateTest, DataWipeWithStaleSnapshots); 429 FRIEND_TEST(SnapshotUpdateTest, FlagCheck); 430 FRIEND_TEST(SnapshotUpdateTest, FullUpdateFlow); 431 FRIEND_TEST(SnapshotUpdateTest, MergeCannotRemoveCow); 432 FRIEND_TEST(SnapshotUpdateTest, MergeInRecovery); 433 FRIEND_TEST(SnapshotUpdateTest, QueryStatusError); 434 FRIEND_TEST(SnapshotUpdateTest, SnapshotStatusFileWithoutCow); 435 FRIEND_TEST(SnapshotUpdateTest, SpaceSwapUpdate); 436 FRIEND_TEST(SnapshotUpdateTest, MapAllSnapshotsWithoutSlotSwitch); 437 friend class SnapshotTest; 438 friend class SnapshotUpdateTest; 439 friend class FlashAfterUpdateTest; 440 friend class LockTestConsumer; 441 friend class SnapshotFuzzEnv; 442 friend class MapSnapshots; 443 friend struct AutoDeleteCowImage; 444 friend struct AutoDeleteSnapshot; 445 friend struct PartitionCowCreator; 446 447 using DmTargetSnapshot = android::dm::DmTargetSnapshot; 448 using IImageManager = android::fiemap::IImageManager; 449 using TargetInfo = android::dm::DeviceMapper::TargetInfo; 450 451 explicit SnapshotManager(IDeviceInfo* info); 452 453 // This is created lazily since it can connect via binder. 454 bool EnsureImageManager(); 455 456 // Ensure we're connected to snapuserd. 457 bool EnsureSnapuserdConnected(std::chrono::milliseconds timeout_ms = 10s); 458 459 // Helpers for first-stage init. device()460 const std::unique_ptr<IDeviceInfo>& device() const { return device_; } 461 462 // Helper functions for tests. image_manager()463 IImageManager* image_manager() const { return images_.get(); } set_use_first_stage_snapuserd(bool value)464 void set_use_first_stage_snapuserd(bool value) { use_first_stage_snapuserd_ = value; } 465 466 // Since libsnapshot is included into multiple processes, we flock() our 467 // files for simple synchronization. LockedFile is a helper to assist with 468 // this. It also serves as a proof-of-lock for some functions. 469 class LockedFile final { 470 public: LockedFile(const std::string & path,android::base::unique_fd && fd,int lock_mode)471 LockedFile(const std::string& path, android::base::unique_fd&& fd, int lock_mode) 472 : path_(path), fd_(std::move(fd)), lock_mode_(lock_mode) {} 473 ~LockedFile(); lock_mode()474 int lock_mode() const { return lock_mode_; } 475 476 private: 477 std::string path_; 478 android::base::unique_fd fd_; 479 int lock_mode_; 480 }; 481 static std::unique_ptr<LockedFile> OpenFile(const std::string& file, int lock_flags); 482 483 SnapshotDriver GetSnapshotDriver(LockedFile* lock); 484 485 // Create a new snapshot record. This creates the backing COW store and 486 // persists information needed to map the device. The device can be mapped 487 // with MapSnapshot(). 488 // 489 // |status|.device_size should be the size of the base_device that will be passed 490 // via MapDevice(). |status|.snapshot_size should be the number of bytes in the 491 // base device, starting from 0, that will be snapshotted. |status|.cow_file_size 492 // should be the amount of space that will be allocated to store snapshot 493 // deltas. 494 // 495 // If |status|.snapshot_size < |status|.device_size, then the device will always 496 // be mapped with two table entries: a dm-snapshot range covering 497 // snapshot_size, and a dm-linear range covering the remainder. 498 // 499 // All sizes are specified in bytes, and the device, snapshot, COW partition and COW file sizes 500 // must be a multiple of the sector size (512 bytes). 501 bool CreateSnapshot(LockedFile* lock, PartitionCowCreator* cow_creator, SnapshotStatus* status); 502 503 // |name| should be the base partition name (e.g. "system_a"). Create the 504 // backing COW image using the size previously passed to CreateSnapshot(). 505 Return CreateCowImage(LockedFile* lock, const std::string& name); 506 507 // Map a snapshot device that was previously created with CreateSnapshot. 508 // If a merge was previously initiated, the device-mapper table will have a 509 // snapshot-merge target instead of a snapshot target. If the timeout 510 // parameter greater than zero, this function will wait the given amount 511 // of time for |dev_path| to become available, and fail otherwise. If 512 // timeout_ms is 0, then no wait will occur and |dev_path| may not yet 513 // exist on return. 514 bool MapSnapshot(LockedFile* lock, const std::string& name, const std::string& base_device, 515 const std::string& cow_device, const std::chrono::milliseconds& timeout_ms, 516 std::string* dev_path); 517 518 // Create a dm-user device for a given snapshot. 519 bool MapDmUserCow(LockedFile* lock, const std::string& name, const std::string& cow_file, 520 const std::string& base_device, const std::string& base_path_merge, 521 const std::chrono::milliseconds& timeout_ms, std::string* path); 522 523 // Map the source device used for dm-user. 524 bool MapSourceDevice(LockedFile* lock, const std::string& name, 525 const std::chrono::milliseconds& timeout_ms, std::string* path); 526 527 // Map a COW image that was previous created with CreateCowImage. 528 std::optional<std::string> MapCowImage(const std::string& name, 529 const std::chrono::milliseconds& timeout_ms); 530 531 // Remove the backing copy-on-write image and snapshot states for the named snapshot. The 532 // caller is responsible for ensuring that the snapshot is unmapped. 533 bool DeleteSnapshot(LockedFile* lock, const std::string& name); 534 535 // Unmap a snapshot device previously mapped with MapSnapshotDevice(). 536 bool UnmapSnapshot(LockedFile* lock, const std::string& name); 537 538 // Unmap a COW image device previously mapped with MapCowImage(). 539 bool UnmapCowImage(const std::string& name); 540 541 // Unmap a COW and remove it from a MetadataBuilder. 542 void UnmapAndDeleteCowPartition(MetadataBuilder* current_metadata); 543 544 // Remove invalid snapshots if any 545 void RemoveInvalidSnapshots(LockedFile* lock); 546 547 // Unmap and remove all known snapshots. 548 bool RemoveAllSnapshots(LockedFile* lock); 549 550 // Boot device off snapshots without slot switch 551 bool BootFromSnapshotsWithoutSlotSwitch(); 552 553 // Remove kBootSnapshotsWithoutSlotSwitch so that device can boot 554 // without snapshots on the current slot 555 bool PrepareDeviceToBootWithoutSnapshot(); 556 557 // Is the kBootSnapshotsWithoutSlotSwitch present 558 bool IsSnapshotWithoutSlotSwitch(); 559 560 // List the known snapshot names. 561 bool ListSnapshots(LockedFile* lock, std::vector<std::string>* snapshots, 562 const std::string& suffix = ""); 563 564 // Check for a cancelled or rolled back merge, returning true if such a 565 // condition was detected and handled. 566 bool HandleCancelledUpdate(LockedFile* lock, const std::function<bool()>& before_cancel); 567 568 // Helper for HandleCancelledUpdate. Assumes booting from new slot. 569 bool AreAllSnapshotsCancelled(LockedFile* lock); 570 571 // Determine whether partition names in |snapshots| have been flashed and 572 // store result to |out|. 573 // Return true if values are successfully retrieved and false on error 574 // (e.g. super partition metadata cannot be read). When it returns true, 575 // |out| stores true for partitions that have been flashed and false for 576 // partitions that have not been flashed. 577 bool GetSnapshotFlashingStatus(LockedFile* lock, const std::vector<std::string>& snapshots, 578 std::map<std::string, bool>* out); 579 580 // Remove artifacts created by the update process, such as snapshots, and 581 // set the update state to None. 582 bool RemoveAllUpdateState(LockedFile* lock, const std::function<bool()>& prolog = {}); 583 584 // Interact with /metadata/ota. 585 std::unique_ptr<LockedFile> OpenLock(int lock_flags); 586 std::unique_ptr<LockedFile> LockShared(); 587 std::unique_ptr<LockedFile> LockExclusive(); 588 std::string GetLockPath() const; 589 590 // Interact with /metadata/ota/state. 591 UpdateState ReadUpdateState(LockedFile* file); 592 SnapshotUpdateStatus ReadSnapshotUpdateStatus(LockedFile* file); 593 bool WriteUpdateState(LockedFile* file, UpdateState state, 594 MergeFailureCode failure_code = MergeFailureCode::Ok); 595 bool WriteSnapshotUpdateStatus(LockedFile* file, const SnapshotUpdateStatus& status); 596 std::string GetStateFilePath() const; 597 598 // Interact with /metadata/ota/merge_state. 599 // This file contains information related to the snapshot merge process. 600 std::string GetMergeStateFilePath() const; 601 602 // Helpers for merging. 603 MergeFailureCode MergeSecondPhaseSnapshots(LockedFile* lock); 604 MergeFailureCode SwitchSnapshotToMerge(LockedFile* lock, const std::string& name); 605 MergeFailureCode RewriteSnapshotDeviceTable(const std::string& dm_name); 606 bool MarkSnapshotMergeCompleted(LockedFile* snapshot_lock, const std::string& snapshot_name); 607 void AcknowledgeMergeSuccess(LockedFile* lock); 608 void AcknowledgeMergeFailure(MergeFailureCode failure_code); 609 MergePhase DecideMergePhase(const SnapshotStatus& status); 610 std::unique_ptr<LpMetadata> ReadCurrentMetadata(); 611 612 enum class MetadataPartitionState { 613 // Partition does not exist. 614 None, 615 // Partition is flashed. 616 Flashed, 617 // Partition is created by OTA client. 618 Updated, 619 }; 620 // Helper function to check the state of a partition as described in metadata. 621 MetadataPartitionState GetMetadataPartitionState(const LpMetadata& metadata, 622 const std::string& name); 623 624 // Note that these require the name of the device containing the snapshot, 625 // which may be the "inner" device. Use GetsnapshotDeviecName(). 626 bool QuerySnapshotStatus(const std::string& dm_name, std::string* target_type, 627 DmTargetSnapshot::Status* status); 628 bool IsSnapshotDevice(const std::string& dm_name, TargetInfo* target = nullptr); 629 630 // Internal callback for when merging is complete. 631 bool OnSnapshotMergeComplete(LockedFile* lock, const std::string& name, 632 const SnapshotStatus& status); 633 bool CollapseSnapshotDevice(LockedFile* lock, const std::string& name, 634 const SnapshotStatus& status); 635 636 struct [[nodiscard]] MergeResult { 637 explicit MergeResult(UpdateState state, 638 MergeFailureCode failure_code = MergeFailureCode::Ok) stateMergeResult639 : state(state), failure_code(failure_code) {} 640 UpdateState state; 641 MergeFailureCode failure_code; 642 }; 643 644 // Only the following UpdateStates are used here: 645 // UpdateState::Merging 646 // UpdateState::MergeCompleted 647 // UpdateState::MergeFailed 648 // UpdateState::MergeNeedsReboot 649 MergeResult CheckMergeState(const std::function<bool()>& before_cancel); 650 MergeResult CheckMergeState(LockedFile* lock, const std::function<bool()>& before_cancel); 651 MergeResult CheckTargetMergeState(LockedFile* lock, const std::string& name, 652 const SnapshotUpdateStatus& update_status); 653 654 auto UpdateStateToStr(enum UpdateState state); 655 // Get status or table information about a device-mapper node with a single target. 656 enum class TableQuery { 657 Table, 658 Status, 659 }; 660 bool GetSingleTarget(const std::string& dm_name, TableQuery query, 661 android::dm::DeviceMapper::TargetInfo* target); 662 663 // Interact with status files under /metadata/ota/snapshots. 664 bool WriteSnapshotStatus(LockedFile* lock, const SnapshotStatus& status); 665 bool ReadSnapshotStatus(LockedFile* lock, const std::string& name, SnapshotStatus* status); 666 std::string GetSnapshotStatusFilePath(const std::string& name); 667 668 std::string GetSnapshotBootIndicatorPath(); 669 std::string GetRollbackIndicatorPath(); 670 std::string GetForwardMergeIndicatorPath(); 671 std::string GetOldPartitionMetadataPath(); 672 std::string GetBootSnapshotsWithoutSlotSwitchPath(); 673 674 const LpMetadata* ReadOldPartitionMetadata(LockedFile* lock); 675 676 bool MapAllPartitions(LockedFile* lock, const std::string& super_device, uint32_t slot, 677 const std::chrono::milliseconds& timeout_ms); 678 679 // Reason for calling MapPartitionWithSnapshot. 680 enum class SnapshotContext { 681 // For writing or verification (during update_engine). 682 Update, 683 684 // For mounting a full readable device. 685 Mount, 686 }; 687 688 struct SnapshotPaths { 689 // Target/base device (eg system_b), always present. 690 std::string target_device; 691 692 // COW name (eg system_cow). Not present if no COW is needed. 693 std::string cow_device_name; 694 695 // dm-snapshot instance. Not present in Update mode for VABC. 696 std::string snapshot_device; 697 }; 698 699 // Helpers for OpenSnapshotWriter. 700 std::unique_ptr<ICowWriter> OpenCompressedSnapshotWriter(LockedFile* lock, 701 const SnapshotStatus& status, 702 const SnapshotPaths& paths, 703 std::optional<uint64_t> label); 704 705 // Map the base device, COW devices, and snapshot device. 706 bool MapPartitionWithSnapshot(LockedFile* lock, CreateLogicalPartitionParams params, 707 SnapshotContext context, SnapshotPaths* paths); 708 709 // Map the COW devices, including the partition in super and the images. 710 // |params|: 711 // - |partition_name| should be the name of the top-level partition (e.g. system_b), 712 // not system_b-cow-img 713 // - |device_name| and |partition| is ignored 714 // - |timeout_ms| and the rest is respected 715 // Return the path in |cow_device_path| (e.g. /dev/block/dm-1) and major:minor in 716 // |cow_device_string| 717 bool MapCowDevices(LockedFile* lock, const CreateLogicalPartitionParams& params, 718 const SnapshotStatus& snapshot_status, AutoDeviceList* created_devices, 719 std::string* cow_name); 720 721 // The reverse of MapCowDevices. 722 bool UnmapCowDevices(LockedFile* lock, const std::string& name); 723 724 // The reverse of MapPartitionWithSnapshot. 725 bool UnmapPartitionWithSnapshot(LockedFile* lock, const std::string& target_partition_name); 726 727 // Unmap a dm-user device through snapuserd. 728 bool UnmapDmUserDevice(const std::string& dm_user_name); 729 730 // Unmap a dm-user device for user space snapshots 731 bool UnmapUserspaceSnapshotDevice(LockedFile* lock, const std::string& snapshot_name); 732 733 // If there isn't a previous update, return true. |needs_merge| is set to false. 734 // If there is a previous update but the device has not boot into it, tries to cancel the 735 // update and delete any snapshots. Return true if successful. |needs_merge| is set to false. 736 // If there is a previous update and the device has boot into it, do nothing and return true. 737 // |needs_merge| is set to true. 738 bool TryCancelUpdate(bool* needs_merge); 739 740 // Helper for CreateUpdateSnapshots. 741 // Creates all underlying images, COW partitions and snapshot files. Does not initialize them. 742 Return CreateUpdateSnapshotsInternal( 743 LockedFile* lock, const DeltaArchiveManifest& manifest, 744 PartitionCowCreator* cow_creator, AutoDeviceList* created_devices, 745 std::map<std::string, SnapshotStatus>* all_snapshot_status); 746 747 // Initialize snapshots so that they can be mapped later. 748 // Map the COW partition and zero-initialize the header. 749 Return InitializeUpdateSnapshots( 750 LockedFile* lock, uint32_t cow_version, MetadataBuilder* target_metadata, 751 const LpMetadata* exported_target_metadata, const std::string& target_suffix, 752 const std::map<std::string, SnapshotStatus>& all_snapshot_status); 753 754 // Implementation of UnmapAllSnapshots(), with the lock provided. 755 bool UnmapAllSnapshots(LockedFile* lock); 756 757 // Unmap all partitions that were mapped by CreateLogicalAndSnapshotPartitions. 758 // This should only be called in recovery. 759 bool UnmapAllPartitionsInRecovery(); 760 761 // Check no snapshot overflows. Note that this returns false negatives if the snapshot 762 // overflows, then is remapped and not written afterwards. 763 bool EnsureNoOverflowSnapshot(LockedFile* lock); 764 765 enum class Slot { Unknown, Source, Target }; 766 friend std::ostream& operator<<(std::ostream& os, SnapshotManager::Slot slot); 767 Slot GetCurrentSlot(); 768 769 // Return the suffix we expect snapshots to have. 770 std::string GetSnapshotSlotSuffix(); 771 772 std::string ReadUpdateSourceSlotSuffix(); 773 774 // Helper for RemoveAllSnapshots. 775 // Check whether |name| should be deleted as a snapshot name. 776 bool ShouldDeleteSnapshot(const std::map<std::string, bool>& flashing_status, Slot current_slot, 777 const std::string& name); 778 779 // Create or delete forward merge indicator given |wipe|. Iff wipe is scheduled, 780 // allow forward merge on FDR. 781 bool UpdateForwardMergeIndicator(bool wipe); 782 783 // Helper for HandleImminentDataWipe. 784 // Call ProcessUpdateState and handle states with special rules before data wipe. Specifically, 785 // if |allow_forward_merge| and allow-forward-merge indicator exists, initiate merge if 786 // necessary. 787 UpdateState ProcessUpdateStateOnDataWipe(bool allow_forward_merge, 788 const std::function<bool()>& callback); 789 790 // Return device string of a mapped image, or if it is not available, the mapped image path. 791 bool GetMappedImageDeviceStringOrPath(const std::string& device_name, 792 std::string* device_string_or_mapped_path); 793 794 // Same as above, but for paths only (no major:minor device strings). 795 bool GetMappedImageDevicePath(const std::string& device_name, std::string* device_path); 796 797 // Wait for a device to be created by ueventd (eg, its symlink or node to be populated). 798 // This is needed for any code that uses device-mapper path in first-stage init. If 799 // |timeout_ms| is empty or the given device is not a path, WaitForDevice immediately 800 // returns true. 801 bool WaitForDevice(const std::string& device, std::chrono::milliseconds timeout_ms); 802 803 enum class InitTransition { SELINUX_DETACH, SECOND_STAGE }; 804 805 // Initiate the transition from first-stage to second-stage snapuserd. This 806 // process involves re-creating the dm-user table entries for each device, 807 // so that they connect to the new daemon. Once all new tables have been 808 // activated, we ask the first-stage daemon to cleanly exit. 809 // 810 // If the mode is SELINUX_DETACH, snapuserd_argv must be non-null and will 811 // be populated with a list of snapuserd arguments to pass to execve(). It 812 // is otherwise ignored. 813 bool PerformInitTransition(InitTransition transition, 814 std::vector<std::string>* snapuserd_argv = nullptr); 815 snapuserd_client()816 SnapuserdClient* snapuserd_client() const { return snapuserd_client_.get(); } 817 818 // Helper of UpdateUsesCompression 819 bool UpdateUsesCompression(LockedFile* lock); 820 // Locked and unlocked functions to test whether the current update uses 821 // userspace snapshots. 822 bool UpdateUsesUserSnapshots(LockedFile* lock); 823 824 // Check if io_uring API's need to be used 825 bool UpdateUsesIouring(LockedFile* lock); 826 827 // Check if direct reads are enabled for the source image 828 bool UpdateUsesODirect(LockedFile* lock); 829 830 // Wrapper around libdm, with diagnostics. 831 bool DeleteDeviceIfExists(const std::string& name, 832 const std::chrono::milliseconds& timeout_ms = {}); 833 834 // Set read-ahead size during OTA 835 void SetReadAheadSize(const std::string& entry_block_device, off64_t size_kb); 836 837 // Returns true post OTA reboot if legacy snapuserd is required 838 bool IsLegacySnapuserdPostReboot(); 839 840 android::dm::IDeviceMapper& dm_; 841 std::unique_ptr<IDeviceInfo> device_; 842 std::string metadata_dir_; 843 std::unique_ptr<IImageManager> images_; 844 bool use_first_stage_snapuserd_ = false; 845 bool in_factory_data_reset_ = false; 846 std::function<bool(const std::string&)> uevent_regen_callback_; 847 std::unique_ptr<SnapuserdClient> snapuserd_client_; 848 std::unique_ptr<LpMetadata> old_partition_metadata_; 849 std::optional<bool> is_snapshot_userspace_; 850 std::optional<bool> is_legacy_snapuserd_; 851 }; 852 853 } // namespace snapshot 854 } // namespace android 855 856 #ifdef DEFINED_FRIEND_TEST 857 #undef DEFINED_FRIEND_TEST 858 #undef FRIEND_TEST 859 #endif 860