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 B3F338017D for ; Tue, 10 Nov 2026 21:26:36 +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 6AALPLG2018793; Wed, 11 Nov 2026 00:25:22 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AALPLG2018793 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=default; t=1794345925; bh=F5T2xC2c/yPwtsK2f6jkqE8J4HFg/4dWex0AoUoLiRE=; h=From:To:Date:In-Reply-To:References:Cc:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe; b=PpDRsRSN7BEch9dj5JrxL0f7M4ASfwXOPXHAkSN1nEefsRpixtPclPQ8saF+MHbKB cwb86dquc9+H3v1dgWid47uEPysqbjPGCzd+pXiGnvZ4xANbiBJe8K2QscM4POrvCL +LF7GvsEZqZmi0vVwhoTi0bbv/SP0whJW2yHXuFPLPJXFAJZMrkGj2r3hvJEmWRis2 xz27H4Rv7mgglgowQLTyoB3QHP8YdZGZ9qSbXD/X/KNrKRgJf0vFUJ6CdHPlO3g0B/ Is3LQap9HyE0GJzRR+lXRa+a6d3LpRI/qlGb4+n9LcPJZT4C0e5BeP5VvtGwwZHBIh WwZKV22B7iMWQ== 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 6AALP5sM018771 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Wed, 11 Nov 2026 00:25:06 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AALP5sM018771 Authentication-Results: mail.openvz.org; dkim=pass (2048-bit key) header.d=openvz.org header.i=@openvz.org header.b="qkBlG5nj" Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4a16dc2323cso19266475e9.3 for ; Tue, 10 Nov 2026 13:25:05 -0800 (PST) X-Gm-Message-State: APuU4Z/EElU5bUjHUE+J8so8n3agWxgSKlYoPCJEzxBgXUYnuoOVzRKy ozYL4SPJ3rPeKQoOWg7+EwdZAr4nMo/OiaBPVnf9IdleNn8dr59e8ZGhjqEzRu32H1l15ppVunZ aCgcoonqzaC64qbJewcMyaz7ht+p4lL+96nHrXBWXD7qWiMy+AKjj8Q== X-Gm-Gg: AYBFou3EREQd56bTPcjfCtryaF10a3g8C8NO8zFKUNI0ioLMHtNL1hFPlravQxLya3h /+mGLTM26j8JQ0D3nhPN33V4PzMLlGu9AqDa4l+D2LxCAfdeVJQxNg7svalJReTJwX50Zu6VkSj 7JreKSVwMPibgIkAAdc1kcNF0OXrdQ/gas6a4KrV+EfEPoFWXzdK1inOCOMNhNU9T9ZYbVcxjqF l6Lz7j8MCMQxToL31aI2scZfUOvIz8EiOP/AKJvF34yhJx5QfO1S2cpMMu31APKCyeEoKwjPlDt kstKpJjBxg== X-Received: by 2002:a05:600c:810c:b0:4a3:59c9:a756 with SMTP id 5b1f17b1804b1-4ba09364b9emr5101855e9.6.1794345905404; Tue, 10 Nov 2026 13:25:05 -0800 (PST) X-Received: by 2002:a05:600c:810c:b0:4a3:59c9:a756 with SMTP id 5b1f17b1804b1-4ba09364b9emr5101675e9.6.1794345904970; Tue, 10 Nov 2026 13:25:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1794345904; cv=none; d=google.com; s=arc-20260327; b=fQC9hFJojFqW1genROQqMfkwZ8k47MYWWo5GrVrPAHl96ayWkDGiKHjbbKH+Y8E2IL OAG+RJBLW88ruDUa8WVZcoP2ECy+0i8fWWJ3aih2sPeHiq48zA7sc7MM3zEtUaQSoLfg Xa0Xn0SGrU+wQ5QWztAoqDtWmXByI2O9Afjr9TsIjzuE5yxNpWO5p/TE+HlGAK5xvJIk kBQwqwsklmDogo6mk2tBO+xlI39eK+GnUXTZTZUYZUzmlh+2wkpI4bmtja9j0wLhdLmc 1BHar4vsxAAUB4nw4cV7FB8J8XktvJlyHiFfcBcPsicWQYBqMzH6hFj49CF53knkKeV8 L2/A== 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=xeBDFNJM+TAkHtzK5YXFtEJ8k5jnq9II76n6RmZBIpg=; fh=ozrYJQ1t3QIP6s65KBq5xgJxiFM2qf+5wwn3ABTusyU=; b=cO5ifCE9HvKe9HChUFlcZpg3wCtflEgLnIYLvl53bNnW4/J6bqvaz0mE58kXMWoi1A WxgfWDxvdXMOdLBpmrFp5qKxVVlvoXTeVjmNmE0tUGgmxJX50bcQ2GdKntoA3YIKjsZ3 tj6lqze1967oZv/oVOLMWpAndjTP2iKOL/Xza8NEiMNR2foRcip559Cd0eiaGWm2c5yo qU/o/oa7i9jpM6tCYMNp0lx6lQth7SkaMtERBx1i/wjgq/IKxc1xh/xAftKvRuQN7UKR wRRN4jtnt8uJKBLz0n0SEjwnfupWX+W4jgUXVxj5opouiMA/c0N7WiY4z/MKSs0f3bfV exHQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@openvz.org header.s=google header.b=qkBlG5nj; spf=softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:4f78:6441:d0f1:22af 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 ffacd0b85a97d-4983431d288sor699362f8f.14.2026.11.10.13.25.04 for (Google Transport Security); Tue, 10 Nov 2026 13:25:04 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:4f78:6441:d0f1:22af as permitted sender) client-ip=2a06:5b06:b600:300:4f78:6441:d0f1:22af; Authentication-Results: mx.google.com; dkim=pass header.i=@openvz.org header.s=google header.b=qkBlG5nj; spf=softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:4f78:6441:d0f1:22af 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=1794345904; x=1794950704; 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=xeBDFNJM+TAkHtzK5YXFtEJ8k5jnq9II76n6RmZBIpg=; b=qkBlG5njOF2fOQyX0RzeID/NZmuxSgZ4okB8m9lGsFmRF/q2MJPov+vqxSfNXE9KRo LZ7Ia2lAlByPt9lkDkGODU/6H3CbeRYhIp6Eldj6ahEMzTXMCG4SZrY9BGOg71/pRDaN CogVHI2/jIOd/DNttXeC1O/pPfs0krZDewd0w8+ff5E8LOsFqW0RutXPM6H6DYbrJ85R PCrl6HbzVWAAvjBrZ+qgJ0pDkfzEiNCVSHu/FBDiprdY+CaewWWGQGfrK/P8XrfOny0R rUDcH8tBx4tx/WprvJAyEYPqBw6FtXaH4L5LPK/cyKN1pNr8W53iINnjppxX89AdXT2N 9tOw== X-Received: by 2002:a05:6000:2906:b0:48f:d7cd:a5af with SMTP id ffacd0b85a97d-499c0a6173bmr432990f8f.0.1794345904276; Tue, 10 Nov 2026 13:25:04 -0800 (PST) Received: from athena.sw.ru ([2a06:5b06:b600:300:4f78:6441:d0f1:22af]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-49835d2c05dsm7051999f8f.25.2026.11.10.13.25.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Nov 2026 13:25:03 -0800 (PST) From: "Denis V. Lunev" To: khorenko@virtuozzo.com Date: Tue, 10 Nov 2026 22:24:58 +0100 Message-ID: <20261110212459.3158424-2-den@openvz.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261110212459.3158424-1-den@openvz.org> References: <20261110212459.3158424-1-den@openvz.org> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.1 1/2] 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. 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 --- 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