]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
logprint: factor out a xlog_unpack_rec_header
authorChristoph Hellwig <hch@lst.de>
Fri, 28 Nov 2025 06:29:54 +0000 (07:29 +0100)
committerAndrey Albershteyn <aalbersh@kernel.org>
Wed, 3 Dec 2025 10:48:04 +0000 (11:48 +0100)
Split the log record validation and unpacking logic into a separate
helper.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Andrey Albershteyn <aalbersh@kernel.org>
logprint/log_misc.c

index b15e1334fa180a81d9d3f03a0ce6574c486eae17..23ec8c55228e8363f4f66e934ea079b465389d2e 100644 (file)
@@ -1032,6 +1032,47 @@ xlog_print_op(
        return true;
 }
 
+static bool
+xlog_unpack_rec_header(
+       struct xlog_rec_header  *rhead,
+       struct xlog_rec_ext_header *xhdrs,
+       char                    *ptr,
+       int                     *read_type,
+       int                     i)
+{
+       __be32                  *cycle = (__be32 *)ptr;
+
+       /*
+        * Data should not have magicno as first word as it should be cycle
+        * number.
+        */
+       if (*cycle == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
+                return false;
+
+       /* Verify cycle number. */
+       if (be32_to_cpu(rhead->h_cycle) != be32_to_cpu(*cycle) &&
+           (*read_type == FULL_READ ||
+            be32_to_cpu(rhead->h_cycle) + 1 != be32_to_cpu(*cycle)))
+               return false;
+
+       /* Copy back the data from the header */
+       if (i < XLOG_HEADER_CYCLE_SIZE / BBSIZE) {
+               /* from 1st header */
+               *cycle = rhead->h_cycle_data[i];
+       } else {
+               int             j, k;
+
+               ASSERT(xhdrs != NULL);
+
+               /* from extra headers */
+               j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
+               k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
+               *cycle = xhdrs[j - 1].xh_cycle_data[k];
+       }
+
+       return true;
+}
+
 static int
 xlog_print_record(
        struct xlog             *log,
@@ -1047,7 +1088,7 @@ xlog_print_record(
     char               *buf, *ptr;
     int                        read_len;
     bool               lost_context = false;
-    int                        ret, i, j, k;
+    int                        ret, i;
 
     if (print_no_print)
            return NO_ERROR;
@@ -1100,43 +1141,10 @@ xlog_print_record(
      */
     buf = ptr;
     for (i = 0; ptr < buf + read_len; ptr += BBSIZE, i++) {
-       xlog_rec_header_t *rechead = (xlog_rec_header_t *)ptr;
-
-       /* sanity checks */
-       if (be32_to_cpu(rechead->h_magicno) == XLOG_HEADER_MAGIC_NUM) {
-           /* data should not have magicno as first word
-            * as it should by cycle#
-            */
+       if (!xlog_unpack_rec_header(rhead, xhdrs, ptr, read_type, i)) {
            free(buf);
            return -1;
-       } else {
-           /* verify cycle#
-            * FIXME: cycle+1 should be a macro pv#900369
-            */
-           if (be32_to_cpu(rhead->h_cycle) !=
-                       be32_to_cpu(*(__be32 *)ptr)) {
-               if ((*read_type == FULL_READ) ||
-                   (be32_to_cpu(rhead->h_cycle) + 1 !=
-                               be32_to_cpu(*(__be32 *)ptr))) {
-                       free(buf);
-                       return -1;
-               }
-           }
        }
-
-       /* copy back the data from the header */
-       if (i < XLOG_HEADER_CYCLE_SIZE / BBSIZE) {
-               /* from 1st header */
-               *(__be32 *)ptr = rhead->h_cycle_data[i];
-       }
-       else {
-               ASSERT(xhdrs != NULL);
-               /* from extra headers */
-               j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
-               k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
-               *(__be32 *)ptr = xhdrs[j-1].xh_cycle_data[k];
-       }
-
     }
 
     ptr = buf;