ntfs_trim_fs() keeps a free run pending across bitmap windows. When the cached free-bit count of a window is zero, the loop skips it without ending the pending run. Free clusters at the start of a later window are then added to the old length despite the allocated gap. For example, two eight-cluster holes separated by fully allocated bitmap windows can be submitted as one sixteen-cluster discard starting at the first hole. This discards allocated clusters while leaving the actual second hole untrimmed. Finish a qualifying pending run and reset its length before skipping a fully allocated window. Preserve the minimum-length check and the cached window optimization, matching the analogous handling in wnd_rescan(). Fixes: 3f3b442b5ad2 ("fs/ntfs3: Add bitmap") Cc: stable@vger.kernel.org Signed-off-by: Hungyu Lin --- fs/ntfs3/bitmap.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/fs/ntfs3/bitmap.c b/fs/ntfs3/bitmap.c index 60b362f388d960558cd8ea65680df266e26dabd5..8875e6feff9c9715933545dc2fc173a191302ddc 100644 --- a/fs/ntfs3/bitmap.c +++ b/fs/ntfs3/bitmap.c @@ -1447,8 +1447,16 @@ int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range) if (lcn_wnd > lcn_to) break; - if (!wnd->free_bits[iw]) + if (!wnd->free_bits[iw]) { + if (len >= minlen) { + err = ntfs_discard(sbi, lcn, len); + if (err) + goto out; + done += len; + } + len = 0; continue; + } if (iw + 1 == wnd->nwnd) wbits = wnd->bits_last; base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 -- 2.43.0