From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795774175; b=4hrxX5Gs2VGWgT5J+0S/sfnMABpCbln30zlwGTpRG64O3IeRl87kjOzADWVdSaM4fEam 4cUR6Q2tXCqZ09Vtj9hla9CBkMdltjuMpyxgS2C8mZj8N5mVJbt9vPRs2uHdIkLkAUcap jKHuYUkRM0u0uJfuFFzyPOwaiWKygkWFKZ4ChGhXgnG8OQ/+vCu85sM4JXq4lZYGKt7jP 8ejEcPRYaRfWuVAjoFzYNO6AkQ3v2lQYj5XX8HRmtInVDquVnbkCMlKh2xqs00IlCe0Gv n9f3bTVRJHCG93gwMQCyopaCl4Zi9hGSua9e5BrY32KMiydThP7QSGGLNIp0KMooOUg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795774175; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=PT2edGw1BGuy8Db7HIuBzejTR21TYqKpoKF8KfZWiQo=; b=VMzi4/9IeTOIVPUlcYMn6qTdrOw6fRmdYRIYJjefFll3eeZUDfaJAUoxegfE2RWPjhd1 TggFdVPyJIezxwiz1YQShpRN8o0m4++vsDSlw0dUYZO3eJCZueFjIqPD/inU4OrNHZJtr PxYR8+1Npfdq93dWpa53bIBmjbGhKlKrcalvW4+6ZGpUyvXSx/ZrD2ghgMxIQI0wPXlcG DRENNj/En51YdLLwAFQetqrxQKTRKmk+5Rwc609DQyf1UFLYjCm9RPSQT1rLeLMxieBak E7rvq6+UDoxRiHDFu/4LGAu7t5tBGhrYGfptwIzNITIf8nMVvkLoFEn+OdQirm/dgfw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=google header.b=bwF1a+YE; arc=none smtp.remote-ip=209.85.221.46 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3834606A321 for ; Fri, 27 Nov 2026 10:09:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E3834606A321 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=google header.b=bwF1a+YE Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-4a357b1c649so4556420f8f.3 for ; Fri, 27 Nov 2026 02:09:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1795774173; x=1796378973; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=PT2edGw1BGuy8Db7HIuBzejTR21TYqKpoKF8KfZWiQo=; b=bwF1a+YEEodRJkLfqlE3oxFx1EPVFQVO8IUxkVObcwNs6En182Wl/vYXLklP0K7gfT Mu8Kxn7kJuSlqMcOwXMW44IFaA4TtDdH06pkTNuDCwtmPoacT4NN4psERpK6+saETvpz W9pt/LY5C6PmDa8GdmvPG06lZsMLzAPLThPyl32xxZVdaZOI/NavUosPWDjVpFUbCa5d zRkVLWVSmFMOSuTMAS5O72NCelOmVw/X6ztDyr9f/hOgRxdUWPfT0AE37WmjYMxxFF5x gcHGtGJUy8UPtkj0B5YBkkHncnjlKFyCKf1ClRa9V9t7mJhUYHSwqnoJWwOxdjOVQhNm B8Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795774173; x=1796378973; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=PT2edGw1BGuy8Db7HIuBzejTR21TYqKpoKF8KfZWiQo=; b=ScFqek5TTOP7XSQGloG+aUEvMvMJPTeq/CedwVqMhbPRyuYRMP47jTm3bvSO5PWI8q SR1kJZj/1yZrJILUIRHS0DYlVeIdpAqYjeNM5jV+m+oMLIXauFUDDjOOfZaYyUMS/IDo 1bVHOdegVxjpqG/rh9Lz/p1OZ0b7RlCIAwyR776LLFyPyp6CNuGra9Lrhsh2kpfO5L9Q mZGCq5/AbDTRBwS3JUx6mDhs6nWkvEt5UULAiW1EdKEFiRZEyKmRkoOWhfMJYTagXx6w tyvNrU5fOxO9GSwDbRzMOI6KLDIdot1I7a6ECmtPXXEnJPjahTeu0G4Zsf+PKMeWSZ4+ tUng== X-Forwarded-Encrypted: i=1; AL+hErHrYoG0GD6/HuOiTWiFQ7jRbNTn5Lw5G/OhOZ/WckpJkVCYiqIWnjKhOpm4+BgDFfqAjTrMXZ9/OsP75qY=@vger.kernel.org X-Gm-Message-State: APuU4Z+a7Opa4kOjkNT3NSDZt6h1oPkp5cPpLIXT0LNrRtcLPEw0EJ/1 PruCfRURyYGa6b4+pNhZz/W9jiUdDkZLtpKrPopd9NpX/BB3mdey2E2XPM0bV8fNhw== X-Gm-Gg: AZFdTZfako6xEg/JtmgL9eJXj9rGs+F8M5ZvHP/FbmvF8to4fnEyWO8QzdpTs4NAmhZ QSoS/3xs/8aQpbWu0MZUDhgRWbooD50daktWyvdZJCsRgwc+abQukU9Kj99X4pb+SIQI4rK2GAQ uBvt84dQ/jUmoGVZ5eHe3b/0xM7UElBeGLhxDENCdBIZDzdIWY5DbPp1KyNxd2lc7CeStawX7Sh wq966qRKKOeWG/ydLSSbtxqcRMN/1WAYt4p3D/jF8Q1lR8dPACgm6gplyYKcXXB1zHtPkUmUvN0 xlGqh2FsJzihvkxt8pMjE3P7lZNnDPyElxoZbs9rCU1oRaXjuhhfSeP3dEBbvMcPZx2/7tHOB40 ZHV7LTkfO5yUklGMgCiYbCKfTQ/NisBbwjTGvE59Mz5i9E+GOJFKiiJot/BT3528YO44lNJvbh3 QVL3pl+1sfrV1pZEq2OC+iQ76wJcQHPaTiBWL0z/RyxuiV7KzeUQ5EzOyL/hjIh8cOhlJR+Dhjx 0SJmNwE64A13BavHoS+Qh78tNvz0/S3hqEqymSUrMeAYlrAJreBupeJCzm//zsr49pgpJCGpN73 OZolvfJNaVX8jqfP4inU9fOM+tr9x6xi+MqihgTJ4YvcWDFC1mFFpPiNRtraY/y8W3lXQk9Xv11 OuZ1nrCd+QBR80T6j++N9R/ngyiqCcGXiCw== X-Received: by 2002:a05:6000:4a0b:b0:4a7:4efc:11d0 with SMTP id ffacd0b85a97d-4abaf740155mr43096946f8f.12.1795774173128; Fri, 27 Nov 2026 02:09:33 -0800 (PST) Received: from localhost ([2a07:de40:b240:0:2ad6:ed42:2ad6:ed42]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-4b33a596f9csm22106858f8f.12.2026.11.27.02.09.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Nov 2026 02:09:32 -0800 (PST) From: Wei Gao To: Carlos Maiolino Cc: linux-xfs@vger.kernel.org, "Darrick J . Wong" , Chandan Babu R , linux-kernel@vger.kernel.org, Wei Gao Subject: [PATCH] xfs: clear XFS_EOFBLOCKS_RELEASED on truncate down Date: Fri, 27 Nov 2026 10:09:29 +0000 Message-ID: <20261127100929.32124-1-wegao@suse.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Commit f1204d96450f ("xfs: only free posteof blocks on first close") introduced XFS_EOFBLOCKS_RELEASED to avoid freeing speculative post-EOF preallocations across repeated open/write/close cycles. To reset this flag when a file is truncated down, xfs_file_release() relies on the XFS_ITRUNCATED flag: if (xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED)) { xfs_iflags_clear(ip, XFS_EOFBLOCKS_RELEASED); ... } However, XFS_ITRUNCATED is an ephemeral flush-on-close flag that is cleared as soon as dirty pages are written back to disk by background writeback (xfs_vm_writepages()) or explicit fsync (xfs_file_fsync()). If writeback occurs while the file is being written, or if fsync() is called before close(), XFS_ITRUNCATED is cleared. Consequently, when close() is subsequently called, xfs_iflags_test_and_clear() evaluates to false, XFS_EOFBLOCKS_RELEASED is never cleared, and xfs_free_eofblocks() is skipped. If an open reference (such as a loop device) pins the inode in memory, the post-EOF blocks remain allocated permanently, causing tools and tests (such as LTP swapon04) to observe leaked blocks beyond EOF. The issue can be easily and deterministically reproduced on XFS using fsync(): 1. Create a file, write data, and close it (sets in-core XFS_EOFBLOCKS_RELEASED = 1). 2. Re-open with O_TRUNC (sets XFS_ITRUNCATED = 1) and write 16 MiB sequentially (allocates 32 MiB / 65408 blocks due to speculative preallocation). 3. Call fsync(fd) before close(): xfs_file_fsync() clears XFS_ITRUNCATED. 4. Call close(fd): xfs_file_release() sees XFS_ITRUNCATED is 0, leaves XFS_EOFBLOCKS_RELEASED set, and skips xfs_free_eofblocks(). 5. stat() reports st_blocks = 65408 (32 MiB) instead of 32768 (16 MiB). Any subsequent swapon discard punches only the 16 MiB swap area, leaving 32648 blocks and failing discard assertions. Fix this by clearing XFS_EOFBLOCKS_RELEASED directly in xfs_vn_setattr_size() when the file is truncated down, ensuring that subsequent close() treats the file as truncated regardless of whether fsync() or writeback cleared XFS_ITRUNCATED in the meantime. Fixes: f1204d96450f ("xfs: only free posteof blocks on first close") Signed-off-by: Wei Gao --- fs/xfs/xfs_iops.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/xfs/xfs_iops.c b/fs/xfs/xfs_iops.c index e04b7d9d212d..b216b96d05a4 100644 --- a/fs/xfs/xfs_iops.c +++ b/fs/xfs/xfs_iops.c @@ -1124,6 +1124,7 @@ xfs_vn_setattr_size( xfs_iflags_set(ip, XFS_ITRUNCATED); /* A truncate down always removes post-EOF blocks. */ + xfs_iflags_clear(ip, XFS_EOFBLOCKS_RELEASED); xfs_inode_clear_eofblocks_tag(ip); } -- 2.55.0