e59f31
commit cbca895541facb3a1a00fd0fe6614301a64c0e3a
e59f31
Author: Eric Sandeen <sandeen@redhat.com>
e59f31
Date:   Fri Sep 23 09:16:52 2016 +1000
e59f31
e59f31
    xfs_copy: Fix meta UUID handling on multiple copies
e59f31
    
e59f31
    Zorro reported that when making multiple copies of a V5
e59f31
    filesystem with xfs_copy while generating new UUIDs, all
e59f31
    but the first copy were corrupt.
e59f31
    
e59f31
    Upon inspection, the corruption was related to incorrect UUIDs;
e59f31
    the original UUID, as stamped into every metadata structure,
e59f31
    was not preserved in the sb_meta_uuid field of the superblock
e59f31
    on any but the first copy.
e59f31
    
e59f31
    This happened because sb_update_uuid was using the UUID present in
e59f31
    the ag_hdr structure as the unchanging meta-uuid which is to match
e59f31
    existing structures, but it also /updates/ that UUID with the
e59f31
    new identifying UUID present in tcarg.  So the newly-generated
e59f31
    UUIDs moved transitively from tcarg->uuid to ag_hdr->xfs_sb->sb_uuid
e59f31
    to ag_hdr->xfs_sb->sb_meta_uuid each time the function got called.
e59f31
    
e59f31
    Fix this by looking instead to the unchanging, original UUID
e59f31
    present in the xfs_sb_t we are given, which reflects the original
e59f31
    filesystem's metadata UUID, and copy /that/ UUID into each target
e59f31
    filesystem's meta_uuid field.
e59f31
    
e59f31
    Most of this patch is changing comments and re-ordering tests
e59f31
    to match; the functional change is to simply use the *sb rather
e59f31
    than the *ag_hdr to identify the proper metadata UUID.
e59f31
    
e59f31
    Reported-and-tested-by: Zorro Lang <zlang@redhat.com>
e59f31
    Signed-off-by: Eric Sandeen <sandeen@redhat.com>
e59f31
    Reviewed-by: Dave Chinner <dchinner@redhat.com>
e59f31
    Signed-off-by: Dave Chinner <david@fromorbit.com>
e59f31
e59f31
diff --git a/copy/xfs_copy.c b/copy/xfs_copy.c
e59f31
index 3c8998c..22ded6b 100644
e59f31
--- a/copy/xfs_copy.c
e59f31
+++ b/copy/xfs_copy.c
e59f31
@@ -494,27 +494,29 @@ write_wbuf(void)
e59f31
 
e59f31
 void
e59f31
 sb_update_uuid(
e59f31
-	xfs_sb_t	*sb,
e59f31
-	ag_header_t	*ag_hdr,
e59f31
-	thread_args	*tcarg)
e59f31
+	xfs_sb_t	*sb,		/* Original fs superblock */
e59f31
+	ag_header_t	*ag_hdr,	/* AG hdr to update for this copy */
e59f31
+	thread_args	*tcarg)		/* Args for this thread, with UUID */
e59f31
 {
e59f31
 	/*
e59f31
 	 * If this filesystem has CRCs, the original UUID is stamped into
e59f31
-	 * all metadata.  If we are changing the UUID in the copy, we need
e59f31
-	 * to copy the original UUID into the meta_uuid slot and set the
e59f31
-	 * set the incompat flag if that hasn't already been done.
e59f31
+	 * all metadata.  If we don't have an existing meta_uuid field in the
e59f31
+	 * the original filesystem and we are changing the UUID in this copy,
e59f31
+	 * we must copy the original sb_uuid to the sb_meta_uuid slot and set
e59f31
+	 * the incompat flag for the feature on this copy.
e59f31
 	 */
e59f31
-	if (!uuid_equal(&tcarg->uuid, &ag_hdr->xfs_sb->sb_uuid) &&
e59f31
-	    xfs_sb_version_hascrc(sb) && !xfs_sb_version_hasmetauuid(sb)) {
e59f31
+	if (xfs_sb_version_hascrc(sb) && !xfs_sb_version_hasmetauuid(sb) &&
e59f31
+	    !uuid_equal(&tcarg->uuid, &sb->sb_uuid)) {
e59f31
 		__be32 feat;
e59f31
 
e59f31
 		feat = be32_to_cpu(ag_hdr->xfs_sb->sb_features_incompat);
e59f31
 		feat |= XFS_SB_FEAT_INCOMPAT_META_UUID;
e59f31
 		ag_hdr->xfs_sb->sb_features_incompat = cpu_to_be32(feat);
e59f31
 		platform_uuid_copy(&ag_hdr->xfs_sb->sb_meta_uuid,
e59f31
-				   &ag_hdr->xfs_sb->sb_uuid);
e59f31
+				   &sb->sb_uuid);
e59f31
 	}
e59f31
 
e59f31
+	/* Copy the (possibly new) fs-identifier UUID into sb_uuid */
e59f31
 	platform_uuid_copy(&ag_hdr->xfs_sb->sb_uuid, &tcarg->uuid);
e59f31
 
e59f31
 	/* We may have changed the UUID, so update the superblock CRC */