Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3698988pxv; Mon, 26 Jul 2021 09:40:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy3P1UsGJyK6deorJgueMTE8gP6zoaDEBTilGSsvsIdHMr/5dXZwUdjtF6RUL0mlzW6pqmR X-Received: by 2002:a17:906:bc87:: with SMTP id lv7mr3285113ejb.365.1627317510950; Mon, 26 Jul 2021 09:38:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627317510; cv=none; d=google.com; s=arc-20160816; b=qOIQOubR9F6g0x+nZChRE92wel3SQKcwnOyf+lNIIZ9M+qW12IKPK5VpgWZG9zSmym Ft4mSKOjBmP0Eto8tF+jUWrK7YOy/kVFFJeCyDWgnEcY20+5OWTWdmJOKpvxj7dtezHX S4VyFpNXS63jMMtUlgTyJluo+8eODKfj1YMIsqJ2dcRYW3PhZ2xEZ6Ut5KJ6yULJwMWa B7uZjwjiWQwOhryDr38fXEt20Yb8FPn0J+Vg6y9Iq+dt50htVf1FEBw/YpsNJ6LnN4zp cOz3bMQWIDUW24pAquLY5NZOPTacKv0BSD6Cs27ZYT4uWk0uwWs+pw3D3Vhh885OAZAr nLPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=mh5bwBS+6xaiMHdAJBZV8L3DMfE0/Jm+b/KJgukkf9s=; b=hN8nBX+HBxkIMTWWGBYgQitahG5OC03gQOi7BIzHWS8t59U8Gc8ubmfE6OWz+ir1I/ ke+2hbfjBVXmcRNc/hIgQkjVo+qwX7jYfFYncsJiJDs7/L1vMSSfnrBYMrZ+cGeWQX1V lduh6N0P/SqkWdL6MqY9P1iGZREUOL9U2dcsqUs7JM7P1ELNf783RYPY66tiDV5N1GJf lFuobf/q8KUMjI0HcHqlVWxl4Fgnx6BMLvpA6qJIywhC+06DAfgKQqlSD9a1FmBeEjwJ P76LqAPrQKsGsj5dhICxiAoAo2/NWq/SGMlg9IrDA4fCNoRxC2ZX6Bxnl2qjV4sLB8zd sxaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YMipkVdS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b9si421682ede.326.2021.07.26.09.38.08; Mon, 26 Jul 2021 09:38:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YMipkVdS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240153AbhGZPzC (ORCPT + 99 others); Mon, 26 Jul 2021 11:55:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:51498 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236415AbhGZPeU (ORCPT ); Mon, 26 Jul 2021 11:34:20 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7198260F5B; Mon, 26 Jul 2021 16:14:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627316088; bh=LlJRIH297gQ9s/nqy999hnUVaJwbNMos0tPuVtk3Uas=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YMipkVdSs5YTbcRPzEocjhWRLN6matth3XxIe5pCeq48yOWK72lZMsNmCtwpHknlo +JBaG8VDOzzFqZGvTEj6C58rp70z9IMKgfhMU8mpFnLRA5EljdwV6gvc8w/yNmO/S+ 3jvz0e7SMAtGkJyc3klBjAmkThClryEi2OreQsOE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Filipe Manana , David Sterba Subject: [PATCH 5.13 182/223] btrfs: fix unpersisted i_size on fsync after expanding truncate Date: Mon, 26 Jul 2021 17:39:34 +0200 Message-Id: <20210726153852.146665699@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210726153846.245305071@linuxfoundation.org> References: <20210726153846.245305071@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Filipe Manana commit 9acc8103ab594f72250788cb45a43427f36d685d upstream. If we have an inode that does not have the full sync flag set, was changed in the current transaction, then it is logged while logging some other inode (like its parent directory for example), its i_size is increased by a truncate operation, the log is synced through an fsync of some other inode and then finally we explicitly call fsync on our inode, the new i_size is not persisted. The following example shows how to trigger it, with comments explaining how and why the issue happens: $ mkfs.btrfs -f /dev/sdc $ mount /dev/sdc /mnt $ touch /mnt/foo $ xfs_io -f -c "pwrite -S 0xab 0 1M" /mnt/bar $ sync # Fsync bar, this will be a noop since the file has not yet been # modified in the current transaction. The goal here is to clear # BTRFS_INODE_NEEDS_FULL_SYNC from the inode's runtime flags. $ xfs_io -c "fsync" /mnt/bar # Now rename both files, without changing their parent directory. $ mv /mnt/bar /mnt/bar2 $ mv /mnt/foo /mnt/foo2 # Increase the size of bar2 with a truncate operation. $ xfs_io -c "truncate 2M" /mnt/bar2 # Now fsync foo2, this results in logging its parent inode (the root # directory), and logging the parent results in logging the inode of # file bar2 (its inode item and the new name). The inode of file bar2 # is logged with an i_size of 0 bytes since it's logged in # LOG_INODE_EXISTS mode, meaning we are only logging its names (and # xattrs if it had any) and the i_size of the inode will not be changed # when the log is replayed. $ xfs_io -c "fsync" /mnt/foo2 # Now explicitly fsync bar2. This resulted in doing nothing, not # logging the inode with the new i_size of 2M and the hole from file # offset 1M to 2M. Because the inode did not have the flag # BTRFS_INODE_NEEDS_FULL_SYNC set, when it was logged through the # fsync of file foo2, its last_log_commit field was updated, # resulting in this explicit of file bar2 not doing anything. $ xfs_io -c "fsync" /mnt/bar2 # File bar2 content and size before a power failure. $ od -A d -t x1 /mnt/bar2 0000000 ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab * 1048576 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 * 2097152 # Mount the filesystem to replay the log. $ mount /dev/sdc /mnt # Read the file again, should have the same content and size as before # the power failure happened, but it doesn't, i_size is still at 1M. $ od -A d -t x1 /mnt/bar2 0000000 ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab * 1048576 This started to happen after commit 209ecbb8585bf6 ("btrfs: remove stale comment and logic from btrfs_inode_in_log()"), since btrfs_inode_in_log() no longer checks if the inode's list of modified extents is not empty. However, checking that list is not the right way to address this case and the check was added long time ago in commit 125c4cf9f37c98 ("Btrfs: set inode's logged_trans/last_log_commit after ranged fsync") for a different purpose, to address consecutive ranged fsyncs. The reason that checking for the list emptiness makes this test pass is because during an expanding truncate we create an extent map to represent a hole from the old i_size to the new i_size, and add that extent map to the list of modified extents in the inode. However if we are low on available memory and we can not allocate a new extent map, then we don't treat it as an error and just set the full sync flag on the inode, so that the next fsync does not rely on the list of modified extents - so checking for the emptiness of the list to decide if the inode needs to be logged is not reliable, and results in not logging the inode if it was not possible to allocate the extent map for the hole. Fix this by ensuring that if we are only logging that an inode exists (inode item, names/references and xattrs), we don't update the inode's last_log_commit even if it does not have the full sync runtime flag set. A test case for fstests follows soon. CC: stable@vger.kernel.org # 5.13+ Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/tree-log.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -5515,16 +5515,29 @@ log_extents: spin_lock(&inode->lock); inode->logged_trans = trans->transid; /* - * Don't update last_log_commit if we logged that an inode exists - * after it was loaded to memory (full_sync bit set). - * This is to prevent data loss when we do a write to the inode, - * then the inode gets evicted after all delalloc was flushed, - * then we log it exists (due to a rename for example) and then - * fsync it. This last fsync would do nothing (not logging the - * extents previously written). + * Don't update last_log_commit if we logged that an inode exists. + * We do this for two reasons: + * + * 1) We might have had buffered writes to this inode that were + * flushed and had their ordered extents completed in this + * transaction, but we did not previously log the inode with + * LOG_INODE_ALL. Later the inode was evicted and after that + * it was loaded again and this LOG_INODE_EXISTS log operation + * happened. We must make sure that if an explicit fsync against + * the inode is performed later, it logs the new extents, an + * updated inode item, etc, and syncs the log. The same logic + * applies to direct IO writes instead of buffered writes. + * + * 2) When we log the inode with LOG_INODE_EXISTS, its inode item + * is logged with an i_size of 0 or whatever value was logged + * before. If later the i_size of the inode is increased by a + * truncate operation, the log is synced through an fsync of + * some other inode and then finally an explicit fsync against + * this inode is made, we must make sure this fsync logs the + * inode with the new i_size, the hole between old i_size and + * the new i_size, and syncs the log. */ - if (inode_only != LOG_INODE_EXISTS || - !test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags)) + if (inode_only != LOG_INODE_EXISTS) inode->last_log_commit = inode->last_sub_trans; spin_unlock(&inode->lock); }