Blame SOURCES/0019-thin_check-Allow-using-clear-needs-check-and-skip-ma.patch

0f0231
From 6ce838b83c28d9b64f34cf0c68cfebaf9affac11 Mon Sep 17 00:00:00 2001
0f0231
From: Ming-Hung Tsai <mtsai@redhat.com>
0f0231
Date: Fri, 21 Aug 2020 18:26:48 +0800
0f0231
Subject: [PATCH] [thin_check] Allow using --clear-needs-check and
0f0231
 --skip-mappings together
0f0231
0f0231
Although it is not recommended to clear the flag without a full
0f0231
examination, however, the usage has been documented as an approach
0f0231
to reduce lvchange run time [1]. For the purpose of backward
0f0231
compatibility and avoiding boot failure after upgrading thin_check [2],
0f0231
the limitation is now removed.
0f0231
0f0231
[1] https://wiki.archlinux.org/index.php/LVM#Thinly-provisioned_root_volume_device_times_out
0f0231
[2] Community feedback on previous commit:
0f0231
    https://github.com/jthornber/thin-provisioning-tools/commit/b278f4f
0f0231
0f0231
(cherry picked from commit f4675e3f32aad3d1518869e4f3824e05230c6a5d)
0f0231
---
0f0231
 functional-tests/thin-functional-tests.scm | 65 +++++++++++++---
0f0231
 thin-provisioning/metadata_checker.cc      | 88 +++++++++++-----------
0f0231
 thin-provisioning/metadata_checker.h       |  5 +-
0f0231
 thin-provisioning/thin_check.cc            |  2 +-
0f0231
 4 files changed, 106 insertions(+), 54 deletions(-)
0f0231
0f0231
diff --git a/functional-tests/thin-functional-tests.scm b/functional-tests/thin-functional-tests.scm
0f0231
index 37d3df9..5b1423c 100644
0f0231
--- a/functional-tests/thin-functional-tests.scm
0f0231
+++ b/functional-tests/thin-functional-tests.scm
0f0231
@@ -189,15 +189,6 @@
0f0231
       (run-fail (thin-check "--auto-repair" "--skip-mappings" md))
0f0231
       (run-fail (thin-check "--auto-repair" "--ignore-non-fatal-errors" md))))
0f0231
 
0f0231
-  (define-scenario (thin-check incompatible-options clear-needs-check-flag)
0f0231
-    "Incompatible options should cause failure"
0f0231
-    (with-valid-metadata (md)
0f0231
-      (run-fail (thin-check "--clear-needs-check-flag" "-m" md))
0f0231
-      (run-fail (thin-check "--clear-needs-check-flag" "--override-mapping-root 123" md))
0f0231
-      (run-fail (thin-check "--clear-needs-check-flag" "--super-block-only" md))
0f0231
-      (run-fail (thin-check "--clear-needs-check-flag" "--skip-mappings" md))
0f0231
-      (run-fail (thin-check "--clear-needs-check-flag" "--ignore-non-fatal-errors" md))))
0f0231
-
0f0231
   (define-scenario (thin-check superblock-only-valid)
0f0231
     "--super-block-only check passes on valid metadata"
0f0231
     (with-valid-metadata (md)
0f0231
@@ -230,6 +221,62 @@
0f0231
     (with-valid-metadata (md)
0f0231
       (run-ok (thin-check "--clear-needs-check-flag" md))))
0f0231
 
0f0231
+  (define-scenario (thin-check mixing-clear-needs-check-flag super-block-only-should-pass)
0f0231
+    "Accepts --clear-needs-check and --super-block-only"
0f0231
+    (with-valid-metadata (md)
0f0231
+      (run-ok (thin-check "--clear-needs-check-flag" "--super-block-only" md))))
0f0231
+
0f0231
+  (define-scenario (thin-check mixing-clear-needs-check-flag skip-mappings-should-pass)
0f0231
+    "Accepts --clear-needs-check and --skip-mappings"
0f0231
+    (with-valid-metadata (md)
0f0231
+      (run-ok (thin-check "--clear-needs-check-flag" "--skip-mappings" md))))
0f0231
+
0f0231
+  (define-scenario (thin-check mixing-clear-needs-check-flag ignore-non-fatal-errors-should-pass)
0f0231
+    "Accepts --clear-needs-check and --ignore-non-fatal-errors"
0f0231
+    (with-valid-metadata (md)
0f0231
+      (run-ok (thin-check "--clear-needs-check-flag" "--ignore-non-fatal-errors" md))))
0f0231
+
0f0231
+  (define-scenario (thin-check try-clear-needs-check-flag super-block-only-should-clear)
0f0231
+    "--clear-needs-check is a noop while using --super-block-only"
0f0231
+    (with-valid-metadata (md)
0f0231
+      (set-needs-check-flag md)
0f0231
+      (assert-metadata-needs-check md)
0f0231
+      (run-ok (thin-check "--clear-needs-check-flag" "--super-block-only" md))
0f0231
+      (assert-metadata-clean md)))
0f0231
+
0f0231
+  (define-scenario (thin-check try-clear-needs-check-flag skip-mappings-should-clear)
0f0231
+    "--clear-needs-check is a noop while using --skip-mappings"
0f0231
+    (with-valid-metadata (md)
0f0231
+      (set-needs-check-flag md)
0f0231
+      (assert-metadata-needs-check md)
0f0231
+      (run-ok (thin-check "--clear-needs-check-flag" "--skip-mappings" md))
0f0231
+      (assert-metadata-clean md)))
0f0231
+
0f0231
+  (define-scenario (thin-check try-clear-needs-check-flag ignore-non-fatal-errors-should-clear)
0f0231
+    "--clear-needs-check works while using --ignore-non-fatal-errors"
0f0231
+    (with-valid-metadata (md)
0f0231
+      (set-needs-check-flag md)
0f0231
+      (assert-metadata-needs-check md)
0f0231
+      (run-ok (thin-check "--clear-needs-check-flag" "--ignore-non-fatal-errors" md))
0f0231
+      (assert-metadata-clean md)))
0f0231
+
0f0231
+  (define-scenario (thin-check try-clear-needs-check-flag no-errors-should-clear)
0f0231
+    "--clear-needs-check works if there's no errors"
0f0231
+    (with-valid-metadata (md)
0f0231
+      (set-needs-check-flag md)
0f0231
+      (assert-metadata-needs-check md)
0f0231
+      (run-ok (thin-check "--clear-needs-check-flag" md))
0f0231
+      (assert-metadata-clean md)))
0f0231
+
0f0231
+  (define-scenario (thin-check try-clear-needs-check-flag fatal-errors-should-keep)
0f0231
+    "--clear-needs-check is a noop if there's fatal errors"
0f0231
+    (with-valid-metadata (md)
0f0231
+      (set-needs-check-flag md)
0f0231
+      (tamper-mapping-root md 10)
0f0231
+      (assert-metadata-needs-check md)
0f0231
+      (run-fail (thin-check "--clear-needs-check-flag" md))
0f0231
+      (assert-metadata-needs-check md)))
0f0231
+
0f0231
   (define-scenario (thin-check auto-repair)
0f0231
     "Accepts --auto-repair"
0f0231
     (with-valid-metadata (md)
0f0231
diff --git a/thin-provisioning/metadata_checker.cc b/thin-provisioning/metadata_checker.cc
0f0231
index e81e22c..1c9734e 100644
0f0231
--- a/thin-provisioning/metadata_checker.cc
0f0231
+++ b/thin-provisioning/metadata_checker.cc
0f0231
@@ -371,7 +371,8 @@ namespace {
0f0231
 			  out_(cerr, 2),
0f0231
 			  info_out_(cout, 0),
0f0231
 			  expected_rc_(true), // set stop on the first error
0f0231
-			  err_(NO_ERROR) {
0f0231
+			  err_(NO_ERROR),
0f0231
+			  metadata_checked_(false) {
0f0231
 
0f0231
 			if (output_opts == OUTPUT_QUIET) {
0f0231
 				out_.disable();
0f0231
@@ -381,7 +382,29 @@ namespace {
0f0231
 			sb_location_ = get_superblock_location();
0f0231
 		}
0f0231
 
0f0231
-		void check() {
0f0231
+		void check_and_repair() {
0f0231
+			if (!check())
0f0231
+				return;
0f0231
+
0f0231
+			if (!options_.use_metadata_snap_ &&
0f0231
+			    !options_.override_mapping_root_) {
0f0231
+				if (options_.sm_opts_ == check_options::SPACE_MAP_FULL &&
0f0231
+				    options_.fix_metadata_leaks_)
0f0231
+					fix_metadata_leaks(options_.open_transaction_);
0f0231
+				if (options_.clear_needs_check_)
0f0231
+					clear_needs_check_flag();
0f0231
+			}
0f0231
+		}
0f0231
+
0f0231
+		bool get_status() const {
0f0231
+			if (options_.ignore_non_fatal_)
0f0231
+				return (err_ == FATAL) ? false : true;
0f0231
+
0f0231
+			return (err_ == NO_ERROR) ? true : false;
0f0231
+		}
0f0231
+
0f0231
+	private:
0f0231
+		bool check() {
0f0231
 			block_manager::ptr bm = open_bm(path_, block_manager::READ_ONLY,
0f0231
 							!options_.use_metadata_snap_);
0f0231
 
0f0231
@@ -389,7 +412,7 @@ namespace {
0f0231
 			if (err_ == FATAL) {
0f0231
 				if (check_for_xml(bm))
0f0231
 					out_ << "This looks like XML.  thin_check only checks the binary metadata format." << end_message();
0f0231
-				return;
0f0231
+				return false;
0f0231
 			}
0f0231
 
0f0231
 			transaction_manager::ptr tm = open_tm(bm, sb_location_);
0f0231
@@ -407,7 +430,7 @@ namespace {
0f0231
 				err_ << examine_data_mappings(tm, sb, options_.data_mapping_opts_, out_, core_sm);
0f0231
 
0f0231
 				if (err_ == FATAL)
0f0231
-					return;
0f0231
+					return false;
0f0231
 
0f0231
 				// if we're checking everything, and there were no errors,
0f0231
 				// then we should check the space maps too.
0f0231
@@ -419,10 +442,14 @@ namespace {
0f0231
 			} else
0f0231
 				err_ << examine_data_mappings(tm, sb, options_.data_mapping_opts_, out_,
0f0231
 							      optional<space_map::ptr>());
0f0231
+
0f0231
+			metadata_checked_ = true;
0f0231
+
0f0231
+			return true;
0f0231
 		}
0f0231
 
0f0231
 		bool fix_metadata_leaks(bool open_transaction) {
0f0231
-			if (!verify_preconditions_before_fixing()) {
0f0231
+			if (!metadata_checked_) {
0f0231
 				out_ << "metadata has not been fully examined" << end_message();
0f0231
 				return false;
0f0231
 			}
0f0231
@@ -458,12 +485,13 @@ namespace {
0f0231
 		}
0f0231
 
0f0231
 		bool clear_needs_check_flag() {
0f0231
-			if (!verify_preconditions_before_fixing()) {
0f0231
+			if (!metadata_checked_) {
0f0231
 				out_ << "metadata has not been fully examined" << end_message();
0f0231
 				return false;
0f0231
 			}
0f0231
 
0f0231
-			if (err_ != NO_ERROR)
0f0231
+			if (err_ == FATAL ||
0f0231
+			    (err_ == NON_FATAL && !options_.ignore_non_fatal_))
0f0231
 				return false;
0f0231
 
0f0231
 			block_manager::ptr bm = open_bm(path_, block_manager::READ_WRITE);
0f0231
@@ -480,14 +508,6 @@ namespace {
0f0231
 			return true;
0f0231
 		}
0f0231
 
0f0231
-		bool get_status() const {
0f0231
-			if (options_.ignore_non_fatal_)
0f0231
-				return (err_ == FATAL) ? false : true;
0f0231
-
0f0231
-			return (err_ == NO_ERROR) ? true : false;
0f0231
-		}
0f0231
-
0f0231
-	private:
0f0231
 		block_address
0f0231
 		get_superblock_location() {
0f0231
 			block_address sb_location = superblock_detail::SUPERBLOCK_LOCATION;
0f0231
@@ -545,19 +565,6 @@ namespace {
0f0231
 			return err;
0f0231
 		}
0f0231
 
0f0231
-		bool verify_preconditions_before_fixing() const {
0f0231
-			if (options_.use_metadata_snap_ ||
0f0231
-			    !!options_.override_mapping_root_ ||
0f0231
-			    options_.sm_opts_ != check_options::SPACE_MAP_FULL ||
0f0231
-			    options_.data_mapping_opts_ != check_options::DATA_MAPPING_LEVEL2)
0f0231
-				return false;
0f0231
-
0f0231
-			if (!expected_rc_.get_counts().size())
0f0231
-				return false;
0f0231
-
0f0231
-			return true;
0f0231
-		}
0f0231
-
0f0231
 		std::string const &path_;
0f0231
 		check_options options_;
0f0231
 		nested_output out_;
0f0231
@@ -565,6 +572,7 @@ namespace {
0f0231
 		block_address sb_location_;
0f0231
 		block_counter expected_rc_;
0f0231
 		base::error_state err_; // metadata state
0f0231
+		bool metadata_checked_;
0f0231
 	};
0f0231
 }
0f0231
 
0f0231
@@ -603,8 +611,9 @@ void check_options::set_ignore_non_fatal() {
0f0231
 	ignore_non_fatal_ = true;
0f0231
 }
0f0231
 
0f0231
-void check_options::set_fix_metadata_leaks() {
0f0231
+void check_options::set_auto_repair() {
0f0231
 	fix_metadata_leaks_ = true;
0f0231
+	clear_needs_check_ = true;
0f0231
 }
0f0231
 
0f0231
 void check_options::set_clear_needs_check() {
0f0231
@@ -612,7 +621,7 @@ void check_options::set_clear_needs_check() {
0f0231
 }
0f0231
 
0f0231
 bool check_options::check_conformance() {
0f0231
-	if (fix_metadata_leaks_ || clear_needs_check_) {
0f0231
+	if (fix_metadata_leaks_) {
0f0231
 		if (ignore_non_fatal_) {
0f0231
 			cerr << "cannot perform fix by ignoring non-fatal errors" << endl;
0f0231
 			return false;
0f0231
@@ -627,12 +636,12 @@ bool check_options::check_conformance() {
0f0231
 			cerr << "cannot perform fix with an overridden mapping root" << endl;
0f0231
 			return false;
0f0231
 		}
0f0231
+	}
0f0231
 
0f0231
-		if (data_mapping_opts_ != DATA_MAPPING_LEVEL2 ||
0f0231
-		    sm_opts_ != SPACE_MAP_FULL) {
0f0231
-			cerr << "cannot perform fix without a full examination" << endl;
0f0231
-			return false;
0f0231
-		}
0f0231
+	if (fix_metadata_leaks_ &&
0f0231
+	    (data_mapping_opts_ != DATA_MAPPING_LEVEL2 || sm_opts_ != SPACE_MAP_FULL)) {
0f0231
+		cerr << "cannot perform fix without a full examination" << endl;
0f0231
+		return false;
0f0231
 	}
0f0231
 
0f0231
 	return true;
0f0231
@@ -646,14 +655,7 @@ thin_provisioning::check_metadata(std::string const &path,
0f0231
 				  output_options output_opts)
0f0231
 {
0f0231
 	metadata_checker checker(path, check_opts, output_opts);
0f0231
-
0f0231
-	checker.check();
0f0231
-	if (check_opts.fix_metadata_leaks_)
0f0231
-		checker.fix_metadata_leaks(check_opts.open_transaction_);
0f0231
-	if (check_opts.fix_metadata_leaks_ ||
0f0231
-	    check_opts.clear_needs_check_)
0f0231
-		checker.clear_needs_check_flag();
0f0231
-
0f0231
+	checker.check_and_repair();
0f0231
 	return checker.get_status();
0f0231
 }
0f0231
 
0f0231
diff --git a/thin-provisioning/metadata_checker.h b/thin-provisioning/metadata_checker.h
0f0231
index 5569d27..ea66dc3 100644
0f0231
--- a/thin-provisioning/metadata_checker.h
0f0231
+++ b/thin-provisioning/metadata_checker.h
0f0231
@@ -45,14 +45,17 @@ namespace thin_provisioning {
0f0231
 		void set_override_mapping_root(bcache::block_address b);
0f0231
 		void set_metadata_snap();
0f0231
 		void set_ignore_non_fatal();
0f0231
-		void set_fix_metadata_leaks();
0f0231
+		void set_auto_repair();
0f0231
 		void set_clear_needs_check();
0f0231
 
0f0231
+		// flags for checking
0f0231
 		bool use_metadata_snap_;
0f0231
 		data_mapping_options data_mapping_opts_;
0f0231
 		space_map_options sm_opts_;
0f0231
 		boost::optional<bcache::block_address> override_mapping_root_;
0f0231
 		bool ignore_non_fatal_;
0f0231
+
0f0231
+		// flags for repairing
0f0231
 		bool fix_metadata_leaks_;
0f0231
 		bool clear_needs_check_;
0f0231
 		bool open_transaction_;
0f0231
diff --git a/thin-provisioning/thin_check.cc b/thin-provisioning/thin_check.cc
0f0231
index 60f7838..e3c9db3 100644
0f0231
--- a/thin-provisioning/thin_check.cc
0f0231
+++ b/thin-provisioning/thin_check.cc
0f0231
@@ -166,7 +166,7 @@ thin_check_cmd::run(int argc, char **argv)
0f0231
 
0f0231
 		case 6:
0f0231
 			// auto-repair
0f0231
-			fs.check_opts.set_fix_metadata_leaks();
0f0231
+			fs.check_opts.set_auto_repair();
0f0231
 			break;
0f0231
 
0f0231
 		default:
0f0231
-- 
0f0231
2.31.1
0f0231