[Devel] [PATCH RH8 6/7] ploop: Allocate deltas in only place

Kirill Tkhai ktkhai at virtuozzo.com
Tue May 4 16:05:28 MSK 2021


Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 drivers/md/dm-ploop-cmd.c    |   13 +------------
 drivers/md/dm-ploop-target.c |   15 ++++++++++++++-
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/drivers/md/dm-ploop-cmd.c b/drivers/md/dm-ploop-cmd.c
index 1626cd1b881a..ff696065fdb7 100644
--- a/drivers/md/dm-ploop-cmd.c
+++ b/drivers/md/dm-ploop-cmd.c
@@ -620,11 +620,9 @@ static int apply_delta_mappings(struct ploop *ploop, struct ploop_delta *deltas,
 		delta_bat_entries += PAGE_SIZE / sizeof(map_index_t);
 	}
 
-	swap(ploop->deltas, deltas);
 	ploop->nr_deltas++;
 	write_unlock_irq(&ploop->bat_rwlock);
 
-	kfree(deltas);
 	get_file(ploop->deltas[level].file);
 	return 0;
 }
@@ -647,16 +645,13 @@ static int ploop_check_delta_length(struct ploop *ploop, struct file *file,
  */
 int ploop_add_delta(struct ploop *ploop, int fd, bool is_raw)
 {
+	struct ploop_delta *deltas = ploop->deltas;
 	unsigned int level = ploop->nr_deltas;
-	struct ploop_delta *deltas;
-	unsigned int size;
 	struct file *file;
 	u64 size_in_clus;
 	void *hdr = NULL;
 	int ret;
 
-	if (level == BAT_LEVEL_TOP)
-		return -EMFILE;
 	file = fget(fd);
 	if (!file)
 		return -ENOENT;
@@ -664,12 +659,6 @@ int ploop_add_delta(struct ploop *ploop, int fd, bool is_raw)
 	if (!(file->f_mode & FMODE_READ))
 		goto out;
 
-	ret = -ENOMEM;
-	deltas = kcalloc(level + 1, sizeof(*deltas), GFP_KERNEL);
-	if (!deltas)
-		goto out;
-	size = level * sizeof(*deltas);
-	memcpy(deltas, ploop->deltas, size);
 	deltas[level].file = file;
 	deltas[level].is_raw = is_raw;
 
diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index b4a21840fba7..5581e2722e0b 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -111,10 +111,23 @@ static int ploop_check_origin_dev(struct dm_target *ti, struct ploop *ploop)
 
 static int ploop_add_deltas_stack(struct ploop *ploop, char **argv, int argc)
 {
-	int i, delta_fd, ret;
+	struct ploop_delta *deltas;
+	int i, delta_fd, ret = 0;
 	const char *arg;
 	bool is_raw;
 
+	if (!argc)
+		goto out;
+	ret = -EINVAL;
+	if (argc > BAT_LEVEL_TOP - 1)
+		goto out;
+
+	ret = -ENOMEM;
+	deltas = kcalloc(argc, sizeof(*deltas), GFP_KERNEL);
+	if (!deltas)
+		goto out;
+	ploop->deltas = deltas;
+
 	ret = -EINVAL;
 	for (i = 0; i < argc; i++) {
 		arg = argv[i];




More information about the Devel mailing list