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

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

snapuserd: Split more methods out of Worker.

This moves ReadWorker-specific methods out of Worker, and moves
remaining Worker methods into a separate worker.cpp file.

Bug: 288273605
Test: snapuserd_test
Change-Id: I59c31318e127db61a5f3a673956865dac97a6e5f
parent d9675914
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ cc_library_static {
        "user-space-merge/snapuserd_readahead.cpp",
        "user-space-merge/snapuserd_transitions.cpp",
        "user-space-merge/snapuserd_verify.cpp",
        "user-space-merge/worker.cpp",
    ],
    static_libs: [
        "libbase",
+2 −74
Original line number Diff line number Diff line
@@ -25,61 +25,12 @@ using namespace android;
using namespace android::dm;
using android::base::unique_fd;

Worker::Worker(const std::string& cow_device, const std::string& backing_device,
               const std::string& control_device, const std::string& misc_name,
               const std::string& base_path_merge, std::shared_ptr<SnapshotHandler> snapuserd) {
    cow_device_ = cow_device;
    backing_store_device_ = backing_device;
    control_device_ = control_device;
    misc_name_ = misc_name;
    base_path_merge_ = base_path_merge;
    snapuserd_ = snapuserd;
}

ReadWorker::ReadWorker(const std::string& cow_device, const std::string& backing_device,
                       const std::string& control_device, const std::string& misc_name,
                       const std::string& base_path_merge,
                       std::shared_ptr<SnapshotHandler> snapuserd)
    : Worker(cow_device, backing_device, control_device, misc_name, base_path_merge, snapuserd) {}

bool Worker::InitializeFds() {
    backing_store_fd_.reset(open(backing_store_device_.c_str(), O_RDONLY));
    if (backing_store_fd_ < 0) {
        SNAP_PLOG(ERROR) << "Open Failed: " << backing_store_device_;
        return false;
    }

    cow_fd_.reset(open(cow_device_.c_str(), O_RDWR));
    if (cow_fd_ < 0) {
        SNAP_PLOG(ERROR) << "Open Failed: " << cow_device_;
        return false;
    }

    ctrl_fd_.reset(open(control_device_.c_str(), O_RDWR));
    if (ctrl_fd_ < 0) {
        SNAP_PLOG(ERROR) << "Unable to open " << control_device_;
        return false;
    }

    // Base device used by merge thread
    base_path_merge_fd_.reset(open(base_path_merge_.c_str(), O_RDWR));
    if (base_path_merge_fd_ < 0) {
        SNAP_PLOG(ERROR) << "Open Failed: " << base_path_merge_;
        return false;
    }

    return true;
}

bool Worker::InitReader() {
    reader_ = snapuserd_->CloneReaderForWorker();

    if (!reader_->InitForMerge(std::move(cow_fd_))) {
        return false;
    }
    return true;
}

// Start the replace operation. This will read the
// internal COW format and if the block is compressed,
// it will be de-compressed.
@@ -96,7 +47,7 @@ bool Worker::ProcessReplaceOp(const CowOperation* cow_op) {
    return true;
}

bool Worker::ReadFromSourceDevice(const CowOperation* cow_op) {
bool ReadWorker::ReadFromSourceDevice(const CowOperation* cow_op) {
    void* buffer = bufsink_.GetPayloadBuffer(BLOCK_SZ);
    if (buffer == nullptr) {
        SNAP_LOG(ERROR) << "ReadFromBaseDevice: Failed to get payload buffer";
@@ -254,29 +205,6 @@ bool ReadWorker::ProcessCowOp(const CowOperation* cow_op) {
    return false;
}

void Worker::InitializeBufsink() {
    // Allocate the buffer which is used to communicate between
    // daemon and dm-user. The buffer comprises of header and a fixed payload.
    // If the dm-user requests a big IO, the IO will be broken into chunks
    // of PAYLOAD_BUFFER_SZ.
    size_t buf_size = sizeof(struct dm_user_header) + PAYLOAD_BUFFER_SZ;
    bufsink_.Initialize(buf_size);
}

bool Worker::Init() {
    InitializeBufsink();

    if (!InitializeFds()) {
        return false;
    }

    if (!InitReader()) {
        return false;
    }

    return true;
}

bool ReadWorker::Init() {
    if (!Worker::Init()) {
        return false;
@@ -325,7 +253,7 @@ bool ReadWorker::WriteDmUserPayload(size_t size) {
    return true;
}

bool Worker::ReadDataFromBaseDevice(sector_t sector, size_t read_size) {
bool ReadWorker::ReadDataFromBaseDevice(sector_t sector, size_t read_size) {
    CHECK(read_size <= BLOCK_SZ);

    void* buffer = bufsink_.GetPayloadBuffer(BLOCK_SZ);
+6 −1
Original line number Diff line number Diff line
@@ -14,7 +14,10 @@

#pragma once

#include "snapuserd_core.h"
#include <utility>
#include <vector>

#include "worker.h"

namespace android {
namespace snapshot {
@@ -44,6 +47,8 @@ class ReadWorker : public Worker {
    bool ReadUnalignedSector(sector_t sector, size_t size);
    int ReadUnalignedSector(sector_t sector, size_t size,
                            std::vector<std::pair<sector_t, const CowOperation*>>::iterator& it);
    bool ReadFromSourceDevice(const CowOperation* cow_op);
    bool ReadDataFromBaseDevice(sector_t sector, size_t read_size);

    XorSink xorsink_;
    bool header_response_ = false;
+0 −53
Original line number Diff line number Diff line
@@ -77,7 +77,6 @@ enum class MERGE_IO_TRANSITION {

class MergeWorker;
class ReadWorker;
class SnapshotHandler;

enum class MERGE_GROUP_STATE {
    GROUP_MERGE_PENDING,
@@ -100,58 +99,6 @@ struct MergeGroupState {
        : merge_state_(state), num_ios_in_progress(n_ios) {}
};

class Worker {
  public:
    Worker(const std::string& cow_device, const std::string& backing_device,
           const std::string& control_device, const std::string& misc_name,
           const std::string& base_path_merge, std::shared_ptr<SnapshotHandler> snapuserd);
    virtual ~Worker() = default;

    virtual bool Init();

  protected:
    // Initialization
    void InitializeBufsink();
    bool InitializeFds();
    bool InitReader();
    void CloseFds() {
        ctrl_fd_ = {};
        backing_store_fd_ = {};
        base_path_merge_fd_ = {};
    }

    // IO Path
    bool IsBlockAligned(size_t size) { return ((size & (BLOCK_SZ - 1)) == 0); }

    bool ReadDataFromBaseDevice(sector_t sector, size_t read_size);
    bool ReadFromSourceDevice(const CowOperation* cow_op);

    // Processing COW operations
    bool ProcessReplaceOp(const CowOperation* cow_op);
    bool ProcessZeroOp();

    sector_t ChunkToSector(chunk_t chunk) { return chunk << CHUNK_SHIFT; }
    chunk_t SectorToChunk(sector_t sector) { return sector >> CHUNK_SHIFT; }

    std::unique_ptr<CowReader> reader_;
    BufferSink bufsink_;

    std::string cow_device_;
    std::string backing_store_device_;
    std::string control_device_;
    std::string misc_name_;
    std::string base_path_merge_;

    unique_fd cow_fd_;
    unique_fd backing_store_fd_;
    unique_fd base_path_merge_fd_;
    unique_fd ctrl_fd_;

    std::unique_ptr<ICowOpIter> cowop_iter_;

    std::shared_ptr<SnapshotHandler> snapuserd_;
};

class SnapshotHandler : public std::enable_shared_from_this<SnapshotHandler> {
  public:
    SnapshotHandler(std::string misc_name, std::string cow_device, std::string backing_device,
+3 −1
Original line number Diff line number Diff line
@@ -13,7 +13,9 @@
// limitations under the License.
#pragma once

#include "snapuserd_core.h"
#include "worker.h"

#include <liburing.h>

namespace android {
namespace snapshot {
Loading