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

Commit f42f9721 authored by Nikita Ioffe's avatar Nikita Ioffe Committed by Android (Google) Code Review
Browse files

Merge "PrepareZramBackingDevice: use loop_control.h" into rvc-dev

parents 2f8bbb97 c8313adf
Loading
Loading
Loading
Loading
+12 −27
Original line number Original line Diff line number Diff line
@@ -62,6 +62,7 @@
#include <fs_mgr_overlayfs.h>
#include <fs_mgr_overlayfs.h>
#include <fscrypt/fscrypt.h>
#include <fscrypt/fscrypt.h>
#include <libdm/dm.h>
#include <libdm/dm.h>
#include <libdm/loop_control.h>
#include <liblp/metadata_format.h>
#include <liblp/metadata_format.h>
#include <linux/fs.h>
#include <linux/fs.h>
#include <linux/loop.h>
#include <linux/loop.h>
@@ -105,6 +106,7 @@ using android::base::unique_fd;
using android::dm::DeviceMapper;
using android::dm::DeviceMapper;
using android::dm::DmDeviceState;
using android::dm::DmDeviceState;
using android::dm::DmTargetLinear;
using android::dm::DmTargetLinear;
using android::dm::LoopControl;


// Realistically, this file should be part of the android::fs_mgr namespace;
// Realistically, this file should be part of the android::fs_mgr namespace;
using namespace android::fs_mgr;
using namespace android::fs_mgr;
@@ -1927,19 +1929,6 @@ static bool PrepareZramBackingDevice(off64_t size) {
    constexpr const char* file_path = "/data/per_boot/zram_swap";
    constexpr const char* file_path = "/data/per_boot/zram_swap";
    if (size == 0) return true;
    if (size == 0) return true;


    // Get free loopback
    unique_fd loop_fd(TEMP_FAILURE_RETRY(open("/dev/loop-control", O_RDWR | O_CLOEXEC)));
    if (loop_fd.get() == -1) {
        PERROR << "Cannot open loop-control";
        return false;
    }

    int num = ioctl(loop_fd.get(), LOOP_CTL_GET_FREE);
    if (num == -1) {
        PERROR << "Cannot get free loop slot";
        return false;
    }

    // Prepare target path
    // Prepare target path
    unique_fd target_fd(TEMP_FAILURE_RETRY(open(file_path, O_RDWR | O_CREAT | O_CLOEXEC, 0600)));
    unique_fd target_fd(TEMP_FAILURE_RETRY(open(file_path, O_RDWR | O_CREAT | O_CLOEXEC, 0600)));
    if (target_fd.get() == -1) {
    if (target_fd.get() == -1) {
@@ -1951,25 +1940,21 @@ static bool PrepareZramBackingDevice(off64_t size) {
        return false;
        return false;
    }
    }


    // Connect loopback (device_fd) to target path (target_fd)
    // Allocate loop device and attach it to file_path.
    std::string device = android::base::StringPrintf("/dev/block/loop%d", num);
    LoopControl loop_control;
    unique_fd device_fd(TEMP_FAILURE_RETRY(open(device.c_str(), O_RDWR | O_CLOEXEC)));
    std::string device;
    if (device_fd.get() == -1) {
    if (!loop_control.Attach(target_fd.get(), 5s, &device)) {
        PERROR << "Cannot open /dev/block/loop" << num;
        return false;
    }

    if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get())) {
        PERROR << "Cannot set loopback to target path";
        return false;
        return false;
    }
    }


    // set block size & direct IO
    // set block size & direct IO
    if (ioctl(device_fd.get(), LOOP_SET_BLOCK_SIZE, 4096)) {
    unique_fd device_fd(TEMP_FAILURE_RETRY(open(device.c_str(), O_RDWR | O_CLOEXEC)));
        PWARNING << "Cannot set 4KB blocksize to /dev/block/loop" << num;
    if (device_fd.get() == -1) {
        PERROR << "Cannot open " << device;
        return false;
    }
    }
    if (ioctl(device_fd.get(), LOOP_SET_DIRECT_IO, 1)) {
    if (!LoopControl::EnableDirectIo(device_fd.get())) {
        PWARNING << "Cannot set direct_io to /dev/block/loop" << num;
        return false;
    }
    }


    return InstallZramDevice(device);
    return InstallZramDevice(device);