[Devel] [PATCH RH8 3/4] ploop: Add deltas in normal order

Kirill Tkhai ktkhai at virtuozzo.com
Wed May 5 13:11:21 MSK 2021


>From newest to oldest.

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 drivers/md/dm-ploop-bat.c    |   40 ++++++++++++++++++----------------------
 drivers/md/dm-ploop-target.c |   12 +++++++-----
 2 files changed, 25 insertions(+), 27 deletions(-)

diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index 135427c759dd..45f651ce5549 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -409,8 +409,15 @@ int ploop_read_delta_metadata(struct ploop *ploop, struct file *file,
 	return ret;
 }
 
-static int apply_delta_mappings(struct ploop *ploop, struct ploop_delta *deltas,
-				u32 level, void *hdr, u64 size_in_clus)
+/*
+ * Prefer first added delta, since the order is:
+ * 1)add top device
+ * 2)add newest delta
+ * ...
+ * n)add oldest delta
+ */
+static void apply_delta_mappings(struct ploop *ploop, struct ploop_delta *deltas,
+				 u32 level, void *hdr, u64 size_in_clus)
 {
 	map_index_t *bat_entries, *delta_bat_entries;
 	unsigned int i, end, dst_cluster;
@@ -423,14 +430,13 @@ static int apply_delta_mappings(struct ploop *ploop, struct ploop_delta *deltas,
 	is_raw = deltas[level].is_raw;
 
 	write_lock_irq(&ploop->bat_rwlock);
-
-	/* FIXME: Stop on old delta's nr_bat_entries */
 	ploop_for_each_md_page(ploop, md, node) {
 		init_bat_entries_iter(ploop, md->id, &i, &end);
 		bat_entries = kmap_atomic(md->page);
 		for (; i <= end; i++) {
-			if (md_page_cluster_is_in_top_delta(md, i))
+			if (bat_entries[i] != BAT_ENTRY_NONE)
 				continue;
+
 			if (!is_raw)
 				dst_cluster = delta_bat_entries[i];
 			else {
@@ -440,15 +446,6 @@ static int apply_delta_mappings(struct ploop *ploop, struct ploop_delta *deltas,
 			}
 			if (dst_cluster == BAT_ENTRY_NONE)
 				continue;
-			/*
-			 * Prefer last added delta, since the order is:
-			 * 1)add top device
-			 * 2)add oldest delta
-			 * ...
-			 * n)add newest delta
-			 * Keep in mind, top device is current image, and
-			 * it is added first in contrary the "age" order.
-			 */
 			md->bat_levels[i] = level;
 			bat_entries[i] = dst_cluster;
 
@@ -457,9 +454,6 @@ static int apply_delta_mappings(struct ploop *ploop, struct ploop_delta *deltas,
 		delta_bat_entries += PAGE_SIZE / sizeof(map_index_t);
 	}
 	write_unlock_irq(&ploop->bat_rwlock);
-
-	get_file(ploop->deltas[level].file);
-	return 0;
 }
 
 static int ploop_check_delta_length(struct ploop *ploop, struct file *file,
@@ -493,9 +487,6 @@ int ploop_add_delta(struct ploop *ploop, u32 level, int fd, bool is_raw)
 	if (!(file->f_mode & FMODE_READ))
 		goto out;
 
-	deltas[level].file = file;
-	deltas[level].is_raw = is_raw;
-
 	ret = ploop_check_delta_length(ploop, file, &size_in_clus);
 	if (ret)
 		goto out;
@@ -505,9 +496,14 @@ int ploop_add_delta(struct ploop *ploop, u32 level, int fd, bool is_raw)
 	if (ret)
 		goto out;
 
-	ret = apply_delta_mappings(ploop, deltas, level, hdr, size_in_clus);
+	apply_delta_mappings(ploop, deltas, level, hdr, size_in_clus);
+
+	deltas[level].file = file;
+	deltas[level].is_raw = is_raw;
+	ret = 0;
 out:
 	vfree(hdr);
-	fput(file);
+	if (ret)
+		fput(file);
 	return ret;
 }
diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index 4e5f5adba40e..39c39a77b05f 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -84,8 +84,10 @@ static void ploop_destroy(struct ploop *ploop)
 	for (i = 0; i < 2; i++)
 		percpu_ref_exit(&ploop->inflight_bios_ref[i]);
 	/* Nobody uses it after destroy_workqueue() */
-	while (ploop->nr_deltas-- > 0)
-		fput(ploop->deltas[ploop->nr_deltas].file);
+	while (ploop->nr_deltas-- > 0) {
+		if (ploop->deltas[ploop->nr_deltas].file)
+			fput(ploop->deltas[ploop->nr_deltas].file);
+	}
 	WARN_ON(!RB_EMPTY_ROOT(&ploop->exclusive_bios_rbtree));
 	WARN_ON(!RB_EMPTY_ROOT(&ploop->inflight_bios_rbtree));
 	kfree(ploop->deltas);
@@ -133,9 +135,10 @@ static int ploop_add_deltas_stack(struct ploop *ploop, char **argv, int argc)
 	if (!deltas)
 		goto out;
 	ploop->deltas = deltas;
+	ploop->nr_deltas = argc;
 
 	ret = -EINVAL;
-	for (i = 0; i < argc; i++) {
+	for (i = argc - 1; i >= 0; i--) {
 		arg = argv[i];
 		is_raw = false;
 		if (strncmp(arg, "raw@", 4) == 0) {
@@ -147,11 +150,10 @@ static int ploop_add_deltas_stack(struct ploop *ploop, char **argv, int argc)
 		if (kstrtos32(arg, 10, &delta_fd) < 0)
 			goto out;
 
-		level = ploop->nr_deltas;
+		level = i;
 		ret = ploop_add_delta(ploop, level, delta_fd, is_raw);
 		if (ret < 0)
 			goto out;
-		ploop->nr_deltas++;
 	}
 
 	ret = 0;




More information about the Devel mailing list