From 50ce7e090ae71d7c950e889cef342faab444c851 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 25 Feb 2019 17:31:56 -0800 Subject: [PATCH] libfiemap_writer: Allow callers to query the block device of a file. Bug: 126249541 Test: manual test Change-Id: I478bcf60caf0cedc84189c39fee4b6c4f94b6ed6 --- fs_mgr/libfiemap_writer/fiemap_writer.cpp | 9 +++++++-- .../include/libfiemap_writer/fiemap_writer.h | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/fs_mgr/libfiemap_writer/fiemap_writer.cpp b/fs_mgr/libfiemap_writer/fiemap_writer.cpp index b9b75f87e..fc24a173f 100644 --- a/fs_mgr/libfiemap_writer/fiemap_writer.cpp +++ b/fs_mgr/libfiemap_writer/fiemap_writer.cpp @@ -126,7 +126,8 @@ static bool DeviceMapperStackPop(const std::string& bdev, std::string* bdev_raw) return DeviceMapperStackPop(bdev_next, bdev_raw); } -static bool FileToBlockDevicePath(const std::string& file_path, std::string* bdev_path) { +bool FiemapWriter::GetBlockDeviceForFile(const std::string& file_path, std::string* bdev_path, + bool* uses_dm) { struct stat sb; if (stat(file_path.c_str(), &sb)) { PLOG(ERROR) << "Failed to get stat for: " << file_path; @@ -146,6 +147,10 @@ static bool FileToBlockDevicePath(const std::string& file_path, std::string* bde return false; } + if (uses_dm) { + *uses_dm = (bdev_raw != bdev); + } + LOG(DEBUG) << "Popped device (" << bdev_raw << ") from device mapper stack starting with (" << bdev << ")"; @@ -458,7 +463,7 @@ FiemapUniquePtr FiemapWriter::Open(const std::string& file_path, uint64_t file_s } std::string bdev_path; - if (!FileToBlockDevicePath(abs_path, &bdev_path)) { + if (!GetBlockDeviceForFile(abs_path, &bdev_path)) { LOG(ERROR) << "Failed to get block dev path for file: " << file_path; cleanup(abs_path, create); return nullptr; diff --git a/fs_mgr/libfiemap_writer/include/libfiemap_writer/fiemap_writer.h b/fs_mgr/libfiemap_writer/include/libfiemap_writer/fiemap_writer.h index edbae7721..b5472c9f7 100644 --- a/fs_mgr/libfiemap_writer/include/libfiemap_writer/fiemap_writer.h +++ b/fs_mgr/libfiemap_writer/include/libfiemap_writer/fiemap_writer.h @@ -57,6 +57,13 @@ class FiemapWriter final { // FiemapWriter::Open). static bool HasPinnedExtents(const std::string& file_path); + // Returns the underlying block device of a file. This will look past device-mapper layers. + // If an intermediate device-mapper layer would not maintain a 1:1 mapping (i.e. is a non- + // trivial dm-linear), then this will fail. If device-mapper nodes are encountered, then + // |uses_dm| will be set to true. + static bool GetBlockDeviceForFile(const std::string& file_path, std::string* bdev_path, + bool* uses_dm = nullptr); + // The counter part of Write(). It is an error for the offset to be unaligned with // the block device's block size. // In case of error, the contents of buffer MUST be discarded.