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

Commit e24e5bf0 authored by Akilesh Kailash's avatar Akilesh Kailash Committed by Automerger Merge Worker
Browse files

Merge "snapuserd: I/O request on sectors not mapping to any COW op" am: db3c0a4a

Original change: https://android-review.googlesource.com/c/platform/system/core/+/1824032

Change-Id: I96932acbe2d995908da4285484578f9b51fb97f1
parents 7dc38fe2 db3c0a4a
Loading
Loading
Loading
Loading
+74 −0
Original line number Diff line number Diff line
@@ -108,6 +108,7 @@ class CowSnapuserdTest final {
    void MergeInterruptFixed(int duration);
    void MergeInterruptRandomly(int max_duration);
    void ReadDmUserBlockWithoutDaemon();
    void ReadLastBlock();

    std::string snapshot_dev() const { return snapshot_dev_->path(); }

@@ -256,6 +257,73 @@ void CowSnapuserdTest::StartSnapuserdDaemon() {
    }
}

void CowSnapuserdTest::ReadLastBlock() {
    unique_fd rnd_fd;
    total_base_size_ = BLOCK_SZ * 2;

    base_fd_ = CreateTempFile("base_device", total_base_size_);
    ASSERT_GE(base_fd_, 0);

    rnd_fd.reset(open("/dev/random", O_RDONLY));
    ASSERT_TRUE(rnd_fd > 0);

    std::unique_ptr<uint8_t[]> random_buffer = std::make_unique<uint8_t[]>(BLOCK_SZ);

    for (size_t j = 0; j < ((total_base_size_) / BLOCK_SZ); j++) {
        ASSERT_EQ(ReadFullyAtOffset(rnd_fd, (char*)random_buffer.get(), BLOCK_SZ, 0), true);
        ASSERT_EQ(android::base::WriteFully(base_fd_, random_buffer.get(), BLOCK_SZ), true);
    }

    ASSERT_EQ(lseek(base_fd_, 0, SEEK_SET), 0);

    base_loop_ = std::make_unique<LoopDevice>(base_fd_, 10s);
    ASSERT_TRUE(base_loop_->valid());

    std::string path = android::base::GetExecutableDirectory();
    cow_system_ = std::make_unique<TemporaryFile>(path);

    std::unique_ptr<uint8_t[]> random_buffer_1_ = std::make_unique<uint8_t[]>(total_base_size_);
    loff_t offset = 0;

    // Fill random data
    for (size_t j = 0; j < (total_base_size_ / BLOCK_SZ); j++) {
        ASSERT_EQ(ReadFullyAtOffset(rnd_fd, (char*)random_buffer_1_.get() + offset, BLOCK_SZ, 0),
                  true);

        offset += BLOCK_SZ;
    }

    CowOptions options;
    options.compression = "gz";
    CowWriter writer(options);

    ASSERT_TRUE(writer.Initialize(cow_system_->fd));

    ASSERT_TRUE(writer.AddRawBlocks(0, random_buffer_1_.get(), BLOCK_SZ));
    ASSERT_TRUE(writer.AddRawBlocks(1, (char*)random_buffer_1_.get() + BLOCK_SZ, BLOCK_SZ));

    ASSERT_TRUE(writer.Finalize());

    SetDeviceControlName();

    StartSnapuserdDaemon();
    InitCowDevice();

    CreateDmUserDevice();
    InitDaemon();

    CreateSnapshotDevice();

    unique_fd snapshot_fd(open(snapshot_dev_->path().c_str(), O_RDONLY));
    ASSERT_TRUE(snapshot_fd > 0);

    std::unique_ptr<uint8_t[]> snapuserd_buffer = std::make_unique<uint8_t[]>(BLOCK_SZ);

    offset = 7680;
    ASSERT_EQ(ReadFullyAtOffset(snapshot_fd, snapuserd_buffer.get(), 512, offset), true);
    ASSERT_EQ(memcmp(snapuserd_buffer.get(), (char*)random_buffer_1_.get() + offset, 512), 0);
}

void CowSnapuserdTest::CreateBaseDevice() {
    unique_fd rnd_fd;

@@ -1143,6 +1211,12 @@ TEST(Snapuserd_Test, Snapshot_IO_TEST) {
    harness.Shutdown();
}

TEST(Snapuserd_Test, Snapshot_END_IO_TEST) {
    CowSnapuserdTest harness;
    harness.ReadLastBlock();
    harness.Shutdown();
}

TEST(Snapuserd_Test, Snapshot_COPY_Overlap_TEST_1) {
    CowSnapuserdTest harness;
    ASSERT_TRUE(harness.SetupCopyOverlap_1());
+24 −4
Original line number Diff line number Diff line
@@ -350,16 +350,36 @@ int WorkerThread::ReadData(sector_t sector, size_t size) {
    it = std::lower_bound(chunk_vec.begin(), chunk_vec.end(), std::make_pair(sector, nullptr),
                          Snapuserd::compare);

    if (!(it != chunk_vec.end())) {
        SNAP_LOG(ERROR) << "ReadData: Sector " << sector << " not found in chunk_vec";
        return -1;
    bool read_end_of_device = false;
    if (it == chunk_vec.end()) {
        // |-------|-------|-------|
        // 0       1       2       3
        //
        // Block 0 - op 1
        // Block 1 - op 2
        // Block 2 - op 3
        //
        // chunk_vec will have block 0, 1, 2 which maps to relavant COW ops.
        //
        // Each block is 4k bytes. Thus, the last block will span 8 sectors
        // ranging till block 3 (However, block 3 won't be in chunk_vec as
        // it doesn't have any mapping to COW ops. Now, if we get an I/O request for a sector
        // spanning between block 2 and block 3, we need to step back
        // and get hold of the last element.
        //
        // Additionally, dm-snapshot makes sure that I/O request beyond block 3
        // will not be routed to the daemon. Hence, it is safe to assume that
        // if a sector is not available in the chunk_vec, the I/O falls in the
        // end of region.
        it = std::prev(chunk_vec.end());
        read_end_of_device = true;
    }

    // We didn't find the required sector; hence find the previous sector
    // as lower_bound will gives us the value greater than
    // the requested sector
    if (it->first != sector) {
        if (it != chunk_vec.begin()) {
        if (it != chunk_vec.begin() && !read_end_of_device) {
            --it;
        }