From ac1aa04ba75e6bdbcea738820e57cf410b408a6f Mon Sep 17 00:00:00 2001 From: Jim Harris Date: Wed, 13 Dec 2017 09:34:05 -0700 Subject: [PATCH] blob: add _spdk_bs_load_fail helper routine This eliminates a bunch of code duplication. This also fixes a couple of places where the ctx->bs was not being freed in the load fail path. Signed-off-by: Jim Harris Change-Id: Ie6b0a4a653b5c80edf14086801b75457852a4736 Reviewed-on: https://review.gerrithub.io/391693 Reviewed-by: Paul Luse Tested-by: SPDK Automated Test System Reviewed-by: Daniel Verkamp Reviewed-by: Ben Walker --- lib/blob/blobstore.c | 93 +++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 58 deletions(-) diff --git a/lib/blob/blobstore.c b/lib/blob/blobstore.c index 27843e081b..0d64c44547 100644 --- a/lib/blob/blobstore.c +++ b/lib/blob/blobstore.c @@ -1569,8 +1569,26 @@ struct spdk_bs_load_ctx { uint32_t page_index; uint32_t cur_page; struct spdk_blob_md_page *page; + bool is_load; }; +static void +_spdk_bs_load_ctx_fail(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno) +{ + assert(bserrno != 0); + + spdk_dma_free(ctx->super); + /* + * Only free the blobstore when a load fails. If an unload fails (for some reason) + * we want to keep the blobstore in case the caller wants to try again. + */ + if (ctx->is_load) { + _spdk_bs_free(ctx->bs); + } + free(ctx); + spdk_bs_sequence_finish(seq, bserrno); +} + static void _spdk_bs_set_mask(struct spdk_bit_array *array, struct spdk_bs_md_mask *mask) { @@ -1609,9 +1627,7 @@ _spdk_bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequenc mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); if (!ctx->mask) { - spdk_dma_free(ctx->super); - free(ctx); - spdk_bs_sequence_finish(seq, -ENOMEM); + _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); return; } @@ -1634,9 +1650,7 @@ _spdk_bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); if (!ctx->mask) { - spdk_dma_free(ctx->super); - free(ctx); - spdk_bs_sequence_finish(seq, -ENOMEM); + _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); return; } @@ -1667,11 +1681,8 @@ _spdk_bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserr rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters); if (rc < 0) { - spdk_dma_free(ctx->super); spdk_dma_free(ctx->mask); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -ENOMEM); + _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); return; } @@ -1713,11 +1724,8 @@ _spdk_bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length); if (rc < 0) { - spdk_dma_free(ctx->super); spdk_dma_free(ctx->mask); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -ENOMEM); + _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); return; } @@ -1736,10 +1744,7 @@ _spdk_bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); if (!ctx->mask) { - spdk_dma_free(ctx->super); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -ENOMEM); + _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); return; } lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); @@ -1758,10 +1763,7 @@ _spdk_bs_load_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); if (!ctx->mask) { - spdk_dma_free(ctx->super); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -ENOMEM); + _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); return; } @@ -1874,10 +1876,7 @@ _spdk_bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) uint32_t page_num; if (bserrno != 0) { - spdk_dma_free(ctx->super); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, bserrno); + _spdk_bs_load_ctx_fail(seq, ctx, bserrno); return; } @@ -1886,10 +1885,7 @@ _spdk_bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) if (ctx->page->sequence_num == 0 || ctx->in_page_chain == true) { spdk_bit_array_set(ctx->bs->used_md_pages, page_num); if (_spdk_bs_load_replay_md_parse_page(ctx->page, ctx->bs)) { - spdk_dma_free(ctx->super); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -EILSEQ); + _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); return; } if (ctx->page->next != SPDK_INVALID_MD_PAGE) { @@ -1940,10 +1936,7 @@ _spdk_bs_load_replay_md(spdk_bs_sequence_t *seq, void *cb_arg) SPDK_BS_PAGE_SIZE, NULL); if (!ctx->page) { - spdk_dma_free(ctx->super); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -ENOMEM); + _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); return; } _spdk_bs_load_replay_cur_md_page(seq, cb_arg); @@ -1957,19 +1950,13 @@ _spdk_bs_recover(spdk_bs_sequence_t *seq, void *cb_arg) rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len); if (rc < 0) { - spdk_dma_free(ctx->super); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -ENOMEM); + _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); return; } rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters); if (rc < 0) { - spdk_dma_free(ctx->super); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -ENOMEM); + _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); return; } @@ -1986,28 +1973,19 @@ _spdk_bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) if (ctx->super->version > SPDK_BS_VERSION || ctx->super->version < SPDK_BS_INITIAL_VERSION) { - spdk_dma_free(ctx->super); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -EILSEQ); + _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); return; } if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, sizeof(ctx->super->signature)) != 0) { - spdk_dma_free(ctx->super); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -EILSEQ); + _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); return; } crc = _spdk_blob_md_page_calc_crc(ctx->super); if (crc != ctx->super->crc) { - spdk_dma_free(ctx->super); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -EILSEQ); + _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); return; } @@ -2019,10 +1997,7 @@ _spdk_bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Unexpected bstype\n"); SPDK_TRACEDUMP(SPDK_LOG_BLOB, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); SPDK_TRACEDUMP(SPDK_LOG_BLOB, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); - spdk_dma_free(ctx->super); - _spdk_bs_free(ctx->bs); - free(ctx); - spdk_bs_sequence_finish(seq, -ENXIO); + _spdk_bs_load_ctx_fail(seq, ctx, -ENXIO); return; } @@ -2082,6 +2057,7 @@ spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, } ctx->bs = bs; + ctx->is_load = true; /* Allocate memory for the super block */ ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); @@ -2446,6 +2422,7 @@ spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_a } ctx->bs = bs; + ctx->is_load = false; ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); if (!ctx->super) {