Blame SOURCES/0018-thin_repair-thin_dump-Check-consistency-of-thin_ids-.patch

9a0ec1
From 992bb5feeb9e1994d4c31b4b13dbae594dd3c87a Mon Sep 17 00:00:00 2001
9a0ec1
From: Ming-Hung Tsai <mtsai@redhat.com>
9a0ec1
Date: Tue, 24 Aug 2021 16:20:50 +0800
9a0ec1
Subject: [PATCH 3/3] [thin_repair/thin_dump] Check consistency of thin_ids
9a0ec1
 before running a regular dump
9a0ec1
9a0ec1
(cherry picked from commit 73dda15b5977dfa45cbfa36edddf4c19d279767b)
9a0ec1
---
9a0ec1
 functional-tests/thin-functional-tests.scm | 56 ++++++++++++++++++++++++++++++
9a0ec1
 thin-provisioning/metadata_dumper.cc       |  8 +++--
9a0ec1
 2 files changed, 62 insertions(+), 2 deletions(-)
9a0ec1
9a0ec1
diff --git a/functional-tests/thin-functional-tests.scm b/functional-tests/thin-functional-tests.scm
9a0ec1
index d06a5a3..37d3df9 100644
9a0ec1
--- a/functional-tests/thin-functional-tests.scm
9a0ec1
+++ b/functional-tests/thin-functional-tests.scm
9a0ec1
@@ -81,6 +81,15 @@
9a0ec1
           (let ((csum (checksum-block b (ftype-sizeof unsigned-32) superblock-salt)))
9a0ec1
             (ftype-set! ThinSuperblock (csum) sb csum))))))
9a0ec1
 
9a0ec1
+  (define (tamper-mapping-root md mapping-root)
9a0ec1
+    (with-bcache (cache md 1)
9a0ec1
+      (with-block (b cache 0 (get-flags dirty))
9a0ec1
+        (let ((sb (block->superblock b)))
9a0ec1
+          (ftype-set! ThinSuperblock (data-mapping-root) sb mapping-root)
9a0ec1
+          ;;;;;; Update the csum manually since the block validator for ft-lib is not ready
9a0ec1
+          (let ((csum (checksum-block b (ftype-sizeof unsigned-32) superblock-salt)))
9a0ec1
+            (ftype-set! ThinSuperblock (csum) sb csum))))))
9a0ec1
+
9a0ec1
   (define (get-superblock-flags md)
9a0ec1
     (with-bcache (cache md 1)
9a0ec1
       (with-block (b cache 0 (get-flags))
9a0ec1
@@ -97,6 +106,26 @@
9a0ec1
   ;; to run.
9a0ec1
   (define (register-thin-tests) #t)
9a0ec1
 
9a0ec1
+  ;; An deterministic simple XML for testing
9a0ec1
+  (define simple-thin-xml
9a0ec1
+    (fmt #f
9a0ec1
+      (tag 'superblock `((uuid . "")
9a0ec1
+                         (time . 0)
9a0ec1
+                         (transaction . 1)
9a0ec1
+                         (flags . 0)
9a0ec1
+                         (version . 2)
9a0ec1
+                         (data-block-size . 128)
9a0ec1
+                         (nr-data-blocks . 1024))
9a0ec1
+        (tag 'device `((dev-id . 1)
9a0ec1
+                       (mapped-blocks . 16)
9a0ec1
+                       (transaction . 0)
9a0ec1
+                       (creation-time . 0)
9a0ec1
+                       (snap-time . 0))
9a0ec1
+          (tag 'range-mapping  `((origin-begin . 0)
9a0ec1
+                                 (data-begin . 0)
9a0ec1
+                                 (length . 16)
9a0ec1
+                                 (time . 0)))))))
9a0ec1
+
9a0ec1
   ;; XML of metadata with empty thins
9a0ec1
   (define xml-with-empty-thins
9a0ec1
     (fmt #f
9a0ec1
@@ -414,6 +443,18 @@
9a0ec1
           (assert-eof stderr)
9a0ec1
           (assert-equal expected-xml repaired-xml)))))
9a0ec1
 
9a0ec1
+  (define-scenario (thin-dump repair-superblock inconsistent-device-ids)
9a0ec1
+    "metadata with inconsistent device ids should be repaired"
9a0ec1
+    (with-temp-file-sized ((md "thin.bin" (meg 4)))
9a0ec1
+      (with-temp-file-containing ((xml "thin.xml" simple-thin-xml))
9a0ec1
+        (run-ok (thin-restore "-i" xml "-o" md)))
9a0ec1
+      (run-ok-rcv (expected-xml _) (thin-dump md)
9a0ec1
+        ;;;;;; simulate multiple activation by replacing the mapping root with a bottom-level leaf
9a0ec1
+        (tamper-mapping-root md 10)
9a0ec1
+        (run-ok-rcv (repaired-xml stderr) (thin-dump "--repair" md)
9a0ec1
+          (assert-eof stderr)
9a0ec1
+          (assert-equal expected-xml repaired-xml)))))
9a0ec1
+
9a0ec1
   ;;;-----------------------------------------------------------
9a0ec1
   ;;; thin_rmap scenarios
9a0ec1
   ;;;-----------------------------------------------------------
9a0ec1
@@ -624,6 +665,21 @@
9a0ec1
             (assert-eof stderr)
9a0ec1
             (assert-equal expected-xml repaired-xml))))))
9a0ec1
 
9a0ec1
+  (define-scenario (thin-repair superblock inconsistent-device-ids)
9a0ec1
+    "metadata with inconsistent device ids should be repaired"
9a0ec1
+    (with-temp-file-sized ((md1 "thin.bin" (meg 4)))
9a0ec1
+      (with-temp-file-containing ((xml "thin.xml" simple-thin-xml))
9a0ec1
+        (run-ok (thin-restore "-i" xml "-o" md1)))
9a0ec1
+      (run-ok-rcv (expected-xml _) (thin-dump md1)
9a0ec1
+        ;;;;;; simulate multiple activation by replacing the mapping root with a bottom-level leaf
9a0ec1
+        (tamper-mapping-root md1 10)
9a0ec1
+        (with-empty-metadata (md2)
9a0ec1
+          (run-ok-rcv (_ stderr) (thin-repair "-i" md1 "-o" md2)
9a0ec1
+            (assert-eof stderr))
9a0ec1
+          (run-ok-rcv (repaired-xml stderr) (thin-dump md2)
9a0ec1
+            (assert-eof stderr)
9a0ec1
+            (assert-equal expected-xml repaired-xml))))))
9a0ec1
+
9a0ec1
   ;;;-----------------------------------------------------------
9a0ec1
   ;;; thin_metadata_pack scenarios
9a0ec1
   ;;;-----------------------------------------------------------
9a0ec1
diff --git a/thin-provisioning/metadata_dumper.cc b/thin-provisioning/metadata_dumper.cc
9a0ec1
index d169c27..0ca4afe 100644
9a0ec1
--- a/thin-provisioning/metadata_dumper.cc
9a0ec1
+++ b/thin-provisioning/metadata_dumper.cc
9a0ec1
@@ -412,6 +412,9 @@ namespace {
9a0ec1
 				if (rhs == ms.end())
9a0ec1
 					continue;
9a0ec1
 
9a0ec1
+				if (lhs->second != rhs->second)
9a0ec1
+					continue;
9a0ec1
+
9a0ec1
 				filtered.push_back(make_pair(p.first.b, p.second.b));
9a0ec1
 			}
9a0ec1
 
9a0ec1
@@ -886,8 +889,9 @@ namespace {
9a0ec1
 
9a0ec1
 		auto tm = open_tm(bm, superblock_detail::SUPERBLOCK_LOCATION);
9a0ec1
 
9a0ec1
-		if (!get_dev_ids(*tm, msb->device_details_root_) ||
9a0ec1
-	            !get_map_ids(*tm, msb->data_mapping_root_))
9a0ec1
+		auto maybe_dev_ids = get_dev_ids(*tm, msb->device_details_root_);
9a0ec1
+		auto maybe_map_ids = get_map_ids(*tm, msb->data_mapping_root_);
9a0ec1
+		if (!maybe_dev_ids || !maybe_map_ids || (*maybe_dev_ids) != (*maybe_map_ids))
9a0ec1
 			find_better_roots_(bm, *msb);
9a0ec1
 
9a0ec1
 		emit_trees_(bm, *msb, e, opts);
9a0ec1
-- 
9a0ec1
1.8.3.1
9a0ec1