Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4819956ybl; Wed, 22 Jan 2020 05:22:22 -0800 (PST) X-Google-Smtp-Source: APXvYqzNSiCDJHX46D4YrVmlaWy95YqCue+5jE1qSEQfH8yLWSIVzdii3z89P25pxFG/RaDQ5Sb6 X-Received: by 2002:a9d:75da:: with SMTP id c26mr7394493otl.40.1579699342739; Wed, 22 Jan 2020 05:22:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579699342; cv=none; d=google.com; s=arc-20160816; b=FBX4kl+U0nP9w8Y/viWjbPFTZjVAC6+FJXJGFDHTrLGe7Y9sgerEq1dj1GOUdI+X2U Q2IuzMIZYC3QSvekF4PEVqhDKgIp983dGB5KVkqc+rLF8qgBc4ObX/NqFXGUqYdW8M/V dnv9To6UwtCR6TWL4RlGilWbLqUqjexlM7rf6D1AQlkgbZgrQIVFAH6rcGkfK3aFvUvu Bt+04C62qj0+pW4h1Ek3uYcIMpSxzBTXKPnGEa+LuJf+JlAe6ob2k2rr1IM8ecu/iIVf aSmJR5CHOhlCByhgCnCUkuE4eKK+GkvmGO6ILAOKQWBffrdXLz8NATEbFmQL0MFwB3DO uZ6w== 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=v5nwczxEdwoNnYZ3U8akvn+fkSGfsvYmADP6+ZTlmnA=; b=wfY1P+Vq5wNINLO0ThbrSxVvO/847k9jDgUMdDmwVW61JJdnhwX66nNYbWnBSb41sv v+iIvFO3FtF8L+FhS9KRF2X0HZJfvZ1XLkFYfdH/wblY/ru6NQLuUYgAxk3foTZjmU8t JZqhCFcQxtsRvQO7LycGRIKivVpzNnXtrmo3nZ0T19I5Idn5a1s4q+NSyIyHH7cNBn/Q rpmjsxA8o6oMBTb70X4iK0+6ZXjQUqnd93tSh8wAofnPWvMa9i+i86gyrLGOqso189Vb 4Kvz/Apn2iuagHTRqJw493vKPHwRBh4g9rdfTBp5xUSX3V6cwABzvGF+waTm+yGWglSk iAUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="LYSvb5k/"; 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 n18si24909874otf.285.2020.01.22.05.22.10; Wed, 22 Jan 2020 05:22:22 -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="LYSvb5k/"; 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 S1729372AbgAVNUl (ORCPT + 99 others); Wed, 22 Jan 2020 08:20:41 -0500 Received: from mail.kernel.org ([198.145.29.99]:37000 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726590AbgAVNUj (ORCPT ); Wed, 22 Jan 2020 08:20:39 -0500 Received: from localhost (unknown [84.241.205.26]) (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 BB6242467A; Wed, 22 Jan 2020 13:20:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579699239; bh=XPBQSiM5KqGKjrSjNzH8x1J5nmiAzP2iCbS/ejLm77Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LYSvb5k/kG9rfgyMhZazAR13iB8fqucMN9PKy9usBWGZQrVjjpV/ZGqs6/dGUAc4H TOmLREIzdlKFiUBxRczo84CYNvSoMljCyAQ2JavdJlmZYAOuMQRCEkM3FPOSqbsFap 87sQxBy+u6IBS94NS3IrZLJCJH9zr7Rc4Epy01Gg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Josef Bacik , David Sterba Subject: [PATCH 5.4 084/222] btrfs: fix invalid removal of root ref Date: Wed, 22 Jan 2020 10:27:50 +0100 Message-Id: <20200122092839.725614756@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200122092833.339495161@linuxfoundation.org> References: <20200122092833.339495161@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: Josef Bacik commit d49d3287e74ffe55ae7430d1e795e5f9bf7359ea upstream. If we have the following sequence of events btrfs sub create A btrfs sub create A/B btrfs sub snap A C mkdir C/foo mv A/B C/foo rm -rf * We will end up with a transaction abort. The reason for this is because we create a root ref for B pointing to A. When we create a snapshot of C we still have B in our tree, but because the root ref points to A and not C we will make it appear to be empty. The problem happens when we move B into C. This removes the root ref for B pointing to A and adds a ref of B pointing to C. When we rmdir C we'll see that we have a ref to our root and remove the root ref, despite not actually matching our reference name. Now btrfs_del_root_ref() allowing this to work is a bug as well, however we know that this inode does not actually point to a root ref in the first place, so we shouldn't be calling btrfs_del_root_ref() in the first place and instead simply look up our dir index for this item and do the rest of the removal. CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Josef Bacik Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/inode.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -4260,13 +4260,16 @@ static int btrfs_unlink_subvol(struct bt } btrfs_release_path(path); - ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid, - dir_ino, &index, name, name_len); - if (ret < 0) { - if (ret != -ENOENT) { - btrfs_abort_transaction(trans, ret); - goto out; - } + /* + * This is a placeholder inode for a subvolume we didn't have a + * reference to at the time of the snapshot creation. In the meantime + * we could have renamed the real subvol link into our snapshot, so + * depending on btrfs_del_root_ref to return -ENOENT here is incorret. + * Instead simply lookup the dir_index_item for this entry so we can + * remove it. Otherwise we know we have a ref to the root and we can + * call btrfs_del_root_ref, and it _shouldn't_ fail. + */ + if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { di = btrfs_search_dir_index_item(root, path, dir_ino, name, name_len); if (IS_ERR_OR_NULL(di)) { @@ -4281,8 +4284,16 @@ static int btrfs_unlink_subvol(struct bt leaf = path->nodes[0]; btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); index = key.offset; + btrfs_release_path(path); + } else { + ret = btrfs_del_root_ref(trans, objectid, + root->root_key.objectid, dir_ino, + &index, name, name_len); + if (ret) { + btrfs_abort_transaction(trans, ret); + goto out; + } } - btrfs_release_path(path); ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); if (ret) {