]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - libxfs/xfs_da_btree.c
xfs: explicitly pass buffer size to xfs_corruption_error
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_da_btree.c
index 441bef4ec1d7949447ece2b2ef5ce1eb17f2e3d8..467e96dc9e350522b3435fe6cf84991cb3415946 100644 (file)
@@ -123,7 +123,7 @@ xfs_da_state_free(xfs_da_state_t *state)
        kmem_zone_free(xfs_da_state_zone, state);
 }
 
-static bool
+static xfs_failaddr_t
 xfs_da3_node_verify(
        struct xfs_buf          *bp)
 {
@@ -140,22 +140,24 @@ xfs_da3_node_verify(
                struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
 
                if (ichdr.magic != XFS_DA3_NODE_MAGIC)
-                       return false;
+                       return __this_address;
 
-               if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_uuid))
-                       return false;
+               if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid))
+                       return __this_address;
                if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
-                       return false;
+                       return __this_address;
+               if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn)))
+                       return __this_address;
        } else {
                if (ichdr.magic != XFS_DA_NODE_MAGIC)
-                       return false;
+                       return __this_address;
        }
        if (ichdr.level == 0)
-               return false;
+               return __this_address;
        if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
-               return false;
+               return __this_address;
        if (ichdr.count == 0)
-               return false;
+               return __this_address;
 
        /*
         * we don't know if the node is for and attribute or directory tree,
@@ -163,11 +165,11 @@ xfs_da3_node_verify(
         */
        if (ichdr.count > mp->m_dir_geo->node_ents &&
            ichdr.count > mp->m_attr_geo->node_ents)
-               return false;
+               return __this_address;
 
        /* XXX: hash order check? */
 
-       return true;
+       return NULL;
 }
 
 static void
@@ -175,12 +177,13 @@ xfs_da3_node_write_verify(
        struct xfs_buf  *bp)
 {
        struct xfs_mount        *mp = bp->b_target->bt_mount;
-       struct xfs_buf_log_item *bip = bp->b_fspriv;
+       struct xfs_buf_log_item *bip = bp->b_log_item;
        struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
+       xfs_failaddr_t          fa;
 
-       if (!xfs_da3_node_verify(bp)) {
-               xfs_buf_ioerror(bp, -EFSCORRUPTED);
-               xfs_verifier_error(bp);
+       fa = xfs_da3_node_verify(bp);
+       if (fa) {
+               xfs_verifier_error(bp, -EFSCORRUPTED, fa);
                return;
        }
 
@@ -204,19 +207,20 @@ xfs_da3_node_read_verify(
        struct xfs_buf          *bp)
 {
        struct xfs_da_blkinfo   *info = bp->b_addr;
+       xfs_failaddr_t          fa;
 
        switch (be16_to_cpu(info->magic)) {
                case XFS_DA3_NODE_MAGIC:
                        if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
-                               xfs_buf_ioerror(bp, -EFSBADCRC);
+                               xfs_verifier_error(bp, -EFSBADCRC,
+                                               __this_address);
                                break;
                        }
                        /* fall through */
                case XFS_DA_NODE_MAGIC:
-                       if (!xfs_da3_node_verify(bp)) {
-                               xfs_buf_ioerror(bp, -EFSCORRUPTED);
-                               break;
-                       }
+                       fa = xfs_da3_node_verify(bp);
+                       if (fa)
+                               xfs_verifier_error(bp, -EFSCORRUPTED, fa);
                        return;
                case XFS_ATTR_LEAF_MAGIC:
                case XFS_ATTR3_LEAF_MAGIC:
@@ -229,16 +233,40 @@ xfs_da3_node_read_verify(
                        bp->b_ops->verify_read(bp);
                        return;
                default:
+                       xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
                        break;
        }
+}
+
+/* Verify the structure of a da3 block. */
+static xfs_failaddr_t
+xfs_da3_node_verify_struct(
+       struct xfs_buf          *bp)
+{
+       struct xfs_da_blkinfo   *info = bp->b_addr;
 
-       /* corrupt block */
-       xfs_verifier_error(bp);
+       switch (be16_to_cpu(info->magic)) {
+       case XFS_DA3_NODE_MAGIC:
+       case XFS_DA_NODE_MAGIC:
+               return xfs_da3_node_verify(bp);
+       case XFS_ATTR_LEAF_MAGIC:
+       case XFS_ATTR3_LEAF_MAGIC:
+               bp->b_ops = &xfs_attr3_leaf_buf_ops;
+               return bp->b_ops->verify_struct(bp);
+       case XFS_DIR2_LEAFN_MAGIC:
+       case XFS_DIR3_LEAFN_MAGIC:
+               bp->b_ops = &xfs_dir3_leafn_buf_ops;
+               return bp->b_ops->verify_struct(bp);
+       default:
+               return __this_address;
+       }
 }
 
 const struct xfs_buf_ops xfs_da3_node_buf_ops = {
+       .name = "xfs_da3_node",
        .verify_read = xfs_da3_node_read_verify,
        .verify_write = xfs_da3_node_write_verify,
+       .verify_struct = xfs_da3_node_verify_struct,
 };
 
 int
@@ -254,7 +282,7 @@ xfs_da3_node_read(
 
        err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
                                        which_fork, &xfs_da3_node_buf_ops);
-       if (!err && tp) {
+       if (!err && tp && *bpp) {
                struct xfs_da_blkinfo   *info = (*bpp)->b_addr;
                int                     type;
 
@@ -272,9 +300,11 @@ xfs_da3_node_read(
                        type = XFS_BLFT_DIR_LEAFN_BUF;
                        break;
                default:
-                       type = 0;
-                       ASSERT(0);
-                       break;
+                       XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
+                                       tp->t_mountp, info, sizeof(*info));
+                       xfs_trans_brelse(tp, *bpp);
+                       *bpp = NULL;
+                       return -EFSCORRUPTED;
                }
                xfs_trans_buf_set_type(tp, *bpp, type);
        }
@@ -317,10 +347,11 @@ xfs_da3_node_create(
        if (xfs_sb_version_hascrc(&mp->m_sb)) {
                struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
 
+               memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
                ichdr.magic = XFS_DA3_NODE_MAGIC;
                hdr3->info.blkno = cpu_to_be64(bp->b_bn);
                hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
-               uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_uuid);
+               uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
        } else {
                ichdr.magic = XFS_DA_NODE_MAGIC;
        }
@@ -346,7 +377,6 @@ xfs_da3_split(
        struct xfs_da_state_blk *newblk;
        struct xfs_da_state_blk *addblk;
        struct xfs_da_intnode   *node;
-       struct xfs_buf          *bp;
        int                     max;
        int                     action = 0;
        int                     error;
@@ -387,7 +417,9 @@ xfs_da3_split(
                                break;
                        }
                        /*
-                        * Entry wouldn't fit, split the leaf again.
+                        * Entry wouldn't fit, split the leaf again. The new
+                        * extrablk will be consumed by xfs_da3_node_split if
+                        * the node is split.
                         */
                        state->extravalid = 1;
                        if (state->inleaf) {
@@ -435,6 +467,14 @@ xfs_da3_split(
        if (!addblk)
                return 0;
 
+       /*
+        * xfs_da3_node_split() should have consumed any extra blocks we added
+        * during a double leaf split in the attr fork. This is guaranteed as
+        * we can't be here if the attr fork only has a single leaf block.
+        */
+       ASSERT(state->extravalid == 0 ||
+              state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
+
        /*
         * Split the root node.
         */
@@ -447,43 +487,33 @@ xfs_da3_split(
        }
 
        /*
-        * Update pointers to the node which used to be block 0 and
-        * just got bumped because of the addition of a new root node.
-        * There might be three blocks involved if a double split occurred,
-        * and the original block 0 could be at any position in the list.
+        * Update pointers to the node which used to be block 0 and just got
+        * bumped because of the addition of a new root node.  Note that the
+        * original block 0 could be at any position in the list of blocks in
+        * the tree.
         *
-        * Note: the magic numbers and sibling pointers are in the same
-        * physical place for both v2 and v3 headers (by design). Hence it
-        * doesn't matter which version of the xfs_da_intnode structure we use
-        * here as the result will be the same using either structure.
+        * Note: the magic numbers and sibling pointers are in the same physical
+        * place for both v2 and v3 headers (by design). Hence it doesn't matter
+        * which version of the xfs_da_intnode structure we use here as the
+        * result will be the same using either structure.
         */
        node = oldblk->bp->b_addr;
        if (node->hdr.info.forw) {
-               if (be32_to_cpu(node->hdr.info.forw) == addblk->blkno) {
-                       bp = addblk->bp;
-               } else {
-                       ASSERT(state->extravalid);
-                       bp = state->extrablk.bp;
-               }
-               node = bp->b_addr;
+               ASSERT(be32_to_cpu(node->hdr.info.forw) == addblk->blkno);
+               node = addblk->bp->b_addr;
                node->hdr.info.back = cpu_to_be32(oldblk->blkno);
-               xfs_trans_log_buf(state->args->trans, bp,
-                   XFS_DA_LOGRANGE(node, &node->hdr.info,
-                   sizeof(node->hdr.info)));
+               xfs_trans_log_buf(state->args->trans, addblk->bp,
+                                 XFS_DA_LOGRANGE(node, &node->hdr.info,
+                                 sizeof(node->hdr.info)));
        }
        node = oldblk->bp->b_addr;
        if (node->hdr.info.back) {
-               if (be32_to_cpu(node->hdr.info.back) == addblk->blkno) {
-                       bp = addblk->bp;
-               } else {
-                       ASSERT(state->extravalid);
-                       bp = state->extrablk.bp;
-               }
-               node = bp->b_addr;
+               ASSERT(be32_to_cpu(node->hdr.info.back) == addblk->blkno);
+               node = addblk->bp->b_addr;
                node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
-               xfs_trans_log_buf(state->args->trans, bp,
-                   XFS_DA_LOGRANGE(node, &node->hdr.info,
-                   sizeof(node->hdr.info)));
+               xfs_trans_log_buf(state->args->trans, addblk->bp,
+                                 XFS_DA_LOGRANGE(node, &node->hdr.info,
+                                 sizeof(node->hdr.info)));
        }
        addblk->bp = NULL;
        return 0;
@@ -1273,7 +1303,7 @@ xfs_da3_fixhashpath(
                        return;
                break;
        case XFS_DIR2_LEAFN_MAGIC:
-               lasthash = xfs_dir2_leafn_lasthash(dp, blk->bp, &count);
+               lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count);
                if (count == 0)
                        return;
                break;
@@ -1457,6 +1487,7 @@ xfs_da3_node_lookup_int(
        int                     max;
        int                     error;
        int                     retval;
+       unsigned int            expected_level = 0;
        struct xfs_inode        *dp = state->args->dp;
 
        args = state->args;
@@ -1465,7 +1496,7 @@ xfs_da3_node_lookup_int(
         * Descend thru the B-tree searching each level for the right
         * node to use, until the right hashval is found.
         */
-       blkno = (args->whichfork == XFS_DATA_FORK)? args->geo->leafblk : 0;
+       blkno = args->geo->leafblk;
        for (blk = &state->path.blk[0], state->path.active = 1;
                         state->path.active <= XFS_DA_NODE_MAXDEPTH;
                         blk++, state->path.active++) {
@@ -1493,8 +1524,8 @@ xfs_da3_node_lookup_int(
                if (blk->magic == XFS_DIR2_LEAFN_MAGIC ||
                    blk->magic == XFS_DIR3_LEAFN_MAGIC) {
                        blk->magic = XFS_DIR2_LEAFN_MAGIC;
-                       blk->hashval = xfs_dir2_leafn_lasthash(args->dp,
-                                                              blk->bp, NULL);
+                       blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
+                                                             blk->bp, NULL);
                        break;
                }
 
@@ -1508,6 +1539,18 @@ xfs_da3_node_lookup_int(
                dp->d_ops->node_hdr_from_disk(&nodehdr, node);
                btree = dp->d_ops->node_tree_p(node);
 
+               /* Tree taller than we can handle; bail out! */
+               if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
+                       return -EFSCORRUPTED;
+
+               /* Check the level from the root. */
+               if (blkno == args->geo->leafblk)
+                       expected_level = nodehdr.level - 1;
+               else if (expected_level != nodehdr.level)
+                       return -EFSCORRUPTED;
+               else
+                       expected_level--;
+
                max = nodehdr.count;
                blk->hashval = be32_to_cpu(btree[max - 1].hashval);
 
@@ -1553,8 +1596,15 @@ xfs_da3_node_lookup_int(
                        blk->index = probe;
                        blkno = be32_to_cpu(btree[probe].before);
                }
+
+               /* We can't point back to the root. */
+               if (blkno == args->geo->leafblk)
+                       return -EFSCORRUPTED;
        }
 
+       if (expected_level != 0)
+               return -EFSCORRUPTED;
+
        /*
         * A leaf block that ends in the hashval that we are interested in
         * (final hashval == search hashval) means that the next block may
@@ -1818,6 +1868,7 @@ xfs_da3_path_shift(
        struct xfs_da_args      *args;
        struct xfs_da_node_entry *btree;
        struct xfs_da3_icnode_hdr nodehdr;
+       struct xfs_buf          *bp;
        xfs_dablk_t             blkno = 0;
        int                     level;
        int                     error;
@@ -1862,20 +1913,24 @@ xfs_da3_path_shift(
         */
        for (blk++, level++; level < path->active; blk++, level++) {
                /*
-                * Release the old block.
-                * (if it's dirty, trans won't actually let go)
+                * Read the next child block into a local buffer.
                 */
-               if (release)
-                       xfs_trans_brelse(args->trans, blk->bp);
+               error = xfs_da3_node_read(args->trans, dp, blkno, -1, &bp,
+                                         args->whichfork);
+               if (error)
+                       return error;
 
                /*
-                * Read the next child block.
+                * Release the old block (if it's dirty, the trans doesn't
+                * actually let go) and swap the local buffer into the path
+                * structure. This ensures failure of the above read doesn't set
+                * a NULL buffer in an active slot in the path.
                 */
+               if (release)
+                       xfs_trans_brelse(args->trans, blk->bp);
                blk->blkno = blkno;
-               error = xfs_da3_node_read(args->trans, dp, blkno, -1,
-                                       &blk->bp, args->whichfork);
-               if (error)
-                       return error;
+               blk->bp = bp;
+
                info = blk->bp->b_addr;
                ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
                       info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
@@ -1915,8 +1970,8 @@ xfs_da3_path_shift(
                        blk->magic = XFS_DIR2_LEAFN_MAGIC;
                        ASSERT(level == path->active-1);
                        blk->index = 0;
-                       blk->hashval = xfs_dir2_leafn_lasthash(args->dp,
-                                                              blk->bp, NULL);
+                       blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
+                                                             blk->bp, NULL);
                        break;
                default:
                        ASSERT(0);
@@ -1938,7 +1993,7 @@ xfs_da3_path_shift(
  * This is implemented with some source-level loop unrolling.
  */
 xfs_dahash_t
-xfs_da_hashname(const __uint8_t *name, int namelen)
+xfs_da_hashname(const uint8_t *name, int namelen)
 {
        xfs_dahash_t hash;
 
@@ -2015,7 +2070,7 @@ xfs_da_grow_inode_int(
        error = xfs_bmapi_write(tp, dp, *bno, count,
                        xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
                        args->firstblock, args->total, &map, &nmap,
-                       args->flist);
+                       args->dfops);
        if (error)
                return error;
 
@@ -2038,7 +2093,7 @@ xfs_da_grow_inode_int(
                        error = xfs_bmapi_write(tp, dp, b, c,
                                        xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
                                        args->firstblock, args->total,
-                                       &mapp[mapi], &nmap, args->flist);
+                                       &mapp[mapi], &nmap, args->dfops);
                        if (error)
                                goto out_free_map;
                        if (nmap < 1)
@@ -2347,8 +2402,8 @@ xfs_da_shrink_inode(
                 * the last block to the place we want to kill.
                 */
                error = xfs_bunmapi(tp, dp, dead_blkno, count,
-                                   xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
-                                   0, args->firstblock, args->flist, &done);
+                                   xfs_bmapi_aflag(w), 0, args->firstblock,
+                                   args->dfops, &done);
                if (error == -ENOSPC) {
                        if (w != XFS_DATA_FORK)
                                break;
@@ -2619,7 +2674,7 @@ out_free:
 /*
  * Readahead the dir/attr block.
  */
-xfs_daddr_t
+int
 xfs_da_reada_buf(
        struct xfs_inode        *dp,
        xfs_dablk_t             bno,
@@ -2650,7 +2705,5 @@ out_free:
        if (mapp != &map)
                kmem_free(mapp);
 
-       if (error)
-               return -1;
-       return mappedbno;
+       return error;
 }