* [PATCH] ocfs2: fix cached cluster count after suballocator reclaim
@ 2026-08-05 7:08 Matthias Goergens
2026-08-05 7:38 ` Heming Zhao
2026-08-05 8:49 ` Heming Zhao
0 siblings, 2 replies; 6+ messages in thread
From: Matthias Goergens @ 2026-08-05 7:08 UTC (permalink / raw)
To: ocfs2-devel
Cc: mark, jlbec, joseph.qi, heming.zhao, glass.su, akpm, linux-kernel,
stable, Matthias Goergens
When reclaiming a suballocator block group, first reduce the on-disk
cluster count by cl_cpg. The current code then subtracts that new count
from the old cached count.
For an allocator with N groups, that leaves the cache at
N * cl_cpg - (N - 1) * cl_cpg = cl_cpg
regardless of N. This happens to be correct when reclaiming from two
groups, but undercounts the clusters from three groups onwards. The
incorrect cache value is also used immediately to update i_blocks.
Assign the updated on-disk count to the cache, matching the allocation and
inode refresh paths.
In a QEMU test using a clean 256 MiB OCFS2 image and a 10,000-file
create/delete workload, the first buggy reclaim left the on-disk and cached
counts at 2048 and 512 clusters respectively; later reclaims underflowed
the cache. With this change, the cache matched the on-disk count across
all four reclaims: 2048, 1536, 1024, and 512 clusters.
Fixes: 4a54331616b3 ("ocfs2: give ocfs2 the ability to reclaim suballocator free bg")
Cc: stable@vger.kernel.org
Signed-off-by: Matthias Goergens <matthias.goergens@gmail.com>
---
fs/ocfs2/suballoc.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c
index a4a2b87a45fe3..20c3aec6b9873 100644
--- a/fs/ocfs2/suballoc.c
+++ b/fs/ocfs2/suballoc.c
@@ -2759,7 +2759,7 @@ static int _ocfs2_reclaim_suballoc_to_main(handle_t *handle,
fe->i_clusters = cpu_to_le32(tmp_used - le16_to_cpu(cl->cl_cpg));
spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
- OCFS2_I(alloc_inode)->ip_clusters -= le32_to_cpu(fe->i_clusters);
+ OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
le32_to_cpu(fe->i_clusters)));
spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
--
2.55.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH] ocfs2: fix cached cluster count after suballocator reclaim 2026-08-05 7:08 [PATCH] ocfs2: fix cached cluster count after suballocator reclaim Matthias Goergens @ 2026-08-05 7:38 ` Heming Zhao 2026-08-05 7:56 ` Matthias Goergens 2026-08-05 8:49 ` Heming Zhao 1 sibling, 1 reply; 6+ messages in thread From: Heming Zhao @ 2026-08-05 7:38 UTC (permalink / raw) To: Matthias Goergens Cc: ocfs2-devel, mark, jlbec, joseph.qi, glass.su, akpm, linux-kernel, stable Hello Matthias, Could you share your test case, I want to reproduce the issue and verify the fix. Thanks, Heming On Wed, Aug 05, 2026 at 03:08:37PM +0800, Matthias Goergens wrote: > When reclaiming a suballocator block group, first reduce the on-disk > cluster count by cl_cpg. The current code then subtracts that new count > from the old cached count. > > For an allocator with N groups, that leaves the cache at > > N * cl_cpg - (N - 1) * cl_cpg = cl_cpg > > regardless of N. This happens to be correct when reclaiming from two > groups, but undercounts the clusters from three groups onwards. The > incorrect cache value is also used immediately to update i_blocks. > > Assign the updated on-disk count to the cache, matching the allocation and > inode refresh paths. > > In a QEMU test using a clean 256 MiB OCFS2 image and a 10,000-file > create/delete workload, the first buggy reclaim left the on-disk and cached > counts at 2048 and 512 clusters respectively; later reclaims underflowed > the cache. With this change, the cache matched the on-disk count across > all four reclaims: 2048, 1536, 1024, and 512 clusters. > > Fixes: 4a54331616b3 ("ocfs2: give ocfs2 the ability to reclaim suballocator free bg") > Cc: stable@vger.kernel.org > Signed-off-by: Matthias Goergens <matthias.goergens@gmail.com> > --- > fs/ocfs2/suballoc.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c > index a4a2b87a45fe3..20c3aec6b9873 100644 > --- a/fs/ocfs2/suballoc.c > +++ b/fs/ocfs2/suballoc.c > @@ -2759,7 +2759,7 @@ static int _ocfs2_reclaim_suballoc_to_main(handle_t *handle, > fe->i_clusters = cpu_to_le32(tmp_used - le16_to_cpu(cl->cl_cpg)); > > spin_lock(&OCFS2_I(alloc_inode)->ip_lock); > - OCFS2_I(alloc_inode)->ip_clusters -= le32_to_cpu(fe->i_clusters); > + OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); > fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb, > le32_to_cpu(fe->i_clusters))); > spin_unlock(&OCFS2_I(alloc_inode)->ip_lock); > -- > 2.55.0 > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] ocfs2: fix cached cluster count after suballocator reclaim 2026-08-05 7:38 ` Heming Zhao @ 2026-08-05 7:56 ` Matthias Goergens 0 siblings, 0 replies; 6+ messages in thread From: Matthias Goergens @ 2026-08-05 7:56 UTC (permalink / raw) To: Heming Zhao Cc: Matthias Goergens, ocfs2-devel, mark, jlbec, joseph.qi, glass.su, akpm, linux-kernel, stable Hello Heming, On Wed, Aug 05, 2026 at 03:38:05PM +0800, Heming Zhao wrote: > Could you share your test case, I want to reproduce the issue and verify the > fix. Sure. The test runs in a QEMU guest on a snapshot-only virtio disk (the host image is never modified). The image is a clean 256 MiB single-node local OCFS2 filesystem made with ocfs2-tools 1.8.9: truncate --size=256M accounting.ocfs2 mkfs.ocfs2 -F -N 1 -M local accounting.ocfs2 The guest runs the small program below: mount with heartbeat=none,localflocks, create 10,000 empty files, sync, then unlink them all, sync. That drives four suballocator reclaims on this image. To observe the accounting I used a temporary diagnostic, added immediately after the ip_clusters update in _ocfs2_reclaim_suballoc_to_main() (not part of the submitted patch): pr_warn("OCFS2_RECLAIM_ACCOUNTING old=%u disk=%u cache=%u cpg=%u\n", tmp_used, le32_to_cpu(fe->i_clusters), OCFS2_I(alloc_inode)->ip_clusters, le16_to_cpu(cl->cl_cpg)); On the stock kernel the first reclaim already leaves the cache at 512 while the disk holds 2048, and later reclaims underflow the cache. With the patch the cache matches the disk at every reclaim: 2048, 1536, 1024, 512. The guest reproducer (statically linked, run as init in a minimal initramfs): #define _GNU_SOURCE #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <sys/mount.h> #include <sys/stat.h> #include <unistd.h> #define FILE_LIMIT 10000 int main(void) { int created = 0; if (mkdir("/mnt", 0777) && errno != EEXIST) { perror("mkdir /mnt"); return 1; } if (mount("/dev/vda", "/mnt", "ocfs2", MS_NODIRATIME, "heartbeat=none,localflocks")) { perror("mount /dev/vda"); return 1; } if (chdir("/mnt")) { perror("chdir /mnt"); return 1; } puts("ACCOUNTING_TEST: mounted; creating files"); for (int i = 0; i < FILE_LIMIT; i++) { char path[32]; int fd; snprintf(path, sizeof(path), "inode-%05d", i); fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600); if (fd < 0) { printf("ACCOUNTING_TEST: create failed at %d: %s\n", i, strerror(errno)); break; } close(fd); created++; } sync(); printf("ACCOUNTING_TEST: unlinking %d files\n", created); for (int i = created - 1; i >= 0; i--) { char path[32]; snprintf(path, sizeof(path), "inode-%05d", i); if (unlink(path)) { printf("ACCOUNTING_TEST: unlink failed at %d: %s\n", i, strerror(errno)); return 1; } } sync(); puts("ACCOUNTING_TEST: complete"); return 0; } I also have the full harness (image builder, initramfs builder, QEMU runner with a log-checking mode) if you want it; happy to post it or send it privately. Thanks, Matthias ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] ocfs2: fix cached cluster count after suballocator reclaim 2026-08-05 7:08 [PATCH] ocfs2: fix cached cluster count after suballocator reclaim Matthias Goergens 2026-08-05 7:38 ` Heming Zhao @ 2026-08-05 8:49 ` Heming Zhao 2026-08-05 11:39 ` [PATCH v2] " Matthias Goergens 1 sibling, 1 reply; 6+ messages in thread From: Heming Zhao @ 2026-08-05 8:49 UTC (permalink / raw) To: Matthias Goergens Cc: ocfs2-devel, mark, jlbec, joseph.qi, glass.su, akpm, linux-kernel, stable The code looks good to me. However, the commit log needs some revision. On Wed, Aug 05, 2026 at 03:08:37PM +0800, Matthias Goergens wrote: > When reclaiming a suballocator block group, first reduce the on-disk > cluster count by cl_cpg. The current code then subtracts that new count > from the old cached count. The current code then sbtracts that new count (fe->i_clusters) from the old cached count (OCFS2_I(alloc_inode)->ip_clusters). > > For an allocator with N groups, that leaves the cache at For an allocator with N block groups, that leaves the cache at > > N * cl_cpg - (N - 1) * cl_cpg = cl_cpg N * cl_cpg - (N * cl_cpg - cl_cpg) = cl_cpg i.e.: ->ip_clusters -= (fe->i_clusters - cl->cl_cgp) => ->ip_clusters equal to cl_cpg > > regardless of N. This happens to be correct when reclaiming from two > groups, but undercounts the clusters from three groups onwards. The s/groups/block groups/ > incorrect cache value is also used immediately to update i_blocks. > > Assign the updated on-disk count to the cache, matching the allocation and > inode refresh paths. > > In a QEMU test using a clean 256 MiB OCFS2 image and a 10,000-file > create/delete workload, the first buggy reclaim left the on-disk and cached create/delete workload, the first buggy reclaim left the on-disk (fe->i_cluster) and cached (->ip_clusters) Thanks, Heming > counts at 2048 and 512 clusters respectively; later reclaims underflowed > the cache. With this change, the cache matched the on-disk count across > all four reclaims: 2048, 1536, 1024, and 512 clusters. > > Fixes: 4a54331616b3 ("ocfs2: give ocfs2 the ability to reclaim suballocator free bg") > Cc: stable@vger.kernel.org > Signed-off-by: Matthias Goergens <matthias.goergens@gmail.com> > --- > fs/ocfs2/suballoc.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c > index a4a2b87a45fe3..20c3aec6b9873 100644 > --- a/fs/ocfs2/suballoc.c > +++ b/fs/ocfs2/suballoc.c > @@ -2759,7 +2759,7 @@ static int _ocfs2_reclaim_suballoc_to_main(handle_t *handle, > fe->i_clusters = cpu_to_le32(tmp_used - le16_to_cpu(cl->cl_cpg)); > > spin_lock(&OCFS2_I(alloc_inode)->ip_lock); > - OCFS2_I(alloc_inode)->ip_clusters -= le32_to_cpu(fe->i_clusters); > + OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); > fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb, > le32_to_cpu(fe->i_clusters))); > spin_unlock(&OCFS2_I(alloc_inode)->ip_lock); > -- > 2.55.0 > ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2] ocfs2: fix cached cluster count after suballocator reclaim 2026-08-05 8:49 ` Heming Zhao @ 2026-08-05 11:39 ` Matthias Goergens 2026-08-05 12:20 ` Joseph Qi 0 siblings, 1 reply; 6+ messages in thread From: Matthias Goergens @ 2026-08-05 11:39 UTC (permalink / raw) To: ocfs2-devel Cc: heming.zhao, mark, jlbec, joseph.qi, glass.su, akpm, linux-kernel, stable, Matthias Goergens When reclaiming a suballocator block group, first reduce the on-disk cluster count by cl_cpg. The current code then subtracts that new count (fe->i_clusters) from the old cached count (OCFS2_I(alloc_inode)->ip_clusters). For an allocator with N block groups, that leaves the cache at N * cl_cpg - (N * cl_cpg - cl_cpg) = cl_cpg i.e. ip_clusters -= (fe->i_clusters - cl_cpg) leaves ip_clusters equal to cl_cpg regardless of N. This happens to be correct when reclaiming from two block groups, but undercounts the clusters from three block groups onwards. The incorrect cache value is also used immediately to update i_blocks. Assign the updated on-disk count to the cache, matching the allocation and inode refresh paths. In a QEMU test using a clean 256 MiB OCFS2 image and a 10,000-file create/delete workload, the first buggy reclaim left the on-disk (fe->i_clusters) and cached (ip_clusters) counts at 2048 and 512 clusters respectively; later reclaims underflowed the cache. With this change, the cache matched the on-disk count across all four reclaims: 2048, 1536, 1024, and 512 clusters. Fixes: 4a54331616b3 ("ocfs2: give ocfs2 the ability to reclaim suballocator free bg") Cc: stable@vger.kernel.org Signed-off-by: Matthias Goergens <matthias.goergens@gmail.com> --- v2: commit-log wording revisions suggested by Heming Zhao; code unchanged. fs/ocfs2/suballoc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c index a4a2b87a45fe3..20c3aec6b9873 100644 --- a/fs/ocfs2/suballoc.c +++ b/fs/ocfs2/suballoc.c @@ -2759,7 +2759,7 @@ static int _ocfs2_reclaim_suballoc_to_main(handle_t *handle, fe->i_clusters = cpu_to_le32(tmp_used - le16_to_cpu(cl->cl_cpg)); spin_lock(&OCFS2_I(alloc_inode)->ip_lock); - OCFS2_I(alloc_inode)->ip_clusters -= le32_to_cpu(fe->i_clusters); + OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb, le32_to_cpu(fe->i_clusters))); spin_unlock(&OCFS2_I(alloc_inode)->ip_lock); -- 2.55.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2] ocfs2: fix cached cluster count after suballocator reclaim 2026-08-05 11:39 ` [PATCH v2] " Matthias Goergens @ 2026-08-05 12:20 ` Joseph Qi 0 siblings, 0 replies; 6+ messages in thread From: Joseph Qi @ 2026-08-05 12:20 UTC (permalink / raw) To: Matthias Goergens, Andrew Morton Cc: heming.zhao, mark, jlbec, glass.su, ocfs2-devel, linux-kernel On 8/5/26 7:39 PM, Matthias Goergens wrote: > When reclaiming a suballocator block group, first reduce the on-disk > cluster count by cl_cpg. The current code then subtracts that new > count (fe->i_clusters) from the old cached count > (OCFS2_I(alloc_inode)->ip_clusters). > > For an allocator with N block groups, that leaves the cache at > > N * cl_cpg - (N * cl_cpg - cl_cpg) = cl_cpg > > i.e. ip_clusters -= (fe->i_clusters - cl_cpg) leaves ip_clusters equal > to cl_cpg regardless of N. This happens to be correct when reclaiming > from two block groups, but undercounts the clusters from three block > groups onwards. The incorrect cache value is also used immediately to > update i_blocks. > > Assign the updated on-disk count to the cache, matching the allocation > and inode refresh paths. > > In a QEMU test using a clean 256 MiB OCFS2 image and a 10,000-file > create/delete workload, the first buggy reclaim left the on-disk > (fe->i_clusters) and cached (ip_clusters) counts at 2048 and 512 > clusters respectively; later reclaims underflowed the cache. With this > change, the cache matched the on-disk count across all four reclaims: > 2048, 1536, 1024, and 512 clusters. > > Fixes: 4a54331616b3 ("ocfs2: give ocfs2 the ability to reclaim suballocator free bg") > Cc: stable@vger.kernel.org > Signed-off-by: Matthias Goergens <matthias.goergens@gmail.com> Reviewed-by: Joseph Qi <joseph.qi@linux.alibaba.com> > --- > > v2: commit-log wording revisions suggested by Heming Zhao; code unchanged. > fs/ocfs2/suballoc.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c > index a4a2b87a45fe3..20c3aec6b9873 100644 > --- a/fs/ocfs2/suballoc.c > +++ b/fs/ocfs2/suballoc.c > @@ -2759,7 +2759,7 @@ static int _ocfs2_reclaim_suballoc_to_main(handle_t *handle, > fe->i_clusters = cpu_to_le32(tmp_used - le16_to_cpu(cl->cl_cpg)); > > spin_lock(&OCFS2_I(alloc_inode)->ip_lock); > - OCFS2_I(alloc_inode)->ip_clusters -= le32_to_cpu(fe->i_clusters); > + OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); > fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb, > le32_to_cpu(fe->i_clusters))); > spin_unlock(&OCFS2_I(alloc_inode)->ip_lock); ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-08-05 12:20 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-08-05 7:08 [PATCH] ocfs2: fix cached cluster count after suballocator reclaim Matthias Goergens 2026-08-05 7:38 ` Heming Zhao 2026-08-05 7:56 ` Matthias Goergens 2026-08-05 8:49 ` Heming Zhao 2026-08-05 11:39 ` [PATCH v2] " Matthias Goergens 2026-08-05 12:20 ` Joseph Qi
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox