Commit 5d5243e0 authored by Seth Howell's avatar Seth Howell Committed by Jim Harris
Browse files

bdev: make pointers to part_base opaque.

parent 4a9583f7
Loading
Loading
Loading
Loading
+22 −25
Original line number Diff line number Diff line
@@ -688,21 +688,17 @@ spdk_bdev_io_from_ctx(void *ctx)

struct spdk_bdev_part_base;

typedef void (*spdk_bdev_part_base_free_fn)(struct spdk_bdev_part_base *base);
struct spdk_bdev *spdk_bdev_part_base_get_bdev(struct spdk_bdev_part_base *part_base);

struct spdk_bdev_part_base {
	struct spdk_bdev		*bdev;
	struct spdk_bdev_desc		*desc;
	uint32_t			ref;
	uint32_t			channel_size;
	spdk_bdev_part_base_free_fn	base_free_fn;
	bool				claimed;
	struct spdk_bdev_module		*module;
	struct spdk_bdev_fn_table	*fn_table;
	struct bdev_part_tailq		*tailq;
	spdk_io_channel_create_cb	ch_create_cb;
	spdk_io_channel_destroy_cb	ch_destroy_cb;
};
struct spdk_bdev_desc *spdk_bdev_part_base_get_desc(struct spdk_bdev_part_base *part_base);

struct bdev_part_tailq *spdk_bdev_part_base_get_tailq(struct spdk_bdev_part_base *part_base);

void *spdk_bdev_part_base_get_ctx(struct spdk_bdev_part_base *part_base);

uint32_t spdk_bdev_part_base_get_ref(struct spdk_bdev_part_base *part_base);

typedef void (*spdk_bdev_part_base_free_fn)(void *ctx);

struct spdk_bdev_part {
	struct spdk_bdev		bdev;
@@ -745,13 +741,13 @@ void spdk_bdev_part_base_hotremove(struct spdk_bdev *base_bdev, struct bdev_part
/**
 * Construct a new spdk_bdev_part_base on top of the provided bdev.
 *
 * \param base User allocated spdk_bdev_part_base to be filled by this function.
 * \param bdev The spdk_bdev upon which this base will be built.
 * \param remove_cb Function to be called upon hotremove of the bdev.
 * \param module The module to which this bdev base belongs.
 * \param fn_table Function table for communicating with the bdev backend.
 * \param tailq The head of the list of all spdk_bdev_part structures registered to this base's module.
 * \param free_fn User provided function to free base related context upon bdev removal or shutdown.
 * \param ctx Module specific context for this bdev part base.
 * \param channel_size Channel size in bytes.
 * \param ch_create_cb Called after a new channel is allocated.
 * \param ch_destroy_cb Called upon channel deletion.
@@ -759,12 +755,13 @@ void spdk_bdev_part_base_hotremove(struct spdk_bdev *base_bdev, struct bdev_part
 * \return 0 on success
 * \return -1 if the underlying bdev cannot be opened.
 */
int spdk_bdev_part_base_construct(struct spdk_bdev_part_base *base, struct spdk_bdev *bdev,
struct spdk_bdev_part_base *spdk_bdev_part_base_construct(struct spdk_bdev *bdev,
		spdk_bdev_remove_cb_t remove_cb,
		struct spdk_bdev_module *module,
		struct spdk_bdev_fn_table *fn_table,
		struct bdev_part_tailq *tailq,
		spdk_bdev_part_base_free_fn free_fn,
		void *ctx,
		uint32_t channel_size,
		spdk_io_channel_create_cb ch_create_cb,
		spdk_io_channel_destroy_cb ch_destroy_cb);
+12 −23
Original line number Diff line number Diff line
@@ -96,12 +96,6 @@ static struct spdk_bdev_module error_if = {

SPDK_BDEV_MODULE_REGISTER(&error_if)

static void
spdk_error_free_base(struct spdk_bdev_part_base *base)
{
	free(base);
}

int
spdk_vbdev_inject_error(char *name, uint32_t io_type, uint32_t error_type, uint32_t error_num)
{
@@ -212,7 +206,7 @@ static int
vbdev_error_destruct(void *ctx)
{
	struct error_disk *error_disk = ctx;
	struct spdk_bdev *base_bdev = error_disk->part.base->bdev;
	struct spdk_bdev *base_bdev = spdk_bdev_part_base_get_bdev(error_disk->part.base);
	int rc;

	rc = vbdev_error_config_remove(base_bdev->name);
@@ -227,12 +221,13 @@ static int
vbdev_error_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
{
	struct error_disk *error_disk = ctx;
	struct spdk_bdev *base_bdev = spdk_bdev_part_base_get_bdev(error_disk->part.base);

	spdk_json_write_name(w, "error_disk");
	spdk_json_write_object_begin(w);

	spdk_json_write_name(w, "base_bdev");
	spdk_json_write_string(w, error_disk->part.base->bdev->name);
	spdk_json_write_string(w, base_bdev->name);

	spdk_json_write_object_end(w);

@@ -267,33 +262,27 @@ _spdk_vbdev_error_create(struct spdk_bdev *base_bdev)
	char *name;
	int rc;

	base = calloc(1, sizeof(*base));
	if (!base) {
		SPDK_ERRLOG("Memory allocation failure\n");
		return -1;
	}

	rc = spdk_bdev_part_base_construct(base, base_bdev,
	base = spdk_bdev_part_base_construct(base_bdev,
					     spdk_vbdev_error_base_bdev_hotremove_cb,
					   &error_if, &vbdev_error_fn_table,
					   &g_error_disks, spdk_error_free_base,
					   sizeof(struct error_channel), NULL, NULL);
	if (rc) {
					     &error_if, &vbdev_error_fn_table, &g_error_disks,
					     NULL, NULL, sizeof(struct error_channel),
					     NULL, NULL);
	if (!base) {
		SPDK_ERRLOG("could not construct part base for bdev %s\n", spdk_bdev_get_name(base_bdev));
		return rc;
		return -ENOMEM;
	}

	disk = calloc(1, sizeof(*disk));
	if (!disk) {
		SPDK_ERRLOG("Memory allocation failure\n");
		spdk_error_free_base(base);
		spdk_bdev_part_base_free(base);
		return -ENOMEM;
	}

	name = spdk_sprintf_alloc("EE_%s", spdk_bdev_get_name(base_bdev));
	if (!name) {
		SPDK_ERRLOG("name allocation failure\n");
		spdk_error_free_base(base);
		spdk_bdev_part_base_free(base);
		free(disk);
		return -ENOMEM;
	}
@@ -303,7 +292,7 @@ _spdk_vbdev_error_create(struct spdk_bdev *base_bdev)
	if (rc) {
		SPDK_ERRLOG("could not construct part for bdev %s\n", spdk_bdev_get_name(base_bdev));
		/* spdk_bdev_part_construct will free name on failure */
		spdk_error_free_base(base);
		spdk_bdev_part_base_free(base);
		free(disk);
		return rc;
	}
+28 −23
Original line number Diff line number Diff line
@@ -62,8 +62,8 @@ SPDK_BDEV_MODULE_REGISTER(&gpt_if)

/* Base block device gpt context */
struct gpt_base {
	struct spdk_bdev_part_base	part_base;
	struct spdk_gpt			gpt;
	struct spdk_bdev_part_base	*part_base;

	/* This channel is only used for reading the partition table. */
	struct spdk_io_channel		*ch;
@@ -84,9 +84,9 @@ static SPDK_BDEV_PART_TAILQ g_gpt_disks = TAILQ_HEAD_INITIALIZER(g_gpt_disks);
static bool g_gpt_disabled;

static void
spdk_gpt_base_free(struct spdk_bdev_part_base *base)
spdk_gpt_base_free(void *ctx)
{
	struct gpt_base *gpt_base = SPDK_CONTAINEROF(base, struct gpt_base, part_base);
	struct gpt_base *gpt_base = ctx;

	spdk_dma_free(gpt_base->gpt.buf);
	free(gpt_base);
@@ -113,7 +113,6 @@ spdk_gpt_base_bdev_init(struct spdk_bdev *bdev)
{
	struct gpt_base *gpt_base;
	struct spdk_gpt *gpt;
	int rc;

	gpt_base = calloc(1, sizeof(*gpt_base));
	if (!gpt_base) {
@@ -121,12 +120,13 @@ spdk_gpt_base_bdev_init(struct spdk_bdev *bdev)
		return NULL;
	}

	rc = spdk_bdev_part_base_construct(&gpt_base->part_base, bdev,
	gpt_base->part_base = spdk_bdev_part_base_construct(bdev,
			      spdk_gpt_base_bdev_hotremove_cb,
			      &gpt_if, &vbdev_gpt_fn_table,
					   &g_gpt_disks, spdk_gpt_base_free,
			      &g_gpt_disks, spdk_gpt_base_free, gpt_base,
			      sizeof(struct gpt_channel), NULL, NULL);
	if (rc) {
	if (!gpt_base->part_base) {
		free(gpt_base);
		SPDK_ERRLOG("cannot construct gpt_base");
		return NULL;
	}
@@ -136,7 +136,7 @@ spdk_gpt_base_bdev_init(struct spdk_bdev *bdev)
	gpt->buf = spdk_dma_zmalloc(gpt->buf_size, spdk_bdev_get_buf_align(bdev), NULL);
	if (!gpt->buf) {
		SPDK_ERRLOG("Cannot alloc buf\n");
		spdk_bdev_part_base_free(&gpt_base->part_base);
		spdk_bdev_part_base_free(gpt_base->part_base);
		return NULL;
	}

@@ -192,8 +192,9 @@ write_string_utf16le(struct spdk_json_write_ctx *w, const uint16_t *str, size_t
static int
vbdev_gpt_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
{
	struct gpt_disk *gpt_disk = ctx;
	struct gpt_base *gpt_base = (struct gpt_base *)gpt_disk->part.base;
	struct gpt_disk *gpt_disk = SPDK_CONTAINEROF(ctx, struct gpt_disk, part);
	struct gpt_base *gpt_base = spdk_bdev_part_base_get_ctx(gpt_disk->part.base);
	struct spdk_bdev *part_base_bdev = spdk_bdev_part_base_get_bdev(gpt_disk->part.base);
	struct spdk_gpt *gpt = &gpt_base->gpt;
	struct spdk_gpt_partition_entry *gpt_entry = &gpt->partitions[gpt_disk->partition_index];

@@ -201,7 +202,7 @@ vbdev_gpt_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
	spdk_json_write_object_begin(w);

	spdk_json_write_name(w, "base_bdev");
	spdk_json_write_string(w, spdk_bdev_get_name(gpt_disk->part.base->bdev));
	spdk_json_write_string(w, spdk_bdev_get_name(part_base_bdev));

	spdk_json_write_name(w, "offset_blocks");
	spdk_json_write_uint64(w, gpt_disk->part.offset_blocks);
@@ -258,7 +259,7 @@ vbdev_gpt_create_bdevs(struct gpt_base *gpt_base)
		}

		/* index start at 1 instead of 0 to match the existing style */
		base_bdev = gpt_base->part_base.bdev;
		base_bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base);
		name = spdk_sprintf_alloc("%sp%" PRIu64, spdk_bdev_get_name(base_bdev), i + 1);
		if (!name) {
			SPDK_ERRLOG("name allocation failure\n");
@@ -266,7 +267,7 @@ vbdev_gpt_create_bdevs(struct gpt_base *gpt_base)
			return -1;
		}

		rc = spdk_bdev_part_construct(&d->part, &gpt_base->part_base, name,
		rc = spdk_bdev_part_construct(&d->part, gpt_base->part_base, name,
					      lba_start, lba_end - lba_start, "GPT Disk");
		if (rc) {
			SPDK_ERRLOG("could not construct bdev part\n");
@@ -285,7 +286,8 @@ static void
spdk_gpt_bdev_complete(struct spdk_bdev_io *bdev_io, bool status, void *arg)
{
	struct gpt_base *gpt_base = (struct gpt_base *)arg;
	struct spdk_bdev *bdev = gpt_base->part_base.bdev;
	struct spdk_bdev *bdev = spdk_bdev_part_base_get_bdev(gpt_base->part_base);
	uint32_t part_base_ref;
	int rc;

	spdk_bdev_free_io(bdev_io);
@@ -316,10 +318,11 @@ end:
	 *  callback are now completed.
	 */
	spdk_bdev_module_examine_done(&gpt_if);
	part_base_ref = spdk_bdev_part_base_get_ref(gpt_base->part_base);

	if (gpt_base->part_base.ref == 0) {
	if (part_base_ref == 0) {
		/* If no gpt_disk instances were created, free the base context */
		spdk_bdev_part_base_free(&gpt_base->part_base);
		spdk_bdev_part_base_free(gpt_base->part_base);
	}
}

@@ -327,6 +330,7 @@ static int
vbdev_gpt_read_gpt(struct spdk_bdev *bdev)
{
	struct gpt_base *gpt_base;
	struct spdk_bdev_desc *part_base_desc;
	int rc;

	gpt_base = spdk_gpt_base_bdev_init(bdev);
@@ -335,18 +339,19 @@ vbdev_gpt_read_gpt(struct spdk_bdev *bdev)
		return -1;
	}

	gpt_base->ch = spdk_bdev_get_io_channel(gpt_base->part_base.desc);
	part_base_desc = spdk_bdev_part_base_get_desc(gpt_base->part_base);
	gpt_base->ch = spdk_bdev_get_io_channel(part_base_desc);
	if (gpt_base->ch == NULL) {
		SPDK_ERRLOG("Failed to get an io_channel.\n");
		spdk_bdev_part_base_free(&gpt_base->part_base);
		spdk_bdev_part_base_free(gpt_base->part_base);
		return -1;
	}

	rc = spdk_bdev_read(gpt_base->part_base.desc, gpt_base->ch, gpt_base->gpt.buf, 0,
	rc = spdk_bdev_read(part_base_desc, gpt_base->ch, gpt_base->gpt.buf, 0,
			    gpt_base->gpt.buf_size, spdk_gpt_bdev_complete, gpt_base);
	if (rc < 0) {
		spdk_put_io_channel(gpt_base->ch);
		spdk_bdev_part_base_free(&gpt_base->part_base);
		spdk_bdev_part_base_free(gpt_base->part_base);
		SPDK_ERRLOG("Failed to send bdev_io command\n");
		return -1;
	}
+64 −7
Original line number Diff line number Diff line
@@ -40,6 +40,51 @@

#include "spdk/bdev_module.h"

struct spdk_bdev_part_base {
	struct spdk_bdev		*bdev;
	struct spdk_bdev_desc		*desc;
	uint32_t			ref;
	uint32_t			channel_size;
	spdk_bdev_part_base_free_fn	base_free_fn;
	void				*ctx;
	bool				claimed;
	struct spdk_bdev_module		*module;
	struct spdk_bdev_fn_table	*fn_table;
	struct bdev_part_tailq		*tailq;
	spdk_io_channel_create_cb	ch_create_cb;
	spdk_io_channel_destroy_cb	ch_destroy_cb;
};

struct spdk_bdev *
spdk_bdev_part_base_get_bdev(struct spdk_bdev_part_base *part_base)
{
	return part_base->bdev;
}

struct spdk_bdev_desc *
spdk_bdev_part_base_get_desc(struct spdk_bdev_part_base *part_base)
{
	return part_base->desc;
}

struct bdev_part_tailq *
spdk_bdev_part_base_get_tailq(struct spdk_bdev_part_base *part_base)
{
	return part_base->tailq;
}

void *
spdk_bdev_part_base_get_ctx(struct spdk_bdev_part_base *part_base)
{
	return part_base->ctx;
}

uint32_t
spdk_bdev_part_base_get_ref(struct spdk_bdev_part_base *part_base)
{
	return part_base->ref;
}

void
spdk_bdev_part_base_free(struct spdk_bdev_part_base *base)
{
@@ -47,7 +92,12 @@ spdk_bdev_part_base_free(struct spdk_bdev_part_base *base)
		spdk_bdev_close(base->desc);
		base->desc = NULL;
	}
	base->base_free_fn(base);

	if (base->base_free_fn != NULL) {
		base->base_free_fn(base->ctx);
	}

	free(base);
}

static void
@@ -206,16 +256,22 @@ spdk_bdev_part_channel_destroy_cb(void *io_device, void *ctx_buf)
	spdk_put_io_channel(ch->base_ch);
}

int
spdk_bdev_part_base_construct(struct spdk_bdev_part_base *base, struct spdk_bdev *bdev,
struct spdk_bdev_part_base *
	spdk_bdev_part_base_construct(struct spdk_bdev *bdev,
			      spdk_bdev_remove_cb_t remove_cb, struct spdk_bdev_module *module,
			      struct spdk_bdev_fn_table *fn_table, struct bdev_part_tailq *tailq,
			      spdk_bdev_part_base_free_fn free_fn,
			      spdk_bdev_part_base_free_fn free_fn, void *ctx,
			      uint32_t channel_size, spdk_io_channel_create_cb ch_create_cb,
			      spdk_io_channel_destroy_cb ch_destroy_cb)
{
	int rc;
	struct spdk_bdev_part_base *base;

	base = calloc(1, sizeof(*base));
	if (!base) {
		SPDK_ERRLOG("Memory allocation failure\n");
		return NULL;
	}
	fn_table->get_io_channel = spdk_bdev_part_get_io_channel;
	fn_table->io_type_supported = spdk_bdev_part_io_type_supported;

@@ -225,20 +281,21 @@ spdk_bdev_part_base_construct(struct spdk_bdev_part_base *base, struct spdk_bdev
	base->module = module;
	base->fn_table = fn_table;
	base->tailq = tailq;
	base->base_free_fn = free_fn;
	base->ctx = ctx;
	base->claimed = false;
	base->channel_size = channel_size;
	base->ch_create_cb = ch_create_cb;
	base->ch_destroy_cb = ch_destroy_cb;
	base->base_free_fn = free_fn;

	rc = spdk_bdev_open(bdev, false, remove_cb, bdev, &base->desc);
	if (rc) {
		spdk_bdev_part_base_free(base);
		SPDK_ERRLOG("could not open bdev %s\n", spdk_bdev_get_name(bdev));
		return -1;
		return NULL;
	}

	return 0;
	return base;
}

int
+27 −17
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ struct spdk_vbdev_split_config {
	unsigned split_count;
	uint64_t split_size_mb;

	struct spdk_bdev_part_base split_base;
	struct spdk_bdev_part_base *split_base;
	bool removed;

	TAILQ_ENTRY(spdk_vbdev_split_config) tailq;
@@ -85,11 +85,11 @@ static struct spdk_bdev_module split_if = {
SPDK_BDEV_MODULE_REGISTER(&split_if)

static void
vbdev_split_base_free(struct spdk_bdev_part_base *base)
vbdev_split_base_free(void *ctx)
{
	struct spdk_vbdev_split_config *cfg = SPDK_CONTAINEROF(base, struct spdk_vbdev_split_config,
					      split_base);
	struct spdk_vbdev_split_config *cfg = ctx;

	cfg->split_base = NULL;
	if (cfg->removed) {
		vbdev_split_del_config(cfg);
	}
@@ -121,12 +121,13 @@ static int
vbdev_split_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
{
	struct spdk_bdev_part *part = ctx;
	struct spdk_bdev *split_base_bdev = spdk_bdev_part_base_get_bdev(part->base);

	spdk_json_write_name(w, "split");
	spdk_json_write_object_begin(w);

	spdk_json_write_name(w, "base_bdev");
	spdk_json_write_string(w, spdk_bdev_get_name(part->base->bdev));
	spdk_json_write_string(w, spdk_bdev_get_name(split_base_bdev));
	spdk_json_write_name(w, "offset_blocks");
	spdk_json_write_uint64(w, part->offset_blocks);

@@ -158,6 +159,8 @@ vbdev_split_create(struct spdk_vbdev_split_config *cfg)
	int rc;
	char *name;
	struct spdk_bdev *base_bdev;
	struct spdk_bdev *split_base_bdev;
	struct bdev_part_tailq *split_base_tailq;

	assert(cfg->split_count > 0);

@@ -193,14 +196,14 @@ vbdev_split_create(struct spdk_vbdev_split_config *cfg)
		      " split_size_blocks: %" PRIu64 "\n",
		      spdk_bdev_get_name(base_bdev), split_count, split_size_blocks);

	rc = spdk_bdev_part_base_construct(&cfg->split_base, base_bdev,
	cfg->split_base = spdk_bdev_part_base_construct(base_bdev,
			  vbdev_split_base_bdev_hotremove_cb,
			  &split_if, &vbdev_split_fn_table,
					   &g_split_disks, vbdev_split_base_free,
			  &g_split_disks, vbdev_split_base_free, cfg,
			  sizeof(struct vbdev_split_channel), NULL, NULL);
	if (rc) {
	if (!cfg->split_base) {
		SPDK_ERRLOG("Cannot construct bdev part base\n");
		return rc;
		return -ENOMEM;
	}

	offset_blocks = 0;
@@ -222,7 +225,7 @@ vbdev_split_create(struct spdk_vbdev_split_config *cfg)
			goto err;
		}

		rc = spdk_bdev_part_construct(d, &cfg->split_base, name, offset_blocks, split_size_blocks,
		rc = spdk_bdev_part_construct(d, cfg->split_base, name, offset_blocks, split_size_blocks,
					      "Split Disk");
		if (rc) {
			SPDK_ERRLOG("could not construct bdev part\n");
@@ -237,8 +240,10 @@ vbdev_split_create(struct spdk_vbdev_split_config *cfg)

	return 0;
err:
	split_base_bdev = spdk_bdev_part_base_get_bdev(cfg->split_base);
	split_base_tailq = spdk_bdev_part_base_get_tailq(cfg->split_base);
	cfg->removed = true;
	spdk_bdev_part_base_hotremove(cfg->split_base.bdev, cfg->split_base.tailq);
	spdk_bdev_part_base_hotremove(split_base_bdev, split_base_tailq);
	return rc;
}

@@ -253,9 +258,14 @@ vbdev_split_del_config(struct spdk_vbdev_split_config *cfg)
static void
vbdev_split_destruct_config(struct spdk_vbdev_split_config *cfg)
{
	struct spdk_bdev *split_base_bdev;
	struct bdev_part_tailq *split_base_tailq;

	cfg->removed = true;
	if (cfg->split_base.ref) {
		spdk_bdev_part_base_hotremove(cfg->split_base.bdev, cfg->split_base.tailq);
	if (cfg->split_base != NULL && spdk_bdev_part_base_get_ref(cfg->split_base)) {
		split_base_bdev = spdk_bdev_part_base_get_bdev(cfg->split_base);
		split_base_tailq = spdk_bdev_part_base_get_tailq(cfg->split_base);
		spdk_bdev_part_base_hotremove(split_base_bdev, split_base_tailq);
	} else {
		vbdev_split_del_config(cfg);
	}
@@ -409,7 +419,7 @@ vbdev_split_examine(struct spdk_bdev *bdev)
	struct spdk_vbdev_split_config *cfg = vbdev_split_config_find_by_base_name(bdev->name);

	if (cfg != NULL && cfg->removed == false) {
		assert(cfg->split_base.ref == 0);
		assert(cfg->split_base == NULL || spdk_bdev_part_base_get_ref(cfg->split_base) == 0);

		if (vbdev_split_create(cfg)) {
			SPDK_ERRLOG("could not split bdev %s\n", bdev->name);
Loading