diff mbox series

[16/17] common/xfs: capture realtime devices during metadump/mdrestore

Message ID 170405030547.1826350.12488301162005381994.stgit@frogsfrogsfrogs (mailing list archive)
State New, 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:56 p.m. UTC
From: Darrick J. Wong <djwong@kernel.org>

If xfs_metadump supports the -r switch to capture the contents of
realtime devices and there is a realtime device, add the option to the
command line to enable preservation.

Similarly, if xfs_mdrestore supports the -r switch and there's an
external scratch rtdev, pass the option so that we can restore rtdev
contents.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
---
 common/fuzzy    |    6 ++++-
 common/populate |   12 ++++++++--
 common/xfs      |   65 +++++++++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 71 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/common/fuzzy b/common/fuzzy
index b72ee3f67f..d504f0854e 100644
--- a/common/fuzzy
+++ b/common/fuzzy
@@ -309,7 +309,11 @@  __scratch_xfs_fuzz_mdrestore()
 	[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
 		logdev=$SCRATCH_LOGDEV
 
-	_xfs_mdrestore "${POPULATE_METADUMP}" "${SCRATCH_DEV}" "${logdev}" || \
+	local rtdev=none
+	[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
+		rtdev=$SCRATCH_RTDEV
+
+	_xfs_mdrestore "${POPULATE_METADUMP}" "${SCRATCH_DEV}" "${logdev}" "${rtdev}" || \
 		_fail "${POPULATE_METADUMP}: Could not find metadump to restore?"
 }
 
diff --git a/common/populate b/common/populate
index 450b024bfc..dc89eee70e 100644
--- a/common/populate
+++ b/common/populate
@@ -1017,7 +1017,11 @@  _scratch_populate_restore_cached() {
 
 	case "${FSTYP}" in
 	"xfs")
-		_xfs_mdrestore "${metadump}" "${SCRATCH_DEV}" "${logdev}"
+		local rtdev=none
+		[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
+			rtdev=$SCRATCH_RTDEV
+
+		_xfs_mdrestore "${metadump}" "${SCRATCH_DEV}" "${logdev}" "${rtdev}"
 		return $?
 		;;
 	"ext2"|"ext3"|"ext4")
@@ -1039,8 +1043,12 @@  _scratch_populate_save_metadump()
 		[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
 			logdev=$SCRATCH_LOGDEV
 
+		local rtdev=none
+		[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
+			rtdev=$SCRATCH_RTDEV
+
 		_xfs_metadump "$metadump_file" "$SCRATCH_DEV" "$logdev" \
-				compress
+				"$rtdev" compress
 		res=$?
 		;;
 	"ext2"|"ext3"|"ext4")
diff --git a/common/xfs b/common/xfs
index 1136d685e7..1ff81f4cc2 100644
--- a/common/xfs
+++ b/common/xfs
@@ -681,11 +681,17 @@  _xfs_metadump() {
 	local metadump="$1"
 	local device="$2"
 	local logdev="$3"
-	local compressopt="$4"
-	shift; shift; shift; shift
+	local rtdev="$4"
+	local compressopt="$5"
+	shift; shift; shift; shift; shift
 	local options="$@"
 	test -z "$options" && options="-a -o"
 	local metadump_has_dash_x
+	local metadump_has_dash_r
+
+	# Does metadump support capturing realtime devices?
+	$XFS_METADUMP_PROG --help 2>&1 | grep -q -- '-r rtdev' && \
+			metadump_has_dash_r=1
 
 	# Use metadump v2 format unless the user gave us a specific version
 	$XFS_METADUMP_PROG --help 2>&1 | grep -q -- '-v version' && \
@@ -699,6 +705,10 @@  _xfs_metadump() {
 		options="$options -l $logdev"
 	fi
 
+	if [ "$rtdev" != "none" ] && [ -n "$metadump_has_dash_r" ]; then
+		options="$options -r $rtdev"
+	fi
+
 	$XFS_METADUMP_PROG $options "$device" "$metadump"
 	res=$?
 	[ "$compressopt" = "compress" ] && [ -n "$DUMP_COMPRESSOR" ] &&
@@ -710,14 +720,19 @@  _xfs_mdrestore() {
 	local metadump="$1"
 	local device="$2"
 	local logdev="$3"
-	shift; shift; shift
+	local rtdev="$4"
+	shift; shift; shift; shift
 	local options="$@"
 	local need_repair
 	local mdrestore_has_dash_l
+	local mdrestore_has_dash_r
 
 	# Does mdrestore support restoring to external devices?
 	$XFS_MDRESTORE_PROG --help 2>&1 | grep -q -- '-l logdev' &&
 			mdrestore_has_dash_l=1
+	# Does mdrestore support restoring to realtime devices?
+	$XFS_MDRESTORE_PROG --help 2>&1 | grep -q -- '-r rtdev' &&
+			mdrestore_has_dash_r=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
@@ -743,6 +758,20 @@  _xfs_mdrestore() {
 		$XFS_IO_PROG -d -c 'pwrite -S 0 -q 0 1m' "$logdev"
 	fi
 
+	if [ "$rtdev" != "none" ]; then
+		# We have a realtime device.  If mdrestore supports restoring
+		# to it, configure ourselves to do that.
+		if [ -n "$mdrestore_has_dash_r" ]; then
+			options="$options -r $rtdev"
+		fi
+
+		# Wipe the realtime device.  If mdrestore doesn't support
+		# restoring to realtime devices or the metadump file doesn't
+		# capture the realtime group headers, 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' "$rtdev"
+	fi
+
 	$XFS_MDRESTORE_PROG $options "${metadump}" "${device}"
 	res=$?
 	test $res -ne 0 && return $res
@@ -757,6 +786,16 @@  _xfs_mdrestore() {
 		fi
 	fi
 
+	# If there's a realtime device, check to see if the restore rewrote the
+	# rt group headers.  If not, we need to run xfs_repair to format new
+	# group headers onto the realtime device.
+	if [ "$rtdev" != "none" ] && [ -z "$need_repair" ]; then
+		magic="$($XFS_IO_PROG -c 'pread -q -v 0 4' "$rtdev")"
+		if [ "$magic" = "00000000:  00 00 00 00  ...." ]; then
+			need_repair=1
+		fi
+	fi
+
 	test -z "$need_repair" && return 0
 
 	echo "repairing fs to fix uncaptured parts of fs." >> $seqres.full
@@ -768,12 +807,16 @@  _scratch_xfs_metadump()
 {
 	local metadump=$1
 	shift
+
 	local logdev=none
-
 	[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
 		logdev=$SCRATCH_LOGDEV
 
-	_xfs_metadump "$metadump" "$SCRATCH_DEV" "$logdev" nocompress "$@"
+	local rtdev=none
+	[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
+		rtdev=$SCRATCH_RTDEV
+
+	_xfs_metadump "$metadump" "$SCRATCH_DEV" "$logdev" "$rtdev" nocompress "$@"
 }
 
 # Restore snapshotted metadata on the scratch device
@@ -786,7 +829,11 @@  _scratch_xfs_mdrestore()
 	[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
 		logdev=$SCRATCH_LOGDEV
 
-	_xfs_mdrestore "$metadump" "$SCRATCH_DEV" "$logdev" "$@"
+	local rtdev=none
+	[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
+		rtdev=$SCRATCH_RTDEV
+
+	_xfs_mdrestore "$metadump" "$SCRATCH_DEV" "$logdev" "$rtdev" "$@"
 }
 
 # Do not use xfs_repair (offline fsck) to rebuild the filesystem
@@ -923,7 +970,7 @@  _check_xfs_filesystem()
 	if [ "$ok" -ne 1 ] && [ "$DUMP_CORRUPT_FS" = "1" ]; then
 		local flatdev="$(basename "$device")"
 		_xfs_metadump "$seqres.$flatdev.check.md" "$device" "$logdev" \
-			compress >> $seqres.full
+			"$rtdev" compress >> $seqres.full
 	fi
 
 	# Optionally test the index rebuilding behavior.
@@ -956,7 +1003,7 @@  _check_xfs_filesystem()
 		if [ "$rebuild_ok" -ne 1 ] && [ "$DUMP_CORRUPT_FS" = "1" ]; then
 			local flatdev="$(basename "$device")"
 			_xfs_metadump "$seqres.$flatdev.rebuild.md" "$device" \
-				"$logdev" compress >> $seqres.full
+				"$logdev" "$rtdev" compress >> $seqres.full
 		fi
 	fi
 
@@ -1040,7 +1087,7 @@  _check_xfs_filesystem()
 		if [ "$orebuild_ok" -ne 1 ] && [ "$DUMP_CORRUPT_FS" = "1" ]; then
 			local flatdev="$(basename "$device")"
 			_xfs_metadump "$seqres.$flatdev.orebuild.md" "$device" \
-				"$logdev" compress >> $seqres.full
+				"$logdev" "$rtdev" compress >> $seqres.full
 		fi
 	fi