From 050515f6aefdfaa1056ca403c77908b6850794af Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 16 Feb 2023 23:58:35 -0500 Subject: [PATCH] BFS: Fix missing trim of last range in a corner case. The previous logic of _AddTrim plus _TrimNext meant that if a trim range was specified in the final call to _TrimNext (i.e. with force=true), but the trim buffer was already full at that point, that last range would not actually be trimmed. Now, _AddTrim returns true when the buffer is filled and trimming should be done, rather than delaying it and requiring a second add after trimming. Change-Id: I4b782948e8dc9267c63e61bce0c078fd4e834177 Reviewed-on: https://review.haiku-os.org/c/haiku/+/6070 Tested-by: Automation Reviewed-by: waddlesplash --- .../file_systems/bfs/BlockAllocator.cpp | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/add-ons/kernel/file_systems/bfs/BlockAllocator.cpp b/src/add-ons/kernel/file_systems/bfs/BlockAllocator.cpp index aa5245f84c..6cc66af7d6 100644 --- a/src/add-ons/kernel/file_systems/bfs/BlockAllocator.cpp +++ b/src/add-ons/kernel/file_systems/bfs/BlockAllocator.cpp @@ -1335,14 +1335,15 @@ bool BlockAllocator::_AddTrim(fs_trim_data& trimData, uint32 maxRanges, uint64 offset, uint64 size) { - if (trimData.range_count < maxRanges && size > 0) { - trimData.ranges[trimData.range_count].offset = offset; - trimData.ranges[trimData.range_count].size = size; - trimData.range_count++; - return true; - } + ASSERT(trimData.range_count < maxRanges); + if (size == 0) + return false; - return false; + trimData.ranges[trimData.range_count].offset = offset; + trimData.ranges[trimData.range_count].size = size; + trimData.range_count++; + + return (trimData.range_count == maxRanges); } @@ -1353,9 +1354,9 @@ BlockAllocator::_TrimNext(fs_trim_data& trimData, uint32 maxRanges, PRINT(("_TrimNext(index %" B_PRIu32 ", offset %" B_PRIu64 ", size %" B_PRIu64 ")\n", trimData.range_count, offset, size)); - bool pushed = _AddTrim(trimData, maxRanges, offset, size); + const bool rangesFilled = _AddTrim(trimData, maxRanges, offset, size); - if (!pushed || force) { + if (rangesFilled || force) { // Trim now trimData.trimmed_size = 0; #ifdef DEBUG_TRIM @@ -1375,9 +1376,6 @@ BlockAllocator::_TrimNext(fs_trim_data& trimData, uint32 maxRanges, trimData.range_count = 0; } - if (!pushed) - _AddTrim(trimData, maxRanges, offset, size); - return B_OK; }