From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=109.224.244.108 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796227361; b=NUAAP41HqRrrCTcjCO23QVW1qtT3hByRK9TrWzTlxWBk9W0q4z8spooYJPTYftHVLtzX ajYgNR0V4RjSkvE81TFHUOsmBdz5aGkdjLvDGUoFfY7GaKnjZ9eGlEjFg69ctADLQiB/Z cR6vN+llbQnVdmxEdrt6sV3qfNpEUIMgGvcTCDuNR1Nb0hltZfzfGMgOVzmRb3CeS+ieV rXocuvXr814gGIxxwkNwGpXuyZYZJ15Rgofu9Qp6yM7ya1eZpROF7k+oBcEzMaZCZH9ik rtkQxjx8fugwv0xolXabZas75tY/pIPJRIf+1o0DpXQcJy2B0a6zX51XEKYTWLrHoRg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796227361; h=DMARC-Filter:DKIM-Signature:X-Pm-Submission-Id:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=Kv0rnYc+/NBPn4UV5hGwdT+uQ/hrvmhe5Dm5EyII3Wk=; b=dzUOiCyy/VCcUONccSHcJ/SxEABGSmxlHsC/H02+NCWfsFZXeHghPIja9EuYad6vAUIx 8oKXggBG8ckIEtcosYFQeSA59zFjOUED3MS0y1g/d+hz5+IMSR9PrxDp08tFNxL1M3C5l n7tzRiB0scuYT4VG5Jz0IWrJS/85S/KkEi7QtuLfB+1dyWH3tzCRm/3H7xO8xXnfVlw7o dZbL+Vmd9hbc7SZdgBgjOL6pQse1D2berVNwVbe+5qPmvcuixPSkwj13KUiVRS6XEuwMW 3PRBZoKSCak97qzFMSqqK0AcV1n7rYlHB4oca8RdNKH6tZvIXzhOMUEeEU03yWqBxkA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=raidlab.org; spf=pass smtp.mailfrom=raidlab.org; dkim=pass header.d=raidlab.org header.i=@raidlab.org header.a=rsa-sha256 header.s=protonmail header.b=I+avIkDl; arc=none smtp.remote-ip=109.224.244.108 Received: from mail-244108.protonmail.ch (mail-244108.protonmail.ch [109.224.244.108]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D56BC606A31A for ; Wed, 2 Dec 2026 16:02:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D56BC606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=raidlab.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=raidlab.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=raidlab.org header.i=@raidlab.org header.a=rsa-sha256 header.s=protonmail header.b=I+avIkDl DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raidlab.org; s=protonmail; t=1796227356; x=1796486556; bh=Kv0rnYc+/NBPn4UV5hGwdT+uQ/hrvmhe5Dm5EyII3Wk=; h=From:To:Cc:Subject:Date:Message-ID:From:To:Cc:Date:Subject: Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=I+avIkDlLngHbxMKuKSPOt8H/JD6D/o098Hw8rjPXC1bd1B+v7PdrB5zkqwKArWsf h6R6rkGvEN53XF9FOr7cEUf4Bf2QSvRNCst/k+zHP8caDt7VnjTZzkBzyQKJRVHt34 1sJCM0WDW24MbXzYMb8NXp/fCXnCYasi/4j8uVkdCqlIoHdqKcOhXUlLhatFPYV7FX IFeGDwIQZLSSErZXSM8DQKP3WP8Ijh+zNPBUWNFygCUEJUX8tpR3FgS4OtR8QPjL8j 0HOhzZoOFybNgxb5X/AUMQscN283jCFyOIA3P+0ij8nOWf0KXw9KPDYDq2C/8w9wle eajlWlnvdXCdA== X-Pm-Submission-Id: 4jfjFg4F3Yz2Scdf From: Miroslav Cimerman To: linux-btrfs@vger.kernel.org Cc: David Sterba , Chris Mason Subject: [PATCH] btrfs: fix fsync returning before the log is committed Date: Wed, 2 Dec 2026 16:02:02 +0000 Message-ID: <20261202160202.398649-1-mc@raidlab.org> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-btrfs@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 btrfs_inode_in_log() lets fsync return 0 without syncing the log if the inode was logged after its last change and that log is committed: last_sub_trans <= inode->last_log_commit last_sub_trans <= root->last_log_commit However, last_sub_trans is the log transaction of the inode's last change, not the one the inode was logged into. The two differ when an fsync of another inode commits the log in between. The second check then passes while the log transaction that holds the inode is still being committed: inode F changed in log transaction 0 fsync of another inode commits log 0, root->last_log_commit = 0 fsync A of F fsync B of F btrfs_log_inode() log F into log transaction 1 inode->last_log_commit = 0 btrfs_inode_unlock() btrfs_sync_log() btrfs_write_marked_extents() btrfs_sync_file() skip_inode_logging() btrfs_inode_in_log() == true return 0 write_all_supers() root->last_log_commit = 1 The same happens inside log transaction 0 without another fsync. btrfs_add_log_tree() sets root->last_log_commit to 0 before log transaction 0 is committed, so an inode changed and logged in it passes both checks as soon as it is logged. A power failure before fsync A's log commit completes loses the data that fsync B reported as durable. Start root->last_log_commit at -1, as log_transid_committed already does, and when btrfs_log_inode() logs an inode, set last_sub_trans to the log transaction it was logged into. A later change is still caught. btrfs_set_inode_last_trans() lowers last_log_commit when the inode item is updated, and a write that is not complete yet is in the ordered extent list of the next fsync. With the -1 start, root->last_log_commit is always below the log transaction being committed, so the ASSERT in btrfs_sync_log() can use <. Fixes: 257c62e1bce0 ("Btrfs: avoid tree log commit when there are no changes") CC: stable@vger.kernel.org Signed-off-by: Miroslav Cimerman --- Found with FuzzyRAID, a fault-injection tester for RAID. Reproduced on for-next with a test-only delay at the start of btrfs_sync_log(): after a fresh mount, a direct write, fsync A of the file (delayed), then fsync B of the same file, then a power failure right after fsync B returned (dm-flakey dropping writes). Also with an fsync of another file between the write and fsync A. The write is lost on stock and kept with the fix. Backport note: the btrfs_log_inode() hunk needs the logging_new_name check from bfe3d755ef7c ("btrfs: do not update last_log_commit when logging inode due to a new name"). Without it, inodes logged under btrfs_log_new_name() get ctx->log_transid 0. This patch was prepared with help from an LLM. fs/btrfs/btrfs_inode.h | 2 +- fs/btrfs/disk-io.c | 2 +- fs/btrfs/tree-log.c | 9 +++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h index c9893a1e433b..289d29a25915 100644 --- a/fs/btrfs/btrfs_inode.h +++ b/fs/btrfs/btrfs_inode.h @@ -215,7 +215,7 @@ struct btrfs_inode { u64 logged_trans; /* - * Log transaction ID when this inode was last modified. + * Log transaction ID when this inode was last modified or logged. * Protected by 'lock'. */ int last_sub_trans; diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index fde9f4c4ea11..0b9868b3f60b 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -991,7 +991,7 @@ int btrfs_add_log_tree(struct btrfs_trans_handle *trans, root->log_root = log_root; btrfs_set_root_log_transid(root, 0); root->log_transid_committed = -1; - btrfs_set_root_last_log_commit(root, 0); + btrfs_set_root_last_log_commit(root, -1); return 0; } diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 2a014087dd83..4c7cd9059ee8 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -3575,10 +3575,9 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans, * root->log_commit[index1] to false and any task attempting to sync the * log must wait for the previous log transaction to commit if it's * still in progress or wait for the current log transaction commit if - * someone else already started it. We use <= and not < because the - * first log transaction has an ID of 0. + * someone else already started it. */ - ASSERT(btrfs_get_root_last_log_commit(root) <= log_transid, + ASSERT(btrfs_get_root_last_log_commit(root) < log_transid, "last_log_commit(root)=%d log_transid=%d", btrfs_get_root_last_log_commit(root), log_transid); btrfs_set_root_last_log_commit(root, log_transid); @@ -7271,8 +7270,10 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans, * a power failure unless the log was synced as part of an fsync * against any other unrelated inode. */ - if (!ctx->logging_new_name && log_mode != LOG_INODE_EXISTS) + if (!ctx->logging_new_name && log_mode != LOG_INODE_EXISTS) { + inode->last_sub_trans = ctx->log_transid; inode->last_log_commit = inode->last_sub_trans; + } spin_unlock(&inode->lock); /* base-commit: 6dc1b4de5a72572f84cc3af901172205d2b82ff2 -- 2.53.0