kvm tools, qcow: Flush only dirty L2 tables

This patch improves qcow_l2_cache_write() to only flush dirty L2 tables.

Cc: Asias He <asias.hejun@gmail.com>
Cc: Cyrill Gorcunov <gorcunov@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Prasad Joshi <prasadjoshi124@gmail.com>
Cc: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
diff --git a/disk/qcow.c b/disk/qcow.c
index b71762f..13c8bea 100644
--- a/disk/qcow.c
+++ b/disk/qcow.c
@@ -93,9 +93,17 @@
 	struct qcow_header *header = q->header;
 	u64 size;
 
+	if (!c->dirty)
+		return 0;
+
 	size = 1 << header->l2_bits;
 
-	return pwrite_in_full(q->fd, c->table, size * sizeof(u64), c->offset);
+	if (pwrite_in_full(q->fd, c->table, size * sizeof(u64), c->offset) < 0)
+		return -1;
+
+	c->dirty = 0;
+
+	return 0;
 }
 
 static int cache_table(struct qcow *q, struct qcow_l2_table *c)
@@ -447,6 +455,7 @@
 	if (!clust_start) {
 		clust_start		= ALIGN(f_sz, clust_sz);
 		l2t->table[l2t_idx]	= cpu_to_be64(clust_start);
+		l2t->dirty		= 1;
 	}
 
 	mutex_unlock(&q->mutex);