Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3705301ybv; Mon, 10 Feb 2020 05:03:20 -0800 (PST) X-Google-Smtp-Source: APXvYqxZraj4GYof5v0a2riHEuB1zKpMKlDB7xrx0QEPZgZ/5FdFnBiIjwCIrdEfkN4a9CkWsqLI X-Received: by 2002:a05:6830:43:: with SMTP id d3mr1000952otp.259.1581339800647; Mon, 10 Feb 2020 05:03:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581339800; cv=none; d=google.com; s=arc-20160816; b=cOJJsmgUJ+R5o2fIRyhAkeHVqDLbhJMeUSEPSfSGWRgPYTBa5rnNzqYpvZSaxRHMeh R75vdvmhFLDkQzZUUUON1LpJhMIZMa4hTBdfw95MfVyxPmel0bULDcCVeXrZ56ZWA9qa Ek66RsDzfId96DWeJjHNEuiFeV4oCwOvXBYiBknAm1CPd6nD/YrVLl4jvK6d9taEIPxd gV5JOyud2jN4YIpzAuD5oHLutSxwGx+2x7/nDDrwOnBJTtCgVj83xWhLULpAZzO1VDjD vjHMkdxcuk5GU0wVpCeqMsM6bbMrG8Ld38qEJb4nfQQvZjfTOhJs6pisusn3wU9pTp++ 7BAg== 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=imEi1T64+bwM66jdCWTWxsc3YmPw3ZpswGGZntn0vo0=; b=rKZDUh/uZ7wUy/hEXx1IL2Eu4/ZqXTNWw6FoJwFN++Rh0xdgSTYE1UAH8AJLFzyQpc kBHfwdHAwck7vFgql1P+m/IuY8fsai9A0PZ6HEQs26WHdVSA8h6p330PUlEmZpwLearQ lk3F0PhI3+krovvXk8hzkSYfx6QsJPUr+BHse38PK0BXg/S+D8Hc8+KwmBs4C0oQQPpR lencrcE7naoV4JSYyQOU2L/TWDH22pYIHrI7IxG7WlEjsyGy3b4aeGL0tS+F+VJHhnId EjXeRikRuIyr5c8zi+n18CIcXzc6j2N3vAYjuBnCkdMSVOrQA+71S3vwMnWIr2v1sYnr wQ3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zhdhq6S8; 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 t28si157012otr.16.2020.02.10.05.03.04; Mon, 10 Feb 2020 05:03:20 -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=zhdhq6S8; 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 S1730051AbgBJNB0 (ORCPT + 99 others); Mon, 10 Feb 2020 08:01:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:41782 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729818AbgBJMkw (ORCPT ); Mon, 10 Feb 2020 07:40:52 -0500 Received: from localhost (unknown [209.37.97.194]) (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 3FE7120661; Mon, 10 Feb 2020 12:40:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581338451; bh=N6y3J6xDI4G6Rv3ZfZ7XKKwjKaRnqtJz6uGMUI0REg0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zhdhq6S8IbmfUFJB+kGfs/KmPnD+T2Uj3dNeTLfwiQU8bcfNvD5Wkf4XkgpFJSumV 3k2wN8IeUj5CJNkLGyGuT7st/5qJ+05b25YK/uMHoHCAolKHYOZDPiG8oVkrrTrkyx ZbNKFLC4wYAZN220Df985j5dpeSIQ8Sx/CUaOxqw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Filipe Manana , Josef Bacik , David Sterba Subject: [PATCH 5.5 197/367] Btrfs: fix infinite loop during fsync after rename operations Date: Mon, 10 Feb 2020 04:31:50 -0800 Message-Id: <20200210122442.771729919@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200210122423.695146547@linuxfoundation.org> References: <20200210122423.695146547@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Filipe Manana commit b5e4ff9d465da1233a2d9a47ebce487c70d8f4ab upstream. Recently fsstress (from fstests) sporadically started to trigger an infinite loop during fsync operations. This turned out to be because support for the rename exchange and whiteout operations was added to fsstress in fstests. These operations, unlike any others in fsstress, cause file names to be reused, whence triggering this issue. However it's not necessary to use rename exchange and rename whiteout operations trigger this issue, simple rename operations and file creations are enough to trigger the issue. The issue boils down to when we are logging inodes that conflict (that had the name of any inode we need to log during the fsync operation), we keep logging them even if they were already logged before, and after that we check if there's any other inode that conflicts with them and then add it again to the list of inodes to log. Skipping already logged inodes fixes the issue. Consider the following example: $ mkfs.btrfs -f /dev/sdb $ mount /dev/sdb /mnt $ mkdir /mnt/testdir # inode 257 $ touch /mnt/testdir/zz # inode 258 $ ln /mnt/testdir/zz /mnt/testdir/zz_link $ touch /mnt/testdir/a # inode 259 $ sync # The following 3 renames achieve the same result as a rename exchange # operation ( /mnt/testdir/zz_link to /mnt/testdir/a). $ mv /mnt/testdir/a /mnt/testdir/a/tmp $ mv /mnt/testdir/zz_link /mnt/testdir/a $ mv /mnt/testdir/a/tmp /mnt/testdir/zz_link # The following rename and file creation give the same result as a # rename whiteout operation ( zz to a2). $ mv /mnt/testdir/zz /mnt/testdir/a2 $ touch /mnt/testdir/zz # inode 260 $ xfs_io -c fsync /mnt/testdir/zz --> results in the infinite loop The following steps happen: 1) When logging inode 260, we find that its reference named "zz" was used by inode 258 in the previous transaction (through the commit root), so inode 258 is added to the list of conflicting indoes that need to be logged; 2) After logging inode 258, we find that its reference named "a" was used by inode 259 in the previous transaction, and therefore we add inode 259 to the list of conflicting inodes to be logged; 3) After logging inode 259, we find that its reference named "zz_link" was used by inode 258 in the previous transaction - we add inode 258 to the list of conflicting inodes to log, again - we had already logged it before at step 3. After logging it again, we find again that inode 259 conflicts with him, and we add again 259 to the list, etc - we end up repeating all the previous steps. So fix this by skipping logging of conflicting inodes that were already logged. Fixes: 6b5fc433a7ad67 ("Btrfs: fix fsync after succession of renames of different files") CC: stable@vger.kernel.org # 5.1+ Signed-off-by: Filipe Manana Reviewed-by: Josef Bacik Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/tree-log.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -4836,6 +4836,50 @@ static int log_conflicting_inodes(struct continue; } /* + * If the inode was already logged skip it - otherwise we can + * hit an infinite loop. Example: + * + * From the commit root (previous transaction) we have the + * following inodes: + * + * inode 257 a directory + * inode 258 with references "zz" and "zz_link" on inode 257 + * inode 259 with reference "a" on inode 257 + * + * And in the current (uncommitted) transaction we have: + * + * inode 257 a directory, unchanged + * inode 258 with references "a" and "a2" on inode 257 + * inode 259 with reference "zz_link" on inode 257 + * inode 261 with reference "zz" on inode 257 + * + * When logging inode 261 the following infinite loop could + * happen if we don't skip already logged inodes: + * + * - we detect inode 258 as a conflicting inode, with inode 261 + * on reference "zz", and log it; + * + * - we detect inode 259 as a conflicting inode, with inode 258 + * on reference "a", and log it; + * + * - we detect inode 258 as a conflicting inode, with inode 259 + * on reference "zz_link", and log it - again! After this we + * repeat the above steps forever. + */ + spin_lock(&BTRFS_I(inode)->lock); + /* + * Check the inode's logged_trans only instead of + * btrfs_inode_in_log(). This is because the last_log_commit of + * the inode is not updated when we only log that it exists and + * and it has the full sync bit set (see btrfs_log_inode()). + */ + if (BTRFS_I(inode)->logged_trans == trans->transid) { + spin_unlock(&BTRFS_I(inode)->lock); + btrfs_add_delayed_iput(inode); + continue; + } + spin_unlock(&BTRFS_I(inode)->lock); + /* * We are safe logging the other inode without acquiring its * lock as long as we log with the LOG_INODE_EXISTS mode. We * are safe against concurrent renames of the other inode as