snapuserd: Move GetNumSectors call to snapuserd_server.

Calling this in snapuserd_core fails when the base path is a regular
file. Since the value is only read once, just call it from
snapuserd_server instead, which also means we don't have to add an
S_ISBLK check here.

Bug: 288273605
Test: snapuserd_test
Change-Id: Ic26bf807b24611f2d97829d1b4eb1d0ede2feb6a
This commit is contained in:
David Anderson 2023-07-05 13:35:12 -07:00
parent 95f46b0758
commit 48d1c39da7
5 changed files with 26 additions and 18 deletions

View file

@ -40,6 +40,7 @@ static constexpr uint32_t SNAPSHOT_VALID = 1;
* multiple of 512 bytes. Hence these two constants.
*/
static constexpr uint32_t SECTOR_SHIFT = 9;
static constexpr uint64_t SECTOR_SIZE = (1ULL << SECTOR_SHIFT);
static constexpr size_t BLOCK_SZ = 4096;
static constexpr size_t BLOCK_SHIFT = (__builtin_ffs(BLOCK_SZ) - 1);

View file

@ -280,20 +280,6 @@ bool SnapshotHandler::InitCowDevice() {
return false;
}
unique_fd fd(TEMP_FAILURE_RETRY(open(base_path_merge_.c_str(), O_RDONLY | O_CLOEXEC)));
if (fd < 0) {
SNAP_LOG(ERROR) << "Cannot open block device";
return false;
}
uint64_t dev_sz = get_block_device_size(fd.get());
if (!dev_sz) {
SNAP_LOG(ERROR) << "Failed to find block device size: " << base_path_merge_;
return false;
}
num_sectors_ = dev_sz >> SECTOR_SHIFT;
return ReadMetadata();
}
@ -460,5 +446,21 @@ void SnapshotHandler::FreeResources() {
merge_thread_ = nullptr;
}
uint64_t SnapshotHandler::GetNumSectors() const {
unique_fd fd(TEMP_FAILURE_RETRY(open(base_path_merge_.c_str(), O_RDONLY | O_CLOEXEC)));
if (fd < 0) {
SNAP_LOG(ERROR) << "Cannot open base path: " << base_path_merge_;
return false;
}
uint64_t dev_sz = get_block_device_size(fd.get());
if (!dev_sz) {
SNAP_LOG(ERROR) << "Failed to find block device size: " << base_path_merge_;
return false;
}
return dev_sz / SECTOR_SIZE;
}
} // namespace snapshot
} // namespace android

View file

@ -109,7 +109,7 @@ class SnapshotHandler : public std::enable_shared_from_this<SnapshotHandler> {
const std::string& GetControlDevicePath() { return control_device_; }
const std::string& GetMiscName() { return misc_name_; }
const uint64_t& GetNumSectors() { return num_sectors_; }
uint64_t GetNumSectors() const;
const bool& IsAttached() const { return attached_; }
void AttachControlDevice() { attached_ = true; }
@ -202,8 +202,6 @@ class SnapshotHandler : public std::enable_shared_from_this<SnapshotHandler> {
unique_fd cow_fd_;
uint64_t num_sectors_;
std::unique_ptr<CowReader> reader_;
// chunk_vec stores the pseudo mapping of sector

View file

@ -130,7 +130,12 @@ bool UserSnapshotServer::Receivemsg(android::base::borrowed_fd fd, const std::st
return Sendmsg(fd, "fail");
}
auto retval = "success," + std::to_string(handler->snapuserd()->GetNumSectors());
auto num_sectors = handler->snapuserd()->GetNumSectors();
if (!num_sectors) {
return Sendmsg(fd, "fail");
}
auto retval = "success," + std::to_string(num_sectors);
return Sendmsg(fd, retval);
} else if (cmd == "start") {
// Message format:

View file

@ -540,7 +540,9 @@ void SnapuserdTest::InitCowDevice() {
base_loop_->device(), 1, use_iouring, false);
ASSERT_NE(handler, nullptr);
ASSERT_NE(handler->snapuserd(), nullptr);
#ifdef __ANDROID__
ASSERT_NE(handler->snapuserd()->GetNumSectors(), 0);
#endif
}
void SnapuserdTest::SetDeviceControlName() {