xfs: refactor log recovery item sorting into a generic dispatch structure
authorDarrick J. Wong <darrick.wong@oracle.com>
Fri, 1 May 2020 23:00:45 +0000 (16:00 -0700)
committerDarrick J. Wong <darrick.wong@oracle.com>
Fri, 8 May 2020 15:49:58 +0000 (08:49 -0700)
Create a generic dispatch structure to delegate recovery of different
log item types into various code modules.  This will enable us to move
code specific to a particular log item type out of xfs_log_recover.c and
into the log item source.

The first operation we virtualize is the log item sorting.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Chandan Babu R <chandanrlinux@gmail.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
fs/xfs/Makefile
fs/xfs/libxfs/xfs_log_recover.h
fs/xfs/xfs_bmap_item.c
fs/xfs/xfs_buf_item_recover.c [new file with mode: 0644]
fs/xfs/xfs_dquot_item_recover.c [new file with mode: 0644]
fs/xfs/xfs_extfree_item.c
fs/xfs/xfs_icreate_item.c
fs/xfs/xfs_inode_item_recover.c [new file with mode: 0644]
fs/xfs/xfs_log_recover.c
fs/xfs/xfs_refcount_item.c
fs/xfs/xfs_rmap_item.c

index ff94fb90a2ee5ed9979310e331e80a97e8709830..04611a1068b4934b733e0e3d87800b0c9a0b29ab 100644 (file)
@@ -99,9 +99,12 @@ xfs-y                                += xfs_log.o \
                                   xfs_log_cil.o \
                                   xfs_bmap_item.o \
                                   xfs_buf_item.o \
+                                  xfs_buf_item_recover.o \
+                                  xfs_dquot_item_recover.o \
                                   xfs_extfree_item.o \
                                   xfs_icreate_item.o \
                                   xfs_inode_item.o \
+                                  xfs_inode_item_recover.o \
                                   xfs_refcount_item.o \
                                   xfs_rmap_item.o \
                                   xfs_log_recover.o \
index 148e0cb5d379dd0323c47281368148207dad4ae7..c9c27e6367bbd26c0d721c344f3834b58118fc8e 100644 (file)
@@ -6,6 +6,47 @@
 #ifndef        __XFS_LOG_RECOVER_H__
 #define __XFS_LOG_RECOVER_H__
 
+/*
+ * Each log item type (XFS_LI_*) gets its own xlog_recover_item_ops to
+ * define how recovery should work for that type of log item.
+ */
+struct xlog_recover_item;
+
+/* Sorting hat for log items as they're read in. */
+enum xlog_recover_reorder {
+       XLOG_REORDER_BUFFER_LIST,
+       XLOG_REORDER_ITEM_LIST,
+       XLOG_REORDER_INODE_BUFFER_LIST,
+       XLOG_REORDER_CANCEL_LIST,
+};
+
+struct xlog_recover_item_ops {
+       uint16_t        item_type;      /* XFS_LI_* type code. */
+
+       /*
+        * Help sort recovered log items into the order required to replay them
+        * correctly.  Log item types that always use XLOG_REORDER_ITEM_LIST do
+        * not have to supply a function here.  See the comment preceding
+        * xlog_recover_reorder_trans for more details about what the return
+        * values mean.
+        */
+       enum xlog_recover_reorder (*reorder)(struct xlog_recover_item *item);
+};
+
+extern const struct xlog_recover_item_ops xlog_icreate_item_ops;
+extern const struct xlog_recover_item_ops xlog_buf_item_ops;
+extern const struct xlog_recover_item_ops xlog_inode_item_ops;
+extern const struct xlog_recover_item_ops xlog_dquot_item_ops;
+extern const struct xlog_recover_item_ops xlog_quotaoff_item_ops;
+extern const struct xlog_recover_item_ops xlog_bui_item_ops;
+extern const struct xlog_recover_item_ops xlog_bud_item_ops;
+extern const struct xlog_recover_item_ops xlog_efi_item_ops;
+extern const struct xlog_recover_item_ops xlog_efd_item_ops;
+extern const struct xlog_recover_item_ops xlog_rui_item_ops;
+extern const struct xlog_recover_item_ops xlog_rud_item_ops;
+extern const struct xlog_recover_item_ops xlog_cui_item_ops;
+extern const struct xlog_recover_item_ops xlog_cud_item_ops;
+
 /*
  * Macros, structures, prototypes for internal log manager use.
  */
  */
 struct xlog_recover_item {
        struct list_head        ri_list;
-       int                     ri_type;
        int                     ri_cnt; /* count of regions found */
        int                     ri_total;       /* total regions */
-       xfs_log_iovec_t         *ri_buf;        /* ptr to regions buffer */
+       struct xfs_log_iovec    *ri_buf;        /* ptr to regions buffer */
+       const struct xlog_recover_item_ops *ri_ops;
 };
 
 struct xlog_recover {
index 17eb7cfad5d99530a922e3dd2d2351ef9fb4248f..508b48ca5ced9c0cc3190ab24610ed5c6690fd1b 100644 (file)
@@ -22,6 +22,7 @@
 #include "xfs_bmap_btree.h"
 #include "xfs_trans_space.h"
 #include "xfs_error.h"
+#include "xfs_log_recover.h"
 
 kmem_zone_t    *xfs_bui_zone;
 kmem_zone_t    *xfs_bud_zone;
@@ -557,3 +558,11 @@ err_inode:
        }
        return error;
 }
+
+const struct xlog_recover_item_ops xlog_bui_item_ops = {
+       .item_type              = XFS_LI_BUI,
+};
+
+const struct xlog_recover_item_ops xlog_bud_item_ops = {
+       .item_type              = XFS_LI_BUD,
+};
diff --git a/fs/xfs/xfs_buf_item_recover.c b/fs/xfs/xfs_buf_item_recover.c
new file mode 100644 (file)
index 0000000..5dea632
--- /dev/null
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2000-2006 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ */
+#include "xfs.h"
+#include "xfs_fs.h"
+#include "xfs_shared.h"
+#include "xfs_format.h"
+#include "xfs_log_format.h"
+#include "xfs_trans_resv.h"
+#include "xfs_bit.h"
+#include "xfs_mount.h"
+#include "xfs_trans.h"
+#include "xfs_buf_item.h"
+#include "xfs_trans_priv.h"
+#include "xfs_trace.h"
+#include "xfs_log.h"
+#include "xfs_log_priv.h"
+#include "xfs_log_recover.h"
+
+/*
+ * Sort buffer items for log recovery.  Most buffer items should end up on the
+ * buffer list and are recovered first, with the following exceptions:
+ *
+ * 1. XFS_BLF_CANCEL buffers must be processed last because some log items
+ *    might depend on the incor ecancellation record, and replaying a cancelled
+ *    buffer item can remove the incore record.
+ *
+ * 2. XFS_BLF_INODE_BUF buffers are handled after most regular items so that
+ *    we replay di_next_unlinked only after flushing the inode 'free' state
+ *    to the inode buffer.
+ *
+ * See xlog_recover_reorder_trans for more details.
+ */
+STATIC enum xlog_recover_reorder
+xlog_recover_buf_reorder(
+       struct xlog_recover_item        *item)
+{
+       struct xfs_buf_log_format       *buf_f = item->ri_buf[0].i_addr;
+
+       if (buf_f->blf_flags & XFS_BLF_CANCEL)
+               return XLOG_REORDER_CANCEL_LIST;
+       if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
+               return XLOG_REORDER_INODE_BUFFER_LIST;
+       return XLOG_REORDER_BUFFER_LIST;
+}
+
+const struct xlog_recover_item_ops xlog_buf_item_ops = {
+       .item_type              = XFS_LI_BUF,
+       .reorder                = xlog_recover_buf_reorder,
+};
diff --git a/fs/xfs/xfs_dquot_item_recover.c b/fs/xfs/xfs_dquot_item_recover.c
new file mode 100644 (file)
index 0000000..78fe644
--- /dev/null
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2000-2006 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ */
+#include "xfs.h"
+#include "xfs_fs.h"
+#include "xfs_shared.h"
+#include "xfs_format.h"
+#include "xfs_log_format.h"
+#include "xfs_trans_resv.h"
+#include "xfs_mount.h"
+#include "xfs_inode.h"
+#include "xfs_quota.h"
+#include "xfs_trans.h"
+#include "xfs_buf_item.h"
+#include "xfs_trans_priv.h"
+#include "xfs_qm.h"
+#include "xfs_log.h"
+#include "xfs_log_priv.h"
+#include "xfs_log_recover.h"
+
+const struct xlog_recover_item_ops xlog_dquot_item_ops = {
+       .item_type              = XFS_LI_DQUOT,
+};
+
+const struct xlog_recover_item_ops xlog_quotaoff_item_ops = {
+       .item_type              = XFS_LI_QUOTAOFF,
+};
index 9809637fb84dc004dd17c2ea51581674cffbae38..163d01cb9f9fd7e734d230d506785129c6988ace 100644 (file)
@@ -22,6 +22,7 @@
 #include "xfs_bmap.h"
 #include "xfs_trace.h"
 #include "xfs_error.h"
+#include "xfs_log_recover.h"
 
 kmem_zone_t    *xfs_efi_zone;
 kmem_zone_t    *xfs_efd_zone;
@@ -644,3 +645,11 @@ abort_error:
        xfs_trans_cancel(tp);
        return error;
 }
+
+const struct xlog_recover_item_ops xlog_efi_item_ops = {
+       .item_type              = XFS_LI_EFI,
+};
+
+const struct xlog_recover_item_ops xlog_efd_item_ops = {
+       .item_type              = XFS_LI_EFD,
+};
index 490fee22b878222a0869f2805daf44b399470fce..366c1e722a29621bb2bde44f5f1a9c4072117fdc 100644 (file)
@@ -11,6 +11,8 @@
 #include "xfs_trans_priv.h"
 #include "xfs_icreate_item.h"
 #include "xfs_log.h"
+#include "xfs_log_priv.h"
+#include "xfs_log_recover.h"
 
 kmem_zone_t    *xfs_icreate_zone;              /* inode create item zone */
 
@@ -107,3 +109,21 @@ xfs_icreate_log(
        tp->t_flags |= XFS_TRANS_DIRTY;
        set_bit(XFS_LI_DIRTY, &icp->ic_item.li_flags);
 }
+
+static enum xlog_recover_reorder
+xlog_recover_icreate_reorder(
+               struct xlog_recover_item *item)
+{
+       /*
+        * Inode allocation buffers must be replayed before subsequent inode
+        * items try to modify those buffers.  ICREATE items are the logical
+        * equivalent of logging a newly initialized inode buffer, so recover
+        * these at the same time that we recover logged buffers.
+        */
+       return XLOG_REORDER_BUFFER_LIST;
+}
+
+const struct xlog_recover_item_ops xlog_icreate_item_ops = {
+       .item_type              = XFS_LI_ICREATE,
+       .reorder                = xlog_recover_icreate_reorder,
+};
diff --git a/fs/xfs/xfs_inode_item_recover.c b/fs/xfs/xfs_inode_item_recover.c
new file mode 100644 (file)
index 0000000..b19a151
--- /dev/null
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2000-2006 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ */
+#include "xfs.h"
+#include "xfs_fs.h"
+#include "xfs_shared.h"
+#include "xfs_format.h"
+#include "xfs_log_format.h"
+#include "xfs_trans_resv.h"
+#include "xfs_mount.h"
+#include "xfs_inode.h"
+#include "xfs_trans.h"
+#include "xfs_inode_item.h"
+#include "xfs_trace.h"
+#include "xfs_trans_priv.h"
+#include "xfs_buf_item.h"
+#include "xfs_log.h"
+#include "xfs_error.h"
+#include "xfs_log_priv.h"
+#include "xfs_log_recover.h"
+
+const struct xlog_recover_item_ops xlog_inode_item_ops = {
+       .item_type              = XFS_LI_INODE,
+};
index ed4ed76f8e9c45f6f6a4c7c5a2ec198971b55c14..e44c64fca65f48a01a27cba683797ea1460514ae 100644 (file)
@@ -1785,6 +1785,34 @@ xlog_clear_stale_blocks(
  *
  ******************************************************************************
  */
+static const struct xlog_recover_item_ops *xlog_recover_item_ops[] = {
+       &xlog_buf_item_ops,
+       &xlog_inode_item_ops,
+       &xlog_dquot_item_ops,
+       &xlog_quotaoff_item_ops,
+       &xlog_icreate_item_ops,
+       &xlog_efi_item_ops,
+       &xlog_efd_item_ops,
+       &xlog_rui_item_ops,
+       &xlog_rud_item_ops,
+       &xlog_cui_item_ops,
+       &xlog_cud_item_ops,
+       &xlog_bui_item_ops,
+       &xlog_bud_item_ops,
+};
+
+static const struct xlog_recover_item_ops *
+xlog_find_item_ops(
+       struct xlog_recover_item                *item)
+{
+       unsigned int                            i;
+
+       for (i = 0; i < ARRAY_SIZE(xlog_recover_item_ops); i++)
+               if (ITEM_TYPE(item) == xlog_recover_item_ops[i]->item_type)
+                       return xlog_recover_item_ops[i];
+
+       return NULL;
+}
 
 /*
  * Sort the log items in the transaction.
@@ -1851,41 +1879,10 @@ xlog_recover_reorder_trans(
 
        list_splice_init(&trans->r_itemq, &sort_list);
        list_for_each_entry_safe(item, n, &sort_list, ri_list) {
-               xfs_buf_log_format_t    *buf_f = item->ri_buf[0].i_addr;
+               enum xlog_recover_reorder       fate = XLOG_REORDER_ITEM_LIST;
 
-               switch (ITEM_TYPE(item)) {
-               case XFS_LI_ICREATE:
-                       list_move_tail(&item->ri_list, &buffer_list);
-                       break;
-               case XFS_LI_BUF:
-                       if (buf_f->blf_flags & XFS_BLF_CANCEL) {
-                               trace_xfs_log_recover_item_reorder_head(log,
-                                                       trans, item, pass);
-                               list_move(&item->ri_list, &cancel_list);
-                               break;
-                       }
-                       if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
-                               list_move(&item->ri_list, &inode_buffer_list);
-                               break;
-                       }
-                       list_move_tail(&item->ri_list, &buffer_list);
-                       break;
-               case XFS_LI_INODE:
-               case XFS_LI_DQUOT:
-               case XFS_LI_QUOTAOFF:
-               case XFS_LI_EFD:
-               case XFS_LI_EFI:
-               case XFS_LI_RUI:
-               case XFS_LI_RUD:
-               case XFS_LI_CUI:
-               case XFS_LI_CUD:
-               case XFS_LI_BUI:
-               case XFS_LI_BUD:
-                       trace_xfs_log_recover_item_reorder_tail(log,
-                                                       trans, item, pass);
-                       list_move_tail(&item->ri_list, &item_list);
-                       break;
-               default:
+               item->ri_ops = xlog_find_item_ops(item);
+               if (!item->ri_ops) {
                        xfs_warn(log->l_mp,
                                "%s: unrecognized type of log operation (%d)",
                                __func__, ITEM_TYPE(item));
@@ -1896,11 +1893,33 @@ xlog_recover_reorder_trans(
                         */
                        if (!list_empty(&sort_list))
                                list_splice_init(&sort_list, &trans->r_itemq);
-                       error = -EIO;
-                       goto out;
+                       error = -EFSCORRUPTED;
+                       break;
+               }
+
+               if (item->ri_ops->reorder)
+                       fate = item->ri_ops->reorder(item);
+
+               switch (fate) {
+               case XLOG_REORDER_BUFFER_LIST:
+                       list_move_tail(&item->ri_list, &buffer_list);
+                       break;
+               case XLOG_REORDER_CANCEL_LIST:
+                       trace_xfs_log_recover_item_reorder_head(log,
+                                       trans, item, pass);
+                       list_move(&item->ri_list, &cancel_list);
+                       break;
+               case XLOG_REORDER_INODE_BUFFER_LIST:
+                       list_move(&item->ri_list, &inode_buffer_list);
+                       break;
+               case XLOG_REORDER_ITEM_LIST:
+                       trace_xfs_log_recover_item_reorder_tail(log,
+                                                       trans, item, pass);
+                       list_move_tail(&item->ri_list, &item_list);
+                       break;
                }
        }
-out:
+
        ASSERT(list_empty(&sort_list));
        if (!list_empty(&buffer_list))
                list_splice(&buffer_list, &trans->r_itemq);
index 01bb77daeaeeeba54d94dc3e8d0aef1531487097..2a9465d9a77f4902eb6a79def0bcd948333ed0ed 100644 (file)
@@ -18,6 +18,7 @@
 #include "xfs_log.h"
 #include "xfs_refcount.h"
 #include "xfs_error.h"
+#include "xfs_log_recover.h"
 
 kmem_zone_t    *xfs_cui_zone;
 kmem_zone_t    *xfs_cud_zone;
@@ -570,3 +571,11 @@ abort_error:
        xfs_trans_cancel(tp);
        return error;
 }
+
+const struct xlog_recover_item_ops xlog_cui_item_ops = {
+       .item_type              = XFS_LI_CUI,
+};
+
+const struct xlog_recover_item_ops xlog_cud_item_ops = {
+       .item_type              = XFS_LI_CUD,
+};
index fdb12b01b1781645d43bb043be31ca3874ff7230..0f3af9f0576426eba87c74461b01b2d67700306e 100644 (file)
@@ -18,6 +18,7 @@
 #include "xfs_log.h"
 #include "xfs_rmap.h"
 #include "xfs_error.h"
+#include "xfs_log_recover.h"
 
 kmem_zone_t    *xfs_rui_zone;
 kmem_zone_t    *xfs_rud_zone;
@@ -585,3 +586,11 @@ abort_error:
        xfs_trans_cancel(tp);
        return error;
 }
+
+const struct xlog_recover_item_ops xlog_rui_item_ops = {
+       .item_type              = XFS_LI_RUI,
+};
+
+const struct xlog_recover_item_ops xlog_rud_item_ops = {
+       .item_type              = XFS_LI_RUD,
+};