diff mbox series

[05/17] common/xfs: capture external logs during metadump/mdrestore

Message ID 170405030403.1826350.4983042278295283651.stgit@frogsfrogsfrogs (mailing list archive)
State Deferred, archived
Headers show
Series [01/17] common/populate: refactor caching of metadumps to a helper | expand

Commit Message

Darrick J. Wong Dec. 27, 2023, 1:54 p.m. UTC
From: Darrick J. Wong <djwong@kernel.org>

If xfs_mdrestore supports the -l switch and there's an external scratch
log, pass the option so that we can restore log contents.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
---
 common/xfs |   40 +++++++++++++++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/common/xfs b/common/xfs
index 4e8630b3ab..175de69c63 100644
--- a/common/xfs
+++ b/common/xfs
@@ -668,6 +668,7 @@  _xfs_metadump() {
 	shift; shift; shift; shift
 	local options="$@"
 	test -z "$options" && options="-a -o"
+	local metadump_has_dash_x
 
 	# Use metadump v2 format unless the user gave us a specific version
 	$XFS_METADUMP_PROG --help 2>&1 | grep -q -- '-v version' && \
@@ -694,6 +695,12 @@  _xfs_mdrestore() {
 	local logdev="$3"
 	shift; shift; shift
 	local options="$@"
+	local need_repair
+	local mdrestore_has_dash_l
+
+	# Does mdrestore support restoring to external devices?
+	$XFS_MDRESTORE_PROG --help 2>&1 | grep -q -- '-l logdev' &&
+			mdrestore_has_dash_l=1
 
 	# If we're configured for compressed dumps and there isn't already an
 	# uncompressed dump, see if we can use DUMP_COMPRESSOR to decompress
@@ -705,19 +712,38 @@  _xfs_mdrestore() {
 	fi
 	test -r "$metadump" || return 1
 
+	if [ "$logdev" != "none" ]; then
+		# We have an external log device.  If mdrestore supports
+		# restoring to it, configure ourselves to do that.
+		if [ -n "$mdrestore_has_dash_l" ]; then
+			options="$options -l $logdev"
+		fi
+
+		# Wipe the log device.  If mdrestore doesn't support restoring
+		# to external log devices or the metadump file doesn't capture
+		# the log contents, this is our only chance to signal that the
+		# log header needs to be rewritten.
+		$XFS_IO_PROG -d -c 'pwrite -S 0 -q 0 1m' "$logdev"
+	fi
+
 	$XFS_MDRESTORE_PROG $options "${metadump}" "${device}"
 	res=$?
 	test $res -ne 0 && return $res
 
-	# mdrestore does not know how to restore an external log.  If there is
-	# one, we need to erase the log header and run xfs_repair to format a
-	# new log header onto the log device.
+	# If there's an external log, check to see if the restore rewrote the
+	# log header.  If not, we need to run xfs_repair to format a new log
+	# header onto the log device.
 	if [ "$logdev" != "none" ]; then
-		$XFS_IO_PROG -d -c 'pwrite -S 0 -q 0 1m' "$logdev"
-		_scratch_xfs_repair >> $seqres.full 2>&1
-		res=$?
+		magic="$($XFS_IO_PROG -c 'pread -q -v 0 4' "$logdev")"
+		if [ "$magic" = "00000000:  00 00 00 00  ...." ]; then
+			need_repair=1
+		fi
 	fi
-	return $res
+
+	test -z "$need_repair" && return 0
+
+	echo "repairing fs to fix uncaptured parts of fs." >> $seqres.full
+	_scratch_xfs_repair >> $seqres.full 2>&1
 }
 
 # Snapshot the metadata on the scratch device