From 6da0c725b6a94a202a41333ac138c087dfa2defd Mon Sep 17 00:00:00 2001 From: "huangzhaoxiang.hzx" Date: Thu, 19 Nov 2026 23:59:03 +0800 Subject: [PATCH] lsmt: parallelize appends, serialize resize and restack against in-flight writes Signed-off-by: huangzhaoxiang.hzx --- docs/live-snapshot.md | 2 +- docs/live-snapshot_zh.md | 2 +- src/CMakeLists.txt | 12 - src/image_file.cpp | 64 +---- src/overlaybd/lsmt/CMakeLists.txt | 13 + src/overlaybd/lsmt/file.cpp | 442 +++++++++++++++++++++++------- src/overlaybd/lsmt/file.h | 12 +- src/overlaybd/lsmt/test/test.cpp | 186 ++++++++++++- src/tools/overlaybd-create.cpp | 4 + src/ublk/ublk_device.cpp | 3 +- 10 files changed, 559 insertions(+), 181 deletions(-) diff --git a/docs/live-snapshot.md b/docs/live-snapshot.md index 4aa43dac..2289057f 100644 --- a/docs/live-snapshot.md +++ b/docs/live-snapshot.md @@ -78,5 +78,5 @@ A failed seal needs no rollback: it only appends, leaving an unreachable index a ## Limitations - Snapshots are not supported with a warpfile upper layer (`upper.target`). -- With a hybrid or sparse upper, writes do not take the snapshot's write lock, so the caller must pause writes while a snapshot runs. +- Write paths and snapshots are mutually exclusive through a read-write lock; writes block until the snapshot finishes, so the caller does not need to pause writes. - Snapshots on one device are serialized; a running snapshot holds the device's write path. diff --git a/docs/live-snapshot_zh.md b/docs/live-snapshot_zh.md index f39340d1..4f99ecf0 100644 --- a/docs/live-snapshot_zh.md +++ b/docs/live-snapshot_zh.md @@ -76,5 +76,5 @@ seal 失败处理:seal 只追加写,data 尾部仅留下不可达的 index ## 限制 - upper 为 warpfile(`upper.target`)时,不支持快照。 -- upper 为 hybrid 或 sparse 时,写路径不持快照使用的写锁,快照期间需调用方暂停写入。 +- 写路径和快照通过读写锁互斥,快照期间写请求会阻塞等待快照完成,调用方无需暂停写入。 - 同一设备的快照串行执行;快照期间设备的写路径被占用。 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 552a3481..c37f024b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -33,18 +33,6 @@ target_link_libraries(overlaybd_image_lib ${AIO_LIBRARIES} ) -# Propagate resize .o files to all consumers of overlaybd_image_lib. -# These provide the resize_fs() symbol needed by photon's resize_extfs(). -if (NOT ORIGIN_EXT2FS) - target_link_libraries(overlaybd_image_lib - ${E2FS_RESIZE_DIR}/resize2fs.o - ${E2FS_RESIZE_DIR}/extent.o - ${E2FS_RESIZE_DIR}/resource_track.o - ${E2FS_LIBRARIES} - ) - add_dependencies(overlaybd_image_lib libext2fs_build) -endif() - add_executable(overlaybd-tcmu main.cpp ) diff --git a/src/image_file.cpp b/src/image_file.cpp index 1bbb1fe3..890fb9da 100644 --- a/src/image_file.cpp +++ b/src/image_file.cpp @@ -42,9 +42,6 @@ #include "overlaybd/gzip/gz.h" #include "overlaybd/gzindex/gzfile.h" #include "overlaybd/tar/tar_file.h" -#ifdef PHOTON_ENABLE_RESIZE -#include -#endif #define PARALLEL_LOAD_INDEX 32 using namespace photon::fs; @@ -1051,65 +1048,6 @@ int ImageFile::resize(uint64_t target_size, bool resize_fs) { if (read_only) { LOG_ERROR_RETURN(EROFS, -1, "cannot resize a read-only image (no upper layer)"); } - // a size that is not a multiple of the device block would leave a - // truncated tail block - uint32_t align = LSMT::ALIGNMENT4K; - if (target_size % align != 0) { - LOG_ERROR_RETURN(EINVAL, -1, "target size must be ` aligned, got `", align, target_size); - } - auto rw = (LSMT::IFileRW *)m_file; - struct stat st; - if (m_file->fstat(&st) != 0) { - LOG_ERRNO_RETURN(0, -1, "fstat failed"); - } - uint64_t current = (uint64_t)st.st_size; - - if (target_size == current && !resize_fs) { - LOG_INFO("vsize already equals target (` bytes), nothing to do", target_size); - return 0; - } - - if (target_size > current) { - // Expand: grow block device first, then filesystem - if (rw->update_vsize(target_size) != 0) { - LOG_ERROR_RETURN(0, -1, "failed to update vsize for expand"); - } - } - - if (resize_fs) { -#ifdef PHOTON_ENABLE_RESIZE - auto aligned = new_aligned_file_adaptor(m_file, LSMT::ALIGNMENT4K, true, false); - if (aligned == nullptr) { - LOG_ERROR_RETURN(0, -1, "failed to create 4K aligned adaptor for resize"); - } - DEFER(delete aligned); - if (photon::fs::resize_extfs(aligned, target_size, 0) != 0) { - if (target_size >= current) { - LOG_ERROR("resize_extfs failed. " - "Re-run to retry or use e2fsck to recover."); - } else { - LOG_ERROR("resize_extfs failed during shrink."); - } - return -1; - } -#else - LOG_ERROR_RETURN(ENOSYS, -1, "resize_fs not supported (built without PHOTON_ENABLE_RESIZE)"); -#endif - } - - if (target_size < current) { - // Shrink: shrinking without resizing the filesystem is unsafe. - if (!resize_fs) { - LOG_ERROR_RETURN(EINVAL, -1, "cannot shrink without resizing filesystem"); - } - // Shrink: update vsize after filesystem is shrunk - if (rw->update_vsize(target_size) != 0) { - LOG_ERROR("filesystem resized but failed to update vsize. Re-run to retry."); - return -1; - } - } - - LOG_INFO("resize done: ` -> ` bytes", current, target_size); - return 0; + return rw->resize(target_size, resize_fs); } diff --git a/src/overlaybd/lsmt/CMakeLists.txt b/src/overlaybd/lsmt/CMakeLists.txt index 51a3982c..ee188c6e 100644 --- a/src/overlaybd/lsmt/CMakeLists.txt +++ b/src/overlaybd/lsmt/CMakeLists.txt @@ -5,6 +5,19 @@ target_include_directories(lsmt_lib PUBLIC ${PHOTON_INCLUDE_DIR} ) +# file.cpp calls photon::fs::resize_extfs(), which needs resize_fs() from the +# e2fsprogs resize objects. Pass them as link items to every lsmt_lib consumer: +# a link item is always included, while a member of this static library would +# only be pulled in when already referenced. +if (NOT ORIGIN_EXT2FS) + target_link_libraries(lsmt_lib INTERFACE + ${E2FS_RESIZE_DIR}/resize2fs.o + ${E2FS_RESIZE_DIR}/extent.o + ${E2FS_RESIZE_DIR}/resource_track.o + ${E2FS_LIBRARIES}) + add_dependencies(lsmt_lib libext2fs_build) +endif() + if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)$") option(OVERLAYBD_DISABLE_SVE "Force-disable LSMT SVE acceleration" OFF) include(CheckCXXSourceCompiles) diff --git a/src/overlaybd/lsmt/file.cpp b/src/overlaybd/lsmt/file.cpp index d937b6ee..ef7924f7 100644 --- a/src/overlaybd/lsmt/file.cpp +++ b/src/overlaybd/lsmt/file.cpp @@ -33,10 +33,15 @@ limitations under the License. #include "photon/common/uuid.h" #include "photon/fs/filesystem.h" #include "photon/fs/fiemap.h" +#include "photon/fs/forwardfs.h" #include "photon/fs/virtual-file.h" #include #include #include +#include +#ifdef PHOTON_ENABLE_RESIZE +#include +#endif #define PARALLEL_LOAD_INDEX 32 @@ -215,7 +220,6 @@ static LSMTReadOnlyFile *open_file_ro(IFile *file, bool ownership, bool reserve_ static HeaderTrailer *verify_ht(IFile *file, char *buf, bool is_trailer = false, ssize_t st_size = -1); - static const int ABORT_FLAG_DETECTED = -2; static const char *rw_type_name(RWType type) { @@ -718,7 +722,6 @@ class LSMTReadOnlyFile : public IFileRW { return file->filesystem(); } - UNIMPLEMENTED(int update_vsize(size_t vsize) override); UNIMPLEMENTED(int close_seal(IFileRO **reopen_as = nullptr) override); // It can commit a RO file after close_seal() @@ -752,7 +755,10 @@ class LSMTReadOnlyFile : public IFileRW { } virtual ssize_t seek_data(off_t begin, off_t end, vector &segs) override { - + if (m_index == nullptr) { + errno = EBADF; + return -1; + } begin /= ALIGNMENT; end /= ALIGNMENT; photon::scoped_rwlock ilock(m_index_rwlock, photon::RLOCK); @@ -898,22 +904,26 @@ class IndexLog { class LSMTFile : public LSMTReadOnlyFile { public: - typedef photon::mutex Mutex; - typedef photon::scoped_lock Lock; - atomic_uint64_t m_compacted_idx_size; // count of compacted raw-index bool m_init_concurrency = false; bool m_data_file_direct_io = false; - // append position (in bytes) of the data file of a hybrid layer + bool m_append_only = false; + // append position (in bytes) of the data file of a hybrid layer or an append layer atomic m_data_offset{HeaderTrailer::SPACE}; uint8_t m_rw_tag = 0; RWType m_rw_type = RWType::Append; - // lock order: m_rw_mtx, the IndexLog mutex, m_index_rwlock. close_seal() and - // restack() expect the caller to have stopped all I/O on this file. - Mutex m_rw_mtx; + // Protects the structure and metadata of the stacked LSMTFile: m_files, + // m_rw_tag, m_rw_type, m_log, m_vsize, m_data_offset, m_data_file_direct_io. + // Operations that change these states (restack, resize, close_seal) take the + // write lock. Operations that only modify the data file or index without + // changing the LSMT structure or metadata (pwrite, discard, zero_range) + // take the read lock and run concurrently. + // Lock order: m_lsmt_rwlock -> IndexLog::m_mtx -> m_index_rwlock. + mutable photon::rwlock m_lsmt_rwlock; + photon::mutex m_append_mtx; IndexLog *m_log = nullptr; LSMTFile() { @@ -933,10 +943,11 @@ class LSMTFile : public LSMTReadOnlyFile { ~LSMTFile() { LOG_DEBUG(" ~LSMTFile()"); - if (close() != 0) + if (do_close() != 0) safe_delete(m_log); } + // fstat() calls this while holding m_lsmt_rwlock; do not take the lock here virtual IFile *front_file() override { if (m_files[m_rw_tag]) { return m_files[m_rw_tag]; @@ -945,6 +956,16 @@ class LSMTFile : public LSMTReadOnlyFile { } virtual int fstat(struct stat *buf) override { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } + return do_fstat(buf); + } + + // lock-free, the caller should hold m_lsmt_rwlock by itself + int do_fstat(struct stat *buf) { return LSMTReadOnlyFile::fstat(buf); } @@ -985,6 +1006,12 @@ class LSMTFile : public LSMTReadOnlyFile { if (request == GetType) return LSMTReadOnlyFile::vioctl(request, args); if (request == Set_Data_File_Direct_IO) { + // take the write lock so this update cannot race an in-flight write + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::WLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } m_data_file_direct_io = va_arg(args, int) != 0; if (m_data_file_direct_io) { auto offset = m_data_offset.load(); @@ -992,6 +1019,17 @@ class LSMTFile : public LSMTReadOnlyFile { } return 0; } + if (request == Set_Append_Only) { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::WLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } + if (m_rw_type != RWType::Append) + LOG_ERROR_RETURN(EINVAL, -1, "append only is only supported for append layers"); + m_append_only = va_arg(args, int) != 0; + return 0; + } if (request != Index_Group_Commit) LOG_ERROR_RETURN(EINVAL, -1, "invaid request code"); @@ -1008,27 +1046,26 @@ class LSMTFile : public LSMTReadOnlyFile { } virtual int close() override { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::WLOCK); + return do_close(); + } + + // the caller holds m_lsmt_rwlock; close_seal() already owns it + virtual int do_close() { + if (m_index == nullptr && m_log == nullptr) + return 0; LOG_DEBUG("ownership:`, m_log:`", m_file_ownership, m_log); if (flush_index_log() != 0) return -1; safe_delete(m_log); return LSMTReadOnlyFile::close(); } - // returns appended offset when success, 0 otherwise - static off_t append(IFile *file, const void *buf, size_t count) { - off_t pos = file->lseek(0, SEEK_END); - if (pos < 0) - LOG_ERRNO_RETURN(0, 0, "seek to end failed, file:`", file); - // Positioned writes keep alignment adaptors in the I/O path. - ssize_t ret = file->pwrite(buf, count, pos); - if (ret < (ssize_t)count) { - LOG_ERRNO_RETURN(0, 0, "write failed, file:`, ret:`, pos:`, count:`", file, ret, pos, - count); - } - return pos; - } - virtual ssize_t pread(void *buf, size_t count, off_t offset) override { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } return LSMTReadOnlyFile::pread(buf, count, offset); } @@ -1036,21 +1073,100 @@ class LSMTFile : public LSMTReadOnlyFile { return commit_mapping(m, [&] { photon::scoped_rwlock ilock(m_index_rwlock, photon::WLOCK); rw_index()->insert(m); - m_vsize = max(m_vsize, (uint64_t)m.end() * ALIGNMENT); }); } + // the caller holds m_lsmt_rwlock; returns 0 on success + int pad_for_direct_io(const void *buf, size_t count, const char *&src, size_t &padded, + unique_ptr &tail) { + src = (const char *)buf; + // Direct extents use whole 4K blocks so concurrent appends never enter the + // alignment adaptor's read-modify-write and stale-size truncate path. + padded = m_data_file_direct_io + ? (size_t)((count + ALIGNMENT4K - 1) / ALIGNMENT4K * ALIGNMENT4K) + : count; + if (padded != count) { + tail.reset((char *)aligned_alloc(ALIGNMENT4K, padded)); + if (!tail) + LOG_ERRNO_RETURN(ENOMEM, -1, "failed to allocate ` bytes", padded); + memcpy(tail.get(), src, count); + memset(tail.get() + count, 0, padded - count); + src = tail.get(); + } + return 0; + } + + // the caller holds m_lsmt_rwlock; returns 0 on success + int parallelized_append(const void *buf, size_t count, uint64_t offset_in_blocks, + uint8_t tag) { + SegmentMapping m(offset_in_blocks, (uint32_t)(count / ALIGNMENT), 0); + const char *src = nullptr; + size_t padded = 0; + unique_ptr tail(nullptr, &free); + if (pad_for_direct_io(buf, count, src, padded, tail) != 0) + return -1; + auto pos = m_data_offset.fetch_add(padded); + auto written = m_files[m_rw_tag]->pwrite(src, padded, pos); + if (written != (ssize_t)padded) + LOG_ERRNO_RETURN(0, -1, "append data failed, file:`, ret:`, pos:`, count:`", + m_files[m_rw_tag], written, pos, padded); + m.moffset = pos / ALIGNMENT; + m.tag = tag; + return commit_mapping(m, [&] { index_insert(m); }); + } + + // the caller holds m_lsmt_rwlock; serialized by m_append_mtx + int serialized_append(const void *buf, size_t count, uint64_t offset_in_blocks, + uint8_t tag) { + SegmentMapping m(offset_in_blocks, (uint32_t)(count / ALIGNMENT), 0); + const char *src = nullptr; + size_t padded = 0; + unique_ptr tail(nullptr, &free); + if (pad_for_direct_io(buf, count, src, padded, tail) != 0) + return -1; + photon::scoped_lock lock(m_append_mtx); + auto file = m_files[m_rw_tag]; + auto pos = file->lseek(0, SEEK_END); + if (pos < 0) + LOG_ERRNO_RETURN(0, -1, "seek to end failed, file:`", file); + if (m_data_file_direct_io && (pos % ALIGNMENT4K) != 0) { + auto aligned = (pos + ALIGNMENT4K - 1) / ALIGNMENT4K * ALIGNMENT4K; + if (file->ftruncate(aligned) != 0) + LOG_ERRNO_RETURN(0, -1, "failed to align EOF to 4K, file:`, pos:`", file, pos); + pos = aligned; + } + auto written = file->pwrite(src, padded, pos); + if (written != (ssize_t)padded) + LOG_ERRNO_RETURN(0, -1, "append data failed, file:`, ret:`, pos:`, count:`", + file, written, pos, padded); + m.moffset = pos / ALIGNMENT; + m.tag = tag; + m_data_offset.store(pos + padded); + return commit_mapping(m, [&] { index_insert(m); }); + } + virtual ssize_t pwritev(const struct iovec *iov, int iovcnt, off_t offset) override { return VirtualFile::pwritev(iov, iovcnt, offset); } virtual ssize_t pwrite(const void *buf, size_t count, off_t offset) override { + // restack() and resize() take the write side, so every writer waits here + // while the layer stack, the layer type or m_vsize changes + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } return do_pwrite(buf, count, offset); } - ssize_t do_pwrite(const void *buf, size_t count, off_t offset) { + // lock-free, the caller should hold m_lsmt_rwlock by itself + virtual ssize_t do_pwrite(const void *buf, size_t count, off_t offset) { LOG_DEBUG("{offset:`,length:`}", offset, count); CHECK_ALIGNMENT(count, offset); + if (offset < 0 || (uint64_t)offset > m_vsize || count > m_vsize - (uint64_t)offset) + LOG_ERROR_RETURN(EINVAL, -1, "write beyond virtual size: offset:`, len:`, vsize:`", + offset, count, m_vsize); auto bytes = count; while (count > MAX_IO_SIZE) { auto ret = do_pwrite(buf, MAX_IO_SIZE, offset); @@ -1063,15 +1179,10 @@ class LSMTFile : public LSMTReadOnlyFile { if (hybrid()) return hybrid_pwrite(buf, count, offset) == 0 ? (ssize_t)bytes : -1; - Lock lock(m_rw_mtx); - m_vsize = max(m_vsize, count + offset); - SegmentMapping m((uint64_t)offset / ALIGNMENT, (uint32_t)count / ALIGNMENT, 0); - auto moffset = append(m_files[m_rw_tag], buf, count); - if (moffset == 0) - return -1; - m.moffset = (uint64_t)moffset / ALIGNMENT; - m.tag = m_rw_tag; - if (commit_mapping(m, [&] { index_insert(m); }) != 0) + int ret = m_append_only + ? serialized_append(buf, count, (uint64_t)offset / ALIGNMENT, m_rw_tag) + : parallelized_append(buf, count, (uint64_t)offset / ALIGNMENT, m_rw_tag); + if (ret != 0) return -1; return bytes; } @@ -1079,7 +1190,8 @@ class LSMTFile : public LSMTReadOnlyFile { // The ranges already mapped by this writable layer are rewritten in place, everything // else (holes, zeroed ranges and the ranges of the lower layers) is appended. As in // LSMTSparseFile, concurrent writers are not serialized, since the block layer above - // never issues overlapping writes concurrently. + // never issues overlapping writes concurrently. The caller should hold the read + // side of m_lsmt_rwlock, so the tag and the data file cache read here cannot change. int hybrid_pwrite(const void *buf, size_t count, off_t offset) { auto tag = m_rw_tag; auto file = m_files[tag]; @@ -1089,32 +1201,10 @@ class LSMTFile : public LSMTReadOnlyFile { return (const char *)buf + (block - offset_in_blocks) * ALIGNMENT; }; auto append_data = [&](uint64_t begin, uint64_t end) -> int { - SegmentMapping m(begin, (uint32_t)(end - begin), 0); - auto len = (size_t)m.length * ALIGNMENT; - // Direct extents use whole 4K blocks so concurrent appends never enter the - // alignment adaptor's read-modify-write and stale-size truncate path. - auto padded = m_data_file_direct_io - ? (len + ALIGNMENT4K - 1) / ALIGNMENT4K * ALIGNMENT4K - : len; - auto src = data(begin); - unique_ptr tail(nullptr, &free); - if (padded != len) { - tail.reset((char *)aligned_alloc(ALIGNMENT4K, padded)); - if (!tail) - LOG_ERRNO_RETURN(ENOMEM, -1, "failed to allocate ` bytes", padded); - memcpy(tail.get(), src, len); - memset(tail.get() + len, 0, padded - len); - src = tail.get(); - } - auto pos = m_data_offset.fetch_add(padded); - auto ret = file->pwrite(src, padded, pos); - if (ret != (ssize_t)padded) { - LOG_ERRNO_RETURN(0, -1, "append data failed, file:`, ret:`, pos:`, count:`", - file, ret, pos, padded); - } - m.moffset = pos / ALIGNMENT; - m.tag = tag; - return hybrid_insert(m); + auto len = (size_t)(end - begin) * ALIGNMENT; + return m_append_only + ? serialized_append(data(begin), len, begin, tag) + : parallelized_append(data(begin), len, begin, tag); }; uint64_t cursor = offset_in_blocks; @@ -1206,19 +1296,23 @@ class LSMTFile : public LSMTReadOnlyFile { } virtual int discard(SegmentMapping &m) { - m.tag = m_rw_tag; - if (hybrid()) { - struct stat st; - if (m_files[m_rw_tag]->fstat(&st) != 0) - LOG_ERRNO_RETURN(0, -1, "failed to stat hybrid data file"); - m.moffset = st.st_size / ALIGNMENT; - LOG_DEBUG(m); - return hybrid_insert(m); + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; } - Lock rwlock(m_rw_mtx); + if ((uint64_t)m.end() * ALIGNMENT > m_vsize) + LOG_ERROR_RETURN(EINVAL, -1, + "discard beyond virtual size: offset:`, len:`, vsize:`", + m.offset * ALIGNMENT, m.length * ALIGNMENT, m_vsize); + m.tag = m_rw_tag; off_t pos = m_files[m_rw_tag]->lseek(0, SEEK_END); + if (pos < 0) + LOG_ERRNO_RETURN(0, -1, "seek to end failed, file:`", m_files[m_rw_tag]); m.moffset = (uint64_t)(pos / ALIGNMENT); LOG_DEBUG(m); + if (hybrid()) + return hybrid_insert(m); return commit_mapping(m, [&] { index_insert(m); }); } @@ -1235,7 +1329,9 @@ class LSMTFile : public LSMTReadOnlyFile { return 0; } - virtual int update_vsize(size_t vsize) override { + // not part of IFileRW; LSMT-internal (resize and stack_files). The callers + // hold m_lsmt_rwlock or run without concurrent IO. + virtual int update_vsize(size_t vsize) { LOG_INFO("update vsize for LSMTFile ", VALUE(vsize)); m_vsize = vsize; if (update_header_vsize(m_files[m_rw_tag], vsize) < 0) { @@ -1247,7 +1343,91 @@ class LSMTFile : public LSMTReadOnlyFile { return 0; } + // A view of this layer for resize(), which already holds the write side: + // calls here must not take m_lsmt_rwlock again. Everything the extfs io + // manager does not use fails with ENOSYS. + class LockedView : public photon::fs::VirtualFile { + public: + explicit LockedView(LSMTFile *owner) : m_owner(owner) { + } + IFileSystem *filesystem() override { + errno = ENOSYS; + return nullptr; + } + ssize_t pread(void *buf, size_t count, off_t offset) override { + return m_owner->LSMTReadOnlyFile::pread(buf, count, offset); + } + ssize_t pwrite(const void *buf, size_t count, off_t offset) override { + return m_owner->do_pwrite(buf, count, offset); + } + int fstat(struct stat *buf) override { + return m_owner->do_fstat(buf); + } + UNIMPLEMENTED(int fsync() override); + UNIMPLEMENTED(int fdatasync() override); + UNIMPLEMENTED(int close() override); + UNIMPLEMENTED(int fchmod(mode_t mode) override); + UNIMPLEMENTED(int fchown(uid_t owner, gid_t group) override); + UNIMPLEMENTED(int ftruncate(off_t length) override); + + private: + LSMTFile *m_owner; + }; + + // Resize the layer under the write side of m_lsmt_rwlock. The filesystem + // resize runs through a view that skips the lock. Grow: layer then filesystem; + // shrink: filesystem then layer. A failed filesystem grow keeps the new vsize + // and headers, so the caller must retry the resize. + int resize(uint64_t target_size, bool resize_fs = false) override { + if ((target_size % ALIGNMENT4K) != 0) + LOG_ERROR_RETURN(EINVAL, -1, "target size must be 4K aligned, got `", target_size); + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::WLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } + uint64_t current = m_vsize; + bool shrinking = target_size < current; + if (shrinking && !resize_fs) + LOG_ERROR_RETURN(EINVAL, -1, "cannot shrink without resizing filesystem"); + if (!shrinking && target_size != current && update_vsize(target_size) != 0) + LOG_ERROR_RETURN(0, -1, "failed to update vsize for expand"); + if (resize_fs) { + // resize_extfs() uses a process-wide static backend file, so resizes + // must not run concurrently; TODO: use a standalone resize tool. + static photon::mutex extfs_resize_mtx; + photon::scoped_lock extfs_lock(extfs_resize_mtx); + LockedView view(this); +#ifdef PHOTON_ENABLE_RESIZE + auto aligned = new_aligned_file_adaptor(&view, ALIGNMENT4K, true, false); + if (aligned == nullptr) + LOG_ERROR_RETURN(0, -1, "failed to create 4K aligned adaptor for resize"); + DEFER(delete aligned); + if (photon::fs::resize_extfs(aligned, target_size, 0) != 0) { + if (!shrinking) { + LOG_ERROR("resize_extfs failed. " + "Re-run to retry or use e2fsck to recover."); + } else { + LOG_ERROR("resize_extfs failed during shrink."); + } + return -1; + } +#else + LOG_ERROR_RETURN(ENOSYS, -1, + "resize_fs not supported (built without PHOTON_ENABLE_RESIZE)"); +#endif + } + if (shrinking && update_vsize(target_size) != 0) + LOG_ERROR_RETURN(0, -1, "failed to update vsize"); + return 0; + } + virtual int commit(const CommitArgs &args) const override { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } if (m_files.size() > 1) { LOG_ERROR_RETURN(ENOTSUP, -1, "not supported: commit stacked files"); } @@ -1267,6 +1447,11 @@ class LSMTFile : public LSMTReadOnlyFile { } virtual int close_seal(IFileRO **reopen_as = nullptr) override { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::WLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } if (flush_index_log() != 0) return -1; auto m_index0 = (IMemoryIndex0 *)m_index; @@ -1297,7 +1482,7 @@ class LSMTFile : public LSMTReadOnlyFile { HeaderTrailer::SPACE / ALIGNMENT, index_offset / ALIGNMENT); if (new_index == nullptr) { LOG_ERROR("create memory index of reopen file failed."); - return close(); + return do_close(); } auto p = new LSMTReadOnlyFile; p->m_index = new_index; @@ -1307,10 +1492,15 @@ class LSMTFile : public LSMTReadOnlyFile { m_file_ownership = false; *reopen_as = p; } - return close(); + return do_close(); } virtual int fsync() override { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } auto commit_ret = flush_index_log(); if (commit_ret != 0) return commit_ret; @@ -1333,6 +1523,11 @@ class LSMTFile : public LSMTReadOnlyFile { } virtual DataStat data_stat() const override { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); + if (m_index == nullptr) { + errno = EBADF; + return DataStat(); + } struct stat buf; auto ret = m_files[m_rw_tag]->fstat(&buf); if (ret != 0) { @@ -1347,6 +1542,11 @@ class LSMTFile : public LSMTReadOnlyFile { } virtual int flatten(IFile *as) override { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } unique_ptr pmi; { photon::scoped_rwlock ilock(m_index_rwlock, photon::RLOCK); @@ -1369,8 +1569,11 @@ class LSMTFile : public LSMTReadOnlyFile { IMemoryIndex *new_backing = nullptr; // new backing index of the combo index }; - // the caller holds m_rw_mtx and the WLOCK of m_index_rwlock + // the caller holds m_lsmt_rwlock (write side) and the WLOCK of m_index_rwlock int prepare_restack(LSMTFile *upper, RestackPlan &plan) { + // the upper may have been created before a grow; keep the live size + if (upper->m_vsize != m_vsize && upper->update_vsize(m_vsize) != 0) + LOG_ERROR_RETURN(0, -1, "failed to sync the upper's vsize"); /* ==== close_seal the top RW layer and reopen it. ==== */ auto fseal = (LSMTFile*)open_file_rw(m_files[m_rw_tag], m_log->file(), false); if (fseal==nullptr){ @@ -1415,6 +1618,7 @@ class LSMTFile : public LSMTReadOnlyFile { m_vsize = upper->m_vsize; m_data_offset = upper->m_data_offset.load(); m_data_file_direct_io = upper->m_data_file_direct_io; + m_append_only = upper->m_append_only; ((IComboIndex *)m_index)->apply_commit(plan.new_backing); plan.new_backing = nullptr; // the new upper takes over writes here, under the same locks as the seal @@ -1431,7 +1635,13 @@ class LSMTFile : public LSMTReadOnlyFile { } int restack(IFileRW *upper_layer, const std::function &commit) override { - Lock _(m_rw_mtx); + // exclude every writer: the seal, the commit callback and the layer stack + // swap below must not interleave with a write transaction + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::WLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } if (flush_index_log() != 0) LOG_ERRNO_RETURN(0, -1, "flush index log before restack failed"); photon::scoped_rwlock ilock(m_index_rwlock, photon::WLOCK); @@ -1459,21 +1669,23 @@ class LSMTSparseFile : public LSMTFile { m_filetype = LSMTFileType::SparseRW; } - virtual int close() override { + virtual int do_close() override { + if (m_index == nullptr && m_log == nullptr) + return 0; safe_delete(m_log); - return LSMTReadOnlyFile::close(); + return LSMTFile::do_close(); } - virtual ssize_t pwrite(const void *buf, size_t count, off_t offset) override { - return do_sparse_pwrite(buf, count, offset); - } - - ssize_t do_sparse_pwrite(const void *buf, size_t count, off_t offset) { + // lock-free, the caller should hold m_lsmt_rwlock by itself + ssize_t do_pwrite(const void *buf, size_t count, off_t offset) override { LOG_DEBUG("{offset:`,length:`}", offset, count); CHECK_ALIGNMENT(count, offset); - + if (offset < 0 || (uint64_t)offset > m_vsize || count > m_vsize - (uint64_t)offset) + LOG_ERROR_RETURN(EINVAL, -1, "write beyond virtual size: offset:`, len:`, vsize:`", + offset, count, m_vsize); + auto bytes = count; while (count > MAX_IO_SIZE) { - auto ret = do_sparse_pwrite(buf, MAX_IO_SIZE, offset); + auto ret = do_pwrite(buf, MAX_IO_SIZE, offset); if (ret < (ssize_t)MAX_IO_SIZE) return -1; (char *&)buf += MAX_IO_SIZE; @@ -1487,21 +1699,27 @@ class LSMTSparseFile : public LSMTFile { (uint64_t)moffset / (uint64_t)ALIGNMENT, }; m.tag = m_rw_tag; - ssize_t ret = -1; - { - ret = m_files[m_rw_tag]->pwrite(buf, count, moffset); - if (ret != (ssize_t)count) { - LOG_ERRNO_RETURN(0, -1, "write failed, file:`, ret:`, pos:`, count:`", - m_files[m_rw_tag], ret, moffset, count); - } - LOG_DEBUG("insert segment: `", m); - index_insert(m); + auto ret = m_files[m_rw_tag]->pwrite(buf, count, moffset); + if (ret != (ssize_t)count) { + LOG_ERRNO_RETURN(0, -1, "write failed, file:`, ret:`, pos:`, count:`", + m_files[m_rw_tag], ret, moffset, count); } - return ret; + LOG_DEBUG("insert segment: `", m); + index_insert(m); + return bytes; } // virtual int discard(off_t offset, off_t len) override virtual int discard(SegmentMapping &m) override { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } + if ((uint64_t)m.end() * ALIGNMENT > m_vsize) + LOG_ERROR_RETURN(EINVAL, -1, + "discard beyond virtual size: offset:`, len:`, vsize:`", + m.offset * ALIGNMENT, m.length * ALIGNMENT, m_vsize); m.moffset = (uint64_t)(m.offset + (HeaderTrailer::SPACE / ALIGNMENT)); LOG_DEBUG(m); index_insert(m); @@ -1513,6 +1731,16 @@ class LSMTSparseFile : public LSMTFile { // survives a reopen as FIEMAP_EXTENT_UNWRITTEN, instead of turning into a // hole that falls through to the lower layers int zero_range(off_t offset, off_t len) override { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } + if (offset < 0 || (uint64_t)offset > m_vsize || + (uint64_t)len > m_vsize - (uint64_t)offset) + LOG_ERROR_RETURN(EINVAL, -1, + "zero range beyond virtual size: offset:`, len:`, vsize:`", + offset, len, m_vsize); auto moffset = BASE_MOFFSET + offset; if (m_files[m_rw_tag]->fallocate(FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE, moffset, len) != 0) { @@ -1587,7 +1815,9 @@ class LSMTSparseFile : public LSMTFile { return 0; } - virtual int update_vsize(size_t vsize) override { + // not part of IFileRW; LSMT-internal (resize and stack_files). The callers + // hold m_lsmt_rwlock or run without concurrent IO. + int update_vsize(size_t vsize) override { LOG_INFO("update vsize for LSMTSparseFile ", VALUE(vsize)); if ((vsize % ALIGNMENT4K) != 0) LOG_ERROR_RETURN(EINVAL, -1, "sparse layer virtual size must be 4K aligned, got `", @@ -1616,8 +1846,12 @@ class LSMTWarpFile : public LSMTFile { } } - virtual ssize_t pwrite(const void *buf, size_t count, off_t offset) override { + // lock-free, the caller should hold m_lsmt_rwlock by itself + ssize_t do_pwrite(const void *buf, size_t count, off_t offset) override { LOG_DEBUG("write fs meta {offset: `, len: `}", offset, count); + if (offset < 0 || (uint64_t)offset > m_vsize || count > m_vsize - (uint64_t)offset) + LOG_ERROR_RETURN(EINVAL, -1, "write beyond virtual size: offset:`, len:`, vsize:`", + offset, count, m_vsize); auto tag = m_rw_tag + (uint8_t)SegmentType::fsMeta; SegmentMapping m{ (uint64_t)offset / (uint64_t)ALIGNMENT, @@ -1646,6 +1880,16 @@ class LSMTWarpFile : public LSMTFile { va_end(tmp); LOG_DEBUG("RemoteMapping: {offset: `, count: `, roffset: `}", lba.offset, lba.count, lba.roffset); + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); + if (m_index == nullptr) { + errno = EBADF; + return -1; + } + if (lba.offset < 0 || (uint64_t)lba.offset > m_vsize || + (uint64_t)lba.count > m_vsize - (uint64_t)lba.offset) + LOG_ERROR_RETURN(EINVAL, -1, + "remote mapping beyond virtual size: offset:`, len:`, vsize:`", + lba.offset, lba.count, m_vsize); size_t nwrite = 0; while (lba.count > 0) { SegmentMapping m; @@ -1706,6 +1950,7 @@ class LSMTWarpFile : public LSMTFile { } int commit(const CommitArgs &args) const override { + photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::RLOCK); auto m_index0 = (IMemoryIndex0 *)m_index; unique_ptr mapping; size_t raw_index_size; @@ -2401,6 +2646,7 @@ IFileRW *stack_files(IFileRW *upper_layer, IFileRO *lower_layers, bool ownership rst->m_vsize = u->m_vsize; rst->m_data_offset = u->m_data_offset.load(); rst->m_data_file_direct_io = u->m_data_file_direct_io; + rst->m_append_only = u->m_append_only; rst->m_file_ownership = ownership; rst->m_files.reserve(delta + l->m_files.size()); rst->m_uuid.reserve(1 + l->m_uuid.size()); diff --git a/src/overlaybd/lsmt/file.h b/src/overlaybd/lsmt/file.h index dc7da0ad..0551a09b 100644 --- a/src/overlaybd/lsmt/file.h +++ b/src/overlaybd/lsmt/file.h @@ -93,6 +93,7 @@ class IFileRW : public IFileRO { static const int Index_Group_Commit = 10; static const int RemoteData = 11; static const int Set_Data_File_Direct_IO = 13; + static const int Set_Append_Only = 14; int set_index_group_commit(size_t buffer_size) { return this->ioctl(Index_Group_Commit, buffer_size); @@ -102,8 +103,15 @@ class IFileRW : public IFileRO { return this->ioctl(Set_Data_File_Direct_IO, (int)direct); } - // update vsize for current rw layer - virtual int update_vsize(size_t vsize) = 0; + int set_append_only(bool append_only) { + return this->ioctl(Set_Append_Only, (int)append_only); + } + + // Resize the current rw layer, optionally resizing the filesystem on it. + virtual int resize(uint64_t target_size, bool resize_fs = false) { + errno = ENOSYS; + return -1; + } // commit the written content as a new file, without garbages // return 0 for success, -1 otherwise diff --git a/src/overlaybd/lsmt/test/test.cpp b/src/overlaybd/lsmt/test/test.cpp index 454a1e4d..74175f08 100644 --- a/src/overlaybd/lsmt/test/test.cpp +++ b/src/overlaybd/lsmt/test/test.cpp @@ -24,6 +24,8 @@ IMemoryIndex -> IMemoryIndex0 -> IComboIndex -> Index0 ( set ) -> Co */ #include "lsmt-filetest.h" #include "photon/fs/localfs.h" +#include +#include #include #include #include @@ -33,6 +35,7 @@ IMemoryIndex -> IMemoryIndex0 -> IComboIndex -> Index0 ( set ) -> Co #include #include #include +#include #define USE_PTH true // use pthread @@ -1035,8 +1038,8 @@ TEST_F(HybridFileTest, restack_switches_data_file_mode) { delete file; } -// Hybrid writers hold no m_rw_mtx, so their data writes overlap, while an append-only -// layer still serializes them. Every thread writes its own disjoint 4K blocks. +// Hybrid and append writers share the read lock on m_lsmt_rwlock, so their data +// writes overlap. Every thread writes its own disjoint 4K blocks. TEST_F(HybridFileTest, concurrent_writes) { const int NTHREADS = 16; const uint32_t BLOCK = 4096 / ALIGNMENT; @@ -1076,7 +1079,13 @@ TEST_F(HybridFileTest, concurrent_writes) { auto max_inflight = fdata->max_inflight; delete file; if (type == RWType::Append) { - EXPECT_EQ(1, max_inflight); + // append writers run concurrently; data_size is taken after the first + // round, the index file holds the records of both rounds + EXPECT_GT(max_inflight, 1); + EXPECT_EQ((ssize_t)(HeaderTrailer::SPACE + MAX_IMAGE_SIZE), data_size); + EXPECT_EQ((ssize_t)(HeaderTrailer::SPACE + + 2 * NTHREADS * NWRITES * sizeof(SegmentMapping)), + file_size(lfs, idx_name.back().c_str())); continue; } EXPECT_GT(max_inflight, 1); @@ -1106,6 +1115,87 @@ TEST_F(HybridFileTest, concurrent_writes) { } } +// Writers keep appending while a restack seals the current layer. Every write that +// returned success must stay readable afterwards, no matter which side of the +// restack it landed on. +TEST_F(HybridFileTest, restack_keeps_concurrent_writes) { + const int NTHREADS = 8; + const int NWRITES = 64; + const size_t BLOCK = 4096; + + for (auto type : {RWType::Append, RWType::Hybrid}) { + // restack needs a backing index, so stack the writable layer on a sealed one + auto lower_rw = create_file_rw(); + ASSERT_NE(nullptr, lower_rw); + IFileRO *lower = nullptr; + ASSERT_EQ(0, lower_rw->close_seal(&lower)); + delete lower_rw; + + name_next_layer(); + auto fdata = new YieldingFile( + lfs->open(data_name.back().c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)); + auto findex = lfs->open(idx_name.back().c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRWXU); + LayerInfo args(fdata, findex); + args.rw_type = type; + args.virtual_size = vsize; + auto writer = ::create_file_rw(args, true); + ASSERT_NE(nullptr, writer); + auto file = stack_files(writer, lower, true, false); + ASSERT_NE(nullptr, file); + auto upper = create_file_rw(type); + ASSERT_NE(nullptr, upper); + + std::vector> acked((size_t)NTHREADS * NWRITES); + for (auto &a : acked) + a.store(0); + std::atomic errors{0}; + std::atomic stop{false}; + std::vector threads; + for (int t = 0; t < NTHREADS; t++) { + threads.push_back(photon::thread_enable_join(photon::thread_create11([&, t] { + ALIGNED_MEM4K(buf, BLOCK); + int seq = 0; + while (!stop.load()) { + for (int j = 0; j < NWRITES; j++) { + uint8_t pat = (uint8_t)(++seq * 37 + t + 1); + memset(buf, pat, BLOCK); + off_t off = ((off_t)j * NTHREADS + t) * BLOCK; + if (file->pwrite(buf, BLOCK, off) != (ssize_t)BLOCK) { + errors++; + return; + } + acked[(size_t)j * NTHREADS + t].store(pat); + photon::thread_yield(); + } + } + }))); + } + + // let the writers enter the write path before the seal starts + for (int i = 0; i < 8; i++) + photon::thread_yield(); + int restack_ret = file->restack(upper); + stop = true; + for (auto th : threads) + photon::thread_join(th); + EXPECT_EQ(0, restack_ret) << rw_type_name(type); + EXPECT_EQ(0, errors.load()); + + ALIGNED_MEM4K(rbuf, BLOCK); + for (int i = 0; i < NTHREADS * NWRITES; i++) { + int want = acked[i].load(); + if (want == 0) + continue; + ASSERT_EQ((ssize_t)BLOCK, file->pread(rbuf, BLOCK, (off_t)i * BLOCK)) + << rw_type_name(type) << " block " << i; + for (size_t k = 0; k < BLOCK; k++) + ASSERT_EQ((char)want, rbuf[k]) + << rw_type_name(type) << " block " << i << " byte " << k; + } + delete file; + } +} + // The append positions and the group-commit buffer are shared by the writers of all // the vCPUs serving a hybrid layer. TEST_F(HybridFileTest, multi_vcpu_writes) { @@ -2030,6 +2120,96 @@ TEST_F(WarpFileTest, commit_with_uuid) { EXPECT_EQ(uu, uu_commit); } +TEST_F(FileTest2, resize_with_filesystem) { +#ifndef PHOTON_ENABLE_RESIZE + GTEST_SKIP() << "built without filesystem resize support"; +#endif + auto rw = create_file_rw(); + ASSERT_NE(rw, nullptr); + + auto aligned = photon::fs::new_aligned_file_adaptor(rw, LSMT::ALIGNMENT4K, true, false); + ASSERT_NE(aligned, nullptr); + ASSERT_EQ(0, photon::fs::make_extfs(aligned)); + delete aligned; + + auto expect_ext4_size = [&](uint64_t expected_size) { + uint8_t sb[1024]; + ASSERT_EQ((ssize_t)sizeof(sb), rw->pread(sb, sizeof(sb), 1024)); + uint32_t blocks_lo = 0, log_block_size = 0; + memcpy(&blocks_lo, sb + 0x4, sizeof(blocks_lo)); + memcpy(&log_block_size, sb + 0x18, sizeof(log_block_size)); + EXPECT_EQ(expected_size, (uint64_t)blocks_lo * (1024ULL << log_block_size)); + }; + + struct stat st; + ASSERT_EQ(0, rw->fstat(&st)); + ASSERT_EQ((off_t)vsize, st.st_size); + expect_ext4_size(vsize); + + const uint64_t target = vsize * 2; + ASSERT_EQ(0, rw->resize(target, true)); + ASSERT_EQ(0, rw->fstat(&st)); + ASSERT_EQ((off_t)target, st.st_size); + expect_ext4_size(target); + + ASSERT_EQ(0, rw->resize(vsize, true)); + ASSERT_EQ(0, rw->fstat(&st)); + ASSERT_EQ((off_t)vsize, st.st_size); + expect_ext4_size(vsize); + + delete rw; +} + +TEST_F(FileTest2, reject_io_after_close_seal) { + auto file = create_file_rw(); + ASSERT_NE(file, nullptr); + + char buf[4096] = {}; + ASSERT_EQ((ssize_t)sizeof(buf), file->pwrite(buf, sizeof(buf), 0)); + ASSERT_EQ(0, file->close_seal()); + + errno = 0; + EXPECT_EQ(-1, file->pwrite(buf, sizeof(buf), 0)); + EXPECT_EQ(EBADF, errno); + + errno = 0; + EXPECT_EQ(-1, file->pread(buf, sizeof(buf), 0)); + EXPECT_EQ(EBADF, errno); + + delete file; +} + +TEST_F(FileTest2, append_only_mode) { + auto file = create_file_rw(); + ASSERT_NE(file, nullptr); + ASSERT_EQ(0, file->set_append_only(true)); + + ALIGNED_MEM4K(buf, LSMT::ALIGNMENT4K); + memset(buf, 0x5a, LSMT::ALIGNMENT4K); + ASSERT_EQ((ssize_t)LSMT::ALIGNMENT4K, file->pwrite(buf, LSMT::ALIGNMENT4K, 0)); + memset(buf, 0xa5, LSMT::ALIGNMENT4K); + ASSERT_EQ((ssize_t)LSMT::ALIGNMENT4K, + file->pwrite(buf, LSMT::ALIGNMENT4K, LSMT::ALIGNMENT4K)); + + ALIGNED_MEM4K(rbuf, LSMT::ALIGNMENT4K); + ASSERT_EQ((ssize_t)LSMT::ALIGNMENT4K, file->pread(rbuf, LSMT::ALIGNMENT4K, 0)); + EXPECT_EQ(0x5a, (uint8_t)rbuf[0]); + ASSERT_EQ((ssize_t)LSMT::ALIGNMENT4K, + file->pread(rbuf, LSMT::ALIGNMENT4K, LSMT::ALIGNMENT4K)); + EXPECT_EQ(0xa5, (uint8_t)rbuf[0]); + + delete file; +} + +TEST_F(HybridFileTest, append_only_rejected) { + auto file = create_file_rw(RWType::Hybrid); + ASSERT_NE(file, nullptr); + errno = 0; + EXPECT_EQ(-1, file->set_append_only(true)); + EXPECT_EQ(EINVAL, errno); + delete file; +} + int main(int argc, char **argv) { auto seed = 154574045; diff --git a/src/tools/overlaybd-create.cpp b/src/tools/overlaybd-create.cpp index ad0674e7..193df363 100644 --- a/src/tools/overlaybd-create.cpp +++ b/src/tools/overlaybd-create.cpp @@ -76,6 +76,10 @@ int main(int argc, char **argv) { fprintf(stderr, "-s and --hybrid cannot be used together\n"); return -1; } + if (vsize == 0) { + fprintf(stderr, "virtual size must be greater than 0\n"); + return -1; + } build_turboOCI = build_turboOCI || build_fastoci; diff --git a/src/ublk/ublk_device.cpp b/src/ublk/ublk_device.cpp index 60e522a7..bcdeeb1a 100644 --- a/src/ublk/ublk_device.cpp +++ b/src/ublk/ublk_device.cpp @@ -895,7 +895,8 @@ int UblkDevice::resize(uint64_t new_size_bytes, bool resize_fs, std::string &err err = "kernel lacks UBLK_F_UPDATE_SIZE (mainline >= 6.11 required)"; return -2; } - // image layer first (and ext4 if asked): on failure nothing changed + // image layer first (and ext4 if asked); a failed ext4 grow keeps the new + // vsize in the layer, so the resize must be retried if (file_->resize(new_size_bytes, resize_fs) != 0) { err = "image resize failed (see the overlaybd log)"; return -1;