libdm: Add LoopControl helpers for enabling direct IO.

Bug: 134536978
Test: manual test
Change-Id: Iae25434ac54186fd6006b56eeb7a7f577a880053
This commit is contained in:
David Anderson 2019-06-12 19:13:21 -07:00
parent d106f1e225
commit ea0dda1473
2 changed files with 24 additions and 0 deletions

View file

@ -35,6 +35,9 @@ class LoopControl final {
// Detach the loop device given by 'loopdev' from the attached backing file.
bool Detach(const std::string& loopdev) const;
// Enable Direct I/O on a loop device. This requires kernel 4.9+.
static bool EnableDirectIo(int fd);
LoopControl(const LoopControl&) = delete;
LoopControl& operator=(const LoopControl&) = delete;
LoopControl& operator=(LoopControl&&) = default;

View file

@ -91,6 +91,27 @@ bool LoopControl::FindFreeLoopDevice(std::string* loopdev) const {
return true;
}
bool LoopControl::EnableDirectIo(int fd) {
#if !defined(LOOP_SET_BLOCK_SIZE)
static constexpr int LOOP_SET_BLOCK_SIZE = 0x4C09;
#endif
#if !defined(LOOP_SET_DIRECT_IO)
static constexpr int LOOP_SET_DIRECT_IO = 0x4C08;
#endif
// Note: the block size has to be >= the logical block size of the underlying
// block device, *not* the filesystem block size.
if (ioctl(fd, LOOP_SET_BLOCK_SIZE, 4096)) {
PLOG(ERROR) << "Could not set loop device block size";
return false;
}
if (ioctl(fd, LOOP_SET_DIRECT_IO, 1)) {
PLOG(ERROR) << "Could not set loop direct IO";
return false;
}
return true;
}
LoopDevice::LoopDevice(int fd, bool auto_close) : fd_(fd), owns_fd_(auto_close) {
Init();
}