LCOV - code coverage report
Current view: top level - fs/xfs/scrub - alloc.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 65 75 86.7 %
Date: 2023-07-31 20:08:07 Functions: 9 9 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-or-later
       2             : /*
       3             :  * Copyright (C) 2017-2023 Oracle.  All Rights Reserved.
       4             :  * Author: Darrick J. Wong <djwong@kernel.org>
       5             :  */
       6             : #include "xfs.h"
       7             : #include "xfs_fs.h"
       8             : #include "xfs_shared.h"
       9             : #include "xfs_format.h"
      10             : #include "xfs_trans_resv.h"
      11             : #include "xfs_mount.h"
      12             : #include "xfs_log_format.h"
      13             : #include "xfs_trans.h"
      14             : #include "xfs_btree.h"
      15             : #include "xfs_alloc.h"
      16             : #include "xfs_rmap.h"
      17             : #include "xfs_ag.h"
      18             : #include "scrub/scrub.h"
      19             : #include "scrub/common.h"
      20             : #include "scrub/btree.h"
      21             : #include "scrub/repair.h"
      22             : 
      23             : /*
      24             :  * Set us up to scrub free space btrees.
      25             :  */
      26             : int
      27      397113 : xchk_setup_ag_allocbt(
      28             :         struct xfs_scrub        *sc)
      29             : {
      30      397113 :         int                     error;
      31             : 
      32      397113 :         if (xchk_need_intent_drain(sc))
      33        4789 :                 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
      34             : 
      35      397113 :         error = xchk_setup_ag_btree(sc, false);
      36      397125 :         if (error)
      37             :                 return error;
      38             : 
      39      785266 :         if (xchk_could_repair(sc))
      40       28685 :                 return xrep_setup_ag_allocbt(sc);
      41             : 
      42             :         return 0;
      43             : }
      44             : 
      45             : /* Free space btree scrubber. */
      46             : 
      47             : struct xchk_alloc {
      48             :         /* Previous free space extent. */
      49             :         struct xfs_alloc_rec_incore     prev;
      50             : };
      51             : 
      52             : /*
      53             :  * Ensure there's a corresponding cntbt/bnobt record matching this
      54             :  * bnobt/cntbt record, respectively.
      55             :  */
      56             : STATIC void
      57   383153681 : xchk_allocbt_xref_other(
      58             :         struct xfs_scrub        *sc,
      59             :         xfs_agblock_t           agbno,
      60             :         xfs_extlen_t            len)
      61             : {
      62   383153681 :         struct xfs_btree_cur    **pcur;
      63   383153681 :         xfs_agblock_t           fbno;
      64   383153681 :         xfs_extlen_t            flen;
      65   383153681 :         int                     has_otherrec;
      66   383153681 :         int                     error;
      67             : 
      68   383153681 :         if (sc->sm->sm_type == XFS_SCRUB_TYPE_BNOBT)
      69   210403629 :                 pcur = &sc->sa.cnt_cur;
      70             :         else
      71   172750052 :                 pcur = &sc->sa.bno_cur;
      72   383153681 :         if (!*pcur || xchk_skip_xref(sc->sm))
      73         612 :                 return;
      74             : 
      75   383153069 :         error = xfs_alloc_lookup_le(*pcur, agbno, len, &has_otherrec);
      76   383150993 :         if (!xchk_should_check_xref(sc, &error, pcur))
      77             :                 return;
      78   383151578 :         if (!has_otherrec) {
      79           0 :                 xchk_btree_xref_set_corrupt(sc, *pcur, 0);
      80           0 :                 return;
      81             :         }
      82             : 
      83   383151578 :         error = xfs_alloc_get_rec(*pcur, &fbno, &flen, &has_otherrec);
      84   383152678 :         if (!xchk_should_check_xref(sc, &error, pcur))
      85             :                 return;
      86   383153070 :         if (!has_otherrec) {
      87           0 :                 xchk_btree_xref_set_corrupt(sc, *pcur, 0);
      88           0 :                 return;
      89             :         }
      90             : 
      91   383153070 :         if (fbno != agbno || flen != len)
      92          20 :                 xchk_btree_xref_set_corrupt(sc, *pcur, 0);
      93             : }
      94             : 
      95             : /* Cross-reference with the other btrees. */
      96             : STATIC void
      97   383152578 : xchk_allocbt_xref(
      98             :         struct xfs_scrub        *sc,
      99             :         const struct xfs_alloc_rec_incore *irec)
     100             : {
     101   383152578 :         xfs_agblock_t           agbno = irec->ar_startblock;
     102   383152578 :         xfs_extlen_t            len = irec->ar_blockcount;
     103             : 
     104   383152578 :         if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
     105             :                 return;
     106             : 
     107   383152919 :         xchk_allocbt_xref_other(sc, agbno, len);
     108   383153157 :         xchk_xref_is_not_inode_chunk(sc, agbno, len);
     109   383143665 :         xchk_xref_has_no_owner(sc, agbno, len);
     110   383147793 :         xchk_xref_is_not_shared(sc, agbno, len);
     111   383152369 :         xchk_xref_is_not_cow_staging(sc, agbno, len);
     112             : }
     113             : 
     114             : /* Flag failures for records that could be merged. */
     115             : STATIC void
     116   383152924 : xchk_allocbt_mergeable(
     117             :         struct xchk_btree       *bs,
     118             :         struct xchk_alloc       *ca,
     119             :         const struct xfs_alloc_rec_incore *irec)
     120             : {
     121   383152924 :         if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
     122             :                 return;
     123             : 
     124   383152924 :         if (ca->prev.ar_blockcount > 0 &&
     125   382738959 :             ca->prev.ar_startblock + ca->prev.ar_blockcount == irec->ar_startblock &&
     126           0 :             ca->prev.ar_blockcount + irec->ar_blockcount < (uint32_t)~0U)
     127           0 :                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
     128             : 
     129   766305848 :         memcpy(&ca->prev, irec, sizeof(*irec));
     130             : }
     131             : 
     132             : /* Scrub a bnobt/cntbt record. */
     133             : STATIC int
     134   383152569 : xchk_allocbt_rec(
     135             :         struct xchk_btree               *bs,
     136             :         const union xfs_btree_rec       *rec)
     137             : {
     138   383152569 :         struct xfs_alloc_rec_incore     irec;
     139   383152569 :         struct xchk_alloc       *ca = bs->private;
     140             : 
     141   383152569 :         xfs_alloc_btrec_to_irec(rec, &irec);
     142   383153421 :         if (xfs_alloc_check_irec(bs->cur, &irec) != NULL) {
     143           0 :                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
     144           0 :                 return 0;
     145             :         }
     146             : 
     147   383153569 :         xchk_allocbt_mergeable(bs, ca, &irec);
     148   383152519 :         xchk_allocbt_xref(bs->sc, &irec);
     149             : 
     150   383152519 :         return 0;
     151             : }
     152             : 
     153             : /* Scrub the freespace btrees for some AG. */
     154             : STATIC int
     155      413718 : xchk_allocbt(
     156             :         struct xfs_scrub        *sc,
     157             :         xfs_btnum_t             which)
     158             : {
     159      413718 :         struct xchk_alloc       ca = { };
     160      413718 :         struct xfs_btree_cur    *cur;
     161             : 
     162      413718 :         cur = which == XFS_BTNUM_BNO ? sc->sa.bno_cur : sc->sa.cnt_cur;
     163      413718 :         return xchk_btree(sc, cur, xchk_allocbt_rec, &XFS_RMAP_OINFO_AG, &ca);
     164             : }
     165             : 
     166             : int
     167      214990 : xchk_bnobt(
     168             :         struct xfs_scrub        *sc)
     169             : {
     170      214990 :         return xchk_allocbt(sc, XFS_BTNUM_BNO);
     171             : }
     172             : 
     173             : int
     174      198744 : xchk_cntbt(
     175             :         struct xfs_scrub        *sc)
     176             : {
     177      198744 :         return xchk_allocbt(sc, XFS_BTNUM_CNT);
     178             : }
     179             : 
     180             : /* xref check that the extent is not free */
     181             : void
     182  1248623820 : xchk_xref_is_used_space(
     183             :         struct xfs_scrub        *sc,
     184             :         xfs_agblock_t           agbno,
     185             :         xfs_extlen_t            len)
     186             : {
     187  1248623820 :         enum xbtree_recpacking  outcome;
     188  1248623820 :         int                     error;
     189             : 
     190  1248623820 :         if (!sc->sa.bno_cur || xchk_skip_xref(sc->sm))
     191           0 :                 return;
     192             : 
     193  1248623820 :         error = xfs_alloc_has_records(sc->sa.bno_cur, agbno, len, &outcome);
     194  1248641632 :         if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
     195             :                 return;
     196  1248645201 :         if (outcome != XBTREE_RECPACKING_EMPTY)
     197           0 :                 xchk_btree_xref_set_corrupt(sc, sc->sa.bno_cur, 0);
     198             : }

Generated by: LCOV version 1.14