2018-10-29 12:17:34 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spdk/stdinc.h"
|
|
|
|
#include "spdk/ftl.h"
|
2019-03-22 15:45:46 +00:00
|
|
|
#include "spdk/likely.h"
|
2019-05-15 12:02:00 +00:00
|
|
|
#include "spdk/util.h"
|
2018-10-29 12:17:34 +00:00
|
|
|
|
|
|
|
#include "ftl_io.h"
|
|
|
|
#include "ftl_core.h"
|
|
|
|
#include "ftl_rwb.h"
|
|
|
|
#include "ftl_band.h"
|
|
|
|
|
2019-03-26 10:12:18 +00:00
|
|
|
void
|
2018-10-29 12:17:34 +00:00
|
|
|
ftl_io_inc_req(struct ftl_io *io)
|
|
|
|
{
|
|
|
|
struct ftl_band *band = io->band;
|
|
|
|
|
2019-04-05 11:31:19 +00:00
|
|
|
if (!(io->flags & FTL_IO_CACHE) && io->type != FTL_IO_READ && io->type != FTL_IO_ERASE) {
|
2019-04-26 14:53:13 +00:00
|
|
|
ftl_band_acquire_lba_map(band);
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__atomic_fetch_add(&io->dev->num_inflight, 1, __ATOMIC_SEQ_CST);
|
|
|
|
|
2019-03-26 10:12:18 +00:00
|
|
|
++io->req_cnt;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 10:12:18 +00:00
|
|
|
void
|
2018-10-29 12:17:34 +00:00
|
|
|
ftl_io_dec_req(struct ftl_io *io)
|
|
|
|
{
|
|
|
|
struct ftl_band *band = io->band;
|
|
|
|
unsigned long num_inflight __attribute__((unused));
|
|
|
|
|
2019-04-05 11:31:19 +00:00
|
|
|
if (!(io->flags & FTL_IO_CACHE) && io->type != FTL_IO_READ && io->type != FTL_IO_ERASE) {
|
2019-04-26 14:53:13 +00:00
|
|
|
ftl_band_release_lba_map(band);
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
num_inflight = __atomic_fetch_sub(&io->dev->num_inflight, 1, __ATOMIC_SEQ_CST);
|
|
|
|
|
|
|
|
assert(num_inflight > 0);
|
|
|
|
assert(io->req_cnt > 0);
|
|
|
|
|
2019-03-26 10:12:18 +00:00
|
|
|
--io->req_cnt;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct iovec *
|
|
|
|
ftl_io_iovec(struct ftl_io *io)
|
|
|
|
{
|
2019-05-21 10:24:57 +00:00
|
|
|
return &io->iov[0];
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2019-04-05 12:08:42 +00:00
|
|
|
ftl_io_get_lba(const struct ftl_io *io, size_t offset)
|
2018-10-29 12:17:34 +00:00
|
|
|
{
|
2019-04-05 12:08:42 +00:00
|
|
|
assert(offset < io->lbk_cnt);
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
if (io->flags & FTL_IO_VECTOR_LBA) {
|
2019-04-05 12:08:42 +00:00
|
|
|
return io->lba.vector[offset];
|
2018-10-29 12:17:34 +00:00
|
|
|
} else {
|
2019-04-05 12:08:42 +00:00
|
|
|
return io->lba.single + offset;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 12:08:42 +00:00
|
|
|
uint64_t
|
|
|
|
ftl_io_current_lba(const struct ftl_io *io)
|
|
|
|
{
|
|
|
|
return ftl_io_get_lba(io, io->pos);
|
|
|
|
}
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
void
|
2019-04-04 14:45:22 +00:00
|
|
|
ftl_io_advance(struct ftl_io *io, size_t lbk_cnt)
|
2018-10-29 12:17:34 +00:00
|
|
|
{
|
|
|
|
struct iovec *iov = ftl_io_iovec(io);
|
2019-05-09 11:37:21 +00:00
|
|
|
size_t iov_lbks, lbk_left = lbk_cnt;
|
2018-10-29 12:17:34 +00:00
|
|
|
|
2019-03-25 15:02:16 +00:00
|
|
|
io->pos += lbk_cnt;
|
|
|
|
|
2019-05-09 11:37:21 +00:00
|
|
|
if (io->iov_cnt != 0) {
|
|
|
|
while (lbk_left > 0) {
|
|
|
|
assert(io->iov_pos < io->iov_cnt);
|
2019-05-22 07:06:07 +00:00
|
|
|
iov_lbks = iov[io->iov_pos].iov_len / FTL_BLOCK_SIZE;
|
2019-04-04 14:45:22 +00:00
|
|
|
|
2019-05-09 11:37:21 +00:00
|
|
|
if (io->iov_off + lbk_left < iov_lbks) {
|
|
|
|
io->iov_off += lbk_left;
|
|
|
|
break;
|
|
|
|
}
|
2018-10-29 12:17:34 +00:00
|
|
|
|
2019-05-09 11:37:21 +00:00
|
|
|
assert(iov_lbks > io->iov_off);
|
|
|
|
lbk_left -= (iov_lbks - io->iov_off);
|
|
|
|
io->iov_off = 0;
|
|
|
|
io->iov_pos++;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
2019-05-09 11:37:21 +00:00
|
|
|
}
|
2018-10-29 12:17:34 +00:00
|
|
|
|
2019-05-09 11:37:21 +00:00
|
|
|
if (io->parent) {
|
|
|
|
ftl_io_advance(io->parent, lbk_cnt);
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ftl_iovec_num_lbks(struct iovec *iov, size_t iov_cnt)
|
|
|
|
{
|
|
|
|
size_t lbks = 0, i = 0;
|
|
|
|
|
|
|
|
for (; i < iov_cnt; ++i) {
|
2019-05-22 07:06:07 +00:00
|
|
|
lbks += iov[i].iov_len / FTL_BLOCK_SIZE;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return lbks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
ftl_io_iovec_addr(struct ftl_io *io)
|
|
|
|
{
|
|
|
|
assert(io->iov_pos < io->iov_cnt);
|
2019-05-22 07:06:07 +00:00
|
|
|
assert(io->iov_off * FTL_BLOCK_SIZE < ftl_io_iovec(io)[io->iov_pos].iov_len);
|
2018-10-29 12:17:34 +00:00
|
|
|
|
|
|
|
return (char *)ftl_io_iovec(io)[io->iov_pos].iov_base +
|
2019-05-22 07:06:07 +00:00
|
|
|
io->iov_off * FTL_BLOCK_SIZE;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ftl_io_iovec_len_left(struct ftl_io *io)
|
|
|
|
{
|
|
|
|
struct iovec *iov = ftl_io_iovec(io);
|
2019-05-22 07:06:07 +00:00
|
|
|
return iov[io->iov_pos].iov_len / FTL_BLOCK_SIZE - io->iov_off;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:38:55 +00:00
|
|
|
static void
|
2019-05-15 12:02:00 +00:00
|
|
|
_ftl_io_init_iovec(struct ftl_io *io, const struct iovec *iov, size_t iov_cnt, size_t lbk_cnt)
|
2018-10-29 12:17:34 +00:00
|
|
|
{
|
2019-05-15 12:02:00 +00:00
|
|
|
size_t iov_off;
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
io->iov_pos = 0;
|
2019-05-15 12:02:00 +00:00
|
|
|
io->iov_cnt = iov_cnt;
|
2019-05-09 14:04:26 +00:00
|
|
|
io->lbk_cnt = lbk_cnt;
|
2018-10-29 12:17:34 +00:00
|
|
|
|
2019-05-15 12:02:00 +00:00
|
|
|
memcpy(io->iov, iov, iov_cnt * sizeof(*iov));
|
|
|
|
|
|
|
|
if (lbk_cnt == 0) {
|
|
|
|
for (iov_off = 0; iov_off < iov_cnt; ++iov_off) {
|
2019-05-22 07:06:07 +00:00
|
|
|
io->lbk_cnt += iov[iov_off].iov_len / FTL_BLOCK_SIZE;
|
2019-05-15 12:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _ftl_io_free(struct ftl_io *io);
|
|
|
|
|
|
|
|
static int
|
|
|
|
ftl_io_add_child(struct ftl_io *io, const struct iovec *iov, size_t iov_cnt)
|
|
|
|
{
|
|
|
|
struct ftl_io *child;
|
|
|
|
|
|
|
|
child = ftl_io_alloc_child(io);
|
|
|
|
if (spdk_unlikely(!child)) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ftl_io_init_iovec(child, iov, iov_cnt, 0);
|
|
|
|
|
|
|
|
if (io->flags & FTL_IO_VECTOR_LBA) {
|
|
|
|
child->lba.vector = io->lba.vector + io->lbk_cnt;
|
|
|
|
} else {
|
|
|
|
child->lba.single = io->lba.single + io->lbk_cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
io->lbk_cnt += child->lbk_cnt;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ftl_io_init_iovec(struct ftl_io *io, const struct iovec *iov, size_t iov_cnt, size_t lbk_cnt)
|
|
|
|
{
|
|
|
|
struct ftl_io *child;
|
|
|
|
size_t iov_off = 0, iov_left;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (spdk_likely(iov_cnt <= FTL_IO_MAX_IOVEC)) {
|
|
|
|
_ftl_io_init_iovec(io, iov, iov_cnt, lbk_cnt);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (iov_off < iov_cnt) {
|
|
|
|
iov_left = spdk_min(iov_cnt - iov_off, FTL_IO_MAX_IOVEC);
|
|
|
|
|
|
|
|
rc = ftl_io_add_child(io, &iov[iov_off], iov_left);
|
|
|
|
if (spdk_unlikely(rc != 0)) {
|
|
|
|
while ((child = LIST_FIRST(&io->children))) {
|
|
|
|
assert(LIST_EMPTY(&child->children));
|
|
|
|
LIST_REMOVE(child, child_entry);
|
|
|
|
_ftl_io_free(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
iov_off += iov_left;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(io->lbk_cnt == lbk_cnt);
|
|
|
|
return 0;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:38:55 +00:00
|
|
|
void
|
2019-05-09 14:04:26 +00:00
|
|
|
ftl_io_shrink_iovec(struct ftl_io *io, size_t lbk_cnt)
|
2019-04-09 12:38:55 +00:00
|
|
|
{
|
2019-05-15 12:02:00 +00:00
|
|
|
size_t iov_off = 0, lbk_off = 0;
|
|
|
|
|
2019-05-09 14:04:26 +00:00
|
|
|
assert(io->lbk_cnt >= lbk_cnt);
|
2019-04-09 12:38:55 +00:00
|
|
|
assert(io->pos == 0 && io->iov_pos == 0 && io->iov_off == 0);
|
|
|
|
|
2019-05-15 12:02:00 +00:00
|
|
|
for (; iov_off < io->iov_cnt; ++iov_off) {
|
2019-05-22 07:06:07 +00:00
|
|
|
size_t num_iov = io->iov[iov_off].iov_len / FTL_BLOCK_SIZE;
|
2019-05-15 12:02:00 +00:00
|
|
|
size_t num_left = lbk_cnt - lbk_off;
|
|
|
|
|
|
|
|
if (num_iov >= num_left) {
|
2019-05-22 07:06:07 +00:00
|
|
|
io->iov[iov_off].iov_len = num_left * FTL_BLOCK_SIZE;
|
2019-05-15 12:02:00 +00:00
|
|
|
io->iov_cnt = iov_off + 1;
|
|
|
|
io->lbk_cnt = lbk_cnt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
lbk_off += num_iov;
|
|
|
|
}
|
2019-04-09 12:38:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
static void
|
|
|
|
ftl_io_init(struct ftl_io *io, struct spdk_ftl_dev *dev,
|
2019-05-27 12:56:48 +00:00
|
|
|
ftl_io_fn fn, void *ctx, int flags, int type)
|
2018-10-29 12:17:34 +00:00
|
|
|
{
|
|
|
|
io->flags |= flags | FTL_IO_INITIALIZED;
|
|
|
|
io->type = type;
|
|
|
|
io->dev = dev;
|
2019-05-08 10:46:58 +00:00
|
|
|
io->lba.single = FTL_LBA_INVALID;
|
2019-05-08 11:58:37 +00:00
|
|
|
io->ppa.ppa = FTL_PPA_INVALID;
|
2019-05-27 12:56:48 +00:00
|
|
|
io->cb_fn = fn;
|
|
|
|
io->cb_ctx = ctx;
|
2018-11-30 17:33:15 +00:00
|
|
|
io->trace = ftl_trace_alloc_id(dev);
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ftl_io *
|
|
|
|
ftl_io_init_internal(const struct ftl_io_init_opts *opts)
|
|
|
|
{
|
|
|
|
struct ftl_io *io = opts->io;
|
2019-06-05 11:59:17 +00:00
|
|
|
struct ftl_io *parent = opts->parent;
|
2018-10-29 12:17:34 +00:00
|
|
|
struct spdk_ftl_dev *dev = opts->dev;
|
2019-05-15 12:02:00 +00:00
|
|
|
struct iovec iov = {
|
|
|
|
.iov_base = opts->data,
|
2019-05-22 07:06:07 +00:00
|
|
|
.iov_len = opts->lbk_cnt * FTL_BLOCK_SIZE
|
2019-05-15 12:02:00 +00:00
|
|
|
};
|
2018-10-29 12:17:34 +00:00
|
|
|
|
|
|
|
if (!io) {
|
2019-06-05 11:59:17 +00:00
|
|
|
if (parent) {
|
|
|
|
io = ftl_io_alloc_child(parent);
|
2019-03-22 15:45:46 +00:00
|
|
|
} else {
|
|
|
|
io = ftl_io_alloc(dev->ioch);
|
|
|
|
}
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
if (!io) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ftl_io_clear(io);
|
2019-05-27 12:56:48 +00:00
|
|
|
ftl_io_init(io, dev, opts->cb_fn, opts->cb_ctx, opts->flags | FTL_IO_INTERNAL, opts->type);
|
2018-10-29 12:17:34 +00:00
|
|
|
|
|
|
|
io->rwb_batch = opts->rwb_batch;
|
|
|
|
io->band = opts->band;
|
2019-02-04 14:53:47 +00:00
|
|
|
io->md = opts->md;
|
2018-10-29 12:17:34 +00:00
|
|
|
|
2019-06-05 11:59:17 +00:00
|
|
|
if (parent) {
|
|
|
|
if (parent->flags & FTL_IO_VECTOR_LBA) {
|
|
|
|
io->lba.vector = parent->lba.vector + parent->pos;
|
|
|
|
} else {
|
|
|
|
io->lba.single = parent->lba.single + parent->pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 12:02:00 +00:00
|
|
|
if (ftl_io_init_iovec(io, &iov, 1, opts->lbk_cnt)) {
|
|
|
|
if (!opts->io) {
|
|
|
|
ftl_io_free(io);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-29 12:17:34 +00:00
|
|
|
|
2019-05-10 15:06:04 +00:00
|
|
|
if (opts->flags & FTL_IO_VECTOR_LBA) {
|
|
|
|
io->lba.vector = calloc(io->lbk_cnt, sizeof(uint64_t));
|
|
|
|
if (!io->lba.vector) {
|
|
|
|
ftl_io_free(io);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ftl_io *
|
|
|
|
ftl_io_rwb_init(struct spdk_ftl_dev *dev, struct ftl_band *band,
|
2019-05-27 12:56:48 +00:00
|
|
|
struct ftl_rwb_batch *batch, ftl_io_fn cb)
|
2018-10-29 12:17:34 +00:00
|
|
|
{
|
|
|
|
struct ftl_io_init_opts opts = {
|
|
|
|
.dev = dev,
|
|
|
|
.io = NULL,
|
|
|
|
.rwb_batch = batch,
|
|
|
|
.band = band,
|
|
|
|
.size = sizeof(struct ftl_io),
|
|
|
|
.flags = 0,
|
|
|
|
.type = FTL_IO_WRITE,
|
2019-05-09 14:04:26 +00:00
|
|
|
.lbk_cnt = dev->xfer_size,
|
2019-05-27 12:56:48 +00:00
|
|
|
.cb_fn = cb,
|
2018-10-29 12:17:34 +00:00
|
|
|
.data = ftl_rwb_batch_get_data(batch),
|
|
|
|
.md = ftl_rwb_batch_get_md(batch),
|
|
|
|
};
|
|
|
|
|
|
|
|
return ftl_io_init_internal(&opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ftl_io *
|
2019-05-27 12:56:48 +00:00
|
|
|
ftl_io_erase_init(struct ftl_band *band, size_t lbk_cnt, ftl_io_fn cb)
|
2018-10-29 12:17:34 +00:00
|
|
|
{
|
|
|
|
struct ftl_io *io;
|
|
|
|
struct ftl_io_init_opts opts = {
|
|
|
|
.dev = band->dev,
|
|
|
|
.io = NULL,
|
|
|
|
.rwb_batch = NULL,
|
|
|
|
.band = band,
|
|
|
|
.size = sizeof(struct ftl_io),
|
|
|
|
.flags = FTL_IO_PPA_MODE,
|
|
|
|
.type = FTL_IO_ERASE,
|
2019-05-09 14:04:26 +00:00
|
|
|
.lbk_cnt = 1,
|
2019-05-27 12:56:48 +00:00
|
|
|
.cb_fn = cb,
|
2018-10-29 12:17:34 +00:00
|
|
|
.data = NULL,
|
|
|
|
.md = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
io = ftl_io_init_internal(&opts);
|
2019-03-29 19:16:04 +00:00
|
|
|
if (!io) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
io->lbk_cnt = lbk_cnt;
|
|
|
|
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
2019-05-27 12:56:48 +00:00
|
|
|
static void
|
|
|
|
_ftl_user_cb(struct ftl_io *io, void *arg, int status)
|
|
|
|
{
|
|
|
|
io->user_fn(arg, status);
|
|
|
|
}
|
|
|
|
|
2019-05-15 11:05:06 +00:00
|
|
|
struct ftl_io *
|
|
|
|
ftl_io_user_init(struct spdk_io_channel *_ioch, uint64_t lba, size_t lbk_cnt, struct iovec *iov,
|
2019-05-27 12:56:48 +00:00
|
|
|
size_t iov_cnt, spdk_ftl_fn cb_fn, void *cb_ctx, int type)
|
2018-10-29 12:17:34 +00:00
|
|
|
{
|
2019-05-15 11:05:06 +00:00
|
|
|
struct ftl_io_channel *ioch = spdk_io_channel_get_ctx(_ioch);
|
|
|
|
struct spdk_ftl_dev *dev = ioch->dev;
|
|
|
|
struct ftl_io *io;
|
|
|
|
|
|
|
|
io = ftl_io_alloc(_ioch);
|
|
|
|
if (spdk_unlikely(!io)) {
|
|
|
|
return NULL;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 12:56:48 +00:00
|
|
|
ftl_io_init(io, dev, _ftl_user_cb, cb_ctx, 0, type);
|
2019-05-08 10:46:58 +00:00
|
|
|
io->lba.single = lba;
|
2019-05-27 12:56:48 +00:00
|
|
|
io->user_fn = cb_fn;
|
2018-10-29 12:17:34 +00:00
|
|
|
|
2019-05-15 12:02:00 +00:00
|
|
|
if (ftl_io_init_iovec(io, iov, iov_cnt, lbk_cnt)) {
|
|
|
|
ftl_io_free(io);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-29 12:17:34 +00:00
|
|
|
|
2018-11-30 17:33:15 +00:00
|
|
|
ftl_trace_lba_io_init(io->dev, io);
|
2019-05-15 11:05:06 +00:00
|
|
|
return io;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 15:45:46 +00:00
|
|
|
static void
|
|
|
|
_ftl_io_free(struct ftl_io *io)
|
|
|
|
{
|
|
|
|
struct ftl_io_channel *ioch;
|
|
|
|
|
|
|
|
assert(LIST_EMPTY(&io->children));
|
|
|
|
|
2019-05-10 15:06:04 +00:00
|
|
|
if (io->flags & FTL_IO_VECTOR_LBA) {
|
|
|
|
free(io->lba.vector);
|
|
|
|
}
|
|
|
|
|
2019-03-22 15:45:46 +00:00
|
|
|
if (pthread_spin_destroy(&io->lock)) {
|
|
|
|
SPDK_ERRLOG("pthread_spin_destroy failed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
ioch = spdk_io_channel_get_ctx(io->ioch);
|
|
|
|
spdk_mempool_put(ioch->io_pool, io);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
ftl_io_remove_child(struct ftl_io *io)
|
|
|
|
{
|
|
|
|
struct ftl_io *parent = io->parent;
|
|
|
|
bool parent_done;
|
|
|
|
|
|
|
|
pthread_spin_lock(&parent->lock);
|
|
|
|
LIST_REMOVE(io, child_entry);
|
|
|
|
parent_done = parent->done && LIST_EMPTY(&parent->children);
|
|
|
|
parent->status = parent->status ? : io->status;
|
|
|
|
pthread_spin_unlock(&parent->lock);
|
|
|
|
|
|
|
|
return parent_done;
|
|
|
|
}
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
void
|
|
|
|
ftl_io_complete(struct ftl_io *io)
|
|
|
|
{
|
2019-03-22 15:45:46 +00:00
|
|
|
struct ftl_io *parent = io->parent;
|
2019-05-13 12:11:10 +00:00
|
|
|
bool complete;
|
2018-10-29 12:17:34 +00:00
|
|
|
|
|
|
|
io->flags &= ~FTL_IO_INITIALIZED;
|
|
|
|
|
2019-03-22 15:45:46 +00:00
|
|
|
pthread_spin_lock(&io->lock);
|
|
|
|
complete = LIST_EMPTY(&io->children);
|
|
|
|
io->done = true;
|
|
|
|
pthread_spin_unlock(&io->lock);
|
|
|
|
|
|
|
|
if (complete) {
|
2019-05-27 12:56:48 +00:00
|
|
|
if (io->cb_fn) {
|
|
|
|
io->cb_fn(io, io->cb_ctx, io->status);
|
2019-03-22 15:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parent && ftl_io_remove_child(io)) {
|
|
|
|
ftl_io_complete(parent);
|
|
|
|
}
|
|
|
|
|
2019-05-13 12:11:10 +00:00
|
|
|
_ftl_io_free(io);
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 15:45:46 +00:00
|
|
|
struct ftl_io *
|
|
|
|
ftl_io_alloc_child(struct ftl_io *parent)
|
|
|
|
{
|
|
|
|
struct ftl_io *io;
|
|
|
|
|
|
|
|
io = ftl_io_alloc(parent->ioch);
|
|
|
|
if (spdk_unlikely(!io)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-15 12:02:00 +00:00
|
|
|
ftl_io_init(io, parent->dev, NULL, NULL, parent->flags, parent->type);
|
2019-03-22 15:45:46 +00:00
|
|
|
io->parent = parent;
|
|
|
|
|
|
|
|
pthread_spin_lock(&parent->lock);
|
|
|
|
LIST_INSERT_HEAD(&parent->children, io, child_entry);
|
|
|
|
pthread_spin_unlock(&parent->lock);
|
|
|
|
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
void
|
|
|
|
ftl_io_process_error(struct ftl_io *io, const struct spdk_nvme_cpl *status)
|
|
|
|
{
|
|
|
|
/* TODO: add error handling for specifc cases */
|
|
|
|
if (status->status.sct == SPDK_NVME_SCT_MEDIA_ERROR &&
|
|
|
|
status->status.sc == SPDK_OCSSD_SC_READ_HIGH_ECC) {
|
2019-03-06 13:29:11 +00:00
|
|
|
return;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
2019-03-06 13:29:11 +00:00
|
|
|
|
|
|
|
io->status = -EIO;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 14:45:22 +00:00
|
|
|
void ftl_io_fail(struct ftl_io *io, int status)
|
|
|
|
{
|
|
|
|
io->status = status;
|
|
|
|
ftl_io_advance(io, io->lbk_cnt - io->pos);
|
|
|
|
}
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
void *
|
|
|
|
ftl_io_get_md(const struct ftl_io *io)
|
|
|
|
{
|
|
|
|
if (!io->md) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-28 08:34:16 +00:00
|
|
|
return (char *)io->md + io->pos * io->dev->md_size;
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ftl_io *
|
|
|
|
ftl_io_alloc(struct spdk_io_channel *ch)
|
|
|
|
{
|
|
|
|
struct ftl_io *io;
|
|
|
|
struct ftl_io_channel *ioch = spdk_io_channel_get_ctx(ch);
|
|
|
|
|
|
|
|
io = spdk_mempool_get(ioch->io_pool);
|
|
|
|
if (!io) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(io, 0, ioch->elem_size);
|
2019-03-22 15:45:46 +00:00
|
|
|
io->ioch = ch;
|
|
|
|
|
|
|
|
if (pthread_spin_init(&io->lock, PTHREAD_PROCESS_PRIVATE)) {
|
|
|
|
SPDK_ERRLOG("pthread_spin_init failed\n");
|
|
|
|
spdk_mempool_put(ioch->io_pool, io);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-05-27 12:56:48 +00:00
|
|
|
ftl_io_reinit(struct ftl_io *io, ftl_io_fn cb, void *ctx, int flags, int type)
|
2018-10-29 12:17:34 +00:00
|
|
|
{
|
|
|
|
ftl_io_clear(io);
|
2019-05-27 12:56:48 +00:00
|
|
|
ftl_io_init(io, io->dev, cb, ctx, flags, type);
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ftl_io_clear(struct ftl_io *io)
|
|
|
|
{
|
2019-04-05 11:31:19 +00:00
|
|
|
ftl_io_reset(io);
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
io->flags = 0;
|
|
|
|
io->rwb_batch = NULL;
|
|
|
|
io->band = NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:31:19 +00:00
|
|
|
void
|
|
|
|
ftl_io_reset(struct ftl_io *io)
|
|
|
|
{
|
|
|
|
io->req_cnt = io->pos = io->iov_pos = io->iov_off = 0;
|
|
|
|
io->done = false;
|
|
|
|
}
|
|
|
|
|
2018-10-29 12:17:34 +00:00
|
|
|
void
|
|
|
|
ftl_io_free(struct ftl_io *io)
|
|
|
|
{
|
2019-03-22 15:45:46 +00:00
|
|
|
struct ftl_io *parent = io->parent;
|
2018-10-29 12:17:34 +00:00
|
|
|
|
|
|
|
if (!io) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-22 15:45:46 +00:00
|
|
|
if (parent && ftl_io_remove_child(io)) {
|
|
|
|
ftl_io_complete(parent);
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 15:45:46 +00:00
|
|
|
_ftl_io_free(io);
|
2018-10-29 12:17:34 +00:00
|
|
|
}
|
2019-05-15 09:18:10 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ftl_io_call_foreach_child(struct ftl_io *io, int (*callback)(struct ftl_io *))
|
|
|
|
{
|
|
|
|
struct ftl_io *child, *tmp;
|
|
|
|
|
|
|
|
assert(!io->done);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the IO doesn't have any children, it means that it directly describes a request (i.e.
|
|
|
|
* all of the buffers, LBAs, etc. are filled). Otherwise the IO only groups together several
|
|
|
|
* requests and may be partially filled, so the callback needs to be called on all of its
|
|
|
|
* children instead.
|
|
|
|
*/
|
|
|
|
if (LIST_EMPTY(&io->children)) {
|
|
|
|
callback(io);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIST_FOREACH_SAFE(child, &io->children, child_entry, tmp) {
|
|
|
|
int rc = callback(child);
|
|
|
|
if (rc) {
|
|
|
|
assert(rc != -EAGAIN);
|
|
|
|
ftl_io_fail(io, rc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If all the callbacks were processed or an error occurred, treat this IO as completed.
|
|
|
|
* Multiple calls to ftl_io_call_foreach_child are not supported, resubmissions are supposed
|
|
|
|
* to be handled in the callback.
|
|
|
|
*/
|
|
|
|
ftl_io_complete(io);
|
|
|
|
}
|