From 8c7b2e7d5a50e96087f1f183315499725db5e0b7 Mon Sep 17 00:00:00 2001 From: "yuchen.cc" Date: Wed, 30 Aug 2023 18:19:53 +0800 Subject: [PATCH] Fix bugs in zfile evict and build jump table. Signed-off-by: yuchen.cc --- src/overlaybd/tar/tar_file.cpp | 6 ++++++ src/overlaybd/zfile/zfile.cpp | 19 +++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/overlaybd/tar/tar_file.cpp b/src/overlaybd/tar/tar_file.cpp index 4923705c..57552ead 100644 --- a/src/overlaybd/tar/tar_file.cpp +++ b/src/overlaybd/tar/tar_file.cpp @@ -158,6 +158,12 @@ class TarFile : public ForwardFile_Ownership { virtual ssize_t pwritev(const struct iovec *iov, int iovcnt, off_t offset) override { return m_file->pwritev(iov, iovcnt, offset + base_offset); } + virtual int fallocate(int mode, off_t offset, off_t len) override { + return m_file->fallocate(mode, offset + base_offset, len); + } + virtual int fadvise(off_t offset, off_t len, int advice) override { + return m_file->fadvise(offset + base_offset, len, advice); + } virtual int close() override { if (is_new_tar()) { diff --git a/src/overlaybd/zfile/zfile.cpp b/src/overlaybd/zfile/zfile.cpp index f8577442..381d6e43 100644 --- a/src/overlaybd/zfile/zfile.cpp +++ b/src/overlaybd/zfile/zfile.cpp @@ -182,6 +182,8 @@ class CompressionFile : public VirtualReadOnlyFile { } int build(const uint32_t *ibuf, size_t n, off_t offset_begin, uint32_t block_size) { + partial_offset.clear(); + deltas.clear(); group_size = (uinttype_max + 1) / block_size; partial_offset.reserve(n / group_size + 1); deltas.reserve(n + 1); @@ -196,8 +198,8 @@ class CompressionFile : public VirtualReadOnlyFile { continue; } if ((uint64_t)deltas[i - 1] + ibuf[i - 1] >= (uint64_t)uinttype_max) { - LOG_ERRNO_RETURN(ERANGE, -1, "build block[`] length failed `+` > ` (exceed)", - deltas[i-1], ibuf[i-1], (uint64_t)uinttype_max); + LOG_ERROR_RETURN(ERANGE, -1, "build block[`] length failed `+` > ` (exceed)", + i-1, deltas[i-1], ibuf[i-1], (uint64_t)uinttype_max); } deltas.push_back(deltas[i - 1] + ibuf[i - 1]); } @@ -255,14 +257,16 @@ class CompressionFile : public VirtualReadOnlyFile { int reload(size_t idx) { auto read_size = get_blocks_length(idx, idx + 1); auto begin_offset = m_zfile->m_jump_table[idx]; + LOG_WARN("trim and reload. (idx: `, offset: `, len: `)", idx, begin_offset, read_size); int trim_res = m_zfile->m_file->trim(begin_offset, read_size); if (trim_res < 0) { - LOG_ERROR_RETURN(0, -1, "failed to trim block idx: `", idx); + LOG_ERRNO_RETURN(0, -1, "trim block failed. (idx: `, offset: `, len: `)", + idx, begin_offset, read_size); } auto readn = m_zfile->m_file->pread(m_buf + m_buf_offset, read_size, begin_offset); if (readn != (ssize_t)read_size) { - LOG_ERRNO_RETURN(0, -1, "read compressed blocks failed. (offset: `, len: `)", - begin_offset, read_size); + LOG_ERRNO_RETURN(0, -1, "read compressed blocks failed. (idx: `, offset: `, len: `)", + idx, begin_offset, read_size); } return 0; } @@ -716,9 +720,12 @@ bool load_jump_table(IFile *file, CompressionFile::HeaderTrailer *pheader_traile if (ret < (ssize_t)index_bytes) { LOG_ERRNO_RETURN(0, false, "failed to read index"); } - jump_table.build(ibuf.get(), pht->index_size, + ret = jump_table.build(ibuf.get(), pht->index_size, CompressionFile::HeaderTrailer::SPACE + pht->opt.dict_size, pht->opt.block_size); + if (ret != 0) { + LOG_ERRNO_RETURN(0, false, "failed to build jump table"); + } if (pheader_trailer) *pheader_trailer = *pht; return true;