From 69d574c612f8e42c2028e21faa203ce3fc856d30 Mon Sep 17 00:00:00 2001 From: Akilesh Kailash Date: Fri, 23 Feb 2024 14:20:47 -0800 Subject: [PATCH] libsnapshot: Fetch device size from header Now that V3 is enabled, relax the header version check. For V3, header op_count_max contains the information of the device size. Bug: 299011882 Test: snapshotctl map-snapshots on Pixel with V3 format Change-Id: Ia1cb20b24857136a742e20408ee95e56e98b256a Signed-off-by: Akilesh Kailash --- fs_mgr/libsnapshot/snapshot.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/fs_mgr/libsnapshot/snapshot.cpp b/fs_mgr/libsnapshot/snapshot.cpp index cbe828547..e6c4de623 100644 --- a/fs_mgr/libsnapshot/snapshot.cpp +++ b/fs_mgr/libsnapshot/snapshot.cpp @@ -508,8 +508,6 @@ bool SnapshotManager::MapDmUserCow(LockedFile* lock, const std::string& name, // When snapshots are on current slot, we determine the size // of block device based on the number of COW operations. We cannot // use base device as it will be from older image. - size_t num_ops = 0; - uint64_t dev_sz = 0; unique_fd fd(open(cow_file.c_str(), O_RDONLY | O_CLOEXEC)); if (fd < 0) { PLOG(ERROR) << "Failed to open " << cow_file; @@ -522,13 +520,18 @@ bool SnapshotManager::MapDmUserCow(LockedFile* lock, const std::string& name, return false; } + uint64_t dev_sz = 0; const auto& header = reader.GetHeader(); - if (header.prefix.major_version > 2) { - LOG(ERROR) << "COW format not supported"; - return false; + if (header.prefix.major_version == 2) { + const size_t num_ops = reader.get_num_total_data_ops(); + dev_sz = (num_ops * header.block_size); + } else { + // create_snapshot will skip in-place copy ops. Hence, fetch this + // information directly from v3 header. + const auto& v3_header = reader.header_v3(); + dev_sz = v3_header.op_count_max * v3_header.block_size; } - num_ops = reader.get_num_total_data_ops(); - dev_sz = (num_ops * header.block_size); + base_sectors = dev_sz >> 9; } else { // For userspace snapshots, the size of the base device is taken as the