From patchwork Wed Apr 26 18:47:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sandeen X-Patchwork-Id: 9701871 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 97343603F6 for ; Wed, 26 Apr 2017 18:47:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8B9C51FFB7 for ; Wed, 26 Apr 2017 18:47:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7FCEA28355; Wed, 26 Apr 2017 18:47:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D19352029B for ; Wed, 26 Apr 2017 18:47:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965357AbdDZSrt (ORCPT ); Wed, 26 Apr 2017 14:47:49 -0400 Received: from sandeen.net ([63.231.237.45]:50932 "EHLO sandeen.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965363AbdDZSrs (ORCPT ); Wed, 26 Apr 2017 14:47:48 -0400 Received: from [10.0.0.4] (liberator [10.0.0.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by sandeen.net (Postfix) with ESMTPSA id EE80677B2C9; Wed, 26 Apr 2017 13:47:47 -0500 (CDT) Subject: [PATCH v3 2/4] xfs_db: use iocursor type to guess btree geometry if bad magic To: "Darrick J. Wong" , sandeen@redhat.com References: <149186446125.32572.5134583964816531229.stgit@birch.djwong.org> <149186447366.32572.11858750403661352513.stgit@birch.djwong.org> <20170411002012.GB5094@birch.djwong.org> Cc: linux-xfs@vger.kernel.org From: Eric Sandeen Message-ID: <2b0434c2-a636-87ba-684d-375f30becf00@sandeen.net> Date: Wed, 26 Apr 2017 13:47:47 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <20170411002012.GB5094@birch.djwong.org> Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Darrick J. Wong The function block_to_bt plays an integral role in determining the btree geometry of a block that we want to manipulate with the debugger. Normally we use the block magic to find the geometry profile, but if the magic is bad we'll never find it and return NULL. The callers of this function do not check for NULL and crash. Therefore, if we can't find a geometry profile matching the magic number, use the iocursor type to guess the profile and scowl about that to stdout. This makes it so that even with a corrupt magic we can try to print the fields instead of crashing the debugger. Signed-off-by: Darrick J. Wong Reviewed-by: Darrick J. Wong --- v2: be less macro-happy and only evaluate hascrc once v3: braces around the for loop body v4: proposed maintainer changes from eric: fix comments, add magic assert, keep other asserts which were removed for unknown reasons... --- -- To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/db/btblock.c b/db/btblock.c index 835a5f0..b7eacb5 100644 --- a/db/btblock.c +++ b/db/btblock.c @@ -25,6 +25,8 @@ #include "print.h" #include "bit.h" #include "init.h" +#include "io.h" +#include "output.h" /* * Definition of the possible btree block layouts. @@ -113,22 +115,60 @@ struct xfs_db_btree { }; /* - * Find the right block defintion for a given ondisk block. - * - * We use the least significant bit of the magic number as index into - * the array of block defintions. + * Find the right block definition for a given ondisk block. */ static struct xfs_db_btree * block_to_bt( struct xfs_btree_block *bb) { - struct xfs_db_btree *btp = &btrees[0]; + struct xfs_db_btree *btp; + uint32_t magic; + bool crc; - do { - if (be32_to_cpu((bb)->bb_magic) == btp->magic) + magic = be32_to_cpu((bb)->bb_magic); + for (btp = &btrees[0]; btp->magic != 0; btp++) { + if (magic == btp->magic) + return btp; + } + + /* Magic is invalid/unknown. Guess based on iocur type */ + crc = xfs_sb_version_hascrc(&mp->m_sb); + switch (iocur_top->typ->typnm) { + case TYP_BMAPBTA: + case TYP_BMAPBTD: + magic = crc ? XFS_BMAP_CRC_MAGIC : XFS_BMAP_MAGIC; + break; + case TYP_BNOBT: + magic = crc ? XFS_ABTB_CRC_MAGIC : XFS_ABTB_MAGIC; + break; + case TYP_CNTBT: + magic = crc ? XFS_ABTC_CRC_MAGIC : XFS_ABTC_MAGIC; + break; + case TYP_INOBT: + magic = crc ? XFS_IBT_CRC_MAGIC : XFS_IBT_MAGIC; + break; + case TYP_FINOBT: + magic = crc ? XFS_FIBT_CRC_MAGIC : XFS_FIBT_MAGIC; + break; + case TYP_RMAPBT: + magic = crc ? XFS_RMAP_CRC_MAGIC : 0; + break; + case TYP_REFCBT: + magic = crc ? XFS_REFC_CRC_MAGIC : 0; + break; + default: + ASSERT(0); + } + + ASSERT(magic); + dbprintf(_("Bad btree magic 0x%x; coercing to %s.\n"), + be32_to_cpu((bb)->bb_magic), + iocur_top->typ->name); + + for (btp = &btrees[0]; btp->magic != 0; btp++) { + if (magic == btp->magic) return btp; - btp++; - } while (btp->magic != 0); + } return NULL; }