Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3468443imu; Sun, 11 Nov 2018 15:48:25 -0800 (PST) X-Google-Smtp-Source: AJdET5c1ZfskmZkwJPwhAwQbnERMHqRXIkNT3dPqgcKanYvBHBzL1NagpH10jq2P6bPa2RnRaPKY X-Received: by 2002:a62:250:: with SMTP id 77-v6mr18412478pfc.16.1541980105330; Sun, 11 Nov 2018 15:48:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541980105; cv=none; d=google.com; s=arc-20160816; b=uUNaj8HQV2314TAfcGNHsDhN1QuFJg0dyh8FIvzKCIa0Dp9Rx/xjUZnmwPDiCr/epH ipUNsB9ukLvcdR26EtsYLp+WbA1ftUeno+UM2C/TWPPketHePAKilRIjIMbBl9y4eaUY +5SjJM2f1LQnuWl9HV41eXeDZzyIRBhMffKDbEeqpf9VX83e0OAtR4M/NrIcc95ZbJ3U c8ohofuIXnz8TLb7uNJbrzTmy9JJKXm4alKbYwKOb9He3EE00R8UwdcswZgSCQpFq5/Y k6EhK/CrquzOmIkEEgJdztyCC9zbUF7E1Ip8p6dESfSD+oLVqKDh8EeHeMI0iXfT24Ht JIAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HTdPhqLpzKi1DZeNlehe5THm3DfpUshL1vwbcNb0QRg=; b=be01KZXKzZRFCk8/foJtNY1jiGvrmuZ4IzjeMhHaZOmsiPPXjIqW+JBbRsmSSCmuRd AscW4mpTCdl7nHIM31lg8tJCwiBAiuug0szJ/h80wLLdQ8d5NnW2WgKLVup18ySbr+Iu Hqiak9NRyFvBIxKes71PMA6xV1n9f0SqNgwUv7HZyl3QhWlUe8UqM1SRzZE0G5sHgS+q F/q557iaZSIQIzCBA3d+a+E+fYqC5AOanDRJV9tLOkHGAqEbwkQA44i67wt+2L9+29k6 r30JyHVWEAiK2d91RuNDXvqXeHIMmgmtWqDIJzlGusUBqHSwrUoVo3yLFPr71C4cS04q 02Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=RBmknTM1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bi11-v6si16000277plb.20.2018.11.11.15.48.10; Sun, 11 Nov 2018 15:48:25 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=RBmknTM1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733287AbeKLJiF (ORCPT + 99 others); Mon, 12 Nov 2018 04:38:05 -0500 Received: from mail.kernel.org ([198.145.29.99]:39206 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733143AbeKLISr (ORCPT ); Mon, 12 Nov 2018 03:18:47 -0500 Received: from localhost (unknown [206.108.79.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 73AA2208A3; Sun, 11 Nov 2018 22:28:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975330; bh=B2lQeC+BDcHBd7ESW8oer4LvAwJcLgLc08Mns2hplHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RBmknTM1nIvSzXFi7lDUvKYPc213g6p8mydSSCroiHYcgtxjVnLSpGuvR/mzeJJE6 hIPedkOwWXbSB+QIYCuldJpz/aD/exbamx3HrdgfNXSIFhMM782q6Bf1FVcocCZ9qm cCRWAHrZieqkV1xUVlSwgtjEMsl9g0GTrIiqmIwU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Filipe Manana , David Sterba Subject: [PATCH 4.19 343/361] Btrfs: fix wrong dentries after fsync of file that got its parent replaced Date: Sun, 11 Nov 2018 14:21:30 -0800 Message-Id: <20181111221700.891983575@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221619.915519183@linuxfoundation.org> References: <20181111221619.915519183@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Filipe Manana commit 0f375eed92b5a407657532637ed9652611a682f5 upstream. In a scenario like the following: mkdir /mnt/A # inode 258 mkdir /mnt/B # inode 259 touch /mnt/B/bar # inode 260 sync mv /mnt/B/bar /mnt/A/bar mv -T /mnt/A /mnt/B fsync /mnt/B/bar After replaying the log we end up with file bar having 2 hard links, both with the name 'bar' and one in the directory with inode number 258 and the other in the directory with inode number 259. Also, we end up with the directory inode 259 still existing and with the directory inode 258 still named as 'A', instead of 'B'. In this scenario, file 'bar' should only have one hard link, located at directory inode 258, the directory inode 259 should not exist anymore and the name for directory inode 258 should be 'B'. This incorrect behaviour happens because when attempting to log the old parents of an inode, we skip any parents that no longer exist. Fix this by forcing a full commit if an old parent no longer exists. A test case for fstests follows soon. CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/tree-log.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -5583,9 +5583,33 @@ static int btrfs_log_all_parents(struct dir_inode = btrfs_iget(fs_info->sb, &inode_key, root, NULL); - /* If parent inode was deleted, skip it. */ - if (IS_ERR(dir_inode)) - continue; + /* + * If the parent inode was deleted, return an error to + * fallback to a transaction commit. This is to prevent + * getting an inode that was moved from one parent A to + * a parent B, got its former parent A deleted and then + * it got fsync'ed, from existing at both parents after + * a log replay (and the old parent still existing). + * Example: + * + * mkdir /mnt/A + * mkdir /mnt/B + * touch /mnt/B/bar + * sync + * mv /mnt/B/bar /mnt/A/bar + * mv -T /mnt/A /mnt/B + * fsync /mnt/B/bar + * + * + * If we ignore the old parent B which got deleted, + * after a log replay we would have file bar linked + * at both parents and the old parent B would still + * exist. + */ + if (IS_ERR(dir_inode)) { + ret = PTR_ERR(dir_inode); + goto out; + } if (ctx) ctx->log_new_dentries = false;