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