Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp52304imu; Thu, 8 Nov 2018 14:37:40 -0800 (PST) X-Google-Smtp-Source: AJdET5d/T67GJ1lFqMGD/jpHZMAov3Ady7QGgcbVWqbkVpcRrUaIPYiHmSA4WhSFK+saVBjSAsH7 X-Received: by 2002:a63:585c:: with SMTP id i28mr5374357pgm.178.1541716659947; Thu, 08 Nov 2018 14:37:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541716659; cv=none; d=google.com; s=arc-20160816; b=xRuEuhryEkS6rvOXRMcg8eADSZJeeCRK28HK93N/8w0aa6hdSEArb00r/mPaSrvQTL 96G249GPKYh+nKrsBj+nNvmXcZcbCWGq3TzTnFNPLNlwB+1SRmAp/l0cviXvbISlsS6X iGIPSf7RSFx66ELW5+5sNuUPBATDg0qD8qkg+BNjcOcp9UpEZyOEQrsLZweLnPZ1ibiK Bw1fEeNNeEJSl2+hekkfbnk2KM3kiDecNjbYHdjJFEZTA9RY1TaebPRiNNtNz29ZV/PS TNogcvuFF5pzL/Qt+RSHiHIe0ckP8MABt7R/9BSUT6JhQAUFGR8MBdLup+lldbUK7c5k Fhbw== 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=V6YPvRU6rNohpQn2jnNP3+Zqzeue6nx8Bmqn5hBw8Pg=; b=gGqzmYYZh3IcvJnAG/dKwMShR9g5AhVa2CQAHwDg7ZQDWajzdle4HTnXufshlX2P8C KamAWcSIgyGUuc3c8IMTnul4WPa6wzP27LxB1khOEYoz+dLTYbULJvTGwaxAsa0l4mxf oE7wixTBKbyt4i9bJNIDz9iwjY/svtE5D6/mrq/yXNMv0XhbivbPHOz5OhEW+ZxQ6TfS PyuJ0IpWYOY0zETRKXAM6uc0Gtf0bYlX+HDpRoxUEcHMlgsDr7i9cJWnJqOx1nadtRm3 mtWUgWbd50KmL7W5Ndf9H9hUbP3trPvpSxG1J0nuKUlO/YTbIjFsvWgeOGy1AYYTlTU1 OI9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Bcq1jgtr; 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 v3si5087190pgh.305.2018.11.08.14.37.24; Thu, 08 Nov 2018 14:37:39 -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=Bcq1jgtr; 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 S1729937AbeKIHgr (ORCPT + 99 others); Fri, 9 Nov 2018 02:36:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:54472 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729197AbeKIHgr (ORCPT ); Fri, 9 Nov 2018 02:36:47 -0500 Received: from localhost (unknown [208.72.13.198]) (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 038D52146E; Thu, 8 Nov 2018 21:59:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541714358; bh=Bg/LSBpC+opqEjvKAvRVIfEBxGt4UM045FmBWYZ7Blg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Bcq1jgtrLPXn2hIVCY2wGs7z6AtKJeX1/2h7ZlSCTGukiGelVEExFILAZYgpBwl5g z+ihR18YDIF7HFja3HENFWIutxCHuS9Kn3y6jAwVqQe5iE0mSb/rrXqcaBZdKg3uDl LSWgpZPFJo2nxJzDFMzmZDFQbbM6gU8hI+GvDVDo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jeff Mahoney , David Sterba , Chris Mason , Sasha Levin Subject: [PATCH 4.4 033/114] btrfs: dont create or leak aliased root while cleaning up orphans Date: Thu, 8 Nov 2018 13:50:48 -0800 Message-Id: <20181108215102.115959896@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181108215059.051093652@linuxfoundation.org> References: <20181108215059.051093652@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. ------------------ [ Upstream commit 35bbb97fc898aeb874cb7c8b746f091caa359994 ] commit 909c3a22da3 (Btrfs: fix loading of orphan roots leading to BUG_ON) avoids the BUG_ON but can add an aliased root to the dead_roots list or leak the root. Since we've already been loading roots into the radix tree, we should use it before looking the root up on disk. Cc: # 4.5 Signed-off-by: Jeff Mahoney Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Chris Mason Signed-off-by: Sasha Levin --- fs/btrfs/disk-io.c | 4 ++-- fs/btrfs/disk-io.h | 2 ++ fs/btrfs/root-tree.c | 27 ++++++++++++++++++--------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index a2001abbe379..120363d0f981 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -1608,8 +1608,8 @@ fail: return ret; } -static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, - u64 root_id) +struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, + u64 root_id) { struct btrfs_root *root; diff --git a/fs/btrfs/disk-io.h b/fs/btrfs/disk-io.h index adeb31830b9c..3c9819403487 100644 --- a/fs/btrfs/disk-io.h +++ b/fs/btrfs/disk-io.h @@ -68,6 +68,8 @@ struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info, struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, struct btrfs_key *location); int btrfs_init_fs_root(struct btrfs_root *root); +struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, + u64 root_id); int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root); void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info); diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c index 2c849b08a91b..6a6efb26d52f 100644 --- a/fs/btrfs/root-tree.c +++ b/fs/btrfs/root-tree.c @@ -272,6 +272,23 @@ int btrfs_find_orphan_roots(struct btrfs_root *tree_root) root_key.objectid = key.offset; key.offset++; + /* + * The root might have been inserted already, as before we look + * for orphan roots, log replay might have happened, which + * triggers a transaction commit and qgroup accounting, which + * in turn reads and inserts fs roots while doing backref + * walking. + */ + root = btrfs_lookup_fs_root(tree_root->fs_info, + root_key.objectid); + if (root) { + WARN_ON(!test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, + &root->state)); + if (btrfs_root_refs(&root->root_item) == 0) + btrfs_add_dead_root(root); + continue; + } + root = btrfs_read_fs_root(tree_root, &root_key); err = PTR_ERR_OR_ZERO(root); if (err && err != -ENOENT) { @@ -310,16 +327,8 @@ int btrfs_find_orphan_roots(struct btrfs_root *tree_root) set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); err = btrfs_insert_fs_root(root->fs_info, root); - /* - * The root might have been inserted already, as before we look - * for orphan roots, log replay might have happened, which - * triggers a transaction commit and qgroup accounting, which - * in turn reads and inserts fs roots while doing backref - * walking. - */ - if (err == -EEXIST) - err = 0; if (err) { + BUG_ON(err == -EEXIST); btrfs_free_fs_root(root); break; } -- 2.17.1