From patchwork Wed Mar 4 16:18:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11420363 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E34C0174A for ; Wed, 4 Mar 2020 16:18:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C32AC21739 for ; Wed, 4 Mar 2020 16:18:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="Fa43c+BS" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388240AbgCDQSh (ORCPT ); Wed, 4 Mar 2020 11:18:37 -0500 Received: from mail-qk1-f195.google.com ([209.85.222.195]:44945 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729739AbgCDQSg (ORCPT ); Wed, 4 Mar 2020 11:18:36 -0500 Received: by mail-qk1-f195.google.com with SMTP id f198so2146114qke.11 for ; Wed, 04 Mar 2020 08:18:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=gAK1da/LKBjkl7EBkrL4Rdz0dBqcuF+zieoCZPZIVIM=; b=Fa43c+BSDCkOkgg5clFLvmMOrnaFijJGbVDY1x1HE/TtQj1rDN47wViEH55eQs8HFw +RYMBvQHPuyVm9JOSt+LrVJFzycdMI8fg9/SCVLZpXrqTsa6Sv/98QOUy1pD0t9rbOhy N2kgCnFoiDlGPZXx6iwpns1/pwZaKxCNHjObYKytvBGJV6oetdodPTHo/UPCGAxMOonb 90+AHGkVvwDCy4Ma4gzkg8Wk0BdhVHLPymqWJn5gcb+TRum6fsYQCKYH7w6X5OlP7LTQ X/ai+9sW3yW6aunf/ovHb4FgcjriCriDWyLK0jDGJJZdWUEWsFFigEbdVX36JB6hrp7j OrzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gAK1da/LKBjkl7EBkrL4Rdz0dBqcuF+zieoCZPZIVIM=; b=M2bk6nUsJNbGTFoUwpCmJxucd7xqnhbgp1ly8fI+RB5HBwbJ5GcVeRq9KYz9urPekx wj7DXOoGLe14T3lE9vqta7gmDG/gcRRdRooFjPrbPFs26auB9llkaWkzeKJ5mEWw4yCz g8qwP+jqWGAHItcj8/JztWxBd81kI9iPnPDEvCiwixvk7PJhZ0Vn3CUYM2DEXwtVMvQE 3I8iV2gZ4Eg+WdC6o8zhXn7NxvV5B9g2cV0lnAh+7GRz/c6Xq+LaaHEPHGCQutdZZN6U 9v7TqvrH98U5HZrgTx0Vk71oS+dxg+7m2ngxT20scMDgRWGG4XQSg4bGaAMdqxRAOaUK NHuA== X-Gm-Message-State: ANhLgQ2iN7PbziwKZGEMZeGbpyskSmh9qIU4U/uisIIuvOBeIBkYKMlQ VJaMSK7RglCi8219VrCNvq31ZLnSPmE= X-Google-Smtp-Source: ADFU+vsatHYmY41BIBq6v5y5NcEWz4VCPJPxN9eFx0ZMErp+ruEfymx1FSsTQhiIM4Fxx/8mAk52zQ== X-Received: by 2002:a37:9047:: with SMTP id s68mr3750138qkd.63.1583338714681; Wed, 04 Mar 2020 08:18:34 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id w83sm13576024qkb.83.2020.03.04.08.18.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 08:18:33 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 1/8] btrfs: drop block from cache on error in relocation Date: Wed, 4 Mar 2020 11:18:23 -0500 Message-Id: <20200304161830.2360-2-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200304161830.2360-1-josef@toxicpanda.com> References: <20200304161830.2360-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org If we have an error while building the backref tree in relocation we'll process all the pending edges and then free the node. However if we integrated some edges into the cache we'll lose our link to those edges by simply freeing this node, which means we'll leak memory and references to any roots that we've found. Instead we need to use remove_backref_node(), which walks through all of the edges that are still linked to this node and free's them up and drops any root references we may be holding. Signed-off-by: Josef Bacik Reviewed-by: Qu Wenruo --- fs/btrfs/relocation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 4fb7e3cc2aca..507361e99316 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -1244,7 +1244,7 @@ struct backref_node *build_backref_tree(struct reloc_control *rc, free_backref_node(cache, lower); } - free_backref_node(cache, node); + remove_backref_node(cache, node); return ERR_PTR(err); } ASSERT(!node || !node->detached); From patchwork Wed Mar 4 16:18:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11420367 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1C85A17E0 for ; Wed, 4 Mar 2020 16:18:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id F0B4B21739 for ; Wed, 4 Mar 2020 16:18:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="dKG1VhG1" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388280AbgCDQSj (ORCPT ); Wed, 4 Mar 2020 11:18:39 -0500 Received: from mail-qk1-f196.google.com ([209.85.222.196]:40558 "EHLO mail-qk1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726275AbgCDQSi (ORCPT ); Wed, 4 Mar 2020 11:18:38 -0500 Received: by mail-qk1-f196.google.com with SMTP id m2so2164189qka.7 for ; Wed, 04 Mar 2020 08:18:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=n0y5JizAt/b6Axu+Sx+7mJul6hajBxGZXBIZfNOq4aQ=; b=dKG1VhG1RH5Iu3TJToRAK1H6kNoa6W1nIoUNkulE6LLFlU5QLOm11wqe9plOOiISCl 0Wqyf+PfN7ML0UTAinNSEHjaBtX/B8zhc5339Eb0FrLC9njAdPnmvL+9h5gmIQo5mGeL Viv49Dqbzk//23uigM3nEywG+NWerAFc19/jUTw+66C5tuPOhkUS+7n9BgTXMyDTFGn6 jagXDVJYEOfuwcrbuH5SNbfWJ7V41Y3Zkn3QU57cRz032CKqkqcxnsnTgMwT1QMl5ym+ FvsnbV9+pSPt8G3F0jsyalHoIFeOoEyxPY30mLPl+C7j4Lt37B8VoTOlIiXFP77+hMhN jULA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=n0y5JizAt/b6Axu+Sx+7mJul6hajBxGZXBIZfNOq4aQ=; b=JON5IMr5ogPEBvCZoUX7EieweHSDn6L8oq9f3XZfKB0xWd7TqWesgvKfCAmDCbVZEd g4bIyP3izB1119lPQtWbGJREdUYm0LpIATlb2ftT4XzNpLx4+yo25oVVEu56/IsZMM2E 4UJfXEU9JpLOBFy2Uxao7nBY8G/wBl7bscj8g+7FxV429z64JCjVkjgTuLPkaZnP0Zlv gw3f3n8andS/qta/E5H0MctaLyl8MYMigxPtCk7qxqt9ACICp7JxqUpcP68BnHK6kl2Z QsrlKYbyL3A12vofSoMdUl2TLO7bzxVM37mbrtjVvORhl313eF8bsPtsWC7eDlmnR05+ y8ZA== X-Gm-Message-State: ANhLgQ3ht0uynXRHxuOYkbGtkNQEGUkQDjbvH2w1kQGJ8Rbem1TSj+/m G3bd+fkyyJnTTTHm4f6+r/WScmXh4E4= X-Google-Smtp-Source: ADFU+vu4EIAja6fwi4WW6PNEuHj1Q5bCwU4M3WpF+QKUoy6Yb/p17OkxEOmrqijShBS++WUhcHAFHw== X-Received: by 2002:a37:b48:: with SMTP id 69mr3577405qkl.362.1583338716305; Wed, 04 Mar 2020 08:18:36 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id t3sm12513766qkt.114.2020.03.04.08.18.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 08:18:35 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 2/8] btrfs: do not init a reloc root if we aren't relocating Date: Wed, 4 Mar 2020 11:18:24 -0500 Message-Id: <20200304161830.2360-3-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200304161830.2360-1-josef@toxicpanda.com> References: <20200304161830.2360-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org We previously were checking if the root had a dead root before accessing root->reloc_root in order to avoid a UAF type bug. However this scenario happens after we've unset the reloc control, so we would have been saved if we'd simply checked for fs_info->reloc_control. At this point during relocation we no longer need to be creating new reloc roots, so simply move this check above the reloc_root checks to avoid any future races and confusion. Signed-off-by: Josef Bacik Reviewed-by: Qu Wenruo --- fs/btrfs/relocation.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 507361e99316..2141519a9dd0 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -1527,6 +1527,10 @@ int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, int clear_rsv = 0; int ret; + if (!rc || !rc->create_reloc_tree || + root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) + return 0; + /* * The subvolume has reloc tree but the swap is finished, no need to * create/update the dead reloc tree @@ -1540,10 +1544,6 @@ int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, return 0; } - if (!rc || !rc->create_reloc_tree || - root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) - return 0; - if (!trans->reloc_reserved) { rsv = trans->block_rsv; trans->block_rsv = rc->block_rsv; From patchwork Wed Mar 4 16:18:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11420365 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B6495174A for ; Wed, 4 Mar 2020 16:18:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 97DB621D56 for ; Wed, 4 Mar 2020 16:18:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="PL0IN6fp" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729749AbgCDQSk (ORCPT ); Wed, 4 Mar 2020 11:18:40 -0500 Received: from mail-qk1-f195.google.com ([209.85.222.195]:33813 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388278AbgCDQSj (ORCPT ); Wed, 4 Mar 2020 11:18:39 -0500 Received: by mail-qk1-f195.google.com with SMTP id f3so2173831qkh.1 for ; Wed, 04 Mar 2020 08:18:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=2olMn6iFNaS6/+7+ZxxEYJVX7riI9WVDb0uI0sXvt1c=; b=PL0IN6fphSV6vDpKX82QGPT+25xMAWiR0yIAtANjIGYCGp3zu628WPqpnTOisoxcDX vcuiqHEbIEdZ6svkhFpaV/iAbqeNDKpXVsDWlNeIuesf42FM6PacwZRPUfjBOlfdlQoH vVV7mjAU7DM/y6CRj80zr7MrZt6hdKmKTpKlNuerXn7QNX/IU7LPVPP7Vt7OrB+XSh3S SwY3aeQ2HsKmPX99E+f6NgcBw7d2LPzZ7gVzwAZCwhEmMH/2K0F+qKKctYrcfQHojTfC qbBIEMKmJ42FEqpKapG2Yo9ZNtdcPVyCtA3abSw2zEk6NIVMaBrTimckhg/p853Uu70d Dh6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2olMn6iFNaS6/+7+ZxxEYJVX7riI9WVDb0uI0sXvt1c=; b=Z2QkUP4SI6LcZZu2s5YmvvbM5qCFSblzhWO0fFRNcxuXViwnPAoYFoVsXz9oqD6e5s mRFoxQjXER2eNjCPZn/xmRVBYlp7uNeJJazs7gB9cxPOuwk2SHIpUYEEje09UfFf8O+U WYZqm1/yaXB/ppIPB3k1P79Pr6b0Ed04QzT9H4TkXeZDXXMSoW9tgA7TvycpP+UM8Vcy lQYfGi3S3iDEMnlptdkMKzzyDUfUTL2ayMC9hiBns5Ya7uqYhWoPeA1yjmkaUemYMyF+ CqWzC48KUr5fEj5GdwdG72C3misMAIrHON4cibhy/kUhz9BLUk3ynNHNx8Oc97cpv4zh qK5Q== X-Gm-Message-State: ANhLgQ0H5Nmoo9tsKyc40nzOIWymtHaJVGDA6xo9ZYals3+KvRMfZDJP /Anv1bicPOSFudXSGybdp0+vji4unMo= X-Google-Smtp-Source: ADFU+vs8UkOJ52dHCJfS1p/JtXP015B/QSBCgM/2kRoMZfokHLXtTQ4ki3TIPAzXn/wLdkgXB0yOxA== X-Received: by 2002:a37:b646:: with SMTP id g67mr3206518qkf.52.1583338718059; Wed, 04 Mar 2020 08:18:38 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id f189sm8482689qke.90.2020.03.04.08.18.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 08:18:37 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 3/8] btrfs: unset reloc control if we fail to recover Date: Wed, 4 Mar 2020 11:18:25 -0500 Message-Id: <20200304161830.2360-4-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200304161830.2360-1-josef@toxicpanda.com> References: <20200304161830.2360-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org If we fail to load an fs root, or fail to start a transaction we can bail without unsetting the reloc control, which leads to problems later when we free the reloc control but still have it attached to the file system. In the normal path we'll end up calling unset_reloc_control() twice, but all it does is set fs_info->reloc_control = NULL, and we can only have one balance at a time so it's not racey. Signed-off-by: Josef Bacik Reviewed-by: Qu Wenruo --- fs/btrfs/relocation.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 2141519a9dd0..c496f8ed8c7e 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -4657,9 +4657,8 @@ int btrfs_recover_relocation(struct btrfs_root *root) trans = btrfs_join_transaction(rc->extent_root); if (IS_ERR(trans)) { - unset_reloc_control(rc); err = PTR_ERR(trans); - goto out_free; + goto out_unset; } rc->merge_reloc_tree = 1; @@ -4679,7 +4678,7 @@ int btrfs_recover_relocation(struct btrfs_root *root) if (IS_ERR(fs_root)) { err = PTR_ERR(fs_root); list_add_tail(&reloc_root->root_list, &reloc_roots); - goto out_free; + goto out_unset; } err = __add_reloc_root(reloc_root); @@ -4690,7 +4689,7 @@ int btrfs_recover_relocation(struct btrfs_root *root) err = btrfs_commit_transaction(trans); if (err) - goto out_free; + goto out_unset; merge_reloc_roots(rc); @@ -4706,6 +4705,8 @@ int btrfs_recover_relocation(struct btrfs_root *root) ret = clean_dirty_subvols(rc); if (ret < 0 && !err) err = ret; +out_unset: + unset_reloc_control(rc); out_free: kfree(rc); out: From patchwork Wed Mar 4 16:18:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11420369 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5D6B9174A for ; Wed, 4 Mar 2020 16:18:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3EC2721739 for ; Wed, 4 Mar 2020 16:18:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="C6sFCe1w" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388285AbgCDQSm (ORCPT ); Wed, 4 Mar 2020 11:18:42 -0500 Received: from mail-qt1-f196.google.com ([209.85.160.196]:38932 "EHLO mail-qt1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729750AbgCDQSm (ORCPT ); Wed, 4 Mar 2020 11:18:42 -0500 Received: by mail-qt1-f196.google.com with SMTP id e13so1772539qts.6 for ; Wed, 04 Mar 2020 08:18:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=gMDYNEH7Lg0r+b8JZcPw/d/uiHJ5zj3M3VFjD7L47cs=; b=C6sFCe1wLw8IspNo3VxcDw5D94hsGmjT7F+lUtvXT9PXQpa7TECoeiPNAE/ZBi0ib7 fwb3hZGXMpjFR7rjSQspGMLuwoqdBz7k03/pCx8H0wzVHdoELu+CZBlhw1Us7tFg1Cgk yaiMg8Q6DBmxv+lPFen8NWCRYXKZfNPti9tbN8wc+AV5OU/3qkobfPoc09tbxnjYGAeU GZxpJ3xGldFwcPf4e39kJAxSatcrjeVdMhyPjO6TEe+WbMIf1pQ9NazDdIl/S9YUA4OH izHxU2xLrLVVXy4hvmxzgCujuteUbModxtLutZOyhzCiok/4pZ2a6RVZa1nfktlje6bj WRVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gMDYNEH7Lg0r+b8JZcPw/d/uiHJ5zj3M3VFjD7L47cs=; b=M9nZz4/9B3juxs3r5BoFwX7qdDlTvvtS3yHe8PowgVioMCpnI1lL3nJfVq4a5iMPol Le+sqJVlxgU91ovHsXhjc0Dscary6BrS0rrT+da+hwcJntm1LC8uJLownH5Tw+l6Mqa6 AoPHtM/GH17aqRW7ZGgp2cAXwEm+ZCtb+TQFJBzSZtbtoO0gaErZIKshsaT3zJsehPuT DwEgTiYomHWFiW7QFfnztCwBRnoRnjXm5ByolKnSm5AXR/Uzg30dMM3tI4P8AZg9mj4V GZ9q6hDNReF26oVZsz004vTnGhyndgKiA5/ed2Q91d34w28IqXP3UOESLVNfr+L3cZbB w6IQ== X-Gm-Message-State: ANhLgQ2NxLBpefVE6s6tw2Xwb5R0GAK3LZg3LBLer1nIG/8z3r89uhol V2Va85QELTkRrlxs40igU/ppASPt2mY= X-Google-Smtp-Source: ADFU+vvcr1GEjxtPjfvbrjQsb8pnfqWQQg+z+l6nHJEXwklyaXYSGE7snmKA+KHDCMz2D1pL+7K2Zw== X-Received: by 2002:ac8:4f43:: with SMTP id i3mr3203406qtw.186.1583338719715; Wed, 04 Mar 2020 08:18:39 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id z2sm1141284qtn.23.2020.03.04.08.18.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 08:18:39 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 4/8] btrfs: free the reloc_control in a consistent way Date: Wed, 4 Mar 2020 11:18:26 -0500 Message-Id: <20200304161830.2360-5-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200304161830.2360-1-josef@toxicpanda.com> References: <20200304161830.2360-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org If we have an error while processing the reloc roots we could leak roots that were added to rc->reloc_roots before we hit the error. We could have also not removed the reloct tree mapping from our rb_tree, so clean up any remaining nodes in the reloc root rb_tree. Signed-off-by: Josef Bacik --- fs/btrfs/relocation.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index c496f8ed8c7e..f6237d885fe0 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -4387,6 +4387,20 @@ static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info) return rc; } +static void free_reloc_control(struct reloc_control *rc) +{ + struct rb_node *rb_node; + struct mapping_node *node; + + free_reloc_roots(&rc->reloc_roots); + while ((rb_node = rb_first(&rc->reloc_root_tree.rb_root))) { + node = rb_entry(rb_node, struct mapping_node, rb_node); + rb_erase(rb_node, &rc->reloc_root_tree.rb_root); + kfree(node); + } + kfree(rc); +} + /* * Print the block group being relocated */ @@ -4531,7 +4545,7 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start) btrfs_dec_block_group_ro(rc->block_group); iput(rc->data_inode); btrfs_put_block_group(rc->block_group); - kfree(rc); + free_reloc_control(rc); return err; } @@ -4708,7 +4722,7 @@ int btrfs_recover_relocation(struct btrfs_root *root) out_unset: unset_reloc_control(rc); out_free: - kfree(rc); + free_reloc_control(rc); out: if (!list_empty(&reloc_roots)) free_reloc_roots(&reloc_roots); From patchwork Wed Mar 4 16:18:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11420371 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 70425174A for ; Wed, 4 Mar 2020 16:18:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4F8B921739 for ; Wed, 4 Mar 2020 16:18:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="wY06ozvD" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388288AbgCDQSn (ORCPT ); Wed, 4 Mar 2020 11:18:43 -0500 Received: from mail-qk1-f193.google.com ([209.85.222.193]:45988 "EHLO mail-qk1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388282AbgCDQSn (ORCPT ); Wed, 4 Mar 2020 11:18:43 -0500 Received: by mail-qk1-f193.google.com with SMTP id z12so2144137qkg.12 for ; Wed, 04 Mar 2020 08:18:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=vMlEv+ThwTDRGC5bEBvf0GHUwzFZBrDT7i4XBc7/BqI=; b=wY06ozvDM0IgWMl6DIMqyNsUxcGPwyPZA63sG8qPjcwSLsF4r2o8+Rwv5DFj1wdXww iQ3I7qQth9tOMhOVDzePGO66Zo1mAMuUWDF5vzgGCaJQwTLpXPNiHlm7a+ZpcD+YJjeo EuaWhjFpiZzRvzuQoluaSH9q+0qhm25e6jHyK6NzTweXYbeleP+vuBtJvxqITrz4zXTH LA0/aPUP8ej8vb7u/JmsW7DSC+N10xfsaHGSaigPNXW08SR0nTcwVcwSDy5x+HhxC0Lf QRrDDX47rYi7KUeAzMWCx0/ZAnh9toMGujhWzs9wR9MlmXSVwqnT/9QV2BwDK5kAz/oB SFxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vMlEv+ThwTDRGC5bEBvf0GHUwzFZBrDT7i4XBc7/BqI=; b=WP6hRJIzY17OSp27N+xbUmiZsyuIg6toEuF8cbhW3uGCVA/ZEN0XBYcei/J5iSYNmc T0tXSIWRlmXFLr+Upsydy/gdoW5dpiyReRZfqfI61i2SSAcEn4sQHO2CeCWRi+aJYWMx IW4zfDSnHNbfsh5S/IY/jxjOJOsQkgxSKVZxU3AaPMQv9K3FRvkoLVR1p+FCmJU47xG9 WzVaxI0aTYOIEFJMGPiTACtNLq34wjXhdSyKkpeiasGxtoPs6oe1q5i0dxyDzz31ua5a doz68xYsqY45rJ+kGZBEJFw4Oq/jplfjhMVHq3z8DqUYk3sf7IWFTKXt+pMWskkEUk0x U4pQ== X-Gm-Message-State: ANhLgQ0/+xQonc/EtbyjFYt7IRXs/j2YPJjofMIIUKeDeLYqYQph4VjY nqKFPkKxmweP3nw+cNG16+TD0p4ip9M= X-Google-Smtp-Source: ADFU+vtEAAlCBpEA1Yzzsk1eYfPcNIumpTA5qCJFMW84nXfwNq0Qb45aqywTCK5ILX6F12wGHtMbyg== X-Received: by 2002:a05:620a:388:: with SMTP id q8mr2718596qkm.488.1583338721845; Wed, 04 Mar 2020 08:18:41 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id g2sm13959356qkb.27.2020.03.04.08.18.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 08:18:40 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 5/8] btrfs: run clean_dirty_subvols if we fail to start a trans Date: Wed, 4 Mar 2020 11:18:27 -0500 Message-Id: <20200304161830.2360-6-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200304161830.2360-1-josef@toxicpanda.com> References: <20200304161830.2360-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org If we do merge_reloc_roots() we could insert a few roots onto the dirty subvol roots list, where we hold a ref on them. If we fail to start the transaction we need to run clean_dirty_subvols() in order to cleanup the refs. Signed-off-by: Josef Bacik Reviewed-by: Qu Wenruo --- fs/btrfs/relocation.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index f6237d885fe0..53509c367eff 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -4279,10 +4279,10 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc) goto out_free; } btrfs_commit_transaction(trans); +out_free: ret = clean_dirty_subvols(rc); if (ret < 0 && !err) err = ret; -out_free: btrfs_free_block_rsv(fs_info, rc->block_rsv); btrfs_free_path(path); return err; @@ -4711,6 +4711,7 @@ int btrfs_recover_relocation(struct btrfs_root *root) trans = btrfs_join_transaction(rc->extent_root); if (IS_ERR(trans)) { + clean_dirty_subvols(rc); err = PTR_ERR(trans); goto out_free; } From patchwork Wed Mar 4 16:18:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11420373 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EC1B617E0 for ; Wed, 4 Mar 2020 16:18:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CC4CF215A4 for ; Wed, 4 Mar 2020 16:18:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="ZModImvC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388293AbgCDQSp (ORCPT ); Wed, 4 Mar 2020 11:18:45 -0500 Received: from mail-qt1-f193.google.com ([209.85.160.193]:42788 "EHLO mail-qt1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388282AbgCDQSo (ORCPT ); Wed, 4 Mar 2020 11:18:44 -0500 Received: by mail-qt1-f193.google.com with SMTP id r6so1755877qtt.9 for ; Wed, 04 Mar 2020 08:18:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=52QuRYoyyZaAbgdd8ICEkLGKU9LCKKQnjvmxbkT+rQk=; b=ZModImvC9EYCeSfrNgTkZaCshLFEPiqRxwBKqSp4cViVgckV+i+GzwhY0SreJB4xb7 lltVyysdUhVtjLFxKHcYlU1/vmjT1TK4GLq0Jf/dlPBhhVwdVP5E7iA6aqkPRGFLHiVx 3XfL/++YKBYdzG0z5p++EeUrVVEbgvPkJEPjZwopPEwUpzT3Bg/L26GWCl8yWS8uOZ4s ug91Bv/lQtOkbsh9BmIaXIIhuuzumuH3f7ulRDaYm+vjL8YlDFnnDzo7JygUI2C3sKaW XBIQDG10WWCgpDgd8HQP/d//KKp0Zg7Ns7SMWPJX2nCU1rlLHjXobcS6n6Lc5hIJvIoC f5ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=52QuRYoyyZaAbgdd8ICEkLGKU9LCKKQnjvmxbkT+rQk=; b=S5MxAmRr8A1K5sjMMHo4Znn33lHq/uHS9W1R2H9+r+upQaK4nmnDpKAFUCNLx1Qpxp Q4Y6SW8TLddnUqLYrLd0UfX+hkzdlSMYj30CjpZ/GHMEHqgU5SEEHcg/MMOZj6xFIVdS opXwrpPqEAzIGMkB/vYKa56tDUVm/MCDiuR/O/087T6cOKW7avSSKiidQZpCswsPQxxv wknezwpDFBMBnGENCDa3qkh+NT0D7Hjj5DTTDWXYO371ubGltFHwPacs1i8XyvIDBCdC yh1T7/ylWd/IKCTLFDdiL7s3EI1hQVmIsBH1FM1MhmFjaVhZ7noyZTRyYGQZI1QD1QB7 +Kig== X-Gm-Message-State: ANhLgQ0x7e4bao5UrAY+HZy3Iq3xJiwVcCt6VNH05i1VVG9nZDkDE/pt YydBghCPjBvUJVmv171mNviCcSj1Hoo= X-Google-Smtp-Source: ADFU+vtvnKUd2mPJuXYzTfMEj7IuOruFYk2/OZsmyydoXrRgSY8u0M4InV9bxpEq6ym0kBge2JJYdw== X-Received: by 2002:ac8:6e88:: with SMTP id c8mr3205707qtv.40.1583338723505; Wed, 04 Mar 2020 08:18:43 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id a6sm12659991qkn.104.2020.03.04.08.18.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 08:18:42 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 6/8] btrfs: clear BTRFS_ROOT_DEAD_RELOC_TREE before dropping the reloc root Date: Wed, 4 Mar 2020 11:18:28 -0500 Message-Id: <20200304161830.2360-7-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200304161830.2360-1-josef@toxicpanda.com> References: <20200304161830.2360-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org We were doing the clear dance for the reloc root after doing the drop of the reloc root, which means we have a giant window where we could miss having BTRFS_ROOT_DEAD_RELOC_TREE unset and the reloc_root == NULL. Signed-off-by: Josef Bacik --- fs/btrfs/relocation.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 53509c367eff..ceb90d152cdd 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -2291,18 +2291,19 @@ static int clean_dirty_subvols(struct reloc_control *rc) list_del_init(&root->reloc_dirty_list); root->reloc_root = NULL; - if (reloc_root) { - - ret2 = btrfs_drop_snapshot(reloc_root, NULL, 0, 1); - if (ret2 < 0 && !ret) - ret = ret2; - } /* * Need barrier to ensure clear_bit() only happens after * root->reloc_root = NULL. Pairs with have_reloc_root. */ smp_wmb(); clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state); + + if (reloc_root) { + + ret2 = btrfs_drop_snapshot(reloc_root, NULL, 0, 1); + if (ret2 < 0 && !ret) + ret = ret2; + } btrfs_put_root(root); } else { /* Orphan reloc tree, just clean it up */ From patchwork Wed Mar 4 16:18:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11420377 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E699A174A for ; Wed, 4 Mar 2020 16:18:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BED73215A4 for ; Wed, 4 Mar 2020 16:18:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="BxB7DSzU" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388296AbgCDQSs (ORCPT ); Wed, 4 Mar 2020 11:18:48 -0500 Received: from mail-qv1-f67.google.com ([209.85.219.67]:43887 "EHLO mail-qv1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388282AbgCDQSr (ORCPT ); Wed, 4 Mar 2020 11:18:47 -0500 Received: by mail-qv1-f67.google.com with SMTP id eb12so1021207qvb.10 for ; Wed, 04 Mar 2020 08:18:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=qChTNs5PQFgZpYfNDpPgq1Z7ZD8K2qPiNyh8Z2WttIM=; b=BxB7DSzUZnOF1sWWAyFsREA6BvOaUYX21CesNPXk9ZBolr1QCmwFc39sWTZS++1ypI mtV1vMnMZQSz903/7Or66t8ianqacysg0UfruNvEGKIX95ebFYcNZF6Bs2zez73t2/7D PkEzRCy3rKARUch6yZ3Rcm5JftSPt3/oNEYc0BpUeEub36q3xkvxoDJY+oFMS+SSxr/f NvBg0pCSPXIBfiuxCvyOmXTFhr09+28tFyT3tzJZZn2SOn9svbazPlr+zlfQfmpb3Phx piWfqFCVnLS5q6IIJXSnoKXYVARH17SG3nVNZWf1kmkEfLZ9iK0Hf1qvueJhmgV+xkoh 7wZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qChTNs5PQFgZpYfNDpPgq1Z7ZD8K2qPiNyh8Z2WttIM=; b=Sim61vjXlQubSjsvkI1RVeKFk1+8UAN6UdK0DKqo/3JGkCt+AUzTumHL3MD191/4cW /GsyPfTDFQM3d5gGUrnMgiT91SCAZwgxP7/93LLOUwUJ1T3XSgOrfmR8aPmGqU6SmLGm LDNor66wC+N067NCD+SDYBZHb6c5qVm9sTGQQOTe4mvJhcqoagVgS6RT42RT7n3IJyyX +dyOj+g5Yt/ISutgGs5ZIed7M5B6CNY/gdEx/9QbiHROVkY6pxQixTuEPhz9kWCHEs1U aGU7W9auYBpAKw/3lKMT/jRqZPXu2ukmgIqgJC57t+acKxIrPscJyYxgHwgjFJps4Dfn gVlw== X-Gm-Message-State: ANhLgQ1hsMKr4x9vFfcTcWK+mQvggqA+sbGa/feOho3zN90Kgob8SaUF QJsgQMMP0VBFxWNXWvd23PFtnly59qg= X-Google-Smtp-Source: ADFU+vt8BH+9N2Xf8yMfCQr7RGYeZqtTE/RHI2/1Kno690IoK2MX76x3SsLvXIFi8Nw4v4SIfZdPkg== X-Received: by 2002:a0c:9081:: with SMTP id p1mr536732qvp.38.1583338725157; Wed, 04 Mar 2020 08:18:45 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id t29sm14945942qtt.20.2020.03.04.08.18.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 08:18:44 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 7/8] btrfs: hold a ref on the root->reloc_root Date: Wed, 4 Mar 2020 11:18:29 -0500 Message-Id: <20200304161830.2360-8-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200304161830.2360-1-josef@toxicpanda.com> References: <20200304161830.2360-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org We previously were relying on root->reloc_root to be cleaned up by the drop snapshot, or the error handling. However if btrfs_drop_snapshot() failed it wouldn't drop the ref for the root. Also we sort of depend on the right thing to happen with moving reloc roots between lists and the fs root they belong to, which makes it hard to figure out who owns the reference. Fix this by explicitly holding a reference on the reloc root for roo->reloc_root. This means that we hold two references on reloc roots, one for whichever reloc_roots list it's attached to, and the root->reloc_root we're on. This makes it easier to reason out who owns a reference on the root, and when it needs to be dropped. Signed-off-by: Josef Bacik --- fs/btrfs/relocation.c | 59 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index ceb90d152cdd..0f19a22d7f44 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -1384,6 +1384,7 @@ static void __del_reloc_root(struct btrfs_root *root) struct rb_node *rb_node; struct mapping_node *node = NULL; struct reloc_control *rc = fs_info->reloc_ctl; + bool put_ref = false; if (rc && root->node) { spin_lock(&rc->reloc_root_tree.lock); @@ -1399,9 +1400,22 @@ static void __del_reloc_root(struct btrfs_root *root) BUG_ON((struct btrfs_root *)node->data != root); } + /* + * We only put the reloc root here if it's on the list. There's a lot + * of places where the pattern is to splice the rc->reloc_roots, process + * the reloc roots, and then add the reloc root back onto + * rc->reloc_roots. If we call __del_reloc_root while it's off of the + * list we don't want the reference being dropped, because the guy + * messing with the list is in charge of the reference. + */ spin_lock(&fs_info->trans_lock); - list_del_init(&root->root_list); + if (!list_empty(&root->root_list)) { + put_ref = true; + list_del_init(&root->root_list); + } spin_unlock(&fs_info->trans_lock); + if (put_ref) + btrfs_put_root(root); kfree(node); } @@ -1516,6 +1530,9 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, /* * create reloc tree for a given fs tree. reloc tree is just a * snapshot of the fs tree with special root objectid. + * + * The reloc_root comes out of here with two references, one for + * root->reloc_root, and another for being on the rc->reloc_roots list. */ int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, struct btrfs_root *root) @@ -1555,7 +1572,7 @@ int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, ret = __add_reloc_root(reloc_root); BUG_ON(ret < 0); - root->reloc_root = reloc_root; + root->reloc_root = btrfs_grab_root(reloc_root); return 0; } @@ -1576,6 +1593,13 @@ int btrfs_update_reloc_root(struct btrfs_trans_handle *trans, reloc_root = root->reloc_root; root_item = &reloc_root->root_item; + /* + * We are probably ok here, but __del_reloc_root() will drop its ref of + * the root. We have the ref fro root->reloc_root, but just in case + * hold it while we update the reloc root. + */ + btrfs_grab_root(reloc_root); + /* root->reloc_root will stay until current relocation finished */ if (fs_info->reloc_ctl->merge_reloc_tree && btrfs_root_refs(root_item) == 0) { @@ -1597,7 +1621,7 @@ int btrfs_update_reloc_root(struct btrfs_trans_handle *trans, ret = btrfs_update_root(trans, fs_info->tree_root, &reloc_root->root_key, root_item); BUG_ON(ret); - + btrfs_put_root(reloc_root); out: return 0; } @@ -2297,19 +2321,28 @@ static int clean_dirty_subvols(struct reloc_control *rc) */ smp_wmb(); clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state); - if (reloc_root) { - + /* + * btrfs_drop_snapshot drops our ref we hold for + * ->reloc_root. If it fails however we must + * drop the ref ourselves. + */ ret2 = btrfs_drop_snapshot(reloc_root, NULL, 0, 1); - if (ret2 < 0 && !ret) - ret = ret2; + if (ret2 < 0) { + btrfs_put_root(reloc_root); + if (!ret) + ret = ret2; + } } btrfs_put_root(root); } else { /* Orphan reloc tree, just clean it up */ ret2 = btrfs_drop_snapshot(root, NULL, 0, 1); - if (ret2 < 0 && !ret) - ret = ret2; + if (ret2 < 0) { + btrfs_put_root(root); + if (!ret) + ret = ret2; + } } } return ret; @@ -4698,7 +4731,7 @@ int btrfs_recover_relocation(struct btrfs_root *root) err = __add_reloc_root(reloc_root); BUG_ON(err < 0); /* -ENOMEM or logic error */ - fs_root->reloc_root = reloc_root; + fs_root->reloc_root = btrfs_grab_root(reloc_root); btrfs_put_root(fs_root); } @@ -4888,6 +4921,10 @@ void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending, /* * called after snapshot is created. migrate block reservation * and create reloc root for the newly created snapshot + * + * This is similar to btrfs_init_reloc_root(), we come out of here with two + * references held on the reloc_root, one for root->reloc_root and one for + * rc->reloc_roots. */ int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans, struct btrfs_pending_snapshot *pending) @@ -4920,7 +4957,7 @@ int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans, ret = __add_reloc_root(reloc_root); BUG_ON(ret < 0); - new_root->reloc_root = reloc_root; + new_root->reloc_root = btrfs_grab_root(reloc_root); if (rc->create_reloc_tree) ret = clone_backref_node(trans, rc, root, reloc_root); From patchwork Wed Mar 4 16:18:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11420375 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 607A617E0 for ; Wed, 4 Mar 2020 16:18:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 417FF21739 for ; Wed, 4 Mar 2020 16:18:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="TCSRF1PD" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388300AbgCDQSs (ORCPT ); Wed, 4 Mar 2020 11:18:48 -0500 Received: from mail-qv1-f67.google.com ([209.85.219.67]:42428 "EHLO mail-qv1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388254AbgCDQSs (ORCPT ); Wed, 4 Mar 2020 11:18:48 -0500 Received: by mail-qv1-f67.google.com with SMTP id e7so1026308qvy.9 for ; Wed, 04 Mar 2020 08:18:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=Qu49wBlHMUVbjHma7QtYYz1SYOlxzwRAzTtjNU9X+z8=; b=TCSRF1PDiDcRySQnv6ehUR4QSll0z9Hq8HYwucTJImKdtCUAg2SRBaGMXZ8ST9osPO GlV7W1H50E9895MwnpgDC2swpVgk/F2n7L6z6+MuD8NqH+9y5RgEOLUnrzD6oekn2om0 ZfcFkNyVtFKW9h+9Co9e9RCmXv7M3AnHwhVPmm76Z9jLlRAm5LynbXhFcCtkGl6lmzFl LscM7Nt675pdGXgB7acvrQtTd3feYfvI1CGxrFWv9oszPhxAhW/u5kqWL6xVJMP1iJ4S dXq00SMa5AcmE2pC3RBI4VyqnIo+AGf3IBSAE0nVrzipqpyre1DRNh6YZtQrp/7ocDGI AvDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Qu49wBlHMUVbjHma7QtYYz1SYOlxzwRAzTtjNU9X+z8=; b=qfzop5rqj2gPgZaloRQP5G96wP2ErgnEZ2f+B1PicZ4ydFWYh9BH8Qrj/Qk87FkxtO kMViZbp4MDdYHaZhwhb4KpzG/aWhU5cITNmN+S0tH9Xmlid5K/BzRGcu4/NK8NtA2qpf R1LoISXurfFG34tAdaG0VYMZ4FgpEXkFi5ifCL+M5KvKDH8IFy4dcczO2P5ijpxWB6hi kjZCr67OPeSsCvwEB1FAkeX4r3IkpqPD31g+ey0HyqsUigzpMW2FT9omZUGbU012SEmr mdc6TeFmgF28nwQc5XpeF3InzSCO6icMrjGz0bWeylc4iomxTIYfhgrtSw45t8OYqxcv nyuw== X-Gm-Message-State: ANhLgQ2oGi1cnGGD5dpPyqHeeMvLciM1Fvm1y5k2bsyB+zjq4J+oA6Wp 2xq4KIB/jEAYx06R+yIev+/YeFozwXI= X-Google-Smtp-Source: ADFU+vsDMi3m6Tdvqm97pqzAVG4WQ3wvE1r8JlT6rZ+MWSc4vVIzctCT93iBpjaEFb5m7ym0y/Bx/w== X-Received: by 2002:ad4:4a69:: with SMTP id cn9mr2789930qvb.218.1583338726856; Wed, 04 Mar 2020 08:18:46 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id e7sm5412102qtp.0.2020.03.04.08.18.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 08:18:46 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 8/8] btrfs: remove a BUG_ON() from merge_reloc_roots() Date: Wed, 4 Mar 2020 11:18:30 -0500 Message-Id: <20200304161830.2360-9-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200304161830.2360-1-josef@toxicpanda.com> References: <20200304161830.2360-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org This was pretty subtle, we default to reloc roots having 0 root refs, so if we crash in the middle of the relocation they can just be deleted. If we successfully complete the relocation operations we'll set our root refs to 1 in prepare_to_merge() and then go on to merge_reloc_roots(). At prepare_to_merge() time if any of the reloc roots have a 0 reference still, we will remove that reloc root from our reloc root rb tree, and then clean it up later. However this only happens if we successfully start a transaction. If we've aborted previously we will skip this step completely, and only have reloc roots with a reference count of 0, but were never properly removed from the reloc control's rb tree. This isn't a problem per-se, our references are held by the list the reloc roots are on, and by the original root the reloc root belongs to. If we end up in this situation all the reloc roots will be added to the dirty_reloc_list, and then properly dropped at that point. The reloc control will be free'd and the rb tree is no longer used. There were two options when fixing this, one was to remove the BUG_ON(), the other was to make prepare_to_merge() handle the case where we couldn't start a trans handle. IMO this is the cleaner solution. I started with handling the error in prepare_to_merge(), but it turned out super ugly. And in the end this BUG_ON() simply doesn't matter, the cleanup was happening properly, we were just panicing because this BUG_ON() only matters in the success case. So I've opted to just remove it and add a comment where it was. Signed-off-by: Josef Bacik --- fs/btrfs/relocation.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 0f19a22d7f44..a2b26cf9ee5b 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -2653,7 +2653,21 @@ void merge_reloc_roots(struct reloc_control *rc) free_reloc_roots(&reloc_roots); } - BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root)); + /* + * We used to have + * + * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root)); + * + * here, but it's wrong. If we fail to start the transaction in + * prepare_to_merge() we will have only 0 ref reloc roots, none of which + * have actually been removed from the reloc_root_tree rb tree. This is + * fine because we're bailing here, and we hold a reference on the root + * for the list that holds it, so these roots will be cleaned up when we + * do the reloc_dirty_list afterwards. Meanwhile the root->reloc_root + * will be cleaned up on unmount. + * + * The remaining nodes will be cleaned up by free_reloc_control. + */ } static void free_block_list(struct rb_root *blocks)