Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ea0dda14 authored by David Anderson's avatar David Anderson
Browse files

libdm: Add LoopControl helpers for enabling direct IO.

Bug: 134536978
Test: manual test
Change-Id: Iae25434ac54186fd6006b56eeb7a7f577a880053
parent d106f1e2
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -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;
+21 −0
Original line number Diff line number Diff line
@@ -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();
}