* [Devel] [PATCH hci-8.1 0/2] ext4, xfs: discard the balloon on FITRIM
@ 2026-11-10 21:24 Denis V. Lunev
2026-11-10 21:24 ` [Devel] [PATCH hci-8.1 1/2] ext4: " Denis V. Lunev
2026-11-10 21:24 ` [Devel] [PATCH hci-8.1 2/2] xfs: " Denis V. Lunev
0 siblings, 2 replies; 5+ messages in thread
From: Denis V. Lunev @ 2026-11-10 21:24 UTC (permalink / raw)
To: khorenko; +Cc: devel
The balloon holds filesystem space that never carries data, but its
blocks are allocated, so FITRIM walks past them, and inflating the
balloon is a plain fallocate() that does not discard either. Once the
balloon is inflated over the blocks of removed files, e.g. on an online
shrink of a Container disk, the ploop or qcow2 image keeps those
clusters mapped for good, and compaction, which is FITRIM, does not
give them back (VSTOR-153590).
This series makes FITRIM discard the unwritten extents of the balloon
after the free space, on ext4 (patch 1) and xfs (patch 2). The walk is
read-only, under the shared inode lock of the balloon, which keeps
fallocate, truncate and extent moves away from it, so no block can be
freed and reused under the discard. Physically adjacent extents are
merged into one discard, so that the device below gets whole clusters.
A balloon inflated before this series is released by the next FITRIM,
no userspace change is needed.
The ploop side is openvz/ploop.ovz PR 519: tests for this, and fixes of
the ploop-balloon discard options found on the way:
https://bitbucket.org/openvz/ploop.ovz/pull-requests/519
Tested on 6.12.0-211.52.1.22.19.vz10 rebuilt with this series, alone
and together with the VSTOR-154159 max_discard_segments fixes:
- ploop and qcow2 images with ext4 and xfs: write 2G, remove it without
a trim, inflate the balloon over the free space, ploop-balloon
discard. Stock: the image keeps mapping the removed 2G. Patched: the
image maps about as much as when it was created, a kept file is
intact, fsck and ploop check / qemu-img check are clean;
- fstrim -o/-l discards only the part of the balloon in the range, a
repeated discard changes nothing, and data written over the space of
a deflated balloon reads back intact;
- test_ploop_balloon.py from PR 519: green on ploop and qcow2 with
pytest -n 10. On the stock kernel only the test for the space under
the balloon fails.
Not covered: a read-only mount (the pass is skipped there), ext4
bigalloc, a realtime xfs balloon (skipped). On ploop1, discard works on
whole clusters, so up to two clusters at the ends of each contiguous
piece of the balloon stay mapped.
Denis V. Lunev (2):
ext4: discard the balloon on FITRIM
xfs: discard the balloon on FITRIM
fs/ext4/mballoc.c | 99 +++++++++++++++++++++++++++++++++++++++-
fs/xfs/xfs_discard.c | 106 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 204 insertions(+), 1 deletion(-)
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Devel] [PATCH hci-8.1 1/2] ext4: discard the balloon on FITRIM
2026-11-10 21:24 [Devel] [PATCH hci-8.1 0/2] ext4, xfs: discard the balloon on FITRIM Denis V. Lunev
@ 2026-11-10 21:24 ` Denis V. Lunev
2026-11-10 21:34 ` no-reply
2026-11-10 21:24 ` [Devel] [PATCH hci-8.1 2/2] xfs: " Denis V. Lunev
1 sibling, 1 reply; 5+ messages in thread
From: Denis V. Lunev @ 2026-11-10 21:24 UTC (permalink / raw)
To: khorenko; +Cc: devel
From: Denis V. Lunev <den@openvz.org>
The balloon holds file system space that never carries data, but its
blocks are allocated, so ext4_trim_all_free() never discards them, and
inflating the balloon is a plain fallocate() that does not discard
either. Once the balloon is inflated over blocks that held data, e.g.
on an online shrink after files were removed, the device below keeps
them allocated: a ploop or qcow2 image keeps host disk space the
container can never use, and compaction, which is FITRIM, does not give
it back.
Walk the balloon after the free space in ext4_trim_fs() and discard its
unwritten extents that fall into the requested range. Physically
adjacent extents are merged into one discard, otherwise every cut at
the unwritten extent length limit leaves a partly covered cluster of
the device below. The shared i_rwsem keeps fallocate, truncate and
EXT4_IOC_MOVE_EXT away from the balloon meanwhile, so none of its
blocks can be freed and reused under the discard. An unwritten extent
reads as zeroes whatever the device returns, so the discard changes
nothing visible, and repeating it is harmless.
A balloon inflated before this change is released by the next FITRIM.
https://virtuozzo.atlassian.net/browse/VSTOR-153590
Feature: fs/ext4: fast online shrink support
Signed-off-by: Denis V. Lunev <den@openvz.org>
---
fs/ext4/mballoc.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 98 insertions(+), 1 deletion(-)
diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index aaf2c24c514c..8d205efafac9 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -7009,6 +7009,94 @@ ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
return ret;
}
+static int ext4_trim_balloon_run(struct super_block *sb, ext4_fsblk_t pblk,
+ ext4_fsblk_t len, ext4_fsblk_t start,
+ ext4_fsblk_t end, ext4_fsblk_t minblocks,
+ u64 *trimmed)
+{
+ ext4_fsblk_t last = pblk + len - 1;
+
+ if (!len)
+ return 0;
+ if (pblk < start)
+ pblk = start;
+ if (last > end)
+ last = end;
+ if (last < pblk || last - pblk + 1 < minblocks)
+ return 0;
+
+ len = last - pblk + 1;
+ trace_ext4_discard_blocks(sb, pblk, len);
+ *trimmed += len;
+ return sb_issue_discard(sb, pblk, len, GFP_NOFS, 0);
+}
+
+/*
+ * Blocks of the balloon never hold data, but they are allocated, so
+ * ext4_trim_all_free() never discards them. Discard its unwritten
+ * extents, merging physically adjacent ones, so that the device below
+ * can release the space as it does for free blocks.
+ */
+static int ext4_trim_balloon(struct super_block *sb, ext4_fsblk_t start,
+ ext4_fsblk_t end, ext4_fsblk_t minblocks,
+ u64 *trimmed)
+{
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ ext4_fsblk_t run_pblk = 0, run_len = 0;
+ struct ext4_map_blocks map;
+ struct inode *inode = NULL;
+ ext4_lblk_t lblk, nr_lblks;
+ bool unwritten;
+ int ret = 0, err;
+
+ /* ext4_load_balloon() replaces s_balloon_ino under s_umount */
+ down_read(&sb->s_umount);
+ if (sbi->s_balloon_ino && !sb_rdonly(sb))
+ inode = igrab(sbi->s_balloon_ino);
+ up_read(&sb->s_umount);
+ if (!inode)
+ return 0;
+
+ inode_lock_shared(inode);
+ nr_lblks = min_t(u64, EXT_MAX_BLOCKS,
+ DIV_ROUND_UP(i_size_read(inode), sb->s_blocksize));
+ for (lblk = 0; lblk < nr_lblks; lblk += map.m_len) {
+ if (ext4_trim_interrupted())
+ break;
+ cond_resched();
+
+ map.m_lblk = lblk;
+ map.m_len = nr_lblks - lblk;
+ err = ext4_map_blocks(NULL, inode, &map, 0);
+ if (err < 0) {
+ ret = err;
+ break;
+ }
+ if (!map.m_len)
+ break;
+
+ unwritten = err > 0 && (map.m_flags & EXT4_MAP_UNWRITTEN);
+ if (unwritten && run_len && run_pblk + run_len == map.m_pblk) {
+ run_len += map.m_len;
+ continue;
+ }
+
+ ret = ext4_trim_balloon_run(sb, run_pblk, run_len, start, end,
+ minblocks, trimmed);
+ if (ret)
+ break;
+ run_pblk = map.m_pblk;
+ run_len = unwritten ? map.m_len : 0;
+ }
+ if (!ret && !ext4_trim_interrupted())
+ ret = ext4_trim_balloon_run(sb, run_pblk, run_len, start, end,
+ minblocks, trimmed);
+ inode_unlock_shared(inode);
+ iput(inode);
+
+ return ret;
+}
+
/**
* ext4_trim_fs() -- trim ioctl handle function
* @sb: superblock for filesystem
@@ -7027,7 +7115,8 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
struct ext4_group_info *grp;
ext4_group_t group, first_group, last_group;
ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
- uint64_t start, end, minlen, trimmed = 0;
+ uint64_t start, end, minlen, trimmed = 0, balloon_blks = 0;
+ ext4_fsblk_t last_blk;
ext4_fsblk_t first_data_blk =
le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
@@ -7061,6 +7150,7 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
&first_group, &first_cluster);
ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
&last_group, &last_cluster);
+ last_blk = end;
/* end now represents the last cluster to discard in this group */
end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
@@ -7103,6 +7193,13 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
first_cluster = 0;
}
+ if (!ret && !ext4_trim_interrupted()) {
+ ret = ext4_trim_balloon(sb, start, last_blk,
+ EXT4_C2B(EXT4_SB(sb), minlen),
+ &balloon_blks);
+ trimmed += EXT4_NUM_B2C(EXT4_SB(sb), balloon_blks);
+ }
+
if (!ret)
EXT4_SB(sb)->s_last_trim_minblks = minlen;
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Devel] [PATCH hci-8.1 2/2] xfs: discard the balloon on FITRIM
2026-11-10 21:24 [Devel] [PATCH hci-8.1 0/2] ext4, xfs: discard the balloon on FITRIM Denis V. Lunev
2026-11-10 21:24 ` [Devel] [PATCH hci-8.1 1/2] ext4: " Denis V. Lunev
@ 2026-11-10 21:24 ` Denis V. Lunev
2026-11-10 21:31 ` no-reply
1 sibling, 1 reply; 5+ messages in thread
From: Denis V. Lunev @ 2026-11-10 21:24 UTC (permalink / raw)
To: khorenko; +Cc: devel
From: Denis V. Lunev <den@openvz.org>
The balloon holds file system space that never carries data, but its
blocks are allocated, so the free space walk of xfs_ioc_trim() never
discards them, and inflating the balloon is a plain fallocate() that
does not discard either. Once the balloon is inflated over blocks that
held data, e.g. on an online shrink after files were removed, the
device below keeps them allocated: a ploop or qcow2 image keeps host
disk space the container can never use, and compaction, which is
FITRIM, does not give it back.
Walk the balloon after the free space of the data device and discard
its unwritten extents that fall into the requested range, merging
physically adjacent ones into one discard. The shared IOLOCK keeps
fallocate and truncate away from the balloon meanwhile, so none of its
blocks can be freed and reused under the discard. An unwritten extent
reads as zeroes whatever the device returns, so the discard changes
nothing visible, and repeating it is harmless.
A balloon inflated before this change is released by the next FITRIM.
https://virtuozzo.atlassian.net/browse/VSTOR-153590
Feature: fs/xfs: fast online shrink support
Signed-off-by: Denis V. Lunev <den@openvz.org>
---
fs/xfs/xfs_discard.c | 106 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 106 insertions(+)
diff --git a/fs/xfs/xfs_discard.c b/fs/xfs/xfs_discard.c
index 93943e698eea..a7f1491dc35a 100644
--- a/fs/xfs/xfs_discard.c
+++ b/fs/xfs/xfs_discard.c
@@ -22,6 +22,10 @@
#include "xfs_health.h"
#include "xfs_rtbitmap.h"
#include "xfs_rtgroup.h"
+#include "xfs_bit.h"
+#include "xfs_inode.h"
+#include "xfs_bmap.h"
+#include "xfs_super.h"
/*
* Notes on an efficient, low latency fstrim algorithm
@@ -833,6 +837,102 @@ xfs_trim_rtdev_extents(
# define xfs_trim_rtdev_extents(...) (-EOPNOTSUPP)
#endif /* CONFIG_XFS_RT */
+static int
+xfs_trim_balloon_run(
+ struct xfs_mount *mp,
+ xfs_daddr_t daddr,
+ xfs_daddr_t len,
+ xfs_daddr_t start,
+ xfs_daddr_t end,
+ xfs_extlen_t minlen)
+{
+ xfs_daddr_t last = daddr + len - 1;
+
+ if (!len)
+ return 0;
+ if (daddr < start)
+ daddr = start;
+ if (last > end)
+ last = end;
+ if (last < daddr || last - daddr + 1 < XFS_FSB_TO_BB(mp, minlen))
+ return 0;
+
+ return blkdev_issue_discard(mp->m_ddev_targp->bt_bdev, daddr,
+ last - daddr + 1, GFP_NOFS);
+}
+
+/*
+ * Blocks of the balloon never hold data, but they are allocated, so the
+ * free space btree walk never discards them. Discard its unwritten
+ * extents, merging physically adjacent ones, so that the device below
+ * can release the space as it does for free blocks.
+ */
+static int
+xfs_trim_balloon(
+ struct xfs_mount *mp,
+ xfs_daddr_t start,
+ xfs_daddr_t end,
+ xfs_extlen_t minlen)
+{
+ u64 balloon_ino = READ_ONCE(mp->m_balloon_ino);
+ xfs_daddr_t run_daddr = 0, run_len = 0;
+ struct xfs_bmbt_irec map;
+ struct xfs_inode *ip;
+ xfs_fileoff_t off = 0, end_fsb;
+ unsigned int lock_mode;
+ bool unwritten;
+ int nmaps, error = 0;
+
+ if (!balloon_ino || xfs_is_readonly(mp))
+ return 0;
+ ip = xfs_balloon_get(mp, balloon_ino, 0);
+ if (IS_ERR(ip))
+ return 0;
+ if (XFS_IS_REALTIME_INODE(ip))
+ goto out_rele;
+
+ xfs_ilock(ip, XFS_IOLOCK_SHARED);
+ end_fsb = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
+ while (off < end_fsb) {
+ if (xfs_trim_should_stop())
+ break;
+ cond_resched();
+
+ nmaps = 1;
+ lock_mode = xfs_ilock_data_map_shared(ip);
+ error = xfs_bmapi_read(ip, off, end_fsb - off, &map, &nmaps, 0);
+ xfs_iunlock(ip, lock_mode);
+ if (error || !nmaps || !map.br_blockcount)
+ break;
+ off = map.br_startoff + map.br_blockcount;
+
+ unwritten = xfs_bmap_is_real_extent(&map) &&
+ map.br_state == XFS_EXT_UNWRITTEN;
+ if (unwritten && run_len && run_daddr + run_len ==
+ XFS_FSB_TO_DADDR(mp, map.br_startblock)) {
+ run_len += XFS_FSB_TO_BB(mp, map.br_blockcount);
+ continue;
+ }
+
+ error = xfs_trim_balloon_run(mp, run_daddr, run_len, start,
+ end, minlen);
+ if (error)
+ break;
+ run_len = 0;
+ if (unwritten) {
+ run_daddr = XFS_FSB_TO_DADDR(mp, map.br_startblock);
+ run_len = XFS_FSB_TO_BB(mp, map.br_blockcount);
+ }
+ }
+ if (!error && !xfs_trim_should_stop())
+ error = xfs_trim_balloon_run(mp, run_daddr, run_len, start,
+ end, minlen);
+ xfs_iunlock(ip, XFS_IOLOCK_SHARED);
+out_rele:
+ xfs_irele(ip);
+ return error;
+}
+
/*
* trim a range of the filesystem.
*
@@ -905,6 +1005,12 @@ xfs_ioc_trim(
error = xfs_trim_datadev_extents(mp, start, end, minlen);
if (error)
last_error = error;
+
+ if (!xfs_trim_should_stop()) {
+ error = xfs_trim_balloon(mp, start, end, minlen);
+ if (error)
+ last_error = error;
+ }
}
if (rt_bdev && !xfs_trim_should_stop()) {
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Devel] [PATCH hci-8.1 2/2] xfs: discard the balloon on FITRIM
2026-11-10 21:24 ` [Devel] [PATCH hci-8.1 2/2] xfs: " Denis V. Lunev
@ 2026-11-10 21:31 ` no-reply
0 siblings, 0 replies; 5+ messages in thread
From: no-reply @ 2026-11-10 21:31 UTC (permalink / raw)
To: Denis V. Lunev, khorenko; +Cc: devel
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] xfs: xfs_balloon_get() leaks inode ref on error, now reached by every FITRIM
- [Medium] xfs: xfs_trim_balloon() discards extents sampled unwritten after dropping ILOCK
- [Low] xfs: xfs_trim_balloon() walks only [0, i_size), skipping unwritten extents past EOF
--
commit 53d905c890dd198401bff5f922e47e4a50094d23
Author: Denis V. Lunev <den@openvz.org>
xfs: discard the balloon on FITRIM
This commit makes xfs_ioc_trim() walk the balloon inode after the free
space of the data device. It discards the unwritten extents that fall in
the requested range, merging physically adjacent ones into one discard.
Link: https://virtuozzo.atlassian.net/browse/VSTOR-153590
> diff --git a/fs/xfs/xfs_discard.c b/fs/xfs/xfs_discard.c
> --- a/fs/xfs/xfs_discard.c
> +++ b/fs/xfs/xfs_discard.c
[ ... ]
> +static int
> +xfs_trim_balloon_run(
> + struct xfs_mount *mp,
> + xfs_daddr_t daddr,
> + xfs_daddr_t len,
> + xfs_daddr_t start,
> + xfs_daddr_t end,
> + xfs_extlen_t minlen)
> +{
[ ... ]
> + return blkdev_issue_discard(mp->m_ddev_targp->bt_bdev, daddr,
> + last - daddr + 1, GFP_NOFS);
> +}
[ ... ]
> + ip = xfs_balloon_get(mp, balloon_ino, 0);
> + if (IS_ERR(ip))
> + return 0;
[Severity: Medium]
Can this leak an inode reference on every FITRIM? xfs_balloon_get() calls
xfs_iget() and then returns an error without releasing the inode when the
mode check fails:
fs/xfs/xfs_super.c:xfs_balloon_get() {
...
error = xfs_iget(mp, NULL, balloon_ino, flags, 0, &ip);
if (error)
return ERR_PTR(error);
inode = VFS_I(ip);
if (!S_ISREG(inode->i_mode) || IS_IMMUTABLE(inode))
return ERR_PTR(-EINVAL);
...
}
This problem wasn't introduced by this patch in xfs_balloon_get() itself,
but xfs_trim_balloon() is a new caller that reaches this error path on
every FITRIM. It treats IS_ERR(ip) as nothing to do and returns 0 with no
pointer to release.
If the balloon inode becomes immutable after mount (for example with
chattr +i), each FITRIM would pin one more reference, which could trigger
a busy-inode warning or block a clean unmount. Should xfs_balloon_get()
call xfs_irele(ip) before returning -EINVAL?
> + if (XFS_IS_REALTIME_INODE(ip))
> + goto out_rele;
> +
> + xfs_ilock(ip, XFS_IOLOCK_SHARED);
> + end_fsb = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
[Severity: Low]
Does this walk miss unwritten extents beyond EOF? The loop only covers
[0, i_size):
end_fsb = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
while (off < end_fsb) {
The balloon file is opened through xfs_open_balloon() with the plain
xfs_file_operations. fallocate(FALLOC_FL_KEEP_SIZE) is allowed by
xfs_falloc_newsize() without extending i_size:
fs/xfs/xfs_file.c:xfs_falloc_newsize() {
if ((mode & FALLOC_FL_KEEP_SIZE) || offset + len <= i_size_read(inode))
return 0;
...
}
Unwritten extents allocated past EOF this way would never be examined,
and the free space walk does not cover them either. The commit message
says "A balloon inflated before this change is released by the next
FITRIM." Is that accurate for a balloon with blocks past i_size?
Would walking up to the last mapped extent, or to XFS_MAX_FILEOFF, be
more appropriate than i_size?
> + while (off < end_fsb) {
> + if (xfs_trim_should_stop())
> + break;
> + cond_resched();
> +
> + nmaps = 1;
> + lock_mode = xfs_ilock_data_map_shared(ip);
> + error = xfs_bmapi_read(ip, off, end_fsb - off, &map, &nmaps, 0);
> + xfs_iunlock(ip, lock_mode);
[Severity: Medium]
Can the discard race with a direct I/O write to the balloon? The extent
state is sampled here under ILOCK_SHARED, which is dropped immediately.
The discard is issued later from xfs_trim_balloon_run() at the next
non-adjacent extent or after the loop, with no ILOCK held and no recheck
that the extent is still unwritten.
Only XFS_IOLOCK_SHARED is held across this, and aligned direct I/O
overwrites also take only the shared IOLOCK:
fs/xfs/xfs_file.c:xfs_file_dio_write_aligned() {
unsigned int iolock = XFS_IOLOCK_SHARED;
...
ret = iomap_dio_rw(iocb, from, ops, dops, 0, ac, 0);
}
xfs_open_balloon() hands out the balloon as O_RDWR when mnt_want_write()
succeeds:
fs/xfs/xfs_ioctl.c:xfs_open_balloon() {
ro = mnt_want_write(path.mnt);
if (ro)
mode = O_RDONLY;
else
mode = O_RDWR;
filp = alloc_file(&path, mode, &xfs_file_operations);
}
If a privileged writer issues an aligned O_DIRECT write to an unwritten
balloon extent while xfs_trim_balloon() runs, could its data be discarded
after the extent is converted to written? That would conflict with the
commit message statement "An unwritten extent reads as zeroes whatever the
device returns, so the discard changes nothing visible".
The commit message also says "The shared IOLOCK keeps fallocate and
truncate away from the balloon meanwhile". That holds, since those take
the exclusive IOLOCK, but is it sufficient given that shared-IOLOCK
writers are not excluded?
> + if (error || !nmaps || !map.br_blockcount)
> + break;
[ ... ]
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261110212459.3158424-1-den@openvz.org?part=2
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Devel] [PATCH hci-8.1 1/2] ext4: discard the balloon on FITRIM
2026-11-10 21:24 ` [Devel] [PATCH hci-8.1 1/2] ext4: " Denis V. Lunev
@ 2026-11-10 21:34 ` no-reply
0 siblings, 0 replies; 5+ messages in thread
From: no-reply @ 2026-11-10 21:34 UTC (permalink / raw)
To: Denis V. Lunev, khorenko; +Cc: devel
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] ext4: FITRIM balloon discard races with writeback and shared-lock DIO on unwritten extents
- [Medium] ext4: ext4_trim_balloon() bounds its walk by i_size and misses unwritten extents past EOF
- [Low] ext4: 64-bit DIV_ROUND_UP() on i_size in ext4_trim_balloon() breaks 32-bit builds
- [Low] ext4: FITRIM re-discards and re-counts the whole balloon on every call
--
commit d12d49f2b58d226c5ae16e1ae2d56d927c53b382
Author: Denis V. Lunev <den@openvz.org>
ext4: discard the balloon on FITRIM
This commit makes FITRIM walk the balloon inode after the free space and
discard its unwritten extents inside the requested range. Physically
adjacent extents are merged into one discard. The commit message says the
shared i_rwsem keeps the balloon blocks stable during the discard.
Link: https://virtuozzo.atlassian.net/browse/VSTOR-153590
> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
> --- a/fs/ext4/mballoc.c
> +++ b/fs/ext4/mballoc.c
[ ... ]
> +static int ext4_trim_balloon_run(struct super_block *sb, ext4_fsblk_t pblk,
> + ext4_fsblk_t len, ext4_fsblk_t start,
> + ext4_fsblk_t end, ext4_fsblk_t minblocks,
> + u64 *trimmed)
> +{
> [ ... ]
> + len = last - pblk + 1;
> + trace_ext4_discard_blocks(sb, pblk, len);
> + *trimmed += len;
> + return sb_issue_discard(sb, pblk, len, GFP_NOFS, 0);
> +}
[Severity: Medium]
The commit message says "The shared i_rwsem keeps fallocate, truncate and
EXT4_IOC_MOVE_EXT away from the balloon meanwhile" and "An unwritten
extent reads as zeroes whatever the device returns, so the discard changes
nothing visible".
Can this discard destroy data that is being written into the balloon?
The balloon file is handed to root by ext4_open_balloon() in
fs/ext4/ioctl.c as an O_RDWR file (when mnt_want_write() succeeds) using
ext4_file_operations, so nothing enforces that it never holds data.
Two paths do not appear to be excluded by the shared i_rwsem taken in
ext4_trim_balloon():
buffered write into a fallocated unwritten extent
i_rwsem exclusive, dirty pages left, lock dropped
writeback does not take i_rwsem; conversion happens at I/O end
ext4_trim_balloon()
ext4_map_blocks() still reports EXT4_MAP_UNWRITTEN
sb_issue_discard() <- no filemap_write_and_wait_range()
ext4_dio_write_iter()
inode_lock_shared() for a non-extending overwrite
(see ext4_dio_write_checks())
ext4_trim_balloon()
shared lock taken concurrently, discards blocks being written
In both cases the discard may be issued after or during the data write,
and the extent is then converted to written. Should ext4_trim_balloon()
flush the range first, take i_rwsem exclusive, or should the open path
prevent data writes? As written, the commit message's safety argument
seems incomplete for these cases.
> +static int ext4_trim_balloon(struct super_block *sb, ext4_fsblk_t start,
> + ext4_fsblk_t end, ext4_fsblk_t minblocks,
> + u64 *trimmed)
> +{
> [ ... ]
> + inode_lock_shared(inode);
> + nr_lblks = min_t(u64, EXT_MAX_BLOCKS,
> + DIV_ROUND_UP(i_size_read(inode), sb->s_blocksize));
[Severity: Low]
Does this DIV_ROUND_UP() build on 32-bit targets?
i_size_read() returns a 64-bit loff_t and sb->s_blocksize is unsigned
long, so the plain '/' in DIV_ROUND_UP() should become a libgcc 64-bit
division (__udivdi3 or __aeabi_uldivmod), which the kernel does not link.
Would a shift by sb->s_blocksize_bits (or inode->i_blkbits) with rounding
avoid it?
[Severity: Medium]
Is bounding the walk by i_size correct for the stated goal?
If the balloon has unwritten extents past EOF, for example after
fallocate(FALLOC_FL_KEEP_SIZE) on the fd from EXT4_IOC_OPEN_BALLOON, or
after i_size was lowered without freeing them, then nr_lblks does not
cover them:
nr_lblks = DIV_ROUND_UP(i_size, blocksize)
for (lblk = 0; lblk < nr_lblks; ...) <- never maps blocks past EOF
Those blocks are allocated, so ext4_trim_all_free() skips them as well,
and they are never discarded. I could not confirm which fallocate flags
the userspace balloon tool uses, so this may depend on that. Since
ext4_map_blocks() returns holes in a single call, would walking up to
EXT_MAX_BLOCKS be cheap enough?
> + if (!ret && !ext4_trim_interrupted())
> + ret = ext4_trim_balloon_run(sb, run_pblk, run_len, start, end,
> + minblocks, trimmed);
[ ... ]
> @@ -7103,6 +7193,13 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
> first_cluster = 0;
> }
>
> + if (!ret && !ext4_trim_interrupted()) {
> + ret = ext4_trim_balloon(sb, start, last_blk,
> + EXT4_C2B(EXT4_SB(sb), minlen),
> + &balloon_blks);
> + trimmed += EXT4_NUM_B2C(EXT4_SB(sb), balloon_blks);
> + }
[Severity: Low]
Is it intended that the whole balloon is re-discarded and re-counted on
every FITRIM?
ext4_trim_all_free() skips groups already trimmed via
EXT4_MB_GRP_WAS_TRIMMED and s_last_trim_minblks, but the balloon walk has
no equivalent gate:
FITRIM -> ext4_trim_fs() -> ext4_trim_balloon() -> ext4_trim_balloon_run()
sb_issue_discard() for every unwritten run in range
Each call also adds the full balloon size to trimmed, so range->len (and
the fstrim -v output) reports the balloon as newly trimmed every time.
The commit message says "repeating it is harmless", but this covers only
the data, not the repeated I/O or the reported length.
Also, start and last_blk are block-granular, while the free-space path
converts them to cluster offsets through ext4_get_group_no_and_offset().
On bigalloc, can a FITRIM range that begins or ends mid-cluster produce a
discard that only partly covers a cluster, which the commit message says
it is trying to avoid?
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261110212459.3158424-1-den@openvz.org?part=1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-10 21:35 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-10 21:24 [Devel] [PATCH hci-8.1 0/2] ext4, xfs: discard the balloon on FITRIM Denis V. Lunev
2026-11-10 21:24 ` [Devel] [PATCH hci-8.1 1/2] ext4: " Denis V. Lunev
2026-11-10 21:34 ` no-reply
2026-11-10 21:24 ` [Devel] [PATCH hci-8.1 2/2] xfs: " Denis V. Lunev
2026-11-10 21:31 ` no-reply
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox