From patchwork Sun Oct 2 18:20:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 12996974 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 22C0AC433FE for ; Sun, 2 Oct 2022 18:35:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230108AbiJBSff (ORCPT ); Sun, 2 Oct 2022 14:35:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230102AbiJBSfd (ORCPT ); Sun, 2 Oct 2022 14:35:33 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 462532A956 for ; Sun, 2 Oct 2022 11:35:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4F64360F04 for ; Sun, 2 Oct 2022 18:35:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A98D4C433D6; Sun, 2 Oct 2022 18:35:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1664735731; bh=UBkf+Qtv+Qc5Zqh3Kys0oOebeY2eO+WGm9sAAvAu3GE=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=CotHJ7MOQgegByFqCirrBCKMymTtjw1eo/AviSmKvnWsSe1Lq7HSs9dICmsq7nVvD R6kEW27NhShSlKt8fuV13pP8Zm8AaN2dKJZjdbs1cWDY1mEqQ1R8ThNpgUV2XskXQz 7BUgLciyOgCxBgqYH5er/xotc2h+3rrv+Qw+Xzl4W0ddeQGwL5bnOovP7wl0w6nG3d YQ1+UZ4k3cNndVOBtMVshUSPOALcPqybcEsjcdnIW1uDWTtDihztAX+f/5tQBSHD9y GS1V9eiWPqfA0hym8y3eLsfF4kif/f2Im1jCJ9zjgNHUMwnSCxsc7D8uev7eTO671f 7CqDmgPX7lOxw== Subject: [PATCH 1/6] xfs: change bmap scrubber to store the previous mapping From: "Darrick J. Wong" To: djwong@kernel.org Cc: linux-xfs@vger.kernel.org Date: Sun, 02 Oct 2022 11:20:36 -0700 Message-ID: <166473483619.1084923.6959896326767155292.stgit@magnolia> In-Reply-To: <166473483595.1084923.1946295148534639238.stgit@magnolia> References: <166473483595.1084923.1946295148534639238.stgit@magnolia> User-Agent: StGit/0.19 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong Convert the inode data/attr/cow fork scrubber to remember the entire previous mapping, not just the next expected offset. No behavior changes here, but this will enable some better checking in subsequent patches. Signed-off-by: Darrick J. Wong --- fs/xfs/scrub/bmap.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/fs/xfs/scrub/bmap.c b/fs/xfs/scrub/bmap.c index d310f74fe650..aaa73a2bdd17 100644 --- a/fs/xfs/scrub/bmap.c +++ b/fs/xfs/scrub/bmap.c @@ -94,7 +94,8 @@ xchk_setup_inode_bmap( struct xchk_bmap_info { struct xfs_scrub *sc; struct xfs_iext_cursor icur; - xfs_fileoff_t lastoff; + struct xfs_bmbt_irec prev_rec; + bool is_rt; bool is_shared; bool was_loaded; @@ -402,7 +403,8 @@ xchk_bmap_iextent( * Check for out-of-order extents. This record could have come * from the incore list, for which there is no ordering check. */ - if (irec->br_startoff < info->lastoff) + if (irec->br_startoff < info->prev_rec.br_startoff + + info->prev_rec.br_blockcount) xchk_fblock_set_corrupt(info->sc, info->whichfork, irec->br_startoff); @@ -703,7 +705,8 @@ xchk_bmap_iextent_delalloc( * Check for out-of-order extents. This record could have come * from the incore list, for which there is no ordering check. */ - if (irec->br_startoff < info->lastoff) + if (irec->br_startoff < info->prev_rec.br_startoff + + info->prev_rec.br_blockcount) xchk_fblock_set_corrupt(info->sc, info->whichfork, irec->br_startoff); @@ -797,7 +800,6 @@ xchk_bmap( goto out; /* Scrub extent records. */ - info.lastoff = 0; ifp = xfs_ifork_ptr(ip, whichfork); for_each_xfs_iext(ifp, &info.icur, &irec) { if (xchk_should_terminate(sc, &error) || @@ -814,7 +816,7 @@ xchk_bmap( xchk_bmap_iextent_delalloc(ip, &info, &irec); else xchk_bmap_iextent(ip, &info, &irec); - info.lastoff = irec.br_startoff + irec.br_blockcount; + memcpy(&info.prev_rec, &irec, sizeof(struct xfs_bmbt_irec)); } error = xchk_bmap_check_rmaps(sc, whichfork); From patchwork Sun Oct 2 18:20:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 12996975 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 09117C433F5 for ; Sun, 2 Oct 2022 18:35:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230102AbiJBSfq (ORCPT ); Sun, 2 Oct 2022 14:35:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230101AbiJBSfq (ORCPT ); Sun, 2 Oct 2022 14:35:46 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BCF0F2B625 for ; Sun, 2 Oct 2022 11:35:44 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 80123B80D88 for ; Sun, 2 Oct 2022 18:35:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3CCABC433D6; Sun, 2 Oct 2022 18:35:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1664735742; bh=/1Ir53agzvSCtIdGn9gzJeDDQuthEAXwKkIqgM0lu5Y=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=BovnOEyveTQSem8bw363V90KNVH8mE4GyWPBdgDUA7st8p9BkvFuWvpycO1Gbmk9I jqSkPsfEVJp86YlHW5jqbIxFoRF2i+caSp2ZjnqjJ9u+GXEOH+h8ajoZ0ZcijyGLuJ i14iQXn5eJgt+LjuvtCVyQ+K/6YPVwNDrMdrOq/79pu/VhVutWV0nUlNkGGEwQlBqx M8y6o2xO/oSuPemXAa7rCnevxdP3FHPF+WJGiUDdw9oe/SXr9G0KW181e8qfs2kCxT 27plWCU61tDVkcEVcWYxwhh0tJe1dO6pms3i6bjXHj4utYm/3ZYJfeH5JL+GSrVSAU 337MywT81rW9A== Subject: [PATCH 2/6] xfs: alert the user about data/attr fork mappings that could be merged From: "Darrick J. Wong" To: djwong@kernel.org Cc: linux-xfs@vger.kernel.org Date: Sun, 02 Oct 2022 11:20:36 -0700 Message-ID: <166473483633.1084923.15717918189479146706.stgit@magnolia> In-Reply-To: <166473483595.1084923.1946295148534639238.stgit@magnolia> References: <166473483595.1084923.1946295148534639238.stgit@magnolia> User-Agent: StGit/0.19 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong If the data or attr forks have mappings that could be merged, let the user know that the structure could be optimized. This isn't a filesystem corruption since the regular filesystem does not try to be smart about merging bmbt records. Signed-off-by: Darrick J. Wong --- fs/xfs/scrub/bmap.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/fs/xfs/scrub/bmap.c b/fs/xfs/scrub/bmap.c index aaa73a2bdd17..0f5d7fb61ca1 100644 --- a/fs/xfs/scrub/bmap.c +++ b/fs/xfs/scrub/bmap.c @@ -390,6 +390,29 @@ xchk_bmap_dirattr_extent( xchk_fblock_set_corrupt(info->sc, info->whichfork, off); } +/* Are these two mappings mergeable? */ +static inline bool +xchk_bmap_mergeable( + struct xchk_bmap_info *info, + const struct xfs_bmbt_irec *b2) +{ + const struct xfs_bmbt_irec *b1 = &info->prev_rec; + + /* Skip uninitialized prev_rec and COW fork extents */ + if (b1->br_blockcount == 0) + return false; + if (info->whichfork == XFS_COW_FORK) + return false; + + if (b1->br_startoff + b1->br_blockcount != b2->br_startoff) + return false; + if (b1->br_startblock + b1->br_blockcount != b2->br_startblock) + return false; + if (b1->br_blockcount + b2->br_blockcount > BMBT_BLOCKCOUNT_MASK) + return false; + return b1->br_state == b2->br_state; +} + /* Scrub a single extent record. */ STATIC void xchk_bmap_iextent( @@ -441,6 +464,10 @@ xchk_bmap_iextent( if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) return; + /* Notify the user of mergeable records in the data/attr forks. */ + if (xchk_bmap_mergeable(info, irec)) + xchk_ino_set_preen(info->sc, info->sc->ip->i_ino); + if (info->is_rt) xchk_bmap_rt_iextent_xref(ip, info, irec); else From patchwork Sun Oct 2 18:20:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 12996976 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 D8255C433F5 for ; Sun, 2 Oct 2022 18:35:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230101AbiJBSf5 (ORCPT ); Sun, 2 Oct 2022 14:35:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230099AbiJBSf4 (ORCPT ); Sun, 2 Oct 2022 14:35:56 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F6B62B253 for ; Sun, 2 Oct 2022 11:35:55 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 25507B80D82 for ; Sun, 2 Oct 2022 18:35:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DB632C433D6; Sun, 2 Oct 2022 18:35:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1664735752; bh=1R/peNgFfbvh3HRi+Z9pxPrqHu/NLBLBwInbxPoWoRw=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=fxontAQUh5ENcbbSmqob1mHk2mZwvF/oDgQG8Uhs+4zt8jNxRr3mZ7s9itsFE53uH MWAk0mXgWI9iWh0gjzkg5yLEVd5p/0ojB2Eu12CSZUB+pR9SDm+6pbUrToRUKnCQtR +Af2JiqMP6HGh0Tj94cIaeZ49i7oiv6XYRJlwzg3xdmQCoOqugxVKCR+cSGIjll3u0 6/w3C8JgrPt5xyYr5CyddjGc+7UEngVt2ighjgSZ7QV3bqb+hppwr428ZOIhxdvCTe ub3iL4NwR1SEpToKkE928ddNwRuANAi9VDxR+p8NNRunQlyflsQvrOmQmuhqXl5+Yi BKF0Z6BUiXFQg== Subject: [PATCH 3/6] xfs: flag free space btree records that could be merged From: "Darrick J. Wong" To: djwong@kernel.org Cc: linux-xfs@vger.kernel.org Date: Sun, 02 Oct 2022 11:20:36 -0700 Message-ID: <166473483646.1084923.10060980464175596930.stgit@magnolia> In-Reply-To: <166473483595.1084923.1946295148534639238.stgit@magnolia> References: <166473483595.1084923.1946295148534639238.stgit@magnolia> User-Agent: StGit/0.19 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong Complain if we encounter free space btree records that could be merged. Signed-off-by: Darrick J. Wong --- fs/xfs/scrub/alloc.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/fs/xfs/scrub/alloc.c b/fs/xfs/scrub/alloc.c index 0cd20d998368..de4b7c34275a 100644 --- a/fs/xfs/scrub/alloc.c +++ b/fs/xfs/scrub/alloc.c @@ -31,6 +31,13 @@ xchk_setup_ag_allocbt( } /* Free space btree scrubber. */ + +struct xchk_alloc { + /* Previous free space extent. */ + xfs_agblock_t prev_bno; + xfs_extlen_t prev_len; +}; + /* * Ensure there's a corresponding cntbt/bnobt record matching this * bnobt/cntbt record, respectively. @@ -91,6 +98,25 @@ xchk_allocbt_xref( xchk_xref_is_not_cow_staging(sc, agbno, len); } +/* Flag failures for records that could be merged. */ +STATIC void +xchk_allocbt_mergeable( + struct xchk_btree *bs, + struct xchk_alloc *ca, + xfs_agblock_t bno, + xfs_extlen_t len) +{ + if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) + return; + + if (ca->prev_len > 0 && ca->prev_bno + ca->prev_len == bno && + ca->prev_len + len < (uint32_t)~0U) + xchk_btree_set_corrupt(bs->sc, bs->cur, 0); + + ca->prev_bno = bno; + ca->prev_len = len; +} + /* Scrub a bnobt/cntbt record. */ STATIC int xchk_allocbt_rec( @@ -98,6 +124,7 @@ xchk_allocbt_rec( const union xfs_btree_rec *rec) { struct xfs_perag *pag = bs->cur->bc_ag.pag; + struct xchk_alloc *ca = bs->private; xfs_agblock_t bno; xfs_extlen_t len; @@ -109,6 +136,7 @@ xchk_allocbt_rec( !xfs_verify_agbno(pag, bno + len - 1)) xchk_btree_set_corrupt(bs->sc, bs->cur, 0); + xchk_allocbt_mergeable(bs, ca, bno, len); xchk_allocbt_xref(bs->sc, bno, len); return 0; @@ -120,10 +148,11 @@ xchk_allocbt( struct xfs_scrub *sc, xfs_btnum_t which) { + struct xchk_alloc ca = { .prev_len = 0 }; struct xfs_btree_cur *cur; cur = which == XFS_BTNUM_BNO ? sc->sa.bno_cur : sc->sa.cnt_cur; - return xchk_btree(sc, cur, xchk_allocbt_rec, &XFS_RMAP_OINFO_AG, NULL); + return xchk_btree(sc, cur, xchk_allocbt_rec, &XFS_RMAP_OINFO_AG, &ca); } int From patchwork Sun Oct 2 18:20:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 12996977 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 BC649C433FE for ; Sun, 2 Oct 2022 18:36:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230099AbiJBSgG (ORCPT ); Sun, 2 Oct 2022 14:36:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230109AbiJBSgF (ORCPT ); Sun, 2 Oct 2022 14:36:05 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A0D736840 for ; Sun, 2 Oct 2022 11:36:04 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 07F3D60EDB for ; Sun, 2 Oct 2022 18:36:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 673B6C433D6; Sun, 2 Oct 2022 18:36:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1664735763; bh=Qg/RvxHzwGJLaP0y5Bo8XgGcId6apVaGLdq82D5MXxc=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=YR+oSESLI4Qm5wXOBUq/dlnXR9vt3bseKXr606CXuXs6f1qujxqOf/R7CeQMoa0ON RBuQBgZQn9m7BbC0sj613SmuyGCSvJiiB2hechbsc/fmyw+JTMEdQQTic1NO0Pk+OB Pu+Xni6AeN2zUgU5CfywO5d0eTHnOSSWZu4kunKmYX6WZqkbZdf+lkGwpicckwSNH8 TOpXPtXAabfdoEMjzFUzHdm3oDXWDbEgWOhcbhe1D+svzkpjrNnDHazCiV2a8ltNJN IPoLFVmxqXEaOBxXaA738+Ds3dA4gJnA8va1gScSPcm0SjlWkWVBn6BS5AS+FvyWoy HBfFnjjePikyg== Subject: [PATCH 4/6] xfs: flag refcount btree records that could be merged From: "Darrick J. Wong" To: djwong@kernel.org Cc: linux-xfs@vger.kernel.org Date: Sun, 02 Oct 2022 11:20:36 -0700 Message-ID: <166473483660.1084923.12809453984875476542.stgit@magnolia> In-Reply-To: <166473483595.1084923.1946295148534639238.stgit@magnolia> References: <166473483595.1084923.1946295148534639238.stgit@magnolia> User-Agent: StGit/0.19 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong Complain if we encounter refcount btree records that could be merged. Signed-off-by: Darrick J. Wong --- fs/xfs/scrub/refcount.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/fs/xfs/scrub/refcount.c b/fs/xfs/scrub/refcount.c index 2009efea923c..8282cabd630c 100644 --- a/fs/xfs/scrub/refcount.c +++ b/fs/xfs/scrub/refcount.c @@ -333,6 +333,9 @@ xchk_refcountbt_xref( } struct xchk_refcbt_records { + /* Previous refcount record. */ + struct xfs_refcount_irec prev_rec; + /* The next AG block where we aren't expecting shared extents. */ xfs_agblock_t next_unshared_agbno; @@ -387,6 +390,52 @@ xchk_refcountbt_xref_gaps( xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur); } +static inline bool +xchk_refcount_mergeable( + struct xchk_refcbt_records *rrc, + const struct xfs_refcount_irec *r2) +{ + const struct xfs_refcount_irec *r1 = &rrc->prev_rec; + + /* Ignore if prev_rec is not yet initialized. */ + if (r1->rc_blockcount > 0) + return false; + + if (r1->rc_startblock + r1->rc_blockcount != r2->rc_startblock) + return false; + if (r1->rc_refcount != r2->rc_refcount) + return false; + if ((unsigned long long)r1->rc_blockcount + r2->rc_blockcount > + MAXREFCEXTLEN) + return false; + + return true; +} + +/* Flag failures for records that could be merged. */ +STATIC void +xchk_refcountbt_check_mergeable( + struct xchk_btree *bs, + struct xchk_refcbt_records *rrc, + xfs_agblock_t bno, + xfs_extlen_t len, + xfs_nlink_t refcount) +{ + struct xfs_refcount_irec irec = { + .rc_startblock = bno, + .rc_blockcount = len, + .rc_refcount = refcount, + }; + + if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) + return; + + if (xchk_refcount_mergeable(rrc, &irec)) + xchk_btree_set_corrupt(bs->sc, bs->cur, 0); + + memcpy(&rrc->prev_rec, &irec, sizeof(struct xfs_refcount_irec)); +} + /* Scrub a refcountbt record. */ STATIC int xchk_refcountbt_rec( @@ -421,6 +470,7 @@ xchk_refcountbt_rec( if (refcount == 0) xchk_btree_set_corrupt(bs->sc, bs->cur, 0); + xchk_refcountbt_check_mergeable(bs, rrc, bno, len, refcount); xchk_refcountbt_xref(bs->sc, bno, len, refcount); /* From patchwork Sun Oct 2 18:20:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 12996978 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 20941C433FE for ; Sun, 2 Oct 2022 18:36:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230109AbiJBSgQ (ORCPT ); Sun, 2 Oct 2022 14:36:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230105AbiJBSgP (ORCPT ); Sun, 2 Oct 2022 14:36:15 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0065536840 for ; Sun, 2 Oct 2022 11:36:14 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9125160EDB for ; Sun, 2 Oct 2022 18:36:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00E46C433C1; Sun, 2 Oct 2022 18:36:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1664735774; bh=hXjGPUntruUWSd4qEdA5G44yk7Z4fejiRzDPSkwLjU0=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=suuUhZxhfeI4S22NREmGLV7+3NtP6yjqa/HjhCSbWVh3KuVSsaxchMAXLUs7hdOni F0ehjTZlZIVsuT2qcM/Ga7jVIEWxaO7MlnWjBZZhVtrRenO5XVUwfMawrIU/mSx6kC p21PIDPiAfl0nKbZd3dSwFPaz9BuQqAkfYTa6a6IC6w/2cJFNMTGtQSssT93HT1Kmm qFwo/V60QxC7mdb+LRutRsKQYvr6kRrUc2mUXq+RLJe5o/lCYoUpdcVUCvY0oimXEz IIz4uA7Il+fvstgUp2DdamTBdmcSMa3tKyPGsu1zXRvRWugfkuUb6GvW+m9mwBxMrM /RE17VuzIPqZA== Subject: [PATCH 5/6] xfs: check overlapping rmap btree records From: "Darrick J. Wong" To: djwong@kernel.org Cc: linux-xfs@vger.kernel.org Date: Sun, 02 Oct 2022 11:20:36 -0700 Message-ID: <166473483674.1084923.15327743282226942960.stgit@magnolia> In-Reply-To: <166473483595.1084923.1946295148534639238.stgit@magnolia> References: <166473483595.1084923.1946295148534639238.stgit@magnolia> User-Agent: StGit/0.19 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong The rmap btree scrubber doesn't contain sufficient checking for records that cannot overlap but do anyway. For the other btrees, this is enforced by the inorder checks in xchk_btree_rec, but the rmap btree is special because it allows overlapping records to handle shared data extents. Therefore, enhance the rmap btree record check function to compare each record against the previous one so that we can detect overlapping rmap records for space allocations that do not allow sharing. Signed-off-by: Darrick J. Wong --- fs/xfs/scrub/rmap.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/fs/xfs/scrub/rmap.c b/fs/xfs/scrub/rmap.c index ba8d073b3954..efb13a21afbc 100644 --- a/fs/xfs/scrub/rmap.c +++ b/fs/xfs/scrub/rmap.c @@ -32,6 +32,15 @@ xchk_setup_ag_rmapbt( /* Reverse-mapping scrubber. */ +struct xchk_rmap { + /* + * The furthest-reaching of the rmapbt records that we've already + * processed. This enables us to detect overlapping records for space + * allocations that cannot be shared. + */ + struct xfs_rmap_irec overlap_rec; +}; + /* Cross-reference a rmap against the refcount btree. */ STATIC void xchk_rmapbt_xref_refc( @@ -87,6 +96,56 @@ xchk_rmapbt_xref( xchk_rmapbt_xref_refc(sc, irec); } +static inline bool +xchk_rmapbt_is_shareable( + struct xfs_scrub *sc, + const struct xfs_rmap_irec *irec) +{ + if (!xfs_has_reflink(sc->mp)) + return false; + if (XFS_RMAP_NON_INODE_OWNER(irec->rm_owner)) + return false; + if (irec->rm_flags & (XFS_RMAP_BMBT_BLOCK | XFS_RMAP_ATTR_FORK | + XFS_RMAP_UNWRITTEN)) + return false; + return true; +} + +/* Flag failures for records that overlap but cannot. */ +STATIC void +xchk_rmapbt_check_overlapping( + struct xchk_btree *bs, + struct xchk_rmap *cr, + const struct xfs_rmap_irec *irec) +{ + xfs_agblock_t pnext, inext; + + if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) + return; + + /* No previous record? */ + if (cr->overlap_rec.rm_blockcount == 0) + goto set_prev; + + /* Do overlap_rec and irec overlap? */ + pnext = cr->overlap_rec.rm_startblock + cr->overlap_rec.rm_blockcount; + if (pnext <= irec->rm_startblock) + goto set_prev; + + /* Overlap is only allowed if both records are data fork mappings. */ + if (!xchk_rmapbt_is_shareable(bs->sc, &cr->overlap_rec) || + !xchk_rmapbt_is_shareable(bs->sc, irec)) + xchk_btree_set_corrupt(bs->sc, bs->cur, 0); + + /* Save whichever rmap record extends furthest. */ + inext = irec->rm_startblock + irec->rm_blockcount; + if (pnext > inext) + return; + +set_prev: + memcpy(&cr->overlap_rec, irec, sizeof(struct xfs_rmap_irec)); +} + /* Scrub an rmapbt record. */ STATIC int xchk_rmapbt_rec( @@ -94,6 +153,7 @@ xchk_rmapbt_rec( const union xfs_btree_rec *rec) { struct xfs_mount *mp = bs->cur->bc_mp; + struct xchk_rmap *cr = bs->private; struct xfs_rmap_irec irec; struct xfs_perag *pag = bs->cur->bc_ag.pag; bool non_inode; @@ -158,6 +218,7 @@ xchk_rmapbt_rec( xchk_btree_set_corrupt(bs->sc, bs->cur, 0); } + xchk_rmapbt_check_overlapping(bs, cr, &irec); xchk_rmapbt_xref(bs->sc, &irec); out: return error; @@ -168,8 +229,17 @@ int xchk_rmapbt( struct xfs_scrub *sc) { - return xchk_btree(sc, sc->sa.rmap_cur, xchk_rmapbt_rec, - &XFS_RMAP_OINFO_AG, NULL); + struct xchk_rmap *cr; + int error; + + cr = kzalloc(sizeof(struct xchk_rmap), XCHK_GFP_FLAGS); + if (!cr) + return -ENOMEM; + + error = xchk_btree(sc, sc->sa.rmap_cur, xchk_rmapbt_rec, + &XFS_RMAP_OINFO_AG, cr); + kfree(cr); + return error; } /* xref check that the extent is owned only by a given owner */ From patchwork Sun Oct 2 18:20:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 12996979 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 A8086C433F5 for ; Sun, 2 Oct 2022 18:36:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230105AbiJBSg2 (ORCPT ); Sun, 2 Oct 2022 14:36:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230111AbiJBSg0 (ORCPT ); Sun, 2 Oct 2022 14:36:26 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3E1E3B949 for ; Sun, 2 Oct 2022 11:36:25 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3593960F04 for ; Sun, 2 Oct 2022 18:36:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FDB9C433C1; Sun, 2 Oct 2022 18:36:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1664735784; bh=FurszeHmIic3q3wnRPvexm+049Xqq/0ezG1b9NBAn40=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=Yylo+oOZX67GnkErrrb5icuCuMyLzx74DPerOqmIYhcfPwlKTeGZ1brdwjABUWjnI 6G56GX/QxdCw+JVbi4NTbibTWwLIJkXzu82kZjuMT5oCt/EGo3aQ3CeQHYI1WDf3UD pJh2YmcfGnQDVN+6P5nqi3YAkIrfjr4qSNKFvvZTmHHOo5XiojvLWaaHKfK8z+wB/y 6Ada9lGVJ7+XIVztdyQVSDx3N/H+PTmE3lsCI7WgB5uCg5k2UhMkOYp1ur9GKf+CCs GQF8IguLbFjtLp7ssYIF11R5hSIArPhdutNSYg3VI9zJTNVAVmc9EKpCT3GtsMjdDW ypdXeX/pdzsMw== Subject: [PATCH 6/6] xfs: check for reverse mapping records that could be merged From: "Darrick J. Wong" To: djwong@kernel.org Cc: linux-xfs@vger.kernel.org Date: Sun, 02 Oct 2022 11:20:36 -0700 Message-ID: <166473483687.1084923.4272579683695514551.stgit@magnolia> In-Reply-To: <166473483595.1084923.1946295148534639238.stgit@magnolia> References: <166473483595.1084923.1946295148534639238.stgit@magnolia> User-Agent: StGit/0.19 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong Enhance the rmap scrubber to flag adjacent records that could be merged. Signed-off-by: Darrick J. Wong --- fs/xfs/scrub/rmap.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/fs/xfs/scrub/rmap.c b/fs/xfs/scrub/rmap.c index efb13a21afbc..76ac2279e37d 100644 --- a/fs/xfs/scrub/rmap.c +++ b/fs/xfs/scrub/rmap.c @@ -39,6 +39,12 @@ struct xchk_rmap { * allocations that cannot be shared. */ struct xfs_rmap_irec overlap_rec; + + /* + * The previous rmapbt record, so that we can check for two records + * that could be one. + */ + struct xfs_rmap_irec prev_rec; }; /* Cross-reference a rmap against the refcount btree. */ @@ -146,6 +152,51 @@ xchk_rmapbt_check_overlapping( memcpy(&cr->overlap_rec, irec, sizeof(struct xfs_rmap_irec)); } +/* Decide if two reverse-mapping records can be merged. */ +static inline bool +xchk_rmap_mergeable( + struct xchk_rmap *cr, + const struct xfs_rmap_irec *r2) +{ + const struct xfs_rmap_irec *r1 = &cr->prev_rec; + + /* Ignore if prev_rec is not yet initialized. */ + if (cr->prev_rec.rm_blockcount == 0) + return false; + + if (r1->rm_owner != r2->rm_owner) + return false; + if (r1->rm_startblock + r1->rm_blockcount != r2->rm_startblock) + return false; + if ((unsigned long long)r1->rm_blockcount + r2->rm_blockcount > + XFS_RMAP_LEN_MAX) + return false; + if (XFS_RMAP_NON_INODE_OWNER(r2->rm_owner)) + return true; + /* must be an inode owner below here */ + if (r1->rm_flags != r2->rm_flags) + return false; + if (r1->rm_flags & XFS_RMAP_BMBT_BLOCK) + return true; + return r1->rm_offset + r1->rm_blockcount == r2->rm_offset; +} + +/* Flag failures for records that could be merged. */ +STATIC void +xchk_rmapbt_check_mergeable( + struct xchk_btree *bs, + struct xchk_rmap *cr, + const struct xfs_rmap_irec *irec) +{ + if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) + return; + + if (xchk_rmap_mergeable(cr, irec)) + xchk_btree_set_corrupt(bs->sc, bs->cur, 0); + + memcpy(&cr->prev_rec, irec, sizeof(struct xfs_rmap_irec)); +} + /* Scrub an rmapbt record. */ STATIC int xchk_rmapbt_rec( @@ -218,6 +269,7 @@ xchk_rmapbt_rec( xchk_btree_set_corrupt(bs->sc, bs->cur, 0); } + xchk_rmapbt_check_mergeable(bs, cr, &irec); xchk_rmapbt_check_overlapping(bs, cr, &irec); xchk_rmapbt_xref(bs->sc, &irec); out: