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

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