From 293cd027f7147b495187502558bc2aa3454b10c7 Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Fri, 2 Aug 2019 18:58:34 -0700 Subject: [PATCH] liblp: UpdateMetadataForInPlaceSnapshot For Virtual A/B devices, rename partitions / groups ending in source slot to target slot, so that the metadata can later be written to target metadata slot. Test: liblp_test_static Bug: 135752105 Change-Id: I6a7b25e8da2808b1831f317760f4345df6b22689 --- fs_mgr/liblp/builder.cpp | 7 ++++ fs_mgr/liblp/utility.cpp | 77 ++++++++++++++++++++++++++++++++++ fs_mgr/liblp/utility.h | 5 +++ fs_mgr/liblp/utility_test.cpp | 79 +++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+) diff --git a/fs_mgr/liblp/builder.cpp b/fs_mgr/liblp/builder.cpp index 6dc288e1e..77c9c2878 100644 --- a/fs_mgr/liblp/builder.cpp +++ b/fs_mgr/liblp/builder.cpp @@ -171,6 +171,13 @@ std::unique_ptr MetadataBuilder::NewForUpdate(const IPartitionO } } + if (IPropertyFetcher::GetInstance()->GetBoolProperty("ro.virtual_ab.enabled", false)) { + if (!UpdateMetadataForInPlaceSnapshot(metadata.get(), source_slot_number, + target_slot_number)) { + return nullptr; + } + } + return New(*metadata.get(), &opener); } diff --git a/fs_mgr/liblp/utility.cpp b/fs_mgr/liblp/utility.cpp index 338b52507..17f05aa7c 100644 --- a/fs_mgr/liblp/utility.cpp +++ b/fs_mgr/liblp/utility.cpp @@ -24,6 +24,10 @@ #include #endif +#include +#include +#include + #include #include #include @@ -182,6 +186,14 @@ bool UpdatePartitionGroupName(LpMetadataPartitionGroup* group, const std::string return true; } +bool UpdatePartitionName(LpMetadataPartition* partition, const std::string& name) { + if (name.size() > sizeof(partition->name)) { + return false; + } + strncpy(partition->name, name.c_str(), sizeof(partition->name)); + return true; +} + bool SetBlockReadonly(int fd, bool readonly) { #if defined(__linux__) int val = readonly; @@ -207,5 +219,70 @@ base::unique_fd GetControlFileOrOpen(const char* path, int flags) { return base::unique_fd(open(path, flags)); } +bool UpdateMetadataForInPlaceSnapshot(LpMetadata* metadata, uint32_t source_slot_number, + uint32_t target_slot_number) { + std::string source_slot_suffix = SlotSuffixForSlotNumber(source_slot_number); + std::string target_slot_suffix = SlotSuffixForSlotNumber(target_slot_number); + + // There can be leftover groups with target suffix on retrofit devices. + // They are useless now, so delete. + std::vector new_group_ptrs; + for (auto& group : metadata->groups) { + std::string group_name = GetPartitionGroupName(group); + std::string slot_suffix = GetPartitionSlotSuffix(group_name); + // Don't add groups with target slot suffix. + if (slot_suffix == target_slot_suffix) continue; + // Replace source slot suffix with target slot suffix. + if (slot_suffix == source_slot_suffix) { + std::string new_name = group_name.substr(0, group_name.size() - slot_suffix.size()) + + target_slot_suffix; + if (!UpdatePartitionGroupName(&group, new_name)) { + LERROR << "Group name too long: " << new_name; + return false; + } + } + new_group_ptrs.push_back(&group); + } + + std::vector new_partition_ptrs; + for (auto& partition : metadata->partitions) { + std::string partition_name = GetPartitionName(partition); + std::string slot_suffix = GetPartitionSlotSuffix(partition_name); + // Don't add partitions with target slot suffix. + if (slot_suffix == target_slot_suffix) continue; + // Replace source slot suffix with target slot suffix. + if (slot_suffix == source_slot_suffix) { + std::string new_name = + partition_name.substr(0, partition_name.size() - slot_suffix.size()) + + target_slot_suffix; + if (!UpdatePartitionName(&partition, new_name)) { + LERROR << "Partition name too long: " << new_name; + return false; + } + } + // Update group index. + auto it = std::find(new_group_ptrs.begin(), new_group_ptrs.end(), + &metadata->groups[partition.group_index]); + if (it == new_group_ptrs.end()) { + LWARN << "Removing partition " << partition_name << " from group " + << GetPartitionGroupName(metadata->groups[partition.group_index]) + << "; this partition should not belong to this group!"; + continue; // not adding to new_partition_ptrs + } + partition.group_index = std::distance(new_group_ptrs.begin(), it); + new_partition_ptrs.push_back(&partition); + } + + std::vector new_partitions; + for (auto* p : new_partition_ptrs) new_partitions.emplace_back(std::move(*p)); + metadata->partitions = std::move(new_partitions); + + std::vector new_groups; + for (auto* g : new_group_ptrs) new_groups.emplace_back(std::move(*g)); + metadata->groups = std::move(new_groups); + + return true; +} + } // namespace fs_mgr } // namespace android diff --git a/fs_mgr/liblp/utility.h b/fs_mgr/liblp/utility.h index 96f17174a..25ab66bae 100644 --- a/fs_mgr/liblp/utility.h +++ b/fs_mgr/liblp/utility.h @@ -89,12 +89,17 @@ constexpr uint64_t AlignTo(uint64_t base, uint32_t alignment, uint32_t alignment // Update names from C++ strings. bool UpdateBlockDevicePartitionName(LpMetadataBlockDevice* device, const std::string& name); bool UpdatePartitionGroupName(LpMetadataPartitionGroup* group, const std::string& name); +bool UpdatePartitionName(LpMetadataPartition* partition, const std::string& name); // Call BLKROSET ioctl on fd so that fd is readonly / read-writable. bool SetBlockReadonly(int fd, bool readonly); ::android::base::unique_fd GetControlFileOrOpen(const char* path, int flags); +// For Virtual A/B updates, modify |metadata| so that it can be written to |target_slot_number|. +bool UpdateMetadataForInPlaceSnapshot(LpMetadata* metadata, uint32_t source_slot_number, + uint32_t target_slot_number); + } // namespace fs_mgr } // namespace android diff --git a/fs_mgr/liblp/utility_test.cpp b/fs_mgr/liblp/utility_test.cpp index 15f7fff4e..cac3989e2 100644 --- a/fs_mgr/liblp/utility_test.cpp +++ b/fs_mgr/liblp/utility_test.cpp @@ -15,6 +15,7 @@ */ #include +#include #include #include "utility.h" @@ -75,3 +76,81 @@ TEST(liblp, GetPartitionSlotSuffix) { EXPECT_EQ(GetPartitionSlotSuffix("system_a"), "_a"); EXPECT_EQ(GetPartitionSlotSuffix("system_b"), "_b"); } + +namespace android { +namespace fs_mgr { +// Equality comparison for testing. In reality, equality of device_index doesn't +// necessary mean equality of the block device. +bool operator==(const LinearExtent& l, const LinearExtent& r) { + return l.device_index() == r.device_index() && l.physical_sector() == r.physical_sector() && + l.end_sector() == r.end_sector(); +} +} // namespace fs_mgr +} // namespace android + +static std::vector GetPartitionExtents(Partition* p) { + std::vector extents; + for (auto&& extent : p->extents()) { + auto linear_extent = extent->AsLinearExtent(); + if (!linear_extent) return {}; + extents.push_back(*linear_extent); + } + return extents; +} + +TEST(liblp, UpdateMetadataForInPlaceSnapshot) { + using std::unique_ptr; + + unique_ptr builder = MetadataBuilder::New(1024 * 1024, 1024, 2); + ASSERT_NE(builder, nullptr); + + ASSERT_TRUE(builder->AddGroup("group_a", 256 * 1024)); + Partition* system_a = builder->AddPartition("system_a", "group_a", LP_PARTITION_ATTR_READONLY); + ASSERT_NE(system_a, nullptr); + ASSERT_TRUE(builder->ResizePartition(system_a, 40 * 1024)); + Partition* vendor_a = builder->AddPartition("vendor_a", "group_a", LP_PARTITION_ATTR_READONLY); + ASSERT_NE(vendor_a, nullptr); + ASSERT_TRUE(builder->ResizePartition(vendor_a, 20 * 1024)); + + ASSERT_TRUE(builder->AddGroup("group_b", 258 * 1024)); + Partition* system_b = builder->AddPartition("system_b", "group_b", LP_PARTITION_ATTR_READONLY); + ASSERT_NE(system_b, nullptr); + ASSERT_TRUE(builder->ResizePartition(system_b, 36 * 1024)); + Partition* vendor_b = builder->AddPartition("vendor_b", "group_b", LP_PARTITION_ATTR_READONLY); + ASSERT_NE(vendor_b, nullptr); + ASSERT_TRUE(builder->ResizePartition(vendor_b, 32 * 1024)); + + auto system_a_extents = GetPartitionExtents(system_a); + ASSERT_FALSE(system_a_extents.empty()); + + auto vendor_a_extents = GetPartitionExtents(vendor_a); + ASSERT_FALSE(vendor_a_extents.empty()); + + auto metadata = builder->Export(); + ASSERT_NE(nullptr, metadata); + + ASSERT_TRUE(UpdateMetadataForInPlaceSnapshot(metadata.get(), 0, 1)); + + auto new_builder = MetadataBuilder::New(*metadata); + ASSERT_NE(nullptr, new_builder); + + EXPECT_EQ(nullptr, new_builder->FindGroup("group_a")); + EXPECT_EQ(nullptr, new_builder->FindPartition("system_a")); + EXPECT_EQ(nullptr, new_builder->FindPartition("vendor_a")); + + auto group_b = new_builder->FindGroup("group_b"); + ASSERT_NE(nullptr, group_b); + ASSERT_EQ(256 * 1024, group_b->maximum_size()); + + auto new_system_b = new_builder->FindPartition("system_b"); + ASSERT_NE(nullptr, new_system_b); + EXPECT_EQ(40 * 1024, new_system_b->size()); + auto new_system_b_extents = GetPartitionExtents(new_system_b); + EXPECT_EQ(system_a_extents, new_system_b_extents); + + auto new_vendor_b = new_builder->FindPartition("vendor_b"); + ASSERT_NE(nullptr, new_vendor_b); + EXPECT_EQ(20 * 1024, new_vendor_b->size()); + auto new_vendor_b_extents = GetPartitionExtents(new_vendor_b); + EXPECT_EQ(vendor_a_extents, new_vendor_b_extents); +}