From: NeilBrown <neilb@suse.de>
Subject: md/bitmap: Don't write bitmap while earlier writes might be in-flight
Git-commit: 85c9ccd4f026aad8e91ec1c182206e807cff932d
Patch-mainline: v4.10
References: bnc#771398

As we don't wait for writes to complete in bitmap_daemon_work, they
could still be in-flight when bitmap_unplug writes again.  Or when
bitmap_daemon_work tries to write again.
This can be confusing and could risk the wrong data being written last.

So make sure we wait for old writes to complete before new writes start.
In bitmap_unplug, always wait if anything is written as that is probably
always the case anyway.

Signed-off-by: Neil Brown <neilb@suse.de>

---
 drivers/md/bitmap.c |   27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

--- a/drivers/md/bitmap.c
+++ b/drivers/md/bitmap.c
@@ -446,6 +446,13 @@ void bitmap_update_sb(struct bitmap *bit
 	sb->sectors_reserved = cpu_to_le32(bitmap->mddev->
 					   bitmap_info.space);
 	kunmap_atomic(sb);
+	/* Don't write until any other writes have completed */
+	if (bitmap->storage.file)
+		wait_event(bitmap->write_wait,
+			   atomic_read(&bitmap->pending_writes)==0);
+	else
+		md_super_wait(bitmap->mddev);
+
 	write_page(bitmap, bitmap->storage.sb_page, 1);
 }
 
@@ -976,6 +983,7 @@ void bitmap_unplug(struct bitmap *bitmap
 {
 	unsigned long i;
 	int dirty, need_write;
+	int writing = 0;
 
 	if (!bitmap || !bitmap->storage.filemap ||
 	    test_bit(BITMAP_STALE, &bitmap->flags))
@@ -990,8 +998,20 @@ void bitmap_unplug(struct bitmap *bitmap
 		need_write = test_and_clear_page_attr(bitmap, i,
 						      BITMAP_PAGE_NEEDWRITE);
 		if (dirty || need_write) {
+			if (!writing) {
+				/* Need to ensure any prior writes from
+				 * bitmap_daemon_work have completed.
+				 * We don't want the writes racing.
+				 */
+				if (bitmap->storage.file)
+					wait_event(bitmap->write_wait,
+						   atomic_read(&bitmap->pending_writes)==0);
+				else
+					md_super_wait(bitmap->mddev);
+			}
 			clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING);
 			write_page(bitmap, bitmap->storage.filemap[i], 0);
+			writing = 1;
 		}
 	}
 	if (bitmap->storage.file)
@@ -1282,6 +1302,13 @@ void bitmap_daemon_work(struct mddev *md
 	}
 	spin_unlock_irq(&counts->lock);
 
+	/* Make sure any prior writes have completed */
+	if (bitmap->storage.file)
+		wait_event(bitmap->write_wait,
+			   atomic_read(&bitmap->pending_writes)==0);
+	else
+		md_super_wait(bitmap->mddev);
+
 	/* Now start writeout on any page in NEEDWRITE that isn't DIRTY.
 	 * DIRTY pages need to be written by bitmap_unplug so it can wait
 	 * for them.
