Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2987638imu; Mon, 19 Nov 2018 09:02:10 -0800 (PST) X-Google-Smtp-Source: AJdET5cY46NPswQUsYRgGGv5Gp8pG8r18kFnKcgxXFZIArRzMfkB6d8SN4O+EaryS3XLA+5bNbFb X-Received: by 2002:a62:e0d8:: with SMTP id d85mr23349376pfm.214.1542646930907; Mon, 19 Nov 2018 09:02:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542646930; cv=none; d=google.com; s=arc-20160816; b=DUifdZ1BBvO87eUa9bHxJnKkJdvo72NJckPKtXG5kqxbeENGfRYKENP0Q/Tv6f/zd/ p0Vs1uSI/SUxfR/KxbmazmCX4AtN2VNKRTZ1ZMmGG6IP+CrVX6QXrR9nFbkdtTKXNPIY pl2H6bQU0i5wWoSM8xYtFXwAQ2udHi5MNs/M64pKPuAUB0yAsh5KKUWC6kRzLd6UWQ2e SUf5za4sUUlFZErD8sjpeWECy+K6LOpg7/Er/BiK3E1Cg49N3zWrSaW2KroR1sNEovS3 ZMAY3EnNo22iTsyFIlz403ZQ6yS/19owuezk79iPHv5fBzv1bakRa+hLzizF5ERWf4CE UcwQ== 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=ghpVHzjAADKeU5hTwIJOU1nGMedf5w1QK+ZkeNyhIaw=; b=kH0ym2rSqHuG0o55ehbGQy0yozQ+kmsfzZPiJ19dyJw5XaNc4qbStnVLNudMoBumJ9 ypdX7xfU6b98yrDqY9AEJmmWxl0HwLvXnt0p9IRDRuT0NlR9TqkLqopcUYfWehhnA8oH j8QSbpEq+QDfNPNVl3hPPlH7mYt8OD/iBIOx87RiR8LIbdyMWdA+hjbPUYZ7JZc/6Bel g5XcFR1SOUQTqz8+u13G3bH9Ufi5nAOU3et+Lg92fQCEcXHKCY6Ti3szXSp2vNf627fb JS3zymbbU2CD4PlcIRUc3vQ60V8LE4EFXmzPnkQWJeLMUp+taWp4gntwnwP/3Ba5rqfM Y4XQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2CLZOMzJ; 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 33si14616924plu.169.2018.11.19.09.01.54; Mon, 19 Nov 2018 09:02:10 -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=2CLZOMzJ; 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 S2405193AbeKTDYH (ORCPT + 99 others); Mon, 19 Nov 2018 22:24:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:36328 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404390AbeKTDYG (ORCPT ); Mon, 19 Nov 2018 22:24:06 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 F27A721582; Mon, 19 Nov 2018 16:59:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1542646790; bh=9TM6fTcgyujrbLa7e6bjApQTRJdk6fmwrfsMndsWMNs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2CLZOMzJyNdbVtOUQnuZ1p4dkOXT3zQKCXQPSNEEUlMvIGMP5gSP9OM4XuCXkN3OE 38xhP6TeLyjBe/J4FiSMVDSIgu8jjL/Is5JxXdF1yktUB1OYbUNw/okWEEHuL+qaFp wBf/sXsJVV+uc1ZNETpXqBlSHUCPrPKLSCOz+hNQ= 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.4 091/160] Btrfs: fix wrong dentries after fsync of file that got its parent replaced Date: Mon, 19 Nov 2018 17:28:50 +0100 Message-Id: <20181119162640.060269598@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181119162630.031306128@linuxfoundation.org> References: <20181119162630.031306128@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.4-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 @@ -5240,9 +5240,33 @@ static int btrfs_log_all_parents(struct dir_inode = btrfs_iget(root->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; + } ret = btrfs_log_inode(trans, root, dir_inode, LOG_INODE_ALL, 0, LLONG_MAX, ctx);