]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - libxfs/xfs_dir2_leaf.c
xfs: fix transaction leak on remote attr set/remove failure
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_leaf.c
index 038d0c425f54ca2a7dd049c95db94fe27632b78f..dbe4d2efb069a6ea6f03c21bf71b723ef2e87b3a 100644 (file)
@@ -1,20 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  * Copyright (c) 2013 Red Hat, Inc.
  * All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software Foundation,
- * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 #include "libxfs_priv.h"
 #include "xfs_fs.h"
@@ -78,7 +66,8 @@ xfs_dir3_leaf_check(
        if (!fa)
                return;
        xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
-                       bp->b_addr, __FILE__, __LINE__, fa);
+                       bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
+                       fa);
        ASSERT(0);
 }
 #else
@@ -187,12 +176,16 @@ __read_verify(
        uint16_t        magic)
 {
        struct xfs_mount        *mp = bp->b_target->bt_mount;
+       xfs_failaddr_t          fa;
 
        if (xfs_sb_version_hascrc(&mp->m_sb) &&
             !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
-               xfs_verifier_error(bp, -EFSBADCRC);
-       else if (xfs_dir3_leaf_verify(bp, magic))
-               xfs_verifier_error(bp, -EFSCORRUPTED);
+               xfs_verifier_error(bp, -EFSBADCRC, __this_address);
+       else {
+               fa = xfs_dir3_leaf_verify(bp, magic);
+               if (fa)
+                       xfs_verifier_error(bp, -EFSCORRUPTED, fa);
+       }
 }
 
 static void
@@ -201,11 +194,13 @@ __write_verify(
        uint16_t        magic)
 {
        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_dir3_leaf_hdr *hdr3 = bp->b_addr;
+       xfs_failaddr_t          fa;
 
-       if (xfs_dir3_leaf_verify(bp, magic)) {
-               xfs_verifier_error(bp, -EFSCORRUPTED);
+       fa = xfs_dir3_leaf_verify(bp, magic);
+       if (fa) {
+               xfs_verifier_error(bp, -EFSCORRUPTED, fa);
                return;
        }
 
@@ -218,6 +213,13 @@ __write_verify(
        xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF);
 }
 
+static xfs_failaddr_t
+xfs_dir3_leaf1_verify(
+       struct xfs_buf  *bp)
+{
+       return xfs_dir3_leaf_verify(bp, XFS_DIR2_LEAF1_MAGIC);
+}
+
 static void
 xfs_dir3_leaf1_read_verify(
        struct xfs_buf  *bp)
@@ -232,6 +234,13 @@ xfs_dir3_leaf1_write_verify(
        __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
 }
 
+static xfs_failaddr_t
+xfs_dir3_leafn_verify(
+       struct xfs_buf  *bp)
+{
+       return xfs_dir3_leaf_verify(bp, XFS_DIR2_LEAFN_MAGIC);
+}
+
 static void
 xfs_dir3_leafn_read_verify(
        struct xfs_buf  *bp)
@@ -250,12 +259,14 @@ const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
        .name = "xfs_dir3_leaf1",
        .verify_read = xfs_dir3_leaf1_read_verify,
        .verify_write = xfs_dir3_leaf1_write_verify,
+       .verify_struct = xfs_dir3_leaf1_verify,
 };
 
 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
        .name = "xfs_dir3_leafn",
        .verify_read = xfs_dir3_leafn_read_verify,
        .verify_write = xfs_dir3_leafn_write_verify,
+       .verify_struct = xfs_dir3_leafn_verify,
 };
 
 int
@@ -576,8 +587,8 @@ xfs_dir3_leaf_find_entry(
                                (index - lowstale - 1) *
                                        sizeof(xfs_dir2_leaf_entry_t));
                }
-               *lfloglow = MIN(lowstale, *lfloglow);
-               *lfloghigh = MAX(index - 1, *lfloghigh);
+               *lfloglow = min(lowstale, *lfloglow);
+               *lfloghigh = max(index - 1, *lfloghigh);
                leafhdr->stale--;
                return &ents[index - 1];
        }
@@ -596,8 +607,8 @@ xfs_dir3_leaf_find_entry(
                memmove(&ents[index + 1], &ents[index],
                        (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
        }
-       *lfloglow = MIN(index, *lfloglow);
-       *lfloghigh = MAX(highstale, *lfloghigh);
+       *lfloglow = min(index, *lfloglow);
+       *lfloghigh = max(highstale, *lfloghigh);
        leafhdr->stale--;
        return &ents[index];
 }
@@ -847,14 +858,17 @@ xfs_dir2_leaf_addname(
         */
        dup = (xfs_dir2_data_unused_t *)
              ((char *)hdr + be16_to_cpu(bf[0].offset));
-       ASSERT(be16_to_cpu(dup->length) >= length);
        needscan = needlog = 0;
        /*
         * Mark the initial part of our freespace in use for the new entry.
         */
-       xfs_dir2_data_use_free(args, dbp, dup,
-               (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
-               &needlog, &needscan);
+       error = xfs_dir2_data_use_free(args, dbp, dup,
+                       (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
+                       length, &needlog, &needscan);
+       if (error) {
+               xfs_trans_brelse(tp, lbp);
+               return error;
+       }
        /*
         * Initialize our new entry (at last).
         */
@@ -1390,7 +1404,8 @@ xfs_dir2_leaf_removename(
        oldbest = be16_to_cpu(bf[0].length);
        ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
        bestsp = xfs_dir2_leaf_bests_p(ltp);
-       ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
+       if (be16_to_cpu(bestsp[db]) != oldbest)
+               return -EFSCORRUPTED;
        /*
         * Mark the former data entry unused.
         */