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

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