From 0e300f4c50a8b2624ea8c16d3bf5824033764063 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Wed, 10 Jun 2026 00:38:30 +0300 Subject: [PATCH] Fix incorrect rounding in vitastor-disk trim when --discard_granularity option is passed --- src/blockstore/blockstore_disk.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/blockstore/blockstore_disk.cpp b/src/blockstore/blockstore_disk.cpp index a33fe025..6a5c428c 100644 --- a/src/blockstore/blockstore_disk.cpp +++ b/src/blockstore/blockstore_disk.cpp @@ -541,17 +541,21 @@ int blockstore_disk_t::trim_data(std::function is_used) if (range[0] % discard_granularity) range[0] = range[0] + discard_granularity - (range[0] % discard_granularity); if (range[0] >= range[1]) - continue; - range[1] -= range[0]; + range[1] = 0; + else + range[1] -= range[0]; } - r = ioctl(data_fd, BLKDISCARD, &range); - if (r != 0) + if (range[1] > 0) { - fprintf(stderr, "Failed to execute BLKDISCARD %ju+%ju on %s: %s (code %d)\n", - range[0], range[1], data_device.c_str(), strerror(-r), r); - return -errno; + r = ioctl(data_fd, BLKDISCARD, &range); + if (r != 0) + { + fprintf(stderr, "Failed to execute BLKDISCARD %ju+%ju on %s: %s (code %d)\n", + range[0], range[1], data_device.c_str(), strerror(-r), r); + return -errno; + } + discarded += range[1]; } - discarded += range[1]; } j = i+1; }