fs_mgr: overlay: use alternate for backing storage

When we have multiple physical super partitions, it is prudent to
take the alternate super physical partition in its raw entirety
rather than to construct a logical partition out of the current
super partition's limited remaining space.

SideEffect: alternate boot partitions destroyed for the sake of debug
Test: adb-remount-test.sh (including manually disabled to check
      both code paths).
Bug: 119885423
Change-Id: I368d3a5619f69de3b0d1fcad73c4b42d25d0d969
diff --git a/fs_mgr/README.overlayfs.md b/fs_mgr/README.overlayfs.md
index 171936b..fbb5f5d 100644
--- a/fs_mgr/README.overlayfs.md
+++ b/fs_mgr/README.overlayfs.md
@@ -95,6 +95,12 @@
   multiple reasons.
   NB: This is not a problem for fastbootd or recovery as overrides are
   disabled for those special boot scenarios.
+- For implementation simplicity on retrofit dynamic partition devices,
+  take the whole alternate super (eg: if "*a*" slot, then the whole of
+  "*system_b*").
+  Since landing a filesystem on the alternate super physical device
+  without differentiating if it is setup to support logical or physical,
+  the alternate slot metadata and previous content will be lost.
 - If dynamic partitions runs out of space, resizing a logical
   partition larger may fail because of the scratch partition.
   If this happens, either fastboot flashall or adb enable-verity can
diff --git a/fs_mgr/fs_mgr_overlayfs.cpp b/fs_mgr/fs_mgr_overlayfs.cpp
index 25b32c8..fdfe1c0 100644
--- a/fs_mgr/fs_mgr_overlayfs.cpp
+++ b/fs_mgr/fs_mgr_overlayfs.cpp
@@ -386,8 +386,10 @@
     return SlotNumberForSlotSuffix(fs_mgr_get_slot_suffix());
 }
 
+const auto kPhysicalDevice = "/dev/block/by-name/"s;
+
 std::string fs_mgr_overlayfs_super_device(uint32_t slot_number) {
-    return "/dev/block/by-name/" + fs_mgr_get_super_partition_name(slot_number);
+    return kPhysicalDevice + fs_mgr_get_super_partition_name(slot_number);
 }
 
 bool fs_mgr_overlayfs_has_logical(const fstab* fstab) {
@@ -645,10 +647,16 @@
 std::string fs_mgr_overlayfs_scratch_device() {
     if (!scratch_device_cache.empty()) return scratch_device_cache;
 
-    auto& dm = DeviceMapper::Instance();
-    const auto partition_name = android::base::Basename(kScratchMountPoint);
-    std::string path;
-    if (!dm.GetDmDevicePathByName(partition_name, &path)) return "";
+    // Is this a multiple super device (retrofit)?
+    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) {
+        // Create from within single super device;
+        auto& dm = DeviceMapper::Instance();
+        const auto partition_name = android::base::Basename(kScratchMountPoint);
+        if (!dm.GetDmDevicePathByName(partition_name, &path)) return "";
+    }
     return scratch_device_cache = path;
 }
 
@@ -679,6 +687,10 @@
     *scratch_device = fs_mgr_overlayfs_scratch_device();
     *partition_exists = fs_mgr_rw_access(*scratch_device);
     auto partition_create = !*partition_exists;
+    // Do we need to create a logical "scratch" partition?
+    if (!partition_create && android::base::StartsWith(*scratch_device, kPhysicalDevice)) {
+        return true;
+    }
     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)) return false;
@@ -784,6 +796,7 @@
 bool fs_mgr_overlayfs_scratch_can_be_mounted(const std::string& scratch_device) {
     if (scratch_device.empty()) return false;
     if (fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) return false;
+    if (android::base::StartsWith(scratch_device, kPhysicalDevice)) return true;
     if (fs_mgr_rw_access(scratch_device)) return true;
     auto slot_number = fs_mgr_overlayfs_slot_number();
     auto super_device = fs_mgr_overlayfs_super_device(slot_number);
diff --git a/fs_mgr/tests/adb-remount-test.sh b/fs_mgr/tests/adb-remount-test.sh
index c7c86eb..561debb 100755
--- a/fs_mgr/tests/adb-remount-test.sh
+++ b/fs_mgr/tests/adb-remount-test.sh
@@ -378,6 +378,11 @@
 M=`adb_sh cat /proc/mounts | sed -n 's@\([^ ]*\) /mnt/scratch \([^ ]*\) .*@\2 on \1@p'`
 [ -n "${M}" ] &&
   echo "${BLUE}[     INFO ]${NORMAL} scratch filesystem ${M}"
+uses_dynamic_scratch=true
+if [ "${M}" != "${M##*/dev/block/by-name/}" ]; then
+  uses_dynamic_scratch=false
+  scratch_partition="${M##*/dev/block/by-name/}"
+fi
 scratch_size=`adb_sh df -k /mnt/scratch </dev/null 2>/dev/null |
               while read device kblocks used available use mounted on; do
                 if [ "/mnt/scratch" = "\${mounted}" ]; then
@@ -453,16 +458,30 @@
   fastboot flash vendor ||
   ( fastboot reboot && false) ||
   die "fastboot flash vendor"
-# check ${scratch_partition} via fastboot
-fastboot_getvar partition-type:${scratch_partition} raw &&
-  fastboot_getvar has-slot:${scratch_partition} no &&
-  fastboot_getvar is-logical:${scratch_partition} yes ||
+fastboot_getvar partition-type:${scratch_partition} raw ||
   ( fastboot reboot && false) ||
   die "fastboot can not see ${scratch_partition} parameters"
-echo "${BLUE}[     INFO ]${NORMAL} expect fastboot erase ${scratch_partition} to fail" >&2
-fastboot erase ${scratch_partition} &&
-  ( fastboot reboot || true) &&
-  die "fastboot can erase ${scratch_partition}"
+if ${uses_dynamic_scratch}; then
+  # check ${scratch_partition} via fastboot
+  fastboot_getvar has-slot:${scratch_partition} no &&
+    fastboot_getvar is-logical:${scratch_partition} yes ||
+    ( fastboot reboot && false) ||
+    die "fastboot can not see ${scratch_partition} parameters"
+else
+  fastboot_getvar is-logical:${scratch_partition} no ||
+    ( fastboot reboot && false) ||
+    die "fastboot can not see ${scratch_partition} parameters"
+fi
+if ! ${uses_dynamic_scratch}; then
+  fastboot reboot-bootloader ||
+    die "Reboot into fastboot"
+fi
+if ${uses_dynamic_scratch}; then
+  echo "${BLUE}[     INFO ]${NORMAL} expect fastboot erase ${scratch_partition} to fail" >&2
+  fastboot erase ${scratch_partition} &&
+    ( fastboot reboot || true) &&
+    die "fastboot can erase ${scratch_partition}"
+fi
 echo "${BLUE}[     INFO ]${NORMAL} expect fastboot format ${scratch_partition} to fail" >&2
 fastboot format ${scratch_partition} &&
   ( fastboot reboot || true) &&
@@ -507,12 +526,13 @@
 
 echo "${GREEN}[ RUN      ]${NORMAL} test fastboot flash to ${scratch_partition}" >&2
 
-adb reboot-fastboot &&
-  dd if=/dev/zero of=/tmp/adb-remount-test.img bs=4096 count=16 2>/dev/null &&
+adb reboot-fastboot ||
+  die "Reboot into fastbootd"
+dd if=/dev/zero of=/tmp/adb-remount-test.img bs=4096 count=16 2>/dev/null &&
   fastboot_wait 2m ||
   ( rm /tmp/adb-remount-test.img && false) ||
   die "reboot into fastboot"
-fastboot flash ${scratch_partition} /tmp/adb-remount-test.img
+fastboot flash --force ${scratch_partition} /tmp/adb-remount-test.img
 err=${?}
 rm /tmp/adb-remount-test.img
 fastboot reboot ||