xfs: allow setting and clearing of log incompat feature flags
authorDarrick J. Wong <djwong@kernel.org>
Sun, 8 Aug 2021 15:27:12 +0000 (08:27 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Mon, 9 Aug 2021 22:57:59 +0000 (15:57 -0700)
Log incompat feature flags in the superblock exist for one purpose: to
protect the contents of a dirty log from replay on a kernel that isn't
prepared to handle those dirty contents.  This means that they can be
cleared if (a) we know the log is clean and (b) we know that there
aren't any other threads in the system that might be setting or relying
upon a log incompat flag.

Therefore, clear the log incompat flags when we've finished recovering
the log, when we're unmounting cleanly, remounting read-only, or
freezing; and provide a function so that subsequent patches can start
using this.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Allison Henderson <allison.henderson@oracle.com>
Reviewed-by: Chandan Babu R <chandanrlinux@gmail.com>
fs/xfs/libxfs/xfs_format.h
fs/xfs/xfs_log.c
fs/xfs/xfs_log_recover.c
fs/xfs/xfs_mount.c
fs/xfs/xfs_mount.h

index 37570cf0537e6301ea5dd344a3ad7f63fda4af77..5d8a129150d54718205a851b1ae23569b70c8aac 100644 (file)
@@ -495,6 +495,21 @@ xfs_sb_has_incompat_log_feature(
        return (sbp->sb_features_log_incompat & feature) != 0;
 }
 
+static inline void
+xfs_sb_remove_incompat_log_features(
+       struct xfs_sb   *sbp)
+{
+       sbp->sb_features_log_incompat &= ~XFS_SB_FEAT_INCOMPAT_LOG_ALL;
+}
+
+static inline void
+xfs_sb_add_incompat_log_features(
+       struct xfs_sb   *sbp,
+       unsigned int    features)
+{
+       sbp->sb_features_log_incompat |= features;
+}
+
 /*
  * V5 superblock specific feature checks
  */
index eb8341027cc7fbe012cc56430b446b8bd47d6211..e0d4ffce7a8a377503b422d6a601a07aeb48d190 100644 (file)
@@ -972,6 +972,20 @@ int
 xfs_log_quiesce(
        struct xfs_mount        *mp)
 {
+       /*
+        * Clear log incompat features since we're quiescing the log.  Report
+        * failures, though it's not fatal to have a higher log feature
+        * protection level than the log contents actually require.
+        */
+       if (xfs_clear_incompat_log_features(mp)) {
+               int error;
+
+               error = xfs_sync_sb(mp, false);
+               if (error)
+                       xfs_warn(mp,
+       "Failed to clear log incompat features on quiesce");
+       }
+
        cancel_delayed_work_sync(&mp->m_log->l_work);
        xfs_log_force(mp, XFS_LOG_SYNC);
 
index ea96b5e45364db58e3d51b3cd4d71e5f677ff235..1d324c8bac6bf7c3565346a81266f61cc5e86705 100644 (file)
@@ -3471,6 +3471,22 @@ xlog_recover_finish(
                 */
                xfs_log_force(log->l_mp, XFS_LOG_SYNC);
 
+               /*
+                * Now that we've recovered the log and all the intents, we can
+                * clear the log incompat feature bits in the superblock
+                * because there's no longer anything to protect.  We rely on
+                * the AIL push to write out the updated superblock after
+                * everything else.
+                */
+               if (xfs_clear_incompat_log_features(log->l_mp)) {
+                       error = xfs_sync_sb(log->l_mp, false);
+                       if (error < 0) {
+                               xfs_alert(log->l_mp,
+       "Failed to clear log incompat features on recovery");
+                               return error;
+                       }
+               }
+
                xlog_recover_process_iunlinks(log);
 
                xlog_recover_check_summary(log);
index ff08192d8d2ad063840c5dee16b8b757f0999d68..74349eab5b5836274ff4d82de909de72cd8bff32 100644 (file)
@@ -1241,6 +1241,116 @@ xfs_force_summary_recalc(
        xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
 }
 
+/*
+ * Enable a log incompat feature flag in the primary superblock.  The caller
+ * cannot have any other transactions in progress.
+ */
+int
+xfs_add_incompat_log_feature(
+       struct xfs_mount        *mp,
+       uint32_t                feature)
+{
+       struct xfs_dsb          *dsb;
+       int                     error;
+
+       ASSERT(hweight32(feature) == 1);
+       ASSERT(!(feature & XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
+
+       /*
+        * Force the log to disk and kick the background AIL thread to reduce
+        * the chances that the bwrite will stall waiting for the AIL to unpin
+        * the primary superblock buffer.  This isn't a data integrity
+        * operation, so we don't need a synchronous push.
+        */
+       error = xfs_log_force(mp, XFS_LOG_SYNC);
+       if (error)
+               return error;
+       xfs_ail_push_all(mp->m_ail);
+
+       /*
+        * Lock the primary superblock buffer to serialize all callers that
+        * are trying to set feature bits.
+        */
+       xfs_buf_lock(mp->m_sb_bp);
+       xfs_buf_hold(mp->m_sb_bp);
+
+       if (XFS_FORCED_SHUTDOWN(mp)) {
+               error = -EIO;
+               goto rele;
+       }
+
+       if (xfs_sb_has_incompat_log_feature(&mp->m_sb, feature))
+               goto rele;
+
+       /*
+        * Write the primary superblock to disk immediately, because we need
+        * the log_incompat bit to be set in the primary super now to protect
+        * the log items that we're going to commit later.
+        */
+       dsb = mp->m_sb_bp->b_addr;
+       xfs_sb_to_disk(dsb, &mp->m_sb);
+       dsb->sb_features_log_incompat |= cpu_to_be32(feature);
+       error = xfs_bwrite(mp->m_sb_bp);
+       if (error)
+               goto shutdown;
+
+       /*
+        * Add the feature bits to the incore superblock before we unlock the
+        * buffer.
+        */
+       xfs_sb_add_incompat_log_features(&mp->m_sb, feature);
+       xfs_buf_relse(mp->m_sb_bp);
+
+       /* Log the superblock to disk. */
+       return xfs_sync_sb(mp, false);
+shutdown:
+       xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
+rele:
+       xfs_buf_relse(mp->m_sb_bp);
+       return error;
+}
+
+/*
+ * Clear all the log incompat flags from the superblock.
+ *
+ * The caller cannot be in a transaction, must ensure that the log does not
+ * contain any log items protected by any log incompat bit, and must ensure
+ * that there are no other threads that depend on the state of the log incompat
+ * feature flags in the primary super.
+ *
+ * Returns true if the superblock is dirty.
+ */
+bool
+xfs_clear_incompat_log_features(
+       struct xfs_mount        *mp)
+{
+       bool                    ret = false;
+
+       if (!xfs_sb_version_hascrc(&mp->m_sb) ||
+           !xfs_sb_has_incompat_log_feature(&mp->m_sb,
+                               XFS_SB_FEAT_INCOMPAT_LOG_ALL) ||
+           XFS_FORCED_SHUTDOWN(mp))
+               return false;
+
+       /*
+        * Update the incore superblock.  We synchronize on the primary super
+        * buffer lock to be consistent with the add function, though at least
+        * in theory this shouldn't be necessary.
+        */
+       xfs_buf_lock(mp->m_sb_bp);
+       xfs_buf_hold(mp->m_sb_bp);
+
+       if (xfs_sb_has_incompat_log_feature(&mp->m_sb,
+                               XFS_SB_FEAT_INCOMPAT_LOG_ALL)) {
+               xfs_info(mp, "Clearing log incompat feature flags.");
+               xfs_sb_remove_incompat_log_features(&mp->m_sb);
+               ret = true;
+       }
+
+       xfs_buf_relse(mp->m_sb_bp);
+       return ret;
+}
+
 /*
  * Update the in-core delayed block counter.
  *
index 91a10233d04c3d57fd901b243c137990f4f20c1d..32143102cc916d8912fec4762297ab46a8d7c8cd 100644 (file)
@@ -388,6 +388,8 @@ int xfs_zero_extent(struct xfs_inode *ip, xfs_fsblock_t start_fsb,
 struct xfs_error_cfg * xfs_error_get_cfg(struct xfs_mount *mp,
                int error_class, int error);
 void xfs_force_summary_recalc(struct xfs_mount *mp);
+int xfs_add_incompat_log_feature(struct xfs_mount *mp, uint32_t feature);
+bool xfs_clear_incompat_log_features(struct xfs_mount *mp);
 void xfs_mod_delalloc(struct xfs_mount *mp, int64_t delta);
 
 #endif /* __XFS_MOUNT_H__ */