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

Commit 1094aeb1 authored by Yifan Hong's avatar Yifan Hong Committed by android-build-merger
Browse files

Merge "fs_mgr: Allow to set defaults for CreateLogicalPartitionParams"

am: 50ca4484

Change-Id: Id6c8ac38e2811315ce8ab8216127c402027bf2cf
parents dd97b74b 50ca4484
Loading
Loading
Loading
Loading
+43 −27
Original line number Diff line number Diff line
@@ -154,61 +154,77 @@ bool CreateLogicalPartitions(const LpMetadata& metadata, const std::string& supe
    return true;
}

bool CreateLogicalPartition(const CreateLogicalPartitionParams& params, std::string* path) {
    const LpMetadata* metadata = params.metadata;
bool CreateLogicalPartitionParams::InitDefaults(CreateLogicalPartitionParams::OwnedData* owned) {
    if (block_device.empty()) {
        LOG(ERROR) << "block_device is required for CreateLogicalPartition";
        return false;
    }

    // Read metadata if needed.
    std::unique_ptr<LpMetadata> local_metadata;
    if (!metadata) {
        if (!params.metadata_slot) {
        if (!metadata_slot) {
            LOG(ERROR) << "Either metadata or a metadata slot must be specified.";
            return false;
        }
        auto slot = *params.metadata_slot;
        if (local_metadata = ReadMetadata(params.block_device, slot); !local_metadata) {
            LOG(ERROR) << "Could not read partition table for: " << params.block_device;
        auto slot = *metadata_slot;
        if (owned->metadata = ReadMetadata(block_device, slot); !owned->metadata) {
            LOG(ERROR) << "Could not read partition table for: " << block_device;
            return false;
        }
        metadata = local_metadata.get();
        metadata = owned->metadata.get();
    }

    // Find the partition by name if needed.
    const LpMetadataPartition* partition = params.partition;
    if (!partition) {
        for (const auto& iter : metadata->partitions) {
            if (GetPartitionName(iter) == params.partition_name) {
                partition = &iter;
        for (const auto& metadata_partition : metadata->partitions) {
            if (android::fs_mgr::GetPartitionName(metadata_partition) == partition_name) {
                partition = &metadata_partition;
                break;
            }
        }
    }
    if (!partition) {
            LERROR << "Could not find any partition with name: " << params.partition_name;
        LERROR << "Could not find any partition with name: " << partition_name;
        return false;
    }
    if (partition_name.empty()) {
        partition_name = android::fs_mgr::GetPartitionName(*partition);
    } else if (partition_name != android::fs_mgr::GetPartitionName(*partition)) {
        LERROR << "Inconsistent partition_name " << partition_name << " with partition "
               << android::fs_mgr::GetPartitionName(*partition);
        return false;
    }

    if (!partition_opener) {
        owned->partition_opener = std::make_unique<PartitionOpener>();
        partition_opener = owned->partition_opener.get();
    }

    PartitionOpener default_opener;
    const IPartitionOpener* opener =
            params.partition_opener ? params.partition_opener : &default_opener;
    if (device_name.empty()) {
        device_name = partition_name;
    }

    return true;
}

bool CreateLogicalPartition(CreateLogicalPartitionParams params, std::string* path) {
    CreateLogicalPartitionParams::OwnedData owned_data;
    if (!params.InitDefaults(&owned_data)) return false;

    DmTable table;
    if (!CreateDmTable(*opener, *metadata, *partition, params.block_device, &table)) {
    if (!CreateDmTable(*params.partition_opener, *params.metadata, *params.partition,
                       params.block_device, &table)) {
        return false;
    }
    if (params.force_writable) {
        table.set_readonly(false);
    }

    std::string device_name = params.device_name;
    if (device_name.empty()) {
        device_name = GetPartitionName(*partition);
    }

    DeviceMapper& dm = DeviceMapper::Instance();
    if (!dm.CreateDevice(device_name, table, path, params.timeout_ms)) {
    if (!dm.CreateDevice(params.device_name, table, path, params.timeout_ms)) {
        return false;
    }
    LINFO << "Created logical partition " << device_name << " on device " << *path;
    LINFO << "Created logical partition " << params.device_name << " on device " << *path;
    return true;
}

+16 −1
Original line number Diff line number Diff line
@@ -81,9 +81,24 @@ struct CreateLogicalPartitionParams {
    // Helpers for determining the effective partition and device name.
    std::string GetPartitionName() const;
    std::string GetDeviceName() const;

    // Specify ownership of fields. The ownership of these fields are managed
    // by the caller of InitDefaults().
    // These are not declared in CreateLogicalPartitionParams so that the
    // copy constructor is not deleted.
    struct OwnedData {
        std::unique_ptr<LpMetadata> metadata;
        std::unique_ptr<IPartitionOpener> partition_opener;
    };

    // Fill in default values for |params| that CreateLogicalPartition assumes. Caller does
    // not need to call this before calling CreateLogicalPartition; CreateLogicalPartition sets
    // values when they are missing.
    // Caller is responsible for destroying owned_data when |this| is not used.
    bool InitDefaults(OwnedData* owned);
};

bool CreateLogicalPartition(const CreateLogicalPartitionParams& params, std::string* path);
bool CreateLogicalPartition(CreateLogicalPartitionParams params, std::string* path);

// Destroy the block device for a logical partition, by name. If |timeout_ms|
// is non-zero, then this will block until the device path has been unlinked.