From patchwork Mon Feb 28 00:50:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 12762258 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B4545C433F5 for ; Mon, 28 Feb 2022 00:50:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232116AbiB1AvH (ORCPT ); Sun, 27 Feb 2022 19:51:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231580AbiB1AvG (ORCPT ); Sun, 27 Feb 2022 19:51:06 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B00540A1D for ; Sun, 27 Feb 2022 16:50:29 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 3746F218CE; Mon, 28 Feb 2022 00:50:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1646009428; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IgnRmZ97myXG5YuGyPvhBiObSDv0yoz/8hl3GPMshpU=; b=nm9JbPQA48Os0gMoWz2Qtf939/GZWI3HfAB7dnVfCpQ0dAhri81tAoQSpKUhRZ3W6y7Z3e 3DbANzbwy3nT1YI2/k4L40AY0d8Icr5SKRtMXe3HfyzPZMXjNeWljbGTeNtepQu8w+HzX+ sybHEofTjXIgaYhE+qKvDZGHIszP9CQ= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 59CA9139BD; Mon, 28 Feb 2022 00:50:27 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id gE/WCVMcHGJ3OwAAMHmgww (envelope-from ); Mon, 28 Feb 2022 00:50:27 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: =?utf-8?q?Luca_B=C3=A9la_Palkovics?= Subject: [PATCH 1/2] btrfs-progs: check: add check and repair ability for super num devs mismatch Date: Mon, 28 Feb 2022 08:50:07 +0800 Message-Id: <029df99dabfee5b8fc602bf284bb3ea364176418.1646009185.git.wqu@suse.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org [BUG] There is a bug report of kernel rejecting fs which has a mismatch in super num devices and num devices found in chunk tree. But btrfs-check reports no problem about the fs. [CAUSE] We just didn't verify super num devices against the result found in chunk tree. [FIX] Add such check and repair ability for btrfs-check. The ability is mode independent. Reported-by: Luca Béla Palkovics Link: https://lore.kernel.org/linux-btrfs/CA+8xDSpvdm_U0QLBAnrH=zqDq_cWCOH5TiV46CKmp3igr44okQ@mail.gmail.com/ Signed-off-by: Qu Wenruo --- check/main.c | 1 + check/mode-common.c | 88 +++++++++++++++++++++++++++++++++++++++++++++ check/mode-common.h | 2 ++ 3 files changed, 91 insertions(+) diff --git a/check/main.c b/check/main.c index 8ccba4478de8..b29f6266b974 100644 --- a/check/main.c +++ b/check/main.c @@ -9140,6 +9140,7 @@ static int do_check_chunks_and_extents(void) if (ret > 0) ret = 0; } + ret = check_and_repair_super_num_devs(gfs_info); return ret; } diff --git a/check/mode-common.c b/check/mode-common.c index c3d8bb45c6b6..a2c6c7732f4e 100644 --- a/check/mode-common.c +++ b/check/mode-common.c @@ -1583,3 +1583,91 @@ int fill_csum_tree(struct btrfs_trans_handle *trans, bool search_fs_tree) } return ret; } + +static int get_num_devs_in_chunk_tree(struct btrfs_fs_info *fs_info) +{ + struct btrfs_root *chunk_root = fs_info->chunk_root; + struct btrfs_path path = {0}; + struct btrfs_key key = {0}; + int found_devs = 0; + int ret; + + ret = btrfs_search_slot(NULL, chunk_root, &key, &path, 0, 0); + if (ret < 0) + return ret; + + /* We should be the first slot, and chunk tree should not be empty*/ + ASSERT(path.slots[0] == 0 && btrfs_header_nritems(path.nodes[0])); + + btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); + + while (key.objectid == BTRFS_DEV_ITEMS_OBJECTID && + key.type == BTRFS_DEV_ITEM_KEY) { + found_devs++; + + ret = btrfs_next_item(chunk_root, &path); + if (ret < 0) + break; + + /* + * This should not happen, as we should have CHUNK items after + * dev items, but since we're only to get the num devices, + * no need to bother the problem. + */ + if (ret > 0) { + ret = 0; + break; + } + btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); + } + btrfs_release_path(&path); + if (ret < 0) + return ret; + return found_devs; +} + +int check_and_repair_super_num_devs(struct btrfs_fs_info *fs_info) +{ + struct btrfs_trans_handle *trans; + int found_devs; + int ret; + + ret = get_num_devs_in_chunk_tree(fs_info); + if (ret < 0) + return ret; + + found_devs = ret; + + if (found_devs == btrfs_super_num_devices(fs_info->super_copy)) + return 0; + + /* Now the found devs in chunk tree mismatch with super block*/ + error("super num devices mismatch, have %llu expect %u", + btrfs_super_num_devices(fs_info->super_copy), + found_devs); + + if (!repair) + return -EUCLEAN; + + /* + * Repair is pretty simple, just reset the super block value and + * commit a new transaction. + */ + trans = btrfs_start_transaction(fs_info->tree_root, 0); + if (IS_ERR(trans)) { + ret = PTR_ERR(trans); + errno = -ret; + error("failed to start trans: %m"); + return ret; + } + btrfs_set_super_num_devices(fs_info->super_copy, found_devs); + ret = btrfs_commit_transaction(trans, fs_info->tree_root); + if (ret < 0) { + errno = -ret; + error("failed to commit trans: %m"); + btrfs_abort_transaction(trans, ret); + return ret; + } + printf("Successfully reset super num devices to %u\n", found_devs); + return 0; +} diff --git a/check/mode-common.h b/check/mode-common.h index b5e6b727fe73..d5bab85a4f5e 100644 --- a/check/mode-common.h +++ b/check/mode-common.h @@ -201,4 +201,6 @@ int repair_dev_item_bytes_used(struct btrfs_fs_info *fs_info, int fill_csum_tree(struct btrfs_trans_handle *trans, bool search_fs_tree); +int check_and_repair_super_num_devs(struct btrfs_fs_info *fs_info); + #endif From patchwork Mon Feb 28 00:50:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 12762259 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0EFF5C433EF for ; Mon, 28 Feb 2022 00:50:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232126AbiB1AvJ (ORCPT ); Sun, 27 Feb 2022 19:51:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232125AbiB1AvI (ORCPT ); Sun, 27 Feb 2022 19:51:08 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8493A40A1D for ; Sun, 27 Feb 2022 16:50:30 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 4248D1F892 for ; Mon, 28 Feb 2022 00:50:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1646009429; 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=jg7C+TnPqCSQI45Du40pWldaGcYFUQkqmMHepyGSqNo=; b=sB9zSJA+nHOwlqn2mt48dVCRsrPfVE0VawYoDRdtg8/JUpveg/dMUZLANeMNtW2HpZOF/R vaXDVRXUe2cWLoFfNWynVlRThUC+INnLGo9h7D3IlUk5luuIvVpoyBY0y/r3XTklQ1F+dx osXLkNoCPBdRntrO3ikyByN5NQ8704g= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 97579139BD for ; Mon, 28 Feb 2022 00:50:28 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id KFW1GFQcHGJ3OwAAMHmgww (envelope-from ) for ; Mon, 28 Feb 2022 00:50:28 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/2] btrfs-progs: tests/fsck: add test case for super num devs mismatch Date: Mon, 28 Feb 2022 08:50:08 +0800 Message-Id: X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org The new image will has incorrect super num devices (have 8 expect 1). The image has to be raw format, as btrfs-image restore will reset super num devices to correct value. Signed-off-by: Qu Wenruo --- .../054-super-num-devs-mismatch/default.raw.xz | Bin 0 -> 22028 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/fsck-tests/054-super-num-devs-mismatch/default.raw.xz GIT binary patch literal 22028 zcmeHPXH=8vwoas@6sc0BNCas?L8=&vREg3F0s#a8=}bTvX@U|ugdzw^FQIoOf*4VH z5oywkB8Uc%CP>SfIdkT&`(vD|&c(xz?{8MJ-j#Pf``P<>-o5v(^sgqSAQ0*7V71yI z5HFY$1Om~&H0dM~Pmng6gFvpkL}K?IR&tG zg@0RkxQ=)>&2T{8+erSS`csHwO`hRzZfrhG2sm;bKY`M;G0V8hMaK3fLfiMj?R7HR z{0mR({jztiJw*s5s80C=@O-Z16qMvjZu2W3S;^^9Tn~_JFIMQv8Q&rANbSgEI1fg; zCB{~dFdzDWW^^>NL=Nba_1PC$1i&>lT8?&q=4hEZHV1R%3Nuph*Afyh&Vis@o zoD^IZ7%UKSg&yUb)LQLqh`*d!Wc3A`ovO(HaDA&c5|-YVuRX(|KG@H!(LfDx5~c{> zup7Oe+mxT~=$mYf%T?Nx^*65G4kDi@NL*f65L<+uT5fMoPPLO}Ud4!+ zcUQ}=}^qq?}S@F;&7*FU!@WJ4t%*m25fvPS;Xizi~pLOMn5Ys#^Vv_$&& zO?Pj^=HV`4qM#aG!H)05b_!I?2p@B2HTV|Z_Jx4{<~ruA1X*tRqU$p^9rYuxFh>hZ zEV&+cSDZX%h6qe50?Ru0dN$aD#PB+VFwHFaId#3BJa-0Bo7`7PO2{a7Qg0s}qkalW z7=={xbX*6S+1l8KwN`~&n+Vr~;SD|Q6r6Tn zy)atnxte2xo~IL5b2N^!pW_CRF$C)hGe)&c(HytHgojWWhQcrxvY44kr74l zW-}9I#b<%1>=uQvi9&PPtW1wU>DPKngo{^(wx^I?iioH?{pFZFmK9SSi<;cqK?zwq6c4tFJ9B)1c{w>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