]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - repair/rmap.c
xfs_repair: don't flag log_incompat inconsistencies as corruptions
[thirdparty/xfsprogs-dev.git] / repair / rmap.c
index b907383e7222e65876f98e42a886bc9c73c7ae39..51691fcceaae040a97963d532bf5f00e0066914c 100644 (file)
@@ -3,7 +3,7 @@
  * Copyright (C) 2016 Oracle.  All Rights Reserved.
  * Author: Darrick J. Wong <darrick.wong@oracle.com>
  */
-#include <libxfs.h>
+#include "libxfs.h"
 #include "btree.h"
 #include "err_protos.h"
 #include "libxlog.h"
@@ -49,8 +49,8 @@ bool
 rmap_needs_work(
        struct xfs_mount        *mp)
 {
-       return xfs_sb_version_hasreflink(&mp->m_sb) ||
-              xfs_sb_version_hasrmapbt(&mp->m_sb);
+       return xfs_has_reflink(mp) ||
+              xfs_has_rmapbt(mp);
 }
 
 /*
@@ -387,7 +387,7 @@ rmap_add_fixed_ag_rec(
        /* inodes */
        ino_rec = findfirst_inode_rec(agno);
        for (; ino_rec != NULL; ino_rec = next_ino_rec(ino_rec)) {
-               if (xfs_sb_version_hassparseinodes(&mp->m_sb)) {
+               if (xfs_has_sparseinodes(mp)) {
                        startidx = find_first_zero_bit(ino_rec->ir_sparse);
                        nr = XFS_INODES_PER_CHUNK - popcnt(ino_rec->ir_sparse);
                } else {
@@ -455,7 +455,7 @@ rmap_store_ag_btree_rec(
        struct bitmap           *own_ag_bitmap = NULL;
        int                     error = 0;
 
-       if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
+       if (!xfs_has_rmapbt(mp))
                return 0;
 
        /* Release the ar_rmaps; they were put into the rmapbt during p5. */
@@ -490,7 +490,7 @@ rmap_store_ag_btree_rec(
        error = init_slab_cursor(ag_rmap->ar_raw_rmaps, rmap_compare, &rm_cur);
        if (error)
                goto err;
-       error = -bitmap_init(&own_ag_bitmap);
+       error = -bitmap_alloc(&own_ag_bitmap);
        if (error)
                goto err_slab;
        while ((rm_rec = pop_slab_cursor(rm_cur)) != NULL) {
@@ -512,7 +512,7 @@ rmap_store_ag_btree_rec(
        free_slab_cursor(&rm_cur);
 
        /* Create rmaps for any AGFL blocks that aren't already rmapped. */
-       agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
+       agfl_bno = xfs_buf_to_agfl_bno(agflbp);
        b = agfl_bno + ag_rmap->ar_flcount;
        while (*b != cpu_to_be32(NULLAGBLOCK) &&
               b - agfl_bno < libxfs_agfl_size(mp)) {
@@ -527,7 +527,7 @@ rmap_store_ag_btree_rec(
                }
                b++;
        }
-       libxfs_putbuf(agflbp);
+       libxfs_buf_relse(agflbp);
        agflbp = NULL;
        bitmap_free(&own_ag_bitmap);
 
@@ -545,6 +545,7 @@ rmap_store_ag_btree_rec(
        rm_rec = pop_slab_cursor(rm_cur);
        while (rm_rec) {
                struct xfs_owner_info   oinfo = {};
+               struct xfs_perag        *pag;
 
                error = -libxfs_trans_alloc_rollable(mp, 16, &tp);
                if (error)
@@ -556,8 +557,10 @@ rmap_store_ag_btree_rec(
 
                ASSERT(XFS_RMAP_NON_INODE_OWNER(rm_rec->rm_owner));
                oinfo.oi_owner = rm_rec->rm_owner;
-               error = -libxfs_rmap_alloc(tp, agbp, agno, rm_rec->rm_startblock,
+               pag = libxfs_perag_get(mp, agno);
+               error = -libxfs_rmap_alloc(tp, agbp, pag, rm_rec->rm_startblock,
                                rm_rec->rm_blockcount, &oinfo);
+               libxfs_perag_put(pag);
                if (error)
                        goto err_trans;
 
@@ -579,7 +582,7 @@ err_slab:
        free_slab_cursor(&rm_cur);
 err:
        if (agflbp)
-               libxfs_putbuf(agflbp);
+               libxfs_buf_relse(agflbp);
        if (own_ag_bitmap)
                bitmap_free(&own_ag_bitmap);
        return error;
@@ -758,7 +761,7 @@ compute_refcounts(
        size_t                  old_stack_nr;
        int                     error;
 
-       if (!xfs_sb_version_hasreflink(&mp->m_sb))
+       if (!xfs_has_reflink(mp))
                return 0;
 
        rmaps = ag_rmaps[agno].ar_rmaps;
@@ -902,18 +905,10 @@ rmap_lookup(
        struct xfs_rmap_irec    *tmp,
        int                     *have)
 {
-       int                     error;
-
        /* Use the regular btree retrieval routine. */
-       error = -libxfs_rmap_lookup_le(bt_cur, rm_rec->rm_startblock,
-                               rm_rec->rm_blockcount,
+       return -libxfs_rmap_lookup_le(bt_cur, rm_rec->rm_startblock,
                                rm_rec->rm_owner, rm_rec->rm_offset,
-                               rm_rec->rm_flags, have);
-       if (error)
-               return error;
-       if (*have == 0)
-               return error;
-       return -libxfs_rmap_get_rec(bt_cur, tmp, have);
+                               rm_rec->rm_flags, tmp, have);
 }
 
 /* Look for an rmap in the rmapbt that matches a given rmap. */
@@ -971,64 +966,77 @@ rmap_is_good(
 /*
  * Compare the observed reverse mappings against what's in the ag btree.
  */
-int
+void
 rmaps_verify_btree(
        struct xfs_mount        *mp,
        xfs_agnumber_t          agno)
 {
+       struct xfs_rmap_irec    tmp;
        struct xfs_slab_cursor  *rm_cur;
        struct xfs_btree_cur    *bt_cur = NULL;
-       int                     error;
-       int                     have;
        struct xfs_buf          *agbp = NULL;
        struct xfs_rmap_irec    *rm_rec;
-       struct xfs_rmap_irec    tmp;
-       struct xfs_perag        *pag;           /* per allocation group data */
+       struct xfs_perag        *pag = NULL;
+       int                     have;
+       int                     error;
 
-       if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
-               return 0;
+       if (!xfs_has_rmapbt(mp))
+               return;
        if (rmapbt_suspect) {
                if (no_modify && agno == 0)
                        do_warn(_("would rebuild corrupt rmap btrees.\n"));
-               return 0;
+               return;
        }
 
        /* Create cursors to refcount structures */
        error = rmap_init_cursor(agno, &rm_cur);
-       if (error)
-               return error;
+       if (error) {
+               do_warn(_("Not enough memory to check reverse mappings.\n"));
+               return;
+       }
 
        error = -libxfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
-       if (error)
+       if (error) {
+               do_warn(_("Could not read AGF %u to check rmap btree.\n"),
+                               agno);
                goto err;
+       }
 
        /* Leave the per-ag data "uninitialized" since we rewrite it later */
        pag = libxfs_perag_get(mp, agno);
        pag->pagf_init = 0;
-       libxfs_perag_put(pag);
 
-       bt_cur = libxfs_rmapbt_init_cursor(mp, NULL, agbp, agno);
+       bt_cur = libxfs_rmapbt_init_cursor(mp, NULL, agbp, pag);
        if (!bt_cur) {
-               error = -ENOMEM;
+               do_warn(_("Not enough memory to check reverse mappings.\n"));
                goto err;
        }
 
        rm_rec = pop_slab_cursor(rm_cur);
        while (rm_rec) {
                error = rmap_lookup(bt_cur, rm_rec, &tmp, &have);
-               if (error)
+               if (error) {
+                       do_warn(
+_("Could not read reverse-mapping record for (%u/%u).\n"),
+                                       agno, rm_rec->rm_startblock);
                        goto err;
+               }
+
                /*
                 * Using the range query is expensive, so only do it if
                 * the regular lookup doesn't find anything or if it doesn't
                 * match the observed rmap.
                 */
-               if (xfs_sb_version_hasreflink(&bt_cur->bc_mp->m_sb) &&
+               if (xfs_has_reflink(bt_cur->bc_mp) &&
                                (!have || !rmap_is_good(rm_rec, &tmp))) {
                        error = rmap_lookup_overlapped(bt_cur, rm_rec,
                                        &tmp, &have);
-                       if (error)
+                       if (error) {
+                               do_warn(
+_("Could not read reverse-mapping record for (%u/%u).\n"),
+                                               agno, rm_rec->rm_startblock);
                                goto err;
+                       }
                }
                if (!have) {
                        do_warn(
@@ -1081,10 +1089,11 @@ next_loop:
 err:
        if (bt_cur)
                libxfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
+       if (pag)
+               libxfs_perag_put(pag);
        if (agbp)
-               libxfs_putbuf(agbp);
+               libxfs_buf_relse(agbp);
        free_slab_cursor(&rm_cur);
-       return 0;
 }
 
 /*
@@ -1231,7 +1240,8 @@ _("setting reflink flag on inode %"PRIu64"\n"),
        else
                dino->di_flags2 &= cpu_to_be64(~XFS_DIFLAG2_REFLINK);
        libxfs_dinode_calc_crc(mp, dino);
-       libxfs_writebuf(buf, 0);
+       libxfs_buf_mark_dirty(buf);
+       libxfs_buf_relse(buf);
 
        return 0;
 }
@@ -1330,46 +1340,50 @@ refcount_avoid_check(void)
 /*
  * Compare the observed reference counts against what's in the ag btree.
  */
-int
+void
 check_refcounts(
-       struct xfs_mount        *mp,
-       xfs_agnumber_t          agno)
+       struct xfs_mount                *mp,
+       xfs_agnumber_t                  agno)
 {
-       struct xfs_slab_cursor  *rl_cur;
-       struct xfs_btree_cur    *bt_cur = NULL;
-       int                     error;
-       int                     have;
-       int                     i;
-       struct xfs_buf          *agbp = NULL;
-       struct xfs_refcount_irec        *rl_rec;
        struct xfs_refcount_irec        tmp;
-       struct xfs_perag        *pag;           /* per allocation group data */
+       struct xfs_slab_cursor          *rl_cur;
+       struct xfs_btree_cur            *bt_cur = NULL;
+       struct xfs_buf                  *agbp = NULL;
+       struct xfs_perag                *pag = NULL;
+       struct xfs_refcount_irec        *rl_rec;
+       int                             have;
+       int                             i;
+       int                             error;
 
-       if (!xfs_sb_version_hasreflink(&mp->m_sb))
-               return 0;
+       if (!xfs_has_reflink(mp))
+               return;
        if (refcbt_suspect) {
                if (no_modify && agno == 0)
                        do_warn(_("would rebuild corrupt refcount btrees.\n"));
-               return 0;
+               return;
        }
 
        /* Create cursors to refcount structures */
        error = init_refcount_cursor(agno, &rl_cur);
-       if (error)
-               return error;
+       if (error) {
+               do_warn(_("Not enough memory to check refcount data.\n"));
+               return;
+       }
 
        error = -libxfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
-       if (error)
+       if (error) {
+               do_warn(_("Could not read AGF %u to check refcount btree.\n"),
+                               agno);
                goto err;
+       }
 
        /* Leave the per-ag data "uninitialized" since we rewrite it later */
        pag = libxfs_perag_get(mp, agno);
        pag->pagf_init = 0;
-       libxfs_perag_put(pag);
 
-       bt_cur = libxfs_refcountbt_init_cursor(mp, NULL, agbp, agno);
+       bt_cur = libxfs_refcountbt_init_cursor(mp, NULL, agbp, pag);
        if (!bt_cur) {
-               error = -ENOMEM;
+               do_warn(_("Not enough memory to check refcount data.\n"));
                goto err;
        }
 
@@ -1378,8 +1392,12 @@ check_refcounts(
                /* Look for a refcount record in the btree */
                error = -libxfs_refcount_lookup_le(bt_cur,
                                rl_rec->rc_startblock, &have);
-               if (error)
+               if (error) {
+                       do_warn(
+_("Could not read reference count record for (%u/%u).\n"),
+                                       agno, rl_rec->rc_startblock);
                        goto err;
+               }
                if (!have) {
                        do_warn(
 _("Missing reference count record for (%u/%u) len %u count %u\n"),
@@ -1389,8 +1407,12 @@ _("Missing reference count record for (%u/%u) len %u count %u\n"),
                }
 
                error = -libxfs_refcount_get_rec(bt_cur, &tmp, &i);
-               if (error)
+               if (error) {
+                       do_warn(
+_("Could not read reference count record for (%u/%u).\n"),
+                                       agno, rl_rec->rc_startblock);
                        goto err;
+               }
                if (!i) {
                        do_warn(
 _("Missing reference count record for (%u/%u) len %u count %u\n"),
@@ -1401,8 +1423,8 @@ _("Missing reference count record for (%u/%u) len %u count %u\n"),
 
                /* Compare each refcount observation against the btree's */
                if (tmp.rc_startblock != rl_rec->rc_startblock ||
-                   tmp.rc_blockcount < rl_rec->rc_blockcount ||
-                   tmp.rc_refcount < rl_rec->rc_refcount)
+                   tmp.rc_blockcount != rl_rec->rc_blockcount ||
+                   tmp.rc_refcount != rl_rec->rc_refcount)
                        do_warn(
 _("Incorrect reference count: saw (%u/%u) len %u nlinks %u; should be (%u/%u) len %u nlinks %u\n"),
                                agno, tmp.rc_startblock, tmp.rc_blockcount,
@@ -1416,10 +1438,11 @@ err:
        if (bt_cur)
                libxfs_btree_del_cursor(bt_cur, error ? XFS_BTREE_ERROR :
                                                        XFS_BTREE_NOERROR);
+       if (pag)
+               libxfs_perag_put(pag);
        if (agbp)
-               libxfs_putbuf(agbp);
+               libxfs_buf_relse(agbp);
        free_slab_cursor(&rl_cur);
-       return 0;
 }
 
 /*