All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: darrick.wong@oracle.com
Cc: linux-xfs@vger.kernel.org
Subject: [PATCH 34/47] xfs: implement the metadata repair ioctl flag
Date: Fri, 06 Jan 2017 16:39:27 -0800	[thread overview]
Message-ID: <148374956695.30431.16783286376331915506.stgit@birch.djwong.org> (raw)
In-Reply-To: <148374934333.30431.11042523766304087227.stgit@birch.djwong.org>

Plumb in the pieces necessary to make the "repair" subfunction of
the scrub ioctl actually work.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 fs/xfs/repair/common.c |  100 ++++++++++++++++++++++++++++++++++++++++++++++--
 fs/xfs/xfs_error.h     |    4 +-
 2 files changed, 98 insertions(+), 6 deletions(-)


diff --git a/fs/xfs/repair/common.c b/fs/xfs/repair/common.c
index 458057a..bbcee6b 100644
--- a/fs/xfs/repair/common.c
+++ b/fs/xfs/repair/common.c
@@ -41,6 +41,7 @@
 #include "xfs_refcount_btree.h"
 #include "xfs_rmap.h"
 #include "xfs_rmap_btree.h"
+#include "xfs_error.h"
 #include "repair/xfs_scrub.h"
 #include "repair/common.h"
 #include "repair/btree.h"
@@ -108,8 +109,43 @@
  * the metadata is correct but otherwise suboptimal, there's a "preen"
  * flag to signal that.  Finally, if we were unable to access a data
  * structure to perform cross-referencing, we can signal that as well.
+ *
+ * If a piece of metadata proves corrupt or suboptimal, the userspace
+ * program can ask the kernel to apply some tender loving care (TLC) to
+ * the metadata object.  "Corruption" is defined by metadata violating
+ * the on-disk specification; operations cannot continue if the
+ * violation is left untreated.  It is possible for XFS to continue if
+ * an object is "suboptimal", however performance may be degraded.
+ * Repairs are usually performed by rebuilding the metadata entirely out
+ * of redundant metadata.  Optimizing, on the other hand, can sometimes
+ * be done without rebuilding entire structures.
+ *
+ * Generally speaking, the repair code has the following code structure:
+ * Lock -> scrub -> repair -> commit -> re-lock -> re-scrub -> unlock.
+ * The first check helps us figure out if we need to rebuild or simply
+ * optimize the structure so that the rebuild knows what to do.  The
+ * second check evaluates the completeness of the repair; that is what
+ * is reported to userspace.
  */
 
+/* Fix something if errors were detected and the user asked for repair. */
+static inline bool
+xfs_scrub_should_fix(
+	struct xfs_scrub_metadata	*sm)
+{
+	return (sm->sm_flags & XFS_SCRUB_FLAG_REPAIR) &&
+	       (sm->sm_flags & (XFS_SCRUB_FLAG_CORRUPT | XFS_SCRUB_FLAG_PREEN));
+}
+
+/* Clear the corruption status flags. */
+static inline bool
+xfs_scrub_reset_corruption_flags(
+	struct xfs_scrub_metadata	*sm)
+{
+	return sm->sm_flags &= ~(XFS_SCRUB_FLAG_CORRUPT | XFS_SCRUB_FLAG_PREEN |
+			      XFS_SCRUB_FLAG_XREF_FAIL);
+}
+
 /* Check for operational errors. */
 bool
 xfs_scrub_op_ok(
@@ -602,7 +638,10 @@ xfs_scrub_teardown(
 	if (sc->ag_lock.agmask != sc->ag_lock.__agmask)
 		kmem_free(sc->ag_lock.agmask);
 	sc->ag_lock.agmask = NULL;
-	xfs_trans_cancel(sc->tp);
+	if (error == 0 && (sc->sm->sm_flags & XFS_SCRUB_FLAG_REPAIR))
+		error = xfs_trans_commit(sc->tp);
+	else
+		xfs_trans_cancel(sc->tp);
 	sc->tp = NULL;
 	if (sc->ip != NULL) {
 		xfs_iunlock(sc->ip, XFS_ILOCK_EXCL);
@@ -745,6 +784,8 @@ xfs_scrub_metadata(
 	struct xfs_mount		*mp = ip->i_mount;
 	const struct xfs_scrub_meta_fns	*fns;
 	bool				deadlocked = false;
+	bool				already_fixed = false;
+	bool				was_corrupt = false;
 	int				error = 0;
 
 	trace_xfs_scrub(ip, sm->sm_type, sm->sm_agno, sm->sm_ino, sm->sm_gen,
@@ -758,8 +799,6 @@ xfs_scrub_metadata(
 	sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
 	if (sm->sm_flags & ~XFS_SCRUB_FLAGS_IN)
 		goto out;
-	if (sm->sm_flags & XFS_SCRUB_FLAG_REPAIR)
-		goto out;
 	error = -ENOENT;
 	if (sm->sm_type > XFS_SCRUB_TYPE_MAX)
 		goto out;
@@ -767,6 +806,14 @@ xfs_scrub_metadata(
 	if (fns->scrub == NULL)
 		goto out;
 	error = -EOPNOTSUPP;
+	if ((sm->sm_flags & XFS_SCRUB_FLAG_REPAIR) &&
+	    (fns->repair == NULL || !xfs_sb_version_hascrc(&mp->m_sb)))
+		goto out;
+
+	error = -EROFS;
+	if ((sm->sm_flags & XFS_SCRUB_FLAG_REPAIR) &&
+	    (mp->m_flags & XFS_MOUNT_RDONLY))
+		goto out;
 
 	/* Do we even have this type of metadata? */
 	error = -ENOENT;
@@ -800,8 +847,51 @@ xfs_scrub_metadata(
 	} else if (error)
 		goto out_teardown;
 
-	if (sm->sm_flags & XFS_SCRUB_FLAG_CORRUPT)
-		xfs_alert_ratelimited(mp, "Corruption detected during scrub.");
+	/* Let debug users force us into the repair routines. */
+	if ((sm->sm_flags & XFS_SCRUB_FLAG_REPAIR) && !already_fixed &&
+	    XFS_TEST_ERROR(false, mp,
+			XFS_ERRTAG_FORCE_SCRUB_REPAIR,
+			XFS_RANDOM_FORCE_SCRUB_REPAIR)) {
+		sm->sm_flags |= XFS_SCRUB_FLAG_CORRUPT;
+	}
+	if (!already_fixed)
+		was_corrupt = (sm->sm_flags & XFS_SCRUB_FLAG_CORRUPT);
+
+	if (!already_fixed && xfs_scrub_should_fix(sm)) {
+		xfs_scrub_ag_btcur_free(&sc.sa);
+
+		/* Ok, something's wrong.  Repair it. */
+		trace_xfs_repair_attempt(ip, sm->sm_type, sm->sm_agno,
+			sm->sm_ino, sm->sm_gen, sm->sm_flags, error);
+		error = fns->repair(&sc);
+		trace_xfs_repair_done(ip, sm->sm_type, sm->sm_agno,
+			sm->sm_ino, sm->sm_gen, sm->sm_flags, error);
+		if (error)
+			goto out_teardown;
+
+		/*
+		 * Commit the fixes and perform a second dry-run scrub
+		 * so that we can tell userspace if we fixed the problem.
+		 */
+		error = xfs_scrub_teardown(&sc, ip, error);
+		if (error)
+			goto out;
+		xfs_scrub_reset_corruption_flags(sm);
+		already_fixed = true;
+		goto retry_op;
+	}
+
+	if (sm->sm_flags & XFS_SCRUB_FLAG_CORRUPT) {
+		char	*errstr;
+
+		if (sm->sm_flags & XFS_SCRUB_FLAG_REPAIR)
+			errstr = "Corruption not fixed during online repair.  "
+				 "Unmount and run xfs_repair.";
+		else
+			errstr = "Corruption detected during scrub.";
+		xfs_alert_ratelimited(mp, errstr);
+	} else if (already_fixed && was_corrupt)
+		xfs_alert_ratelimited(mp, "Corruption repaired during scrub.");
 
 out_teardown:
 	error = xfs_scrub_teardown(&sc, ip, error);
diff --git a/fs/xfs/xfs_error.h b/fs/xfs/xfs_error.h
index 05f8666..4c22d9a 100644
--- a/fs/xfs/xfs_error.h
+++ b/fs/xfs/xfs_error.h
@@ -96,7 +96,8 @@ extern void xfs_verifier_error(struct xfs_buf *bp);
 #define XFS_ERRTAG_REFCOUNT_FINISH_ONE			25
 #define XFS_ERRTAG_BMAP_FINISH_ONE			26
 #define XFS_ERRTAG_AG_RESV_CRITICAL			27
-#define XFS_ERRTAG_MAX					28
+#define XFS_ERRTAG_FORCE_SCRUB_REPAIR			28
+#define XFS_ERRTAG_MAX					29
 
 /*
  * Random factors for above tags, 1 means always, 2 means 1/2 time, etc.
@@ -129,6 +130,7 @@ extern void xfs_verifier_error(struct xfs_buf *bp);
 #define XFS_RANDOM_REFCOUNT_FINISH_ONE			1
 #define XFS_RANDOM_BMAP_FINISH_ONE			1
 #define XFS_RANDOM_AG_RESV_CRITICAL			4
+#define XFS_RANDOM_FORCE_SCRUB_REPAIR			1
 
 #ifdef DEBUG
 extern int xfs_error_test_active;


  parent reply	other threads:[~2017-01-07  0:39 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-07  0:35 [PATCH v4 00/47] xfs: online scrub/repair support Darrick J. Wong
2017-01-07  0:35 ` [PATCH 01/47] xfs: plumb in needed functions for range querying of the freespace btrees Darrick J. Wong
2017-01-07  0:35 ` [PATCH 02/47] xfs: provide a query_range function for " Darrick J. Wong
2017-01-07  0:36 ` [PATCH 03/47] xfs: create a function to query all records in a btree Darrick J. Wong
2017-01-07  0:36 ` [PATCH 04/47] xfs: introduce the XFS_IOC_GETFSMAP ioctl Darrick J. Wong
2017-01-07  0:36 ` [PATCH 05/47] xfs: report shared extents in getfsmapx Darrick J. Wong
2017-01-07  0:36 ` [PATCH 06/47] xfs: have getfsmap fall back to the freesp btrees when rmap is not present Darrick J. Wong
2017-01-07  0:36 ` [PATCH 07/47] xfs: getfsmap should fall back to rtbitmap when rtrmapbt " Darrick J. Wong
2017-01-07  0:36 ` [PATCH 08/47] xfs: add scrub tracepoints Darrick J. Wong
2017-01-07  0:36 ` [PATCH 09/47] xfs: create an ioctl to scrub AG metadata Darrick J. Wong
2017-01-07  0:36 ` [PATCH 10/47] xfs: generic functions to scrub metadata and btrees Darrick J. Wong
2017-01-07  0:36 ` [PATCH 11/47] xfs: scrub the backup superblocks Darrick J. Wong
2017-01-07  0:37 ` [PATCH 12/47] xfs: scrub AGF and AGFL Darrick J. Wong
2017-01-07  0:37 ` [PATCH 13/47] xfs: scrub the AGI Darrick J. Wong
2017-01-07  0:37 ` [PATCH 14/47] xfs: support scrubbing free space btrees Darrick J. Wong
2017-01-07  0:37 ` [PATCH 15/47] xfs: support scrubbing inode btrees Darrick J. Wong
2017-01-07  0:37 ` [PATCH 16/47] xfs: support scrubbing rmap btree Darrick J. Wong
2017-01-07  0:37 ` [PATCH 17/47] xfs: support scrubbing refcount btree Darrick J. Wong
2017-01-07  0:37 ` [PATCH 18/47] xfs: scrub inodes Darrick J. Wong
2017-01-07  0:37 ` [PATCH 19/47] xfs: scrub inode block mappings Darrick J. Wong
2017-01-07  0:37 ` [PATCH 20/47] xfs: scrub directory/attribute btrees Darrick J. Wong
2017-01-07  0:38 ` [PATCH 21/47] xfs: scrub directory metadata Darrick J. Wong
2017-01-07  0:38 ` [PATCH 22/47] xfs: scrub extended attributes Darrick J. Wong
2017-01-07  0:38 ` [PATCH 23/47] xfs: scrub symbolic links Darrick J. Wong
2017-01-07  0:38 ` [PATCH 24/47] xfs: scrub realtime bitmap/summary Darrick J. Wong
2017-01-07  0:38 ` [PATCH 25/47] xfs: scrub should cross-reference with the bnobt Darrick J. Wong
2017-01-07  0:38 ` [PATCH 26/47] xfs: cross-reference bnobt records with cntbt Darrick J. Wong
2017-01-07  0:38 ` [PATCH 27/47] xfs: cross-reference extents with AG header Darrick J. Wong
2017-01-07  0:38 ` [PATCH 28/47] xfs: cross-reference inode btrees during scrub Darrick J. Wong
2017-01-07  0:38 ` [PATCH 29/47] xfs: cross-reference reverse-mapping btree Darrick J. Wong
2017-01-07  0:39 ` [PATCH 30/47] xfs: cross-reference refcount btree during scrub Darrick J. Wong
2017-01-07  0:39 ` [PATCH 31/47] xfs: scrub should cross-reference the realtime bitmap Darrick J. Wong
2017-01-07  0:39 ` [PATCH 32/47] xfs: cross-reference the block mappings when possible Darrick J. Wong
2017-01-07  0:39 ` [PATCH 33/47] xfs: create tracepoints for online repair Darrick J. Wong
2017-01-07  0:39 ` Darrick J. Wong [this message]
2017-01-07  0:39 ` [PATCH 35/47] xfs: add helper routines for the repair code Darrick J. Wong
2017-01-07  0:39 ` [PATCH 36/47] xfs: repair superblocks Darrick J. Wong
2017-01-07  0:39 ` [PATCH 37/47] xfs: repair the AGF and AGFL Darrick J. Wong
2017-01-07  0:39 ` [PATCH 38/47] xfs: rebuild the AGI Darrick J. Wong
2017-01-07  0:39 ` [PATCH 39/47] xfs: repair free space btrees Darrick J. Wong
2017-01-07  0:40 ` [PATCH 40/47] xfs: repair inode btrees Darrick J. Wong
2017-01-07  0:40 ` [PATCH 41/47] xfs: rebuild the rmapbt Darrick J. Wong
2017-01-07  0:40 ` [PATCH 42/47] xfs: repair refcount btrees Darrick J. Wong
2017-01-07  0:40 ` [PATCH 43/47] xfs: online repair of inodes Darrick J. Wong
2017-01-07  0:40 ` [PATCH 44/47] xfs: repair inode block maps Darrick J. Wong
2017-01-07  0:40 ` [PATCH 45/47] xfs: repair damaged symlinks Darrick J. Wong
2017-01-07  0:40 ` [PATCH 46/47] xfs: query the per-AG reservation counters Darrick J. Wong
2017-01-07  0:40 ` [PATCH 47/47] xfs: avoid mount-time deadlock in CoW extent recovery Darrick J. Wong
2017-01-09 12:40 ` [PATCH v4 00/47] xfs: online scrub/repair support Amir Goldstein
2017-01-09 21:15   ` Darrick J. Wong
2017-01-10  7:54     ` Eryu Guan
2017-01-10  8:13       ` Amir Goldstein
2017-01-10  8:44         ` Eryu Guan
     [not found]         ` <CAOQ4uxiFg18fVh3RFr-Y1-XRmV82dTxc5r05QH8OFYpv2=juvg@mail.gmail.com>
     [not found]           ` <CAOQ4uxhTPt7t4-4MmQwogy+d4mgyG+=MX=12NX8R4V-hGR1q0w@mail.gmail.com>
2017-01-12 20:10             ` Darrick J. Wong
2017-01-10 18:20       ` Darrick J. Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=148374956695.30431.16783286376331915506.stgit@birch.djwong.org \
    --to=darrick.wong@oracle.com \
    --cc=linux-xfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.