From ea2f32a43861563a5d9402592afec238511096a1 Mon Sep 17 00:00:00 2001 From: Yi-Yo Chiang Date: Wed, 5 Oct 2022 01:05:01 +0800 Subject: [PATCH 1/2] fs_mgr_overlayfs: Refactor scratch mounting code Make the control flow less chaotic and rename to cpp style function name. Bug: 243116800 Test: adb-remount-test Change-Id: Iccfe06f9cb9659b7b0bad085250422e298cc4f27 --- fs_mgr/fs_mgr_overlayfs.cpp | 74 +++++++++++++++---------------------- 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/fs_mgr/fs_mgr_overlayfs.cpp b/fs_mgr/fs_mgr_overlayfs.cpp index df26d8544..20989a230 100644 --- a/fs_mgr/fs_mgr_overlayfs.cpp +++ b/fs_mgr/fs_mgr_overlayfs.cpp @@ -785,8 +785,7 @@ bool fs_mgr_overlayfs_mount(const std::string& mount_point) { } // Mount kScratchMountPoint -bool fs_mgr_overlayfs_mount_scratch(const std::string& device_path, const std::string mnt_type, - bool readonly = false) { +bool MountScratch(const std::string& device_path, bool readonly = false) { if (readonly) { if (!fs_mgr_access(device_path)) { LOG(ERROR) << "Path does not exist: " << device_path; @@ -797,9 +796,12 @@ bool fs_mgr_overlayfs_mount_scratch(const std::string& device_path, const std::s return false; } - auto f2fs = fs_mgr_is_f2fs(device_path); - auto ext4 = fs_mgr_is_ext4(device_path); - if (!f2fs && !ext4) { + std::vector filesystem_candidates; + if (fs_mgr_is_f2fs(device_path)) { + filesystem_candidates = {"f2fs", "ext4"}; + } else if (fs_mgr_is_ext4(device_path)) { + filesystem_candidates = {"ext4", "f2fs"}; + } else { LOG(ERROR) << "Scratch partition is not f2fs or ext4"; return false; } @@ -816,11 +818,7 @@ bool fs_mgr_overlayfs_mount_scratch(const std::string& device_path, const std::s FstabEntry entry; entry.blk_device = device_path; entry.mount_point = kScratchMountPoint; - entry.fs_type = mnt_type; - if ((mnt_type == "f2fs") && !f2fs) entry.fs_type = "ext4"; - if ((mnt_type == "ext4") && !ext4) entry.fs_type = "f2fs"; entry.flags = MS_NOATIME | MS_RDONLY; - auto mounted = true; if (!readonly) { entry.flags &= ~MS_RDONLY; entry.flags |= MS_SYNCHRONOUS; @@ -831,14 +829,12 @@ bool fs_mgr_overlayfs_mount_scratch(const std::string& device_path, const std::s if (fs_mgr_overlayfs_already_mounted("/data", false)) { entry.fs_mgr_flags.check = true; } - if (mounted) mounted = fs_mgr_do_mount_one(entry) == 0; - if (!mounted) { - if ((entry.fs_type == "f2fs") && ext4) { - entry.fs_type = "ext4"; - mounted = fs_mgr_do_mount_one(entry) == 0; - } else if ((entry.fs_type == "ext4") && f2fs) { - entry.fs_type = "f2fs"; - mounted = fs_mgr_do_mount_one(entry) == 0; + bool mounted = false; + for (auto fs_type : filesystem_candidates) { + entry.fs_type = fs_type; + if (fs_mgr_do_mount_one(entry) == 0) { + mounted = true; + break; } } if (!createcon.Restore()) { @@ -854,17 +850,6 @@ bool fs_mgr_overlayfs_mount_scratch(const std::string& device_path, const std::s const std::string kMkF2fs("/system/bin/make_f2fs"); const std::string kMkExt4("/system/bin/mke2fs"); -// Only a suggestion for _first_ try during mounting -std::string fs_mgr_overlayfs_scratch_mount_type() { - if (!access(kMkF2fs.c_str(), X_OK) && fs_mgr_filesystem_available("f2fs")) { - return "f2fs"; - } - if (!access(kMkExt4.c_str(), X_OK) && fs_mgr_filesystem_available("ext4")) { - return "ext4"; - } - return "auto"; -} - // Note: we do not check access() here except for the super partition, since // in first-stage init we wouldn't have registed by-name symlinks for "other" // partitions that won't be mounted. @@ -927,23 +912,27 @@ static std::string GetBootScratchDevice() { return GetPhysicalScratchDevice(); } -bool fs_mgr_overlayfs_make_scratch(const std::string& scratch_device, const std::string& mnt_type) { +bool MakeScratchFilesystem(const std::string& scratch_device) { // Force mkfs by design for overlay support of adb remount, simplify and // thus do not rely on fsck to correct problems that could creep in. + auto fs_type = ""s; auto command = ""s; - if (mnt_type == "f2fs") { + if (!access(kMkF2fs.c_str(), X_OK) && fs_mgr_filesystem_available("f2fs")) { + fs_type = "f2fs"; command = kMkF2fs + " -w 4096 -f -d1 -l" + android::base::Basename(kScratchMountPoint); - } else if (mnt_type == "ext4") { + } else if (!access(kMkExt4.c_str(), X_OK) && fs_mgr_filesystem_available("ext4")) { + fs_type = "ext4"; command = kMkExt4 + " -F -b 4096 -t ext4 -m 0 -O has_journal -M " + kScratchMountPoint; } else { - LERROR << mnt_type << " has no mkfs cookbook"; + LERROR << "No supported mkfs command or filesystem driver available, supported filesystems " + "are: f2fs, ext4"; return false; } command += " " + scratch_device + " >/dev/null 2>/dev/null device, fs_mgr_overlayfs_scratch_mount_type())) { + if (MountScratch(info->device)) { bool should_destroy_scratch = false; fs_mgr_overlayfs_teardown_one(kScratchMountPoint, teardown_dir, ignore_change, &should_destroy_scratch); @@ -1702,7 +1688,7 @@ void TeardownAllOverlayForMountPoint(const std::string& mount_point) { std::string scratch_device; if (MapDsuScratchDevice(&scratch_device)) { fs_mgr_overlayfs_umount_scratch(); - if (fs_mgr_overlayfs_mount_scratch(scratch_device, fs_mgr_overlayfs_scratch_mount_type())) { + if (MountScratch(scratch_device)) { fs_mgr_overlayfs_teardown_one(kScratchMountPoint, teardown_dir, ignore_change); fs_mgr_overlayfs_umount_scratch(); } From 9032c00869ad9f2cedf2e0bf48f94b772e34d510 Mon Sep 17 00:00:00 2001 From: Yi-Yo Chiang Date: Wed, 21 Sep 2022 16:29:21 +0800 Subject: [PATCH 2/2] fs_mgr_overlayfs: Remove support for physical scratch Deprecate physical scratch path, support only dynamic partition scratch and scratch on /cache. Bug: 243116800 Test: adb-remount-test Change-Id: I8b5e08a38e323139b56b169865dcaf1a6620cf20 --- fs_mgr/fs_mgr_overlayfs.cpp | 46 ++----------- fs_mgr/tests/adb-remount-test.sh | 110 ++++++++++++++----------------- 2 files changed, 58 insertions(+), 98 deletions(-) diff --git a/fs_mgr/fs_mgr_overlayfs.cpp b/fs_mgr/fs_mgr_overlayfs.cpp index 20989a230..cc09d09a9 100644 --- a/fs_mgr/fs_mgr_overlayfs.cpp +++ b/fs_mgr/fs_mgr_overlayfs.cpp @@ -850,28 +850,6 @@ bool MountScratch(const std::string& device_path, bool readonly = false) { const std::string kMkF2fs("/system/bin/make_f2fs"); const std::string kMkExt4("/system/bin/mke2fs"); -// Note: we do not check access() here except for the super partition, since -// in first-stage init we wouldn't have registed by-name symlinks for "other" -// partitions that won't be mounted. -static std::string GetPhysicalScratchDevice() { - auto slot_number = fs_mgr_overlayfs_slot_number(); - auto super_device = fs_mgr_overlayfs_super_device(slot_number); - auto path = fs_mgr_overlayfs_super_device(slot_number == 0); - if (super_device != path) { - return path; - } - if (fs_mgr_access(super_device)) { - // Do not try to use system_other on a DAP device. - return ""; - } - - auto other_slot = fs_mgr_get_other_slot_suffix(); - if (!other_slot.empty()) { - return kPhysicalDevice + "system" + other_slot; - } - return ""; -} - // Note: The scratch partition of DSU is managed by gsid, and should be initialized during // first-stage-mount. Just check if the DM device for DSU scratch partition is created or not. static std::string GetDsuScratchDevice() { @@ -908,8 +886,7 @@ static std::string GetBootScratchDevice() { return device; } - // There is no dynamic scratch, so try and find a physical one. - return GetPhysicalScratchDevice(); + return ""; } bool MakeScratchFilesystem(const std::string& scratch_device) { @@ -1112,7 +1089,7 @@ static bool CreateScratchOnData(std::string* scratch_device, bool* partition_exi return true; } -static bool CanUseSuperPartition(const Fstab& fstab, bool* is_virtual_ab) { +static bool CanUseSuperPartition(const Fstab& fstab) { auto slot_number = fs_mgr_overlayfs_slot_number(); auto super_device = fs_mgr_overlayfs_super_device(slot_number); if (!fs_mgr_rw_access(super_device) || !fs_mgr_overlayfs_has_logical(fstab)) { @@ -1122,7 +1099,6 @@ static bool CanUseSuperPartition(const Fstab& fstab, bool* is_virtual_ab) { if (!metadata) { return false; } - *is_virtual_ab = !!(metadata->header.flags & LP_HEADER_FLAG_VIRTUAL_AB_DEVICE); return true; } @@ -1135,23 +1111,15 @@ bool fs_mgr_overlayfs_create_scratch(const Fstab& fstab, std::string* scratch_de return *partition_exists; } - // Try a physical partition first. - *scratch_device = GetPhysicalScratchDevice(); - if (!scratch_device->empty() && fs_mgr_rw_access(*scratch_device)) { - *partition_exists = true; - return true; + // Try ImageManager on /data first. + bool can_use_data = false; + if (FilesystemHasReliablePinning("/data", &can_use_data) && can_use_data) { + return CreateScratchOnData(scratch_device, partition_exists); } - // If that fails, see if we can land on super. - bool is_virtual_ab; - if (CanUseSuperPartition(fstab, &is_virtual_ab)) { - bool can_use_data = false; - if (is_virtual_ab && FilesystemHasReliablePinning("/data", &can_use_data) && can_use_data) { - return CreateScratchOnData(scratch_device, partition_exists); - } + if (CanUseSuperPartition(fstab)) { return CreateDynamicScratch(scratch_device, partition_exists); } - return false; } diff --git a/fs_mgr/tests/adb-remount-test.sh b/fs_mgr/tests/adb-remount-test.sh index a6bdd6c02..eba4f6e47 100755 --- a/fs_mgr/tests/adb-remount-test.sh +++ b/fs_mgr/tests/adb-remount-test.sh @@ -1241,40 +1241,45 @@ adb_sh grep -qE " (/system|/) [^ ]* rw," /proc/mounts /dev/null || - die -t "${T}" "expected overlay to takeover /system after remount" # KISS (we do not support sub-mounts for system partitions currently) adb_sh grep "^overlay " /proc/mounts /dev/null && fastboot_wait ${FASTBOOT_WAIT} || die "reboot into fastboot to flash scratch `usb_status`" - fastboot flash --force ${scratch_partition} ${img} + fastboot flash --force scratch ${img} err=${?} fastboot reboot || die "can not reboot out of fastboot" [ 0 -eq ${err} ] || - die "fastboot flash ${scratch_partition}" + die "fastboot flash scratch" adb_wait ${ADB_WAIT} && adb_root || - die "did not reboot after flashing empty ${scratch_partition} `usb_status`" + die "did not reboot after flashing empty scratch $(usb_status)" T=`adb_date` D=`adb disable-verity 2>&1` err=${?} @@ -1578,7 +1570,7 @@ if ${is_bootloader_fastboot} && [ -n "${scratch_partition}" ]; then [ ${err} = 0 ] && [ X"${D}" = X"${D##*setup failed}" ] && [ X"${D}" != X"${D##*[Uu]sing overlayfs}" ] && - LOG OK "${scratch_partition} recreated" || + LOG OK "recreated scratch" || die -t ${T} "setup for overlayfs" adb remount >&2 || die -t ${T} "remount failed"