From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (mail.openvz.org [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id 0548D803BB for ; Wed, 11 Nov 2026 11:33:20 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6ABBW4MI027675; Wed, 11 Nov 2026 14:32:05 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6ABBW4MI027675 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=default; t=1794396727; bh=yoVJk6cVajAO+qogGUJ8EhiYR/7FGtkGC51wRLlrKeU=; h=From:To:Date:In-Reply-To:References:Cc:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe; b=Wt1uj+ZivSkqT6Y+6FlQBkOHiz2LVxqTYB/iFNfWiWjzu3tHBKCQEAnQlKRSL8Emn rKuOfciyBXKzuHm+eff9dhgIVSOQQsPtQv62MkX48J5YXDa8B6n1PVb97/gd3fx/YZ eJGYX5QsDJvwE7rYNuj8aS++qwwJ1ZzdWpBuS6WdYhzXMcYwvUHD1HA8JTuYLmP2A3 9xKjsweXidooSW5mjPF0NypsTOiIJBhJS20fjvts9sMcESk3N5MFT/98GBQfRgTwiT /B/l6DQHiNfbiJ45zvjfKOQyMhcHs7j50G3i/Dg9MLcZFVTJK/knDDB81gZwvAbcq2 jvoNtIFECnr2w== Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6ABBVhkm027635 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Wed, 11 Nov 2026 14:31:43 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6ABBVhkm027635 Authentication-Results: mail.openvz.org; dkim=pass (2048-bit key) header.d=openvz.org header.i=@openvz.org header.b="MSBbe03h" Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4a2065a2ef9so24265815e9.3 for ; Wed, 11 Nov 2026 03:31:43 -0800 (PST) X-Gm-Message-State: APuU4Z+FcID/SLfIJAuErJ7gOfd/q4UJWwPOjqYeo1WLT/bWcRTRj0yR UalQgU7gYXz7tGG/GZQ3JnweJWboszZhDTX05b/S83n1yBU5USp1fP5ZAkOPFwvbNNTkQfCOuUE YdHiVepbL+W6KvknocVN6afG85liX/npI9JWbrzKUEiEhpFDNRrtj/w== X-Gm-Gg: AYBFou0TRNjTqfLSo9P/3rfENoDRG0kIYX/3qblvGrx+zy8z2BiZxUyCNkQxaICgG31 qvDFvFr6tDNpiEjlpDBbyreexuaR6OwGPRba900Pm2Qu/ayn4seTHG4G4mEHc/xxLTDm72NiAYk fkfDM52CdJjGgw6DLXr9QqrPqtrTwqKKMQLR9/8tzQECI0lam/oLBIXjaUep5NF9u+xAZdzrLiY PZN3Si358Q3xRWTBBWSjJ6eWnaFej/pSlB9cMDQCUr55Z+FWjxoiXQrEElrAdjKZ2eE9327ZHUG e8s3sZvU2Q== X-Received: by 2002:a05:600c:4f12:b0:4a3:59c9:a747 with SMTP id 5b1f17b1804b1-4ba09376ea6mr42821905e9.17.1794396702792; Wed, 11 Nov 2026 03:31:42 -0800 (PST) X-Received: by 2002:a05:600c:4f12:b0:4a3:59c9:a747 with SMTP id 5b1f17b1804b1-4ba09376ea6mr42821415e9.17.1794396702367; Wed, 11 Nov 2026 03:31:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1794396702; cv=none; d=google.com; s=arc-20260327; b=ChwVlD4/ni0JCiU6QklAkk4CCNBhP8b/uD+8pMwO6HXZ/fs928oNiWnB19vKJ2nMWp 61C/9g7BcX+SnzsUUQw3FIQovpmns43RuV2yzSXCwYyPc9SnL4t0p8whC9b9Pxkyzx64 AtMJn81kNJ/c6dJNIJqgO94Nx0B7bi5+lTCMav52XQCyBhuQoNN7MHJHuVmRiibaEorR q7t+bcxdJFYu7enHCPGdwMnBZYmWon45vroHOMYGcbng+xVp0+FqzglgeUrjJmMbLguL kkmHGWt72Zna76CHGllbqmGA4OttO4FRnbTQ9jZdmPOMIdUEx9mheUfK9v+RMQ7I+qE4 0whg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=zFJLomqExmU5UOpzUhs8ekrDG0B/koJI7mgXOXEaCmY=; fh=WpT6aB7RBAGHZeWZzqAr71yzRFbPTYkPWKsRagLn48Q=; b=Y79ltHNdNTnZeVxnRHVhiAi4+nybnNZiunt/m1AocqqvExW0BroQ8iMgv11TkyYaAp 7XICgnma9uN4BRUFFRf2OJfqp7urTfRfIug1ZuoyYmiIjkfY4uiYyURC/9JDLkkQAhhc atOAb9Aaj04ERFT+JtxEgDmaLLvGBRsLbvZs1Kis53gX903NJk36z0LUmQi9uwXq6PMM W+MFAHsBbmJcFvcKDpd2qUMvmDX4SzoJBzQJ0bG56c4J9oVNGQlUVHa+1YbZlOYSBvdD LVcO7VtaJWtWuWhjCO8WsB7edTpHrtFtMbYfxB5CXVdWl9+BaT5CTgokiVdeg9MoJ/L/ sV4A==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@openvz.org header.s=google header.b=MSBbe03h; spf=softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:c3:490a:ca77:c5a4 as permitted sender) smtp.mailfrom=den@openvz.org; dara=neutral header.i=@openvz.org Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id 5b1f17b1804b1-4ba01feb0c5sor10682575e9.4.2026.11.11.03.31.42 for (Google Transport Security); Wed, 11 Nov 2026 03:31:42 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:c3:490a:ca77:c5a4 as permitted sender) client-ip=2a06:5b06:b600:300:c3:490a:ca77:c5a4; Authentication-Results: mx.google.com; dkim=pass header.i=@openvz.org header.s=google header.b=MSBbe03h; spf=softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:c3:490a:ca77:c5a4 as permitted sender) smtp.mailfrom=den@openvz.org; dara=neutral header.i=@openvz.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=google; t=1794396702; x=1795001502; darn=openvz.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=zFJLomqExmU5UOpzUhs8ekrDG0B/koJI7mgXOXEaCmY=; b=MSBbe03h557FhOtE+dMYh3rvtNsEoT+KuKOZ/up5hbP7ETgI1HY7NLeuNeBDjVada8 LuCzHIkAzbprB1DrbJ4GDMq81jsv83A1qBh7f1WTKfwTyZRf47icEUOTBEoAW+GqwnC+ W09rD1id4y617zEYeP5Vf9UGVXvK3vC0xbFFb9HnneU+/AOwtbEKLnEhyBq96CDQqIs4 ogQWbg8MJ7cBxb+ji4J58XmK/elB4GdIflMo6a3x01lzg/SIpvV+p7qUk1S5BVtysoCG RLn1OHqxphuqFKq3ID0z5Kd+hg1gfEMPD5/lkL3EpjggtQphIeImWiL1b/wx9RiOl28E Extg== X-Received: by 2002:a05:600c:3554:b0:4a4:4d57:8487 with SMTP id 5b1f17b1804b1-4ba0936c8admr36248565e9.5.1794396701522; Wed, 11 Nov 2026 03:31:41 -0800 (PST) Received: from athena.sw.ru ([2a06:5b06:b600:300:c3:490a:ca77:c5a4]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4ba08ddbe68sm88679885e9.0.2026.11.11.03.31.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Nov 2026 03:31:41 -0800 (PST) From: "Denis V. Lunev" To: khorenko@virtuozzo.com Date: Wed, 11 Nov 2026 12:31:34 +0100 Message-ID: <20261111113135.4023555-5-den@openvz.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261111113135.4023555-1-den@openvz.org> References: <20261111113135.4023555-1-den@openvz.org> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.1 v3 4/5] ext4: discard the balloon on FITRIM X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org From: Denis V. Lunev 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. The walk goes to the end of the extent tree, as fallocate() with FALLOC_FL_KEEP_SIZE can leave extents past i_size. Physically adjacent extents are merged into one discard, otherwise every cut at the unwritten extent length limit leaves a partly covered cluster of the image below. A merged run is discarded in chunks, with a check for fatal signals and freezing between them. The walk holds i_rwsem and the invalidate lock exclusively, after waiting for direct I/O and flushing the page cache of the balloon, as punch hole does. No block of the balloon can be freed and reused under the discard, and nothing is written to it meanwhile, so an extent that is still unwritten reads as zeroes before and after the discard. The balloon is hidden from lookup and opened only through EXT4_IOC_OPEN_BALLOON, which takes CAP_SYS_ADMIN, so the lock only waits for ploop inflating or shrinking it. An empty balloon is skipped before taking the locks. Any other one is walked whole on every FITRIM, whatever range it asks for, as its extent tree is indexed by file offset; FITRIM is rare enough for that. Discarding the balloon again each time is cheap: dm-ploop completes the discard of an unmapped cluster at once. The balloon is not counted in the trimmed length reported to userspace, and a discard error fails FITRIM. 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 --- Notes: The balloon is locked, flushed and walked whole on every FITRIM, whatever the range. This is deliberate: ploop runs FITRIM on compaction, about once a day. Measured on a 4-CPU VM with a balloon of 84-99G over all the free space of a 100G image: a FITRIM after the first one spends 0.3-0.7s in the balloon, on ploop and qcow2, ext4 and xfs, for a contiguous balloon and for one in up to about 10000 pieces. The time goes mostly into discarding clusters that are already unmapped and grows with the size of the balloon and the number of its pieces. The locks held meanwhile only keep ploop from resizing the balloon. Narrowing the walk to the range would need a reverse map from blocks to file offsets. An empty balloon, the common case, is skipped before the locks are taken. fs/ext4/mballoc.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index aaf2c24c514c..4906f49353bf 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -7009,6 +7009,126 @@ ext4_trim_all_free(struct super_block *sb, ext4_group_t group, return ret; } +#define EXT4_BALLOON_DISCARD_CHUNK (128U << 20) + +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) +{ + unsigned int gran = max_t(unsigned int, 1, + bdev_discard_granularity(sb->s_bdev) >> sb->s_blocksize_bits); + unsigned int chunk = roundup(EXT4_BALLOON_DISCARD_CHUNK >> + sb->s_blocksize_bits, gran); + ext4_fsblk_t last = pblk + len - 1, next; + int ret; + + if (!len) + return 0; + if (pblk < start) + pblk = start; + if (last > end) + last = end; + if (last < pblk || last - pblk + 1 < minblocks) + return 0; + + /* + * Chunks keep a long run interruptible. They end on multiples of the + * discard granularity, so no cluster below is split between two. + */ + while (pblk <= last) { + u64 rem = pblk; + + next = pblk + chunk - do_div(rem, chunk); + if (next > last + 1) + next = last + 1; + trace_ext4_discard_blocks(sb, pblk, next - pblk); + ret = sb_issue_discard(sb, pblk, next - pblk, GFP_NOFS, 0); + if (ret) + return ret; + pblk = next; + if (ext4_trim_interrupted()) + break; + cond_resched(); + } + + return 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) +{ + 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; + bool unwritten; + int ret, err; + + /* ext4_load_balloon() replaces s_balloon_ino under s_umount */ + down_read(&sb->s_umount); + if (sbi->s_balloon_ino && sbi->s_balloon_ino->i_blocks) + inode = igrab(sbi->s_balloon_ino); + up_read(&sb->s_umount); + if (!inode) + return 0; + + /* + * Nobody may write the balloon meanwhile: data written into an + * unwritten extent would be discarded with it. + */ + inode_lock(inode); + inode_dio_wait(inode); + filemap_invalidate_lock(inode->i_mapping); + ret = filemap_write_and_wait(inode->i_mapping); + if (ret) + goto out; + + for (lblk = 0; lblk < EXT_MAX_BLOCKS; lblk += map.m_len) { + if (ext4_trim_interrupted()) + break; + cond_resched(); + + map.m_lblk = lblk; + map.m_len = EXT_MAX_BLOCKS - 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); + 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); +out: + filemap_invalidate_unlock(inode->i_mapping); + inode_unlock(inode); + iput(inode); + + return ret; +} + /** * ext4_trim_fs() -- trim ioctl handle function * @sb: superblock for filesystem @@ -7028,6 +7148,7 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) ext4_group_t group, first_group, last_group; ext4_grpblk_t cnt = 0, first_cluster, last_cluster; uint64_t start, end, minlen, trimmed = 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 +7182,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 +7225,10 @@ 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)); + 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