[05/11] btrfs-progs: handle split-brain scenario for scanned changing device with INCOMPAT_METADATA_UUID
diff mbox series

Message ID 20191212110204.11128-6-Damenly_Su@gmx.com
State New
Headers show
Series
  • btrfs-progs: metadata_uuid feature fixes and portation
Related show

Commit Message

Damenly Su Dec. 12, 2019, 11:01 a.m. UTC
From: Su Yue <Damenly_Su@gmx.com>

For a scanned changing device with INCOMPAT_METADATA_UUID, which means
its fsid and metadata_uuid differs, then situation can be

a) The final changed devices are still own the INCOMPAT_METADATA_UUID
feature. So their metadata_uuid is same as the device's, but fsids must
differ.

b) The new fsid to be changing is same as the scanned device's
metadata_uuid. So the successful synced devices are like some never
changed. fs_devices's fsid is same with their metadata_uuid in memory.
In disk, those synced device's fsid differ from their metadata_uuid,
because the later member is full zero bytes.

c) The device is the newest device, there are old devices with
INCOMPAT_METADATA_UUID or scanned changing devices.

Signed-off-by: Su Yue <Damenly_Su@gmx.com>
---
 volumes.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

Patch
diff mbox series

diff --git a/volumes.c b/volumes.c
index bc1726975ce4..0fd41186c54a 100644
--- a/volumes.c
+++ b/volumes.c
@@ -227,6 +227,54 @@  static struct btrfs_fs_devices *find_fsid_inprogress(
 	return find_fsid(disk_super->fsid, NULL);
 }
 
+static struct btrfs_fs_devices *find_fsid_changed(
+					struct btrfs_super_block *disk_super)
+{
+	struct btrfs_fs_devices *fs_devices;
+
+	/*
+	 * Handles the case where scanned device is part of an fs that had
+	 * multiple successful changes of FSID but currently device didn't
+	 * observe it.
+	 *
+	 * Case 1: the devices already changed still owns the feature, their
+	 * fsid must differ from the disk_super->fsid.
+	 */
+	list_for_each_entry(fs_devices, &fs_uuids, list) {
+		if (fs_devices->fsid_change)
+			continue;
+		if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
+			   BTRFS_FSID_SIZE) != 0 &&
+		    memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
+			   BTRFS_FSID_SIZE) == 0 &&
+		    memcmp(fs_devices->fsid, disk_super->fsid,
+			   BTRFS_FSID_SIZE) != 0) {
+			return fs_devices;
+		}
+	}
+
+	/*
+	 * Case 2: the synced devices doesn't have the metadata_uuid feature.
+	 * NOTE: the fs_devices has same metadata_uuid and fsid in memory, but
+	 * they differs in disk, because fs_id is copied to
+	 * fs_devices->metadata_id while alloc_fs_devices if no metadata
+	 * feature.
+	 */
+	list_for_each_entry(fs_devices, &fs_uuids, list) {
+		if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
+			   BTRFS_FSID_SIZE) == 0 &&
+		    memcmp(fs_devices->fsid, disk_super->metadata_uuid,
+			   BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change)
+			return fs_devices;
+	}
+
+	/*
+	 * Okay, can't found any fs_devices already synced, back to
+	 * search devices unchanged or changing like the device.
+	 */
+	return find_fsid(disk_super->fsid, disk_super->metadata_uuid);
+}
+
 static int device_list_add(const char *path,
 			   struct btrfs_super_block *disk_super,
 			   u64 devid, struct btrfs_fs_devices **fs_devices_ret)
@@ -242,6 +290,8 @@  static int device_list_add(const char *path,
 	if (fsid_change_in_progress) {
 		if (!metadata_uuid)
 			fs_devices = find_fsid_inprogress(disk_super);
+		else
+			fs_devices = find_fsid_changed(disk_super);
 	}
 
 	if (metadata_uuid && !fs_devices)