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

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