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 D1FF98017D for ; Tue, 10 Nov 2026 22:29:30 +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 6AAMSEfm019094; Wed, 11 Nov 2026 01:28:15 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AAMSEfm019094 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=default; t=1794349697; bh=Adw0jI5Phuc8YMhYbkDuYDOPW6PSGM/FyeKXk1okZ6k=; h=From:To:Date:In-Reply-To:References:Cc:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe; b=jxhR/TokRfdsUp04AbPb4/hSbDGUZBa+UmXIq2wE0wIR7YjaOrs7s4MES/i5wQmaB vDqgcWWfuyzKYIrchJBVKrD6chKBSyA3p2XUGatZUBe8StlVOFOwr+NDwgdTMIq5pl EHtRj+ljyKlsr4yhngwPOBJjXF4TSDVOPGvt+MxR4Pi8ZcJ1CCJXpP0ooonsllF1p/ 7S7/1sL+EDO7GLFxoC4OxBeam6lLKKmP1b2pq7NtYpeVmb3+SgQGcaXRmUv8p/q6Ke MpjAB9LnPflXfZ2mUCMtDIdnoNRbs6lHcD2rN8Z/vYZ55xoK38SuhFaNseFS/iKxPy QpJp/XpmwRldQ== Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AAMS2Qb019065 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Wed, 11 Nov 2026 01:28:03 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AAMS2Qb019065 Authentication-Results: mail.openvz.org; dkim=pass (2048-bit key) header.d=openvz.org header.i=@openvz.org header.b="a1yFYnds" Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4a0284121c6so19319585e9.3 for ; Tue, 10 Nov 2026 14:28:03 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErGuOqGOeGinbyTeu2bCA+hXzgf8Oo9JUEKZPAYtoe9rApqxcM/IC7XaafJV4XE1r25QGHURpA==@openvz.org X-Gm-Message-State: APuU4Z+UO8Shwvv2oVqrY8NX12KJA2owRaDZS6ERNSYAtZCdl1zMictm v7uyHSGMua1HMjZ854Ig1t32z3M5FR760FDB5ifBL3nmG9IuY3EjqALOqOifRYSR4z0ZXu0jB4U xvOYuGApNdyCyIS6jouT2obnK4dgmlOHeWmb+hvaUiezOr1pbWLBlFA== X-Gm-Gg: AYBFou2pwhtaDtu9OXzemgxdmZ3g9FzxYI9tF143UOs56K+eXfMAqkd/J1LHe8DO9Ic 5ZFJPvg81Jy98JXQtKoBw4edhfL068UaXLc8VFDA3oKc/4EvBQRyYjwITJh9gMP3emUlwtt4Hfe 52Ll7KuYC0NM7emBt2VbFWPPGfL3Up60Z4sXDykBPM7+LyEDi3WtaFdL+gbMEQY01e3BI6MoZSI zkqcvEbTAYoPCBIXYoMTZghRqLbmkxLfU4AixJGuagTVOhc2nF/Gku0ntMPXmU0/3XUSZT0KDfx IUT3q+VTHg== X-Received: by 2002:a05:600c:540f:b0:4a1:e6c5:e44c with SMTP id 5b1f17b1804b1-4ba09379c0bmr6266165e9.10.1794349682661; Tue, 10 Nov 2026 14:28:02 -0800 (PST) X-Received: by 2002:a05:600c:540f:b0:4a1:e6c5:e44c with SMTP id 5b1f17b1804b1-4ba09379c0bmr6266105e9.10.1794349682274; Tue, 10 Nov 2026 14:28:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1794349682; cv=none; d=google.com; s=arc-20260327; b=aircULdgKqKR7sByTdQVFBoeTW0g0f+dNbY9vCsn6MNUzQojYyTMowA1KZDLtyCosz 8AyDMVSPWiG1yf7TBQNpheyI5SQk0d/FxWqtjiuAas2OU0i6OHGKOkKZ5ZollsiVp4C9 weGUgI4i7vbBLiKBtFDYUm6tKC0sGPHi8vxhOFmWLBdQH+q2mOPJuOtZXUvVPSZimOM+ ENr+8AOhW+CcseTmSw6mSyga89svgZLZdcRty46ZfNaaqCFkkTQiowCxk/KVo6Tfz8qk HaW+M3ie2U5eakDmgqrbxdAG1BzGU+/gN6YyQiWZdZC9cavkhBAi5BwaEeklUmCpxtLe Egjw== 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=gccV3DzteTDiND6Xw1Q1JjY3qOyPz/y/JoTjVJY7O6Y=; fh=T82PFgL+khqX5QCD3bmPtpLKd8azYE0lek2PZj7msos=; b=dQ7dNRi7NfT8guZYo1XsmneGpxEkAtmgLL5Oms/xST2CkemnCZ5iIRV+VI5WJdXeo2 0UFnFHS6jAq/7WIFW7ZGwqjVLEkSidPLpzYKkTGP9k/MpWYBh+1R+uTvGHc0BZaDYLJB xKyBiUJw6n7N7JWIDo3SzRS38zE+OhrMNgUUzI4j62vlGs3NL6PYoPIXqD4n0xWIemLg 2qm3pUrBABND/DUTXg/2ZP3qxWYjWgSGLyrCJocuTqo6DOb87iFautPym06rjWnf93vv 7G72l++EB2UbrmrMAEOaPI6JcBlAZFV8qXl5ULXeJBpksW2ueLjCXjifMAxBip63tCux o9Rw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@openvz.org header.s=google header.b=a1yFYnds; 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 5b1f17b1804b1-4ba01ff0431sor10991815e9.11.2026.11.10.14.28.02 for (Google Transport Security); Tue, 10 Nov 2026 14:28:02 -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=a1yFYnds; 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=1794349682; x=1794954482; 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=gccV3DzteTDiND6Xw1Q1JjY3qOyPz/y/JoTjVJY7O6Y=; b=a1yFYndssJxEG7F7BJDTgHNP+LsSE44PTOEE+kIHICJ9i4iktYZF7kmse8C68hljOY AxntE/7a7eEOlzQ9fbau1M/hAr/qI2ncTlhP1w/k8nf4yi4LfCaOptscC9/G0kGbNRtM wZylhZv2nVrqXVIfd3VUnga8khptmCY9YPqSUT6GTlO8z6LRrvw4OzxNP98gYNoHQoeK MmiVW6/aMrHIJuPbZEtP6X1wdQWj6iKWWWy7C5h9GfSJsUwTETv4zugke7weoCYo4kKQ 4YwEfOtvLj3G4CYDHMFS0vPsqRKsl6Po3nE85CvkpzDPGaQdK9eFH3Ec8Kw02rFOnXSK rlmw== X-Forwarded-Encrypted: i=1; AL+hErHHye/uGonEG5DxYR26dSjyPSeHqyjOOKqeevP1TJUank00g3X+R/Q6pU1K3RWa0xRhERc4GA==@openvz.org X-Received: by 2002:a05:600c:1e04:b0:4a1:9663:7639 with SMTP id 5b1f17b1804b1-4ba093b8200mr6832745e9.34.1794349681703; Tue, 10 Nov 2026 14:28:01 -0800 (PST) Received: from athena.sw.ru ([2a06:5b06:b600:300:4f78:6441:d0f1:22af]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4ba09536ebfsm8923155e9.5.2026.11.10.14.28.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Nov 2026 14:28:01 -0800 (PST) From: "Denis V. Lunev" To: khorenko@virtuozzo.com Date: Tue, 10 Nov 2026 23:27:55 +0100 Message-ID: <20261110222756.3236854-4-den@openvz.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261110222756.3236854-1-den@openvz.org> References: <20261110222756.3236854-1-den@openvz.org> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.1 v2 3/3] xfs: 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 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 walk goes to the end of the file offset space, as fallocate() with FALLOC_FL_KEEP_SIZE can leave extents past i_size. A merged run is discarded in chunks, with a check for fatal signals and freezing between them. The walk holds the IOLOCK and the MMAPLOCK exclusively, after waiting for direct I/O and flushing the page cache of the balloon. 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 XFS_IOC_OPEN_BALLOON, which takes CAP_SYS_ADMIN, so the locks only wait for ploop inflating or shrinking it. Every FITRIM discards the balloon again, which costs little: dm-ploop completes the discard of an unmapped cluster at once. A discard error or a failure to read the balloon inode fails FITRIM; a balloon that is no longer a regular mutable file is skipped. 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 --- fs/xfs/xfs_discard.c | 144 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/fs/xfs/xfs_discard.c b/fs/xfs/xfs_discard.c index 93943e698eea..b9b5a28b0d96 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,140 @@ xfs_trim_rtdev_extents( # define xfs_trim_rtdev_extents(...) (-EOPNOTSUPP) #endif /* CONFIG_XFS_RT */ +#define XFS_BALLOON_DISCARD_CHUNK_BB ((128U << 20) >> BBSHIFT) + +static int +xfs_trim_balloon_run( + struct xfs_mount *mp, + xfs_daddr_t daddr, + uint64_t len, + xfs_daddr_t start, + xfs_daddr_t end, + xfs_extlen_t minlen) +{ + struct block_device *bdev = mp->m_ddev_targp->bt_bdev; + unsigned int gran = max(1U, + bdev_discard_granularity(bdev) >> BBSHIFT); + unsigned int chunk = roundup(XFS_BALLOON_DISCARD_CHUNK_BB, gran); + xfs_daddr_t last = daddr + len - 1, next; + int error; + + 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; + + /* + * Chunks keep a long run interruptible. They end on multiples of the + * discard granularity, so no cluster below is split between two. + */ + while (daddr <= last) { + u64 rem = daddr; + + next = daddr + chunk - do_div(rem, chunk); + if (next > last + 1) + next = last + 1; + error = blkdev_issue_discard(bdev, daddr, next - daddr, + GFP_KERNEL); + if (error) + return error; + daddr = next; + if (xfs_trim_should_stop()) + break; + cond_resched(); + } + + return 0; +} + +/* + * 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; + uint64_t run_len = 0; + struct xfs_bmbt_irec map; + struct xfs_inode *ip; + xfs_fileoff_t off = 0; + unsigned int lock_mode; + bool unwritten; + int nmaps, error; + + if (!balloon_ino) + return 0; + ip = xfs_balloon_get(mp, balloon_ino, 0); + if (IS_ERR(ip)) + return PTR_ERR(ip) == -EINVAL ? 0 : PTR_ERR(ip); + error = 0; + if (XFS_IS_REALTIME_INODE(ip)) + goto out_rele; + + /* + * Nobody may write the balloon meanwhile: data written into an + * unwritten extent would be discarded with it. + */ + xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL); + inode_dio_wait(VFS_I(ip)); + error = filemap_write_and_wait(VFS_I(ip)->i_mapping); + if (error) + goto out_unlock; + + while (off < XFS_MAX_FILEOFF) { + if (xfs_trim_should_stop()) + break; + cond_resched(); + + nmaps = 1; + lock_mode = xfs_ilock_data_map_shared(ip); + error = xfs_bmapi_read(ip, off, XFS_MAX_FILEOFF - 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); +out_unlock: + xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL); +out_rele: + xfs_irele(ip); + return error; +} + /* * trim a range of the filesystem. * @@ -905,6 +1043,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