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

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