Blame SOURCES/0015-thin-Clear-superblock-flags-in-restored-metadata.patch

3be71b
From 637fc5ec0869145d1b5272ee0e1e51e21517be79 Mon Sep 17 00:00:00 2001
3be71b
From: Ming-Hung Tsai <mtsai@redhat.com>
3be71b
Date: Mon, 7 Jun 2021 14:54:04 +0800
3be71b
Subject: [PATCH] [thin] Clear superblock flags in restored metadata
3be71b
3be71b
The needs_check flag is unnecessary for a restored metadata since
3be71b
it is assumed clean and has no errors
3be71b
3be71b
(cherry picked from commit 62536defc86f12363e4178ee5a2cefc40fa416d9)
3be71b
---
3be71b
 ft-lib/bcache.c                            | 20 ++++++++++---
3be71b
 functional-tests/thin-functional-tests.scm | 46 ++++++++++++++++++++++++++++++
3be71b
 functional-tests/thin/xml.scm              |  4 +--
3be71b
 thin-provisioning/restore_emitter.cc       |  2 +-
3be71b
 4 files changed, 65 insertions(+), 7 deletions(-)
3be71b
3be71b
diff --git a/ft-lib/bcache.c b/ft-lib/bcache.c
3be71b
index ee5b6c5..1c63377 100644
3be71b
--- a/ft-lib/bcache.c
3be71b
+++ b/ft-lib/bcache.c
3be71b
@@ -201,8 +201,10 @@ static int engine_issue(struct io_engine *e, int fd, enum dir d,
3be71b
 
3be71b
 	cb_array[0] = &cb->cb;
3be71b
 	r = io_submit(e->aio_context, 1, cb_array);
3be71b
-	if (r < 0)
3be71b
+	if (r < 0) {
3be71b
+		warn("io_submit failed, ret=%d\n", r);
3be71b
 		cb_free(e->cbs, cb);
3be71b
+	}
3be71b
 
3be71b
 	return r;
3be71b
 }
3be71b
@@ -219,7 +221,7 @@ static int engine_wait(struct io_engine *e, struct timespec *ts, complete_fn fn)
3be71b
 	memset(&event, 0, sizeof(event));
3be71b
 	r = io_getevents(e->aio_context, 1, MAX_IO, event, ts);
3be71b
 	if (r < 0) {
3be71b
-		warn("io_getevents failed");
3be71b
+		warn("io_getevents failed, ret=%d\n", r);
3be71b
 		return r;
3be71b
 	}
3be71b
 
3be71b
@@ -514,12 +516,22 @@ static void relink(struct block *b)
3be71b
  */
3be71b
 static int issue_low_level(struct block *b, enum dir d)
3be71b
 {
3be71b
+	int r;
3be71b
 	struct bcache *cache = b->cache;
3be71b
 	sector_t sb = b->index * cache->block_sectors;
3be71b
 	sector_t se = sb + cache->block_sectors;
3be71b
 	set_flags(b, BF_IO_PENDING);
3be71b
+	cache->nr_io_pending++;
3be71b
+	list_add_tail(&b->list, &cache->io_pending);
3be71b
 
3be71b
-	return engine_issue(cache->engine, cache->fd, d, sb, se, b->data, b);
3be71b
+	r = engine_issue(cache->engine, cache->fd, d, sb, se, b->data, b);
3be71b
+	if (r < 0) {
3be71b
+		list_del(&b->list);
3be71b
+		cache->nr_io_pending--;
3be71b
+		clear_flags(b, BF_IO_PENDING);
3be71b
+		return r;
3be71b
+	}
3be71b
+	return 0;
3be71b
 }
3be71b
 
3be71b
 static void issue_read(struct block *b)
3be71b
@@ -709,7 +721,7 @@ struct bcache *bcache_simple(const char *path, unsigned nr_cache_blocks)
3be71b
 	int r;
3be71b
 	struct stat info;
3be71b
 	struct bcache *cache;
3be71b
-	int fd = open(path, O_DIRECT | O_EXCL | O_RDONLY);
3be71b
+	int fd = open(path, O_DIRECT | O_EXCL | O_RDWR);
3be71b
 	uint64_t s;
3be71b
 
3be71b
 	if (fd < 0) {
3be71b
diff --git a/functional-tests/thin-functional-tests.scm b/functional-tests/thin-functional-tests.scm
3be71b
index 55b0e60..fcabddf 100644
3be71b
--- a/functional-tests/thin-functional-tests.scm
3be71b
+++ b/functional-tests/thin-functional-tests.scm
3be71b
@@ -12,6 +12,7 @@
3be71b
     (process)
3be71b
     (scenario-string-constants)
3be71b
     (temp-file)
3be71b
+    (thin metadata)
3be71b
     (thin xml)
3be71b
     (srfi s8 receive))
3be71b
 
3be71b
@@ -30,6 +31,12 @@
3be71b
        (with-temp-file-containing ((v "thin.xml" (fmt #f (generate-xml 10 1000))))
3be71b
                                   b1 b2 ...))))
3be71b
 
3be71b
+  (define-syntax with-needs-check-thin-xml
3be71b
+    (syntax-rules ()
3be71b
+      ((_ (v) b1 b2 ...)
3be71b
+       (with-temp-file-containing ((v "thin.xml" (fmt #f (generate-xml 10 1000 1))))
3be71b
+                                  b1 b2 ...))))
3be71b
+
3be71b
   (define-syntax with-valid-metadata
3be71b
     (syntax-rules ()
3be71b
       ((_ (md) b1 b2 ...)
3be71b
@@ -63,6 +70,28 @@
3be71b
          (damage-superblock md)
3be71b
          b1 b2 ...))))
3be71b
 
3be71b
+  (define superblock-salt 160774)
3be71b
+  (define (set-needs-check-flag md)
3be71b
+    (with-bcache (cache md 1)
3be71b
+      (with-block (b cache 0 (get-flags dirty))
3be71b
+        (let ((sb (block->superblock b)))
3be71b
+          (ftype-set! ThinSuperblock (flags) sb 1)
3be71b
+          ;;;;;; Update the csum manually since the block validator for ft-lib is not ready
3be71b
+          (let ((csum (checksum-block b (ftype-sizeof unsigned-32) superblock-salt)))
3be71b
+            (ftype-set! ThinSuperblock (csum) sb csum))))))
3be71b
+
3be71b
+  (define (get-superblock-flags md)
3be71b
+    (with-bcache (cache md 1)
3be71b
+      (with-block (b cache 0 (get-flags))
3be71b
+        (let ((sb (block->superblock b)))
3be71b
+          (ftype-ref ThinSuperblock (flags) sb)))))
3be71b
+
3be71b
+  (define (assert-metadata-needs-check md)
3be71b
+    (assert-equal (get-superblock-flags md) 1))
3be71b
+
3be71b
+  (define (assert-metadata-clean md)
3be71b
+    (assert-equal (get-superblock-flags md) 0))
3be71b
+
3be71b
   ;; We have to export something that forces all the initialisation expressions
3be71b
   ;; to run.
3be71b
   (define (register-thin-tests) #t)
3be71b
@@ -173,6 +202,13 @@
3be71b
   ;;;-----------------------------------------------------------
3be71b
   ;;; thin_restore scenarios
3be71b
   ;;;-----------------------------------------------------------
3be71b
+  (define-scenario (thin-restore clear-needs-check-flag)
3be71b
+    "thin_restore should clear the needs-check flag"
3be71b
+    (with-empty-metadata (md)
3be71b
+      (with-needs-check-thin-xml (xml)
3be71b
+        (run-ok-rcv (stdout _) (thin-restore "-i" xml "-o" md "-q")
3be71b
+          (assert-eof stdout)))
3be71b
+      (assert-metadata-clean md)))
3be71b
 
3be71b
   (define-scenario (thin-restore print-version-v)
3be71b
     "print help (-V)"
3be71b
@@ -439,6 +475,16 @@
3be71b
   ;;;-----------------------------------------------------------
3be71b
   ;;; thin_repair scenarios
3be71b
   ;;;-----------------------------------------------------------
3be71b
+  (define-scenario (thin-repair clear-needs-check-flag)
3be71b
+    "thin_repair should clear the needs-check flag"
3be71b
+    (with-valid-metadata (md1)
3be71b
+      (set-needs-check-flag md1)
3be71b
+      (assert-metadata-needs-check md1)
3be71b
+      (with-empty-metadata (md2)
3be71b
+        (run-ok-rcv (stdout stderr) (thin-repair "-i" md1 "-o" md2)
3be71b
+          (assert-eof stderr))
3be71b
+        (assert-metadata-clean md2))))
3be71b
+
3be71b
   (define-scenario (thin-repair dont-repair-xml)
3be71b
     "Fails gracefully if run on XML rather than metadata"
3be71b
     (with-thin-xml (xml)
3be71b
diff --git a/functional-tests/thin/xml.scm b/functional-tests/thin/xml.scm
3be71b
index 551a536..7d9314b 100644
3be71b
--- a/functional-tests/thin/xml.scm
3be71b
+++ b/functional-tests/thin/xml.scm
3be71b
@@ -22,7 +22,7 @@
3be71b
                                (length . ,nr-mappings)
3be71b
                                (time . 1)))))
3be71b
 
3be71b
-  (define (generate-xml max-thins max-mappings)
3be71b
+  (define (generate-xml max-thins max-mappings . needs-check)
3be71b
     (let ((nr-thins ((make-uniform-generator 1 max-thins)))
3be71b
           (nr-mappings-g (make-uniform-generator (div-down max-mappings 2)
3be71b
                                                  max-mappings)))
3be71b
@@ -30,7 +30,7 @@
3be71b
        (tag 'superblock `((uuid . "")
3be71b
                          (time . 1)
3be71b
                          (transaction . 1)
3be71b
-                         (flags . 0)
3be71b
+                         (flags . ,(if (null? needs-check) 0 (car needs-check)))
3be71b
                          (version . 2)
3be71b
                          (data-block-size . 128)
3be71b
                          (nr-data-blocks . ,(apply + nr-mappings)))
3be71b
diff --git a/thin-provisioning/restore_emitter.cc b/thin-provisioning/restore_emitter.cc
3be71b
index 6e95a53..114ba4f 100644
3be71b
--- a/thin-provisioning/restore_emitter.cc
3be71b
+++ b/thin-provisioning/restore_emitter.cc
3be71b
@@ -57,7 +57,7 @@ namespace {
3be71b
 			memcpy(&sb.uuid_, uuid.c_str(), std::min(sizeof(sb.uuid_), uuid.length()));
3be71b
 			sb.time_ = time;
3be71b
 			sb.trans_id_ = trans_id;
3be71b
-			sb.flags_ = flags ? *flags : 0;
3be71b
+			sb.flags_ = 0;
3be71b
 			sb.version_ = version ? *version : 1;
3be71b
 			sb.data_block_size_ = data_block_size;
3be71b
 			sb.metadata_snap_ = metadata_snap ? *metadata_snap : 0;
3be71b
-- 
3be71b
1.8.3.1
3be71b