2018-02-26 20:22:38 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Common code for partition-like virtual bdevs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spdk/bdev.h"
|
|
|
|
#include "spdk/log.h"
|
2018-08-30 20:26:50 +00:00
|
|
|
#include "spdk/string.h"
|
2018-02-26 20:22:38 +00:00
|
|
|
|
2018-05-23 21:01:03 +00:00
|
|
|
#include "spdk/bdev_module.h"
|
2018-02-26 20:22:38 +00:00
|
|
|
|
2018-06-21 22:23:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-26 20:22:38 +00:00
|
|
|
void
|
|
|
|
spdk_bdev_part_base_free(struct spdk_bdev_part_base *base)
|
|
|
|
{
|
|
|
|
if (base->desc) {
|
|
|
|
spdk_bdev_close(base->desc);
|
|
|
|
base->desc = NULL;
|
|
|
|
}
|
2018-06-21 22:23:42 +00:00
|
|
|
|
|
|
|
if (base->base_free_fn != NULL) {
|
|
|
|
base->base_free_fn(base->ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(base);
|
2018-02-26 20:22:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 21:45:53 +00:00
|
|
|
static void
|
|
|
|
spdk_bdev_part_free_cb(void *io_device)
|
2018-02-26 20:22:38 +00:00
|
|
|
{
|
2018-04-16 21:45:53 +00:00
|
|
|
struct spdk_bdev_part *part = io_device;
|
2018-02-26 20:22:38 +00:00
|
|
|
struct spdk_bdev_part_base *base;
|
|
|
|
|
|
|
|
assert(part);
|
2018-06-28 17:37:00 +00:00
|
|
|
assert(part->internal.base);
|
2018-02-26 20:22:38 +00:00
|
|
|
|
2018-06-28 17:37:00 +00:00
|
|
|
base = part->internal.base;
|
2018-04-16 21:45:53 +00:00
|
|
|
|
2018-02-26 20:22:38 +00:00
|
|
|
TAILQ_REMOVE(base->tailq, part, tailq);
|
|
|
|
|
|
|
|
if (__sync_sub_and_fetch(&base->ref, 1) == 0) {
|
|
|
|
spdk_bdev_module_release_bdev(base->bdev);
|
|
|
|
spdk_bdev_part_base_free(base);
|
|
|
|
}
|
2018-04-16 21:45:53 +00:00
|
|
|
|
2018-06-28 17:37:00 +00:00
|
|
|
spdk_bdev_destruct_done(&part->internal.bdev, 0);
|
|
|
|
free(part->internal.bdev.name);
|
2018-08-30 14:59:10 +00:00
|
|
|
free(part->internal.bdev.product_name);
|
2018-04-16 21:45:53 +00:00
|
|
|
free(part);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_bdev_part_free(struct spdk_bdev_part *part)
|
|
|
|
{
|
|
|
|
spdk_io_device_unregister(part, spdk_bdev_part_free_cb);
|
|
|
|
|
|
|
|
/* Return 1 to indicate that this is an asynchronous operation that isn't complete
|
|
|
|
* until spdk_bdev_destruct_done is called */
|
|
|
|
return 1;
|
2018-02-26 20:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-26 13:07:28 +00:00
|
|
|
spdk_bdev_part_base_hotremove(struct spdk_bdev_part_base *part_base, struct bdev_part_tailq *tailq)
|
2018-02-26 20:22:38 +00:00
|
|
|
{
|
|
|
|
struct spdk_bdev_part *part, *tmp;
|
|
|
|
|
|
|
|
TAILQ_FOREACH_SAFE(part, tailq, tailq, tmp) {
|
2018-11-26 13:07:28 +00:00
|
|
|
if (part->internal.base == part_base) {
|
2018-06-28 17:37:00 +00:00
|
|
|
spdk_bdev_unregister(&part->internal.bdev, NULL, NULL);
|
2018-02-26 20:22:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
spdk_bdev_part_io_type_supported(void *_part, enum spdk_bdev_io_type io_type)
|
|
|
|
{
|
|
|
|
struct spdk_bdev_part *part = _part;
|
|
|
|
|
2018-09-21 01:30:58 +00:00
|
|
|
return part->internal.base->bdev->fn_table->io_type_supported(part->internal.base->bdev->ctxt,
|
|
|
|
io_type);
|
2018-02-26 20:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct spdk_io_channel *
|
|
|
|
spdk_bdev_part_get_io_channel(void *_part)
|
|
|
|
{
|
|
|
|
struct spdk_bdev_part *part = _part;
|
|
|
|
|
|
|
|
return spdk_get_io_channel(part);
|
|
|
|
}
|
|
|
|
|
2018-06-28 17:37:00 +00:00
|
|
|
struct spdk_bdev *
|
|
|
|
spdk_bdev_part_get_bdev(struct spdk_bdev_part *part)
|
|
|
|
{
|
|
|
|
return &part->internal.bdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spdk_bdev_part_base *
|
|
|
|
spdk_bdev_part_get_base(struct spdk_bdev_part *part)
|
|
|
|
{
|
|
|
|
return part->internal.base;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spdk_bdev *
|
|
|
|
spdk_bdev_part_get_base_bdev(struct spdk_bdev_part *part)
|
|
|
|
{
|
|
|
|
return part->internal.base->bdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
spdk_bdev_part_get_offset_blocks(struct spdk_bdev_part *part)
|
|
|
|
{
|
|
|
|
return part->internal.offset_blocks;
|
|
|
|
}
|
|
|
|
|
2018-02-26 20:22:38 +00:00
|
|
|
static void
|
|
|
|
spdk_bdev_part_complete_io(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
|
|
|
|
{
|
|
|
|
struct spdk_bdev_io *part_io = cb_arg;
|
|
|
|
int status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
|
|
|
|
|
|
|
|
spdk_bdev_io_complete(part_io, status);
|
|
|
|
spdk_bdev_free_io(bdev_io);
|
|
|
|
}
|
|
|
|
|
2018-09-25 03:26:06 +00:00
|
|
|
int
|
2018-02-26 20:22:38 +00:00
|
|
|
spdk_bdev_part_submit_request(struct spdk_bdev_part_channel *ch, struct spdk_bdev_io *bdev_io)
|
|
|
|
{
|
|
|
|
struct spdk_bdev_part *part = ch->part;
|
|
|
|
struct spdk_io_channel *base_ch = ch->base_ch;
|
2018-06-28 17:37:00 +00:00
|
|
|
struct spdk_bdev_desc *base_desc = part->internal.base->desc;
|
2018-02-26 20:22:38 +00:00
|
|
|
uint64_t offset;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
/* Modify the I/O to adjust for the offset within the base bdev. */
|
|
|
|
switch (bdev_io->type) {
|
|
|
|
case SPDK_BDEV_IO_TYPE_READ:
|
2018-06-28 17:37:00 +00:00
|
|
|
offset = bdev_io->u.bdev.offset_blocks + part->internal.offset_blocks;
|
2018-02-26 20:22:38 +00:00
|
|
|
rc = spdk_bdev_readv_blocks(base_desc, base_ch, bdev_io->u.bdev.iovs,
|
|
|
|
bdev_io->u.bdev.iovcnt, offset,
|
|
|
|
bdev_io->u.bdev.num_blocks, spdk_bdev_part_complete_io,
|
|
|
|
bdev_io);
|
|
|
|
break;
|
|
|
|
case SPDK_BDEV_IO_TYPE_WRITE:
|
2018-06-28 17:37:00 +00:00
|
|
|
offset = bdev_io->u.bdev.offset_blocks + part->internal.offset_blocks;
|
2018-02-26 20:22:38 +00:00
|
|
|
rc = spdk_bdev_writev_blocks(base_desc, base_ch, bdev_io->u.bdev.iovs,
|
|
|
|
bdev_io->u.bdev.iovcnt, offset,
|
|
|
|
bdev_io->u.bdev.num_blocks, spdk_bdev_part_complete_io,
|
|
|
|
bdev_io);
|
|
|
|
break;
|
|
|
|
case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
|
2018-06-28 17:37:00 +00:00
|
|
|
offset = bdev_io->u.bdev.offset_blocks + part->internal.offset_blocks;
|
2018-02-26 20:22:38 +00:00
|
|
|
rc = spdk_bdev_write_zeroes_blocks(base_desc, base_ch, offset, bdev_io->u.bdev.num_blocks,
|
|
|
|
spdk_bdev_part_complete_io, bdev_io);
|
|
|
|
break;
|
|
|
|
case SPDK_BDEV_IO_TYPE_UNMAP:
|
2018-06-28 17:37:00 +00:00
|
|
|
offset = bdev_io->u.bdev.offset_blocks + part->internal.offset_blocks;
|
2018-02-26 20:22:38 +00:00
|
|
|
rc = spdk_bdev_unmap_blocks(base_desc, base_ch, offset, bdev_io->u.bdev.num_blocks,
|
|
|
|
spdk_bdev_part_complete_io, bdev_io);
|
|
|
|
break;
|
|
|
|
case SPDK_BDEV_IO_TYPE_FLUSH:
|
2018-06-28 17:37:00 +00:00
|
|
|
offset = bdev_io->u.bdev.offset_blocks + part->internal.offset_blocks;
|
2018-02-26 20:22:38 +00:00
|
|
|
rc = spdk_bdev_flush_blocks(base_desc, base_ch, offset, bdev_io->u.bdev.num_blocks,
|
|
|
|
spdk_bdev_part_complete_io, bdev_io);
|
|
|
|
break;
|
|
|
|
case SPDK_BDEV_IO_TYPE_RESET:
|
|
|
|
rc = spdk_bdev_reset(base_desc, base_ch,
|
|
|
|
spdk_bdev_part_complete_io, bdev_io);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SPDK_ERRLOG("split: unknown I/O type %d\n", bdev_io->type);
|
2018-09-25 03:26:06 +00:00
|
|
|
return SPDK_BDEV_IO_STATUS_FAILED;
|
2018-02-26 20:22:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 03:26:06 +00:00
|
|
|
return rc;
|
2018-02-26 20:22:38 +00:00
|
|
|
}
|
2018-09-25 03:26:06 +00:00
|
|
|
|
2018-02-26 20:22:38 +00:00
|
|
|
static int
|
|
|
|
spdk_bdev_part_channel_create_cb(void *io_device, void *ctx_buf)
|
|
|
|
{
|
|
|
|
struct spdk_bdev_part *part = (struct spdk_bdev_part *)io_device;
|
|
|
|
struct spdk_bdev_part_channel *ch = ctx_buf;
|
|
|
|
|
|
|
|
ch->part = part;
|
2018-06-28 17:37:00 +00:00
|
|
|
ch->base_ch = spdk_bdev_get_io_channel(part->internal.base->desc);
|
2018-02-26 20:22:38 +00:00
|
|
|
if (ch->base_ch == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-06-28 17:37:00 +00:00
|
|
|
if (part->internal.base->ch_create_cb) {
|
|
|
|
return part->internal.base->ch_create_cb(io_device, ctx_buf);
|
2018-02-26 20:22:38 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spdk_bdev_part_channel_destroy_cb(void *io_device, void *ctx_buf)
|
|
|
|
{
|
|
|
|
struct spdk_bdev_part *part = (struct spdk_bdev_part *)io_device;
|
|
|
|
struct spdk_bdev_part_channel *ch = ctx_buf;
|
|
|
|
|
2018-06-28 17:37:00 +00:00
|
|
|
if (part->internal.base->ch_destroy_cb) {
|
|
|
|
part->internal.base->ch_destroy_cb(io_device, ctx_buf);
|
2018-02-26 20:22:38 +00:00
|
|
|
}
|
|
|
|
spdk_put_io_channel(ch->base_ch);
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:23:42 +00:00
|
|
|
struct spdk_bdev_part_base *
|
|
|
|
spdk_bdev_part_base_construct(struct spdk_bdev *bdev,
|
2018-03-09 22:20:21 +00:00
|
|
|
spdk_bdev_remove_cb_t remove_cb, struct spdk_bdev_module *module,
|
2018-02-26 20:22:38 +00:00
|
|
|
struct spdk_bdev_fn_table *fn_table, struct bdev_part_tailq *tailq,
|
2018-06-21 22:23:42 +00:00
|
|
|
spdk_bdev_part_base_free_fn free_fn, void *ctx,
|
2018-02-26 20:22:38 +00:00
|
|
|
uint32_t channel_size, spdk_io_channel_create_cb ch_create_cb,
|
|
|
|
spdk_io_channel_destroy_cb ch_destroy_cb)
|
|
|
|
{
|
|
|
|
int rc;
|
2018-06-21 22:23:42 +00:00
|
|
|
struct spdk_bdev_part_base *base;
|
2018-02-26 20:22:38 +00:00
|
|
|
|
2018-06-21 22:23:42 +00:00
|
|
|
base = calloc(1, sizeof(*base));
|
|
|
|
if (!base) {
|
|
|
|
SPDK_ERRLOG("Memory allocation failure\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-26 20:22:38 +00:00
|
|
|
fn_table->get_io_channel = spdk_bdev_part_get_io_channel;
|
|
|
|
fn_table->io_type_supported = spdk_bdev_part_io_type_supported;
|
|
|
|
|
|
|
|
base->bdev = bdev;
|
|
|
|
base->desc = NULL;
|
|
|
|
base->ref = 0;
|
|
|
|
base->module = module;
|
|
|
|
base->fn_table = fn_table;
|
|
|
|
base->tailq = tailq;
|
2018-06-21 22:23:42 +00:00
|
|
|
base->base_free_fn = free_fn;
|
|
|
|
base->ctx = ctx;
|
2018-02-26 20:22:38 +00:00
|
|
|
base->claimed = false;
|
|
|
|
base->channel_size = channel_size;
|
|
|
|
base->ch_create_cb = ch_create_cb;
|
|
|
|
base->ch_destroy_cb = ch_destroy_cb;
|
|
|
|
|
2018-11-26 12:45:53 +00:00
|
|
|
rc = spdk_bdev_open(bdev, false, remove_cb, base, &base->desc);
|
2018-02-26 20:22:38 +00:00
|
|
|
if (rc) {
|
|
|
|
spdk_bdev_part_base_free(base);
|
2019-01-19 21:50:08 +00:00
|
|
|
SPDK_ERRLOG("could not open bdev %s: %s\n", spdk_bdev_get_name(bdev),
|
|
|
|
spdk_strerror(-rc));
|
2018-06-21 22:23:42 +00:00
|
|
|
return NULL;
|
2018-02-26 20:22:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 22:23:42 +00:00
|
|
|
return base;
|
2018-02-26 20:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_bdev_part_construct(struct spdk_bdev_part *part, struct spdk_bdev_part_base *base,
|
|
|
|
char *name, uint64_t offset_blocks, uint64_t num_blocks,
|
|
|
|
char *product_name)
|
|
|
|
{
|
2018-06-28 17:37:00 +00:00
|
|
|
part->internal.bdev.blocklen = base->bdev->blocklen;
|
|
|
|
part->internal.bdev.blockcnt = num_blocks;
|
|
|
|
part->internal.offset_blocks = offset_blocks;
|
|
|
|
|
|
|
|
part->internal.bdev.write_cache = base->bdev->write_cache;
|
2018-10-24 07:02:31 +00:00
|
|
|
part->internal.bdev.required_alignment = base->bdev->required_alignment;
|
2018-06-28 17:37:00 +00:00
|
|
|
part->internal.bdev.ctxt = part;
|
|
|
|
part->internal.bdev.module = base->module;
|
|
|
|
part->internal.bdev.fn_table = base->fn_table;
|
2018-02-26 20:22:38 +00:00
|
|
|
|
2018-08-30 14:59:10 +00:00
|
|
|
part->internal.bdev.name = strdup(name);
|
|
|
|
part->internal.bdev.product_name = strdup(product_name);
|
|
|
|
|
|
|
|
if (part->internal.bdev.name == NULL) {
|
|
|
|
SPDK_ERRLOG("Failed to allocate name for new part of bdev %s\n", spdk_bdev_get_name(base->bdev));
|
|
|
|
return -1;
|
|
|
|
} else if (part->internal.bdev.product_name == NULL) {
|
|
|
|
free(part->internal.bdev.name);
|
|
|
|
SPDK_ERRLOG("Failed to allocate product name for new part of bdev %s\n",
|
|
|
|
spdk_bdev_get_name(base->bdev));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-26 20:22:38 +00:00
|
|
|
__sync_fetch_and_add(&base->ref, 1);
|
2018-06-28 17:37:00 +00:00
|
|
|
part->internal.base = base;
|
2018-02-26 20:22:38 +00:00
|
|
|
|
|
|
|
if (!base->claimed) {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = spdk_bdev_module_claim_bdev(base->bdev, base->desc, base->module);
|
|
|
|
if (rc) {
|
|
|
|
SPDK_ERRLOG("could not claim bdev %s\n", spdk_bdev_get_name(base->bdev));
|
2018-06-28 17:37:00 +00:00
|
|
|
free(part->internal.bdev.name);
|
2018-08-30 14:59:10 +00:00
|
|
|
free(part->internal.bdev.product_name);
|
2018-02-26 20:22:38 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
base->claimed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
spdk_io_device_register(part, spdk_bdev_part_channel_create_cb,
|
|
|
|
spdk_bdev_part_channel_destroy_cb,
|
2018-08-30 20:26:50 +00:00
|
|
|
base->channel_size,
|
|
|
|
name);
|
|
|
|
|
2019-04-03 12:59:05 +00:00
|
|
|
spdk_bdev_register(&part->internal.bdev);
|
2018-02-26 20:22:38 +00:00
|
|
|
TAILQ_INSERT_TAIL(base->tailq, part, tailq);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|