Skip to contentSkip to conversation

dm-persistent-data: free sm_metadata on failed create

PATCH In review Benjamin Marzinski 3 participants 4 messages Nov 30, 2016 – Dec 2, 2016 Device mapper

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

PATCH 1/1 dm-persistent-data: free sm_metadata on failed create

In dm_sm_metadata_create we temporarily change the dm_space_map operations from ops, whose destroy function deallocates the sm_metadata, to bootstrap_ops, whose destroy function doesn't. If we fail in dm_ll_new_metadata or sm_ll_extend, we exit back to dm_tm_create_internal, which calls dm_sm_destroy with the intention of freeing the sm_metadata, but it doesn't.

This patch sets the dm_space_map operations back to ops if dm_sm_metadata_create fails when it is set to bootstrap_ops.

---
 drivers/md/persistent-data/dm-space-map-metadata.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/md/persistent-data/dm-space-map-metadata.c +6 -8
index 7e44005..20557e2 100644
@@ -775,17 +775,15 @@ int dm_sm_metadata_create(struct dm_space_map *sm,
 	memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
 
 	r = sm_ll_new_metadata(&smm->ll, tm);
+	if (!r) {
+		if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
+			nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
+		r = sm_ll_extend(&smm->ll, nr_blocks);
+	}
+	memcpy(&smm->sm, &ops, sizeof(smm->sm));
 	if (r)
 		return r;
 
-	if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
-		nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
-	r = sm_ll_extend(&smm->ll, nr_blocks);
-	if (r)
-		return r;
-
-	memcpy(&smm->sm, &ops, sizeof(smm->sm));
-
 	/*
 	 * Now we need to update the newly created data structures with the
 	 * allocated blocks that they were built from.
Signature · 1 line
2.1.0
#1 lore raw

ack

Quoted 46 lines · Benjamin Marzinski

In dm_sm_metadata_create we temporarily change the dm_space_map operations from ops, whose destroy function deallocates the sm_metadata, to bootstrap_ops, whose destroy function doesn't. If we fail in dm_ll_new_metadata or sm_ll_extend, we exit back to dm_tm_create_internal, which calls dm_sm_destroy with the intention of freeing the sm_metadata, but it doesn't.

This patch sets the dm_space_map operations back to ops if dm_sm_metadata_create fails when it is set to bootstrap_ops.

---
 drivers/md/persistent-data/dm-space-map-metadata.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/md/persistent-data/dm-space-map-metadata.c +6 -8
index 7e44005..20557e2 100644
@@ -775,17 +775,15 @@ int dm_sm_metadata_create(struct dm_space_map *sm,
 	memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
 
 	r = sm_ll_new_metadata(&smm->ll, tm);
+	if (!r) {
+		if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
+			nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
+		r = sm_ll_extend(&smm->ll, nr_blocks);
+	}
+	memcpy(&smm->sm, &ops, sizeof(smm->sm));
 	if (r)
 		return r;
 
-	if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
-		nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
-	r = sm_ll_extend(&smm->ll, nr_blocks);
-	if (r)
-		return r;
-
-	memcpy(&smm->sm, &ops, sizeof(smm->sm));
-
 	/*
 	 * Now we need to update the newly created data structures with the
 	 * allocated blocks that they were built from.
Signature · 1 line
2.1.0
#2 lore raw

Staged for 4.10 inclusion with a revised header, see:
https://git.kernel.org/cgit/linux/kernel/git/device-mapper/linux-dm.git/commit/?h=dm-4.10&id=b34af3050734ba53a7afa4f3ab9fe9d6bbfbf802

Quoted 54 lines · Edward Thornber

ack

Quoted 46 lines · Benjamin Marzinski

In dm_sm_metadata_create we temporarily change the dm_space_map operations from ops, whose destroy function deallocates the sm_metadata, to bootstrap_ops, whose destroy function doesn't. If we fail in dm_ll_new_metadata or sm_ll_extend, we exit back to dm_tm_create_internal, which calls dm_sm_destroy with the intention of freeing the sm_metadata, but it doesn't.

This patch sets the dm_space_map operations back to ops if dm_sm_metadata_create fails when it is set to bootstrap_ops.

---
 drivers/md/persistent-data/dm-space-map-metadata.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/md/persistent-data/dm-space-map-metadata.c +6 -8
index 7e44005..20557e2 100644
@@ -775,17 +775,15 @@ int dm_sm_metadata_create(struct dm_space_map *sm,
 	memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
 
 	r = sm_ll_new_metadata(&smm->ll, tm);
+	if (!r) {
+		if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
+			nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
+		r = sm_ll_extend(&smm->ll, nr_blocks);
+	}
+	memcpy(&smm->sm, &ops, sizeof(smm->sm));
 	if (r)
 		return r;
 
-	if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
-		nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
-	r = sm_ll_extend(&smm->ll, nr_blocks);
-	if (r)
-		return r;
-
-	memcpy(&smm->sm, &ops, sizeof(smm->sm));
-
 	/*
 	 * Now we need to update the newly created data structures with the
 	 * allocated blocks that they were built from.
Signature · 1 line
2.1.0

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel