From patchwork Mon Jun 28 10:16:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 12347665 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C21BCC2B9F4 for ; Mon, 28 Jun 2021 10:16:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A4E7C61C6C for ; Mon, 28 Jun 2021 10:16:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232431AbhF1KTK (ORCPT ); Mon, 28 Jun 2021 06:19:10 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:39188 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232483AbhF1KTI (ORCPT ); Mon, 28 Jun 2021 06:19:08 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id C337E20251 for ; Mon, 28 Jun 2021 10:16:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1624875402; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=q5oR14tc2HFaKMVUV1iG68+XjG7B/JnKLJjsZ4e70OQ=; b=Bu5TuitQyrmVQ/iBxGG8VSdqbQIYRIKNjO3krgiE8X81C8SUt9wR+MF9ARMsupzQjP1rwr LGDhO48YEcB9oHb5MCqL4FKPJTqsKq/AMRyla1PFzkcmvo2PYgFR1lQcOXLjYmcqPDowG+ JMi3GR3Z5BB9j1FX86Txn7e8ZHvLdz4= Received: from adam-pc.lan (unknown [10.163.16.38]) by relay2.suse.de (Postfix) with ESMTP id B1E23A3B8E for ; Mon, 28 Jun 2021 10:16:41 +0000 (UTC) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/3] btrfs: return -EINVAL if some user wants to remove uuid/data_reloc tree Date: Mon, 28 Jun 2021 18:16:35 +0800 Message-Id: <20210628101637.349718-2-wqu@suse.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210628101637.349718-1-wqu@suse.com> References: <20210628101637.349718-1-wqu@suse.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Ioctl BTRFS_IOC_SNAP_DESTROY_V2 supports a flag to delete a subvolume using root id directly. We check the target root id against BTRFS_FIRST_FREE_OBJECTID, but not again BTRFS_LAST_FREE_OBJECTID. This means if user passes rootid like DATA_RELOC (-9) or TREE_RELOC (-8), we can pass the check, then get caught by later dentry check and got error number -ENOENT, other than -EINVAL. It's not a big deal as we have extra safe nets to prevent those trees get removed, it's still better to do the extra check and return proper -EINVAL error. Signed-off-by: Qu Wenruo Reviewed-by: Anand Jain --- fs/btrfs/ioctl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 0ba98e08a029..889e27c24e3a 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -2932,7 +2932,8 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file, if (err) goto out; } else { - if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) { + if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID || + vol_args2->subvolid > BTRFS_LAST_FREE_OBJECTID) { err = -EINVAL; goto out; } From patchwork Mon Jun 28 10:16:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 12347667 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06992C49EA3 for ; Mon, 28 Jun 2021 10:16:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E3E1D61C6A for ; Mon, 28 Jun 2021 10:16:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232572AbhF1KTL (ORCPT ); Mon, 28 Jun 2021 06:19:11 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:39194 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232415AbhF1KTK (ORCPT ); Mon, 28 Jun 2021 06:19:10 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 6E46220252 for ; Mon, 28 Jun 2021 10:16:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1624875404; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xax51JurA1q0fUV4YPZytFXEDWB2LjndnmsGQLex+o8=; b=P4bykaqzgo/Fn0NNCyXU3HeH4b/buEO9SroShHrNC4IamcU+gsiOvkAjmyeaL3IpZ4XKra uRE3263bcdHo8MaijNc5VAs74QtsRtNERl4VLm6iieb12BtVX/Fpg43k668QnMs3twg6+d e+XMAPm45OKmxh6y3KljXJlif2Ys/t4= Received: from adam-pc.lan (unknown [10.163.16.38]) by relay2.suse.de (Postfix) with ESMTP id 66017A3B8E for ; Mon, 28 Jun 2021 10:16:43 +0000 (UTC) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/3] btrfs: remove dead comment on btrfs_add_dead_root() Date: Mon, 28 Jun 2021 18:16:36 +0800 Message-Id: <20210628101637.349718-3-wqu@suse.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210628101637.349718-1-wqu@suse.com> References: <20210628101637.349718-1-wqu@suse.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org The old comment is from the initial merge of btrfs, but since commit 5d4f98a28c7d ("Btrfs: Mixed back reference (FORWARD ROLLING FORMAT CHANGE)") changed the behavior to not to allocate any extra memory, the comment on the memory allocation part is out-of-date. Fix it by removing the dead part and change it to modern behavior. Signed-off-by: Qu Wenruo Reviewed-by: Anand Jain --- fs/btrfs/transaction.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c index 73df8b81496e..29316c062237 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c @@ -1319,9 +1319,10 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans) } /* - * dead roots are old snapshots that need to be deleted. This allocates - * a dirty root struct and adds it into the list of dead roots that need to - * be deleted + * Dead roots are old snapshots that need to be deleted. + * + * This helper will queue them to the dead_roots list to be deleted by + * cleaner thread. */ void btrfs_add_dead_root(struct btrfs_root *root) { From patchwork Mon Jun 28 10:16:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 12347669 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10EE2C49EAB for ; Mon, 28 Jun 2021 10:16:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ECA7C61C6B for ; Mon, 28 Jun 2021 10:16:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232675AbhF1KTP (ORCPT ); Mon, 28 Jun 2021 06:19:15 -0400 Received: from smtp-out1.suse.de ([195.135.220.28]:35906 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232605AbhF1KTM (ORCPT ); Mon, 28 Jun 2021 06:19:12 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 1E273223FC for ; Mon, 28 Jun 2021 10:16:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1624875406; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QzDkYtdX82z2XHq8+BV0IyHXt/NwHxaEx1LvFR5qx2U=; b=henJGC4RoqsJmrvvzSvkGbpS85OBxRrEZSaxr9ua+g6ntuuFgTLmAH/R7MQf4WSnZND5KI CbRQi/DEO8wCtvt8nr/WSteadq80S2ZsZioi57cQEcFwdjoxo+wK032INQ7VX22zsuxPHb qlwMKI5OHRgtM71chyjl8GOa/ZnaScI= Received: from adam-pc.lan (unknown [10.163.16.38]) by relay2.suse.de (Postfix) with ESMTP id 2A797A3B8E for ; Mon, 28 Jun 2021 10:16:44 +0000 (UTC) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 3/3] btrfs: allow BTRFS_IOC_SNAP_DESTROY_V2 to remove ghost subvolume Date: Mon, 28 Jun 2021 18:16:37 +0800 Message-Id: <20210628101637.349718-4-wqu@suse.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210628101637.349718-1-wqu@suse.com> References: <20210628101637.349718-1-wqu@suse.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org There is a report from the mail list that some subvolumes don't have any ROOT_REF/BACKREF and has 0 ref. But without an ORPHAN item. Such ghost subvolumes can't be deleted by any ioctl but only rely on btrfs-progs to add ORPHAN item. Normally kernel only needs to gracefully abort/reject such corrupted structure, but in this case we have all the needed infrastructures and interface to allow BTRFS_IOC_SNAP_DESTROY_V2 to delete it. So add the ability to delete such ghost subvolumes to BTRFS_IOC_SNAP_DESTROY_V2. Signed-off-by: Qu Wenruo Reported-by: kernel test robot Reported-by: kernel test robot Reported-by: kernel test robot Reported-by: Dan Carpenter --- fs/btrfs/ioctl.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 889e27c24e3a..06d3c293cffd 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -2892,6 +2892,81 @@ static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp) return ret; } +/* + * Special case that some subvolume has missing ORPHAN_ITEM, but its refs is + * already 0 (without any ROOT_REF/BACKREF). + * In that case such subvolume is only taking space while unable to be deleted. + * + * No reproducer to reproduce such corruption, but it won't hurt to cleanup them + * as we can reuse existing code since we only need to insert an orphan item and + * queue them to be deleted. + */ +static int __cold remove_ghost_subvol(struct btrfs_fs_info *fs_info, + u64 rootid) +{ + struct btrfs_trans_handle *trans; + struct btrfs_root *root; + struct btrfs_path *path; + struct btrfs_key key; + int ret; + + root = btrfs_get_fs_root(fs_info, rootid, false); + if (IS_ERR(root)) { + ret = PTR_ERR(root); + return ret; + } + + /* A ghost subvolume is already a problem, better to output a warning */ + btrfs_warn(fs_info, "root %llu has no refs nor orphan item", rootid); + if (btrfs_root_refs(&root->root_item) != 0) { + /* We get some strange root */ + btrfs_warn(fs_info, + "root %llu has %u refs, but no proper root backref", + rootid, btrfs_root_refs(&root->root_item)); + ret = -EUCLEAN; + goto out; + } + + /* Already has orphan inserted */ + if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) + goto out; + + path = btrfs_alloc_path(); + if (!path) { + ret = -ENOMEM; + goto out; + } + key.objectid = BTRFS_ORPHAN_OBJECTID; + key.type = BTRFS_ORPHAN_ITEM_KEY; + key.offset = rootid; + + ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); + btrfs_free_path(path); + /* Either error or there is already an orphan item */ + if (ret <= 0) + goto out; + + trans = btrfs_start_transaction(fs_info->tree_root, 1); + if (IS_ERR(trans)) { + ret = PTR_ERR(trans); + goto out; + } + + ret = btrfs_insert_orphan_item(trans, fs_info->tree_root, rootid); + if (ret < 0 && ret != -EEXIST) { + btrfs_abort_transaction(trans, ret); + goto end_trans; + } + ret = 0; + btrfs_add_dead_root(root); + +end_trans: + btrfs_end_transaction(trans); +out: + btrfs_put_root(root); + return ret; +} + static noinline int btrfs_ioctl_snap_destroy(struct file *file, void __user *arg, bool destroy_v2) @@ -2947,6 +3022,9 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file, vol_args2->subvolid, 0, 0); if (IS_ERR(dentry)) { err = PTR_ERR(dentry); + if (err == -ENOENT) + err = remove_ghost_subvol(fs_info, + vol_args2->subvolid); goto out_drop_write; }