Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a3bf8478 authored by David Anderson's avatar David Anderson
Browse files

remount: Do not run the create-scratch logic on older devices.

The prologue of fs_mgr_overlayfs_create_scratch() will implicitly
succeed on physical block devices, and implicitly fail if for some
reason they can't be accessed.

This patch makes the success and failure cases explicit. The logic
specific to DAP has been moved to CreateDynamicScratch.

fs_mgr_overlayfs_create_scratch now calls GetScratchStrategy, and only
calls CreateDynamicScratch for DAP-launch devices. In the case a
physical block device can be used, no action is taken.

Bug: 134949511
Test: adb remount and adb_remount_test.sh
Change-Id: I0af7cda9bc551416c9e2ffca5a36305f89d3bf46
parent 40a8b07e
Loading
Loading
Loading
Loading
+32 −14
Original line number Original line Diff line number Diff line
@@ -825,7 +825,8 @@ enum class ScratchStrategy {
    kSystemOther
    kSystemOther
};
};


static ScratchStrategy GetScratchStrategy(std::string* backing_device) {
// Return the strategy this device must use for creating a scratch partition.
static ScratchStrategy GetScratchStrategy(std::string* backing_device = nullptr) {
    auto slot_number = fs_mgr_overlayfs_slot_number();
    auto slot_number = fs_mgr_overlayfs_slot_number();
    auto super_device = fs_mgr_overlayfs_super_device(slot_number);
    auto super_device = fs_mgr_overlayfs_super_device(slot_number);
    auto path = fs_mgr_overlayfs_super_device(slot_number == 0);
    auto path = fs_mgr_overlayfs_super_device(slot_number == 0);
@@ -834,11 +835,11 @@ static ScratchStrategy GetScratchStrategy(std::string* backing_device) {
        // wouldn't have registed by-name symlinks for the device as it's
        // wouldn't have registed by-name symlinks for the device as it's
        // normally not needed. The access checks elsewhere in this function
        // normally not needed. The access checks elsewhere in this function
        // are safe because system/super are always required.
        // are safe because system/super are always required.
        *backing_device = path;
        if (backing_device) *backing_device = path;
        return ScratchStrategy::kSuperOther;
        return ScratchStrategy::kSuperOther;
    }
    }
    if (fs_mgr_access(super_device)) {
    if (fs_mgr_access(super_device)) {
        *backing_device = super_device;
        if (backing_device) *backing_device = super_device;
        return ScratchStrategy::kDynamicPartition;
        return ScratchStrategy::kDynamicPartition;
    }
    }


@@ -846,7 +847,7 @@ static ScratchStrategy GetScratchStrategy(std::string* backing_device) {
    if (!other_slot.empty()) {
    if (!other_slot.empty()) {
        path = kPhysicalDevice + "system" + other_slot;
        path = kPhysicalDevice + "system" + other_slot;
        if (fs_mgr_access(path)) {
        if (fs_mgr_access(path)) {
            *backing_device = path;
            if (backing_device) *backing_device = path;
            return ScratchStrategy::kSystemOther;
            return ScratchStrategy::kSystemOther;
        }
        }
    }
    }
@@ -924,16 +925,15 @@ static void TruncatePartitionsWithSuffix(MetadataBuilder* builder, const std::st
    }
    }
}
}


// This is where we find and steal backing storage from the system.
// Create or update a scratch partition within super.
bool fs_mgr_overlayfs_create_scratch(const Fstab& fstab, std::string* scratch_device,
static bool CreateDynamicScratch(const Fstab& fstab, std::string* scratch_device,
                                 bool* partition_exists, bool* change) {
                                 bool* partition_exists, bool* change) {
    *scratch_device = fs_mgr_overlayfs_scratch_device();
    const auto partition_name = android::base::Basename(kScratchMountPoint);
    *partition_exists = fs_mgr_rw_access(*scratch_device);

    auto& dm = DeviceMapper::Instance();
    *partition_exists = dm.GetState(partition_name) != DmDeviceState::INVALID;

    auto partition_create = !*partition_exists;
    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 slot_number = fs_mgr_overlayfs_slot_number();
    auto super_device = fs_mgr_overlayfs_super_device(slot_number);
    auto super_device = fs_mgr_overlayfs_super_device(slot_number);
    if (!fs_mgr_rw_access(super_device)) return false;
    if (!fs_mgr_rw_access(super_device)) return false;
@@ -943,7 +943,6 @@ bool fs_mgr_overlayfs_create_scratch(const Fstab& fstab, std::string* scratch_de
        LERROR << "open " << super_device << " metadata";
        LERROR << "open " << super_device << " metadata";
        return false;
        return false;
    }
    }
    const auto partition_name = android::base::Basename(kScratchMountPoint);
    auto partition = builder->FindPartition(partition_name);
    auto partition = builder->FindPartition(partition_name);
    *partition_exists = partition != nullptr;
    *partition_exists = partition != nullptr;
    auto changed = false;
    auto changed = false;
@@ -1024,6 +1023,25 @@ bool fs_mgr_overlayfs_create_scratch(const Fstab& fstab, std::string* scratch_de
    return true;
    return true;
}
}


bool fs_mgr_overlayfs_create_scratch(const Fstab& fstab, std::string* scratch_device,
                                     bool* partition_exists, bool* change) {
    auto strategy = GetScratchStrategy();
    if (strategy == ScratchStrategy::kDynamicPartition) {
        return CreateDynamicScratch(fstab, scratch_device, partition_exists, change);
    }

    // The scratch partition can only be landed on a physical partition if we
    // get here. If there are no viable candidates that are R/W, just return
    // that there is no device.
    *scratch_device = GetScratchDevice();
    if (scratch_device->empty()) {
        errno = ENXIO;
        return false;
    }
    *partition_exists = true;
    return true;
}

// Create and mount kScratchMountPoint storage if we have logical partitions
// Create and mount kScratchMountPoint storage if we have logical partitions
bool fs_mgr_overlayfs_setup_scratch(const Fstab& fstab, bool* change) {
bool fs_mgr_overlayfs_setup_scratch(const Fstab& fstab, bool* change) {
    if (fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) return true;
    if (fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) return true;