From 43d9f1833fd79d9fef8b7847f9aeb27b4d51fa6f Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 10 Dec 2019 21:18:28 -0800 Subject: [PATCH] remount: Refactor fs_mgr_overlayfs_teardown This pulls code for mapping the scratch device into a separate function. It also avoids implicitly failing by passing an empty device string. Finally, it calls GetScratchDevice, to remove a caller of the deprecated method fs_mgr_overlayfs_scratch_device(). Bug: 134949511 Test: adb remount and adb_remount_test.sh Change-Id: If4a543d3fa26af3f8578ec8b236859c8e4d9bfd8 --- fs_mgr/fs_mgr_overlayfs.cpp | 38 ++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/fs_mgr/fs_mgr_overlayfs.cpp b/fs_mgr/fs_mgr_overlayfs.cpp index a2fe22e5c..e236d79e1 100644 --- a/fs_mgr/fs_mgr_overlayfs.cpp +++ b/fs_mgr/fs_mgr_overlayfs.cpp @@ -1252,6 +1252,27 @@ bool fs_mgr_overlayfs_setup(const char* backing, const char* mount_point, bool* return ret; } +static bool GetAndMapScratchDeviceIfNeeded(std::string* device) { + *device = GetScratchDevice(); + if (!device->empty()) { + return true; + } + + auto strategy = GetScratchStrategy(); + if (strategy == ScratchStrategy::kDynamicPartition) { + auto metadata_slot = fs_mgr_overlayfs_slot_number(); + CreateLogicalPartitionParams params = { + .block_device = fs_mgr_overlayfs_super_device(metadata_slot), + .metadata_slot = metadata_slot, + .partition_name = android::base::Basename(kScratchMountPoint), + .force_writable = true, + .timeout_ms = 10s, + }; + return CreateLogicalPartition(params, device); + } + return false; +} + // Returns false if teardown not permitted, errno set to last error. // If something is altered, set *change. bool fs_mgr_overlayfs_teardown(const char* mount_point, bool* change) { @@ -1261,20 +1282,11 @@ bool fs_mgr_overlayfs_teardown(const char* mount_point, bool* change) { // specific override entries. auto mount_scratch = false; if ((mount_point != nullptr) && !fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) { - auto scratch_device = fs_mgr_overlayfs_scratch_device(); - if (scratch_device.empty()) { - auto metadata_slot = fs_mgr_overlayfs_slot_number(); - CreateLogicalPartitionParams params = { - .block_device = fs_mgr_overlayfs_super_device(metadata_slot), - .metadata_slot = metadata_slot, - .partition_name = android::base::Basename(kScratchMountPoint), - .force_writable = true, - .timeout_ms = 10s, - }; - CreateLogicalPartition(params, &scratch_device); + std::string scratch_device; + if (GetAndMapScratchDeviceIfNeeded(&scratch_device)) { + mount_scratch = fs_mgr_overlayfs_mount_scratch(scratch_device, + fs_mgr_overlayfs_scratch_mount_type()); } - mount_scratch = fs_mgr_overlayfs_mount_scratch(scratch_device, - fs_mgr_overlayfs_scratch_mount_type()); } for (const auto& overlay_mount_point : kOverlayMountPoints) { ret &= fs_mgr_overlayfs_teardown_one(