2017-07-14 22:20:19 +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"
|
2017-08-11 23:27:04 +00:00
|
|
|
#include "spdk/string.h"
|
2017-07-14 22:20:19 +00:00
|
|
|
|
|
|
|
#include <linux/nbd.h>
|
|
|
|
|
|
|
|
#include "spdk/nbd.h"
|
2017-12-04 06:56:49 +00:00
|
|
|
#include "nbd_internal.h"
|
2017-07-14 22:20:19 +00:00
|
|
|
#include "spdk/bdev.h"
|
|
|
|
#include "spdk/endian.h"
|
|
|
|
#include "spdk/env.h"
|
2021-02-08 11:25:32 +00:00
|
|
|
#include "spdk/likely.h"
|
2017-07-14 22:20:19 +00:00
|
|
|
#include "spdk/log.h"
|
|
|
|
#include "spdk/util.h"
|
2018-06-11 20:32:15 +00:00
|
|
|
#include "spdk/thread.h"
|
2017-07-14 22:20:19 +00:00
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
#include "spdk/queue.h"
|
|
|
|
|
2019-02-15 10:31:51 +00:00
|
|
|
#define GET_IO_LOOP_COUNT 16
|
2021-02-05 12:44:39 +00:00
|
|
|
#define NBD_START_BUSY_WAITING_MS 1000
|
2021-02-05 16:24:01 +00:00
|
|
|
#define NBD_STOP_BUSY_WAITING_MS 10000
|
2019-02-15 10:31:51 +00:00
|
|
|
#define NBD_BUSY_POLLING_INTERVAL_US 20000
|
2021-02-01 13:19:14 +00:00
|
|
|
#define NBD_IO_TIMEOUT_S 60
|
2017-12-08 19:54:11 +00:00
|
|
|
|
2017-12-18 06:39:15 +00:00
|
|
|
enum nbd_io_state_t {
|
|
|
|
/* Receiving or ready to receive nbd request header */
|
|
|
|
NBD_IO_RECV_REQ = 0,
|
|
|
|
/* Receiving write payload */
|
|
|
|
NBD_IO_RECV_PAYLOAD,
|
|
|
|
/* Transmitting or ready to transmit nbd response header */
|
|
|
|
NBD_IO_XMIT_RESP,
|
|
|
|
/* Transmitting read payload */
|
|
|
|
NBD_IO_XMIT_PAYLOAD,
|
|
|
|
};
|
|
|
|
|
2017-07-14 22:20:19 +00:00
|
|
|
struct nbd_io {
|
2017-12-18 06:39:15 +00:00
|
|
|
struct spdk_nbd_disk *nbd;
|
|
|
|
enum nbd_io_state_t state;
|
|
|
|
|
2017-07-14 22:20:19 +00:00
|
|
|
void *payload;
|
|
|
|
uint32_t payload_size;
|
|
|
|
|
|
|
|
struct nbd_request req;
|
|
|
|
struct nbd_reply resp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tracks current progress on reading/writing a request,
|
|
|
|
* response, or payload from the nbd socket.
|
|
|
|
*/
|
|
|
|
uint32_t offset;
|
2017-12-20 08:50:43 +00:00
|
|
|
|
2018-09-14 12:19:17 +00:00
|
|
|
/* for bdev io_wait */
|
|
|
|
struct spdk_bdev_io_wait_entry bdev_io_wait;
|
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
TAILQ_ENTRY(nbd_io) tailq;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum nbd_disk_state_t {
|
|
|
|
NBD_DISK_STATE_RUNNING = 0,
|
|
|
|
/* soft disconnection caused by receiving nbd_cmd_disc */
|
|
|
|
NBD_DISK_STATE_SOFTDISC,
|
|
|
|
/* hard disconnection caused by mandatory conditions */
|
|
|
|
NBD_DISK_STATE_HARDDISC,
|
2017-07-14 22:20:19 +00:00
|
|
|
};
|
|
|
|
|
2017-08-02 17:40:38 +00:00
|
|
|
struct spdk_nbd_disk {
|
2017-07-14 22:20:19 +00:00
|
|
|
struct spdk_bdev *bdev;
|
|
|
|
struct spdk_bdev_desc *bdev_desc;
|
|
|
|
struct spdk_io_channel *ch;
|
2017-11-21 19:51:57 +00:00
|
|
|
int dev_fd;
|
2017-12-04 06:56:49 +00:00
|
|
|
char *nbd_path;
|
2017-11-21 19:51:57 +00:00
|
|
|
int kernel_sp_fd;
|
|
|
|
int spdk_sp_fd;
|
2017-12-02 08:59:14 +00:00
|
|
|
struct spdk_poller *nbd_poller;
|
2020-09-16 20:33:19 +00:00
|
|
|
struct spdk_interrupt *intr;
|
2021-01-02 21:52:46 +00:00
|
|
|
bool interrupt_mode;
|
2017-07-14 22:20:19 +00:00
|
|
|
uint32_t buf_align;
|
2017-12-04 05:46:13 +00:00
|
|
|
|
2021-02-05 12:44:39 +00:00
|
|
|
struct spdk_poller *retry_poller;
|
|
|
|
int retry_count;
|
2021-02-05 16:24:01 +00:00
|
|
|
/* Synchronize nbd_start_kernel pthread and nbd_stop */
|
|
|
|
bool has_nbd_pthread;
|
2021-02-05 12:44:39 +00:00
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
struct nbd_io *io_in_recv;
|
|
|
|
TAILQ_HEAD(, nbd_io) received_io_list;
|
|
|
|
TAILQ_HEAD(, nbd_io) executed_io_list;
|
|
|
|
|
|
|
|
enum nbd_disk_state_t state;
|
|
|
|
/* count of nbd_io in spdk_nbd_disk */
|
|
|
|
int io_count;
|
|
|
|
|
2017-12-04 05:46:13 +00:00
|
|
|
TAILQ_ENTRY(spdk_nbd_disk) tailq;
|
2017-07-14 22:20:19 +00:00
|
|
|
};
|
|
|
|
|
2017-12-02 10:15:34 +00:00
|
|
|
struct spdk_nbd_disk_globals {
|
|
|
|
TAILQ_HEAD(, spdk_nbd_disk) disk_head;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct spdk_nbd_disk_globals g_spdk_nbd;
|
2020-12-17 17:12:44 +00:00
|
|
|
static spdk_nbd_fini_cb g_fini_cb_fn;
|
|
|
|
static void *g_fini_cb_arg;
|
2017-12-02 10:15:34 +00:00
|
|
|
|
2020-12-28 15:00:51 +00:00
|
|
|
static void _nbd_fini(void *arg1);
|
|
|
|
|
2018-09-14 12:19:17 +00:00
|
|
|
static int
|
|
|
|
nbd_submit_bdev_io(struct spdk_nbd_disk *nbd, struct nbd_io *io);
|
2021-02-08 11:25:32 +00:00
|
|
|
static int
|
|
|
|
nbd_io_recv_internal(struct spdk_nbd_disk *nbd);
|
2018-09-14 12:19:17 +00:00
|
|
|
|
2017-12-02 10:15:34 +00:00
|
|
|
int
|
|
|
|
spdk_nbd_init(void)
|
|
|
|
{
|
|
|
|
TAILQ_INIT(&g_spdk_nbd.disk_head);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:12:44 +00:00
|
|
|
static void
|
2020-12-28 15:00:51 +00:00
|
|
|
_nbd_stop_async(void *arg)
|
2017-12-02 10:15:34 +00:00
|
|
|
{
|
2020-12-28 15:00:51 +00:00
|
|
|
struct spdk_nbd_disk *nbd = arg;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = spdk_nbd_stop(nbd);
|
|
|
|
if (rc) {
|
|
|
|
/* spdk_nbd_stop failed because some IO are still executing. Send a message
|
|
|
|
* to this thread to try again later. */
|
|
|
|
spdk_thread_send_msg(spdk_get_thread(),
|
|
|
|
_nbd_stop_async, nbd);
|
|
|
|
} else {
|
|
|
|
_nbd_fini(NULL);
|
2020-12-17 17:12:44 +00:00
|
|
|
}
|
2020-12-28 15:00:51 +00:00
|
|
|
}
|
2020-12-17 17:12:44 +00:00
|
|
|
|
2020-12-28 15:00:51 +00:00
|
|
|
static void
|
|
|
|
_nbd_fini(void *arg1)
|
|
|
|
{
|
|
|
|
struct spdk_nbd_disk *nbd_first;
|
|
|
|
|
|
|
|
nbd_first = TAILQ_FIRST(&g_spdk_nbd.disk_head);
|
|
|
|
if (nbd_first) {
|
|
|
|
/* Stop running spdk_nbd_disk */
|
|
|
|
spdk_thread_send_msg(spdk_io_channel_get_thread(nbd_first->ch),
|
|
|
|
_nbd_stop_async, nbd_first);
|
2020-12-17 17:12:44 +00:00
|
|
|
} else {
|
2020-12-28 15:00:51 +00:00
|
|
|
/* We can directly call final function here, because
|
|
|
|
spdk_subsystem_fini_next handles the case: current thread does not equal
|
|
|
|
to g_final_thread */
|
|
|
|
g_fini_cb_fn(g_fini_cb_arg);
|
2017-12-04 05:46:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:12:44 +00:00
|
|
|
void
|
|
|
|
spdk_nbd_fini(spdk_nbd_fini_cb cb_fn, void *cb_arg)
|
|
|
|
{
|
|
|
|
g_fini_cb_fn = cb_fn;
|
|
|
|
g_fini_cb_arg = cb_arg;
|
|
|
|
|
|
|
|
_nbd_fini(NULL);
|
|
|
|
}
|
|
|
|
|
2017-12-04 05:46:13 +00:00
|
|
|
static int
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_disk_register(struct spdk_nbd_disk *nbd)
|
2017-12-04 05:46:13 +00:00
|
|
|
{
|
2021-01-27 17:46:38 +00:00
|
|
|
/* Make sure nbd_path is not used in this SPDK app */
|
2020-05-07 17:27:31 +00:00
|
|
|
if (nbd_disk_find_by_nbd_path(nbd->nbd_path)) {
|
2017-12-04 05:46:13 +00:00
|
|
|
SPDK_NOTICELOG("%s is already exported\n", nbd->nbd_path);
|
2018-11-22 08:46:40 +00:00
|
|
|
return -EBUSY;
|
2017-12-04 05:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&g_spdk_nbd.disk_head, nbd, tailq);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_disk_unregister(struct spdk_nbd_disk *nbd)
|
2017-12-04 05:46:13 +00:00
|
|
|
{
|
|
|
|
struct spdk_nbd_disk *nbd_idx, *nbd_tmp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nbd disk may be stopped before registered.
|
|
|
|
* check whether it was registered.
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH_SAFE(nbd_idx, &g_spdk_nbd.disk_head, tailq, nbd_tmp) {
|
|
|
|
if (nbd == nbd_idx) {
|
|
|
|
TAILQ_REMOVE(&g_spdk_nbd.disk_head, nbd_idx, tailq);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spdk_nbd_disk *
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_disk_find_by_nbd_path(const char *nbd_path)
|
2017-12-04 05:46:13 +00:00
|
|
|
{
|
|
|
|
struct spdk_nbd_disk *nbd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check whether nbd has already been registered by nbd path.
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH(nbd, &g_spdk_nbd.disk_head, tailq) {
|
|
|
|
if (!strcmp(nbd->nbd_path, nbd_path)) {
|
|
|
|
return nbd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-07 17:27:31 +00:00
|
|
|
struct spdk_nbd_disk *nbd_disk_first(void)
|
2017-12-04 05:46:13 +00:00
|
|
|
{
|
|
|
|
return TAILQ_FIRST(&g_spdk_nbd.disk_head);
|
|
|
|
}
|
|
|
|
|
2020-05-07 17:27:31 +00:00
|
|
|
struct spdk_nbd_disk *nbd_disk_next(struct spdk_nbd_disk *prev)
|
2017-12-04 05:46:13 +00:00
|
|
|
{
|
|
|
|
return TAILQ_NEXT(prev, tailq);
|
2017-12-02 10:15:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 06:56:49 +00:00
|
|
|
const char *
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_disk_get_nbd_path(struct spdk_nbd_disk *nbd)
|
2017-12-04 06:56:49 +00:00
|
|
|
{
|
|
|
|
return nbd->nbd_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_disk_get_bdev_name(struct spdk_nbd_disk *nbd)
|
2017-12-04 06:56:49 +00:00
|
|
|
{
|
|
|
|
return spdk_bdev_get_name(nbd->bdev);
|
|
|
|
}
|
|
|
|
|
2018-04-19 15:48:48 +00:00
|
|
|
void
|
2018-08-31 20:39:11 +00:00
|
|
|
spdk_nbd_write_config_json(struct spdk_json_write_ctx *w)
|
2018-04-19 15:48:48 +00:00
|
|
|
{
|
|
|
|
struct spdk_nbd_disk *nbd;
|
|
|
|
|
2018-04-25 17:04:39 +00:00
|
|
|
spdk_json_write_array_begin(w);
|
|
|
|
|
2018-04-19 15:48:48 +00:00
|
|
|
TAILQ_FOREACH(nbd, &g_spdk_nbd.disk_head, tailq) {
|
|
|
|
spdk_json_write_object_begin(w);
|
|
|
|
|
2019-09-17 10:36:40 +00:00
|
|
|
spdk_json_write_named_string(w, "method", "nbd_start_disk");
|
2018-04-19 15:48:48 +00:00
|
|
|
|
|
|
|
spdk_json_write_named_object_begin(w, "params");
|
2020-05-07 17:27:31 +00:00
|
|
|
spdk_json_write_named_string(w, "nbd_device", nbd_disk_get_nbd_path(nbd));
|
|
|
|
spdk_json_write_named_string(w, "bdev_name", nbd_disk_get_bdev_name(nbd));
|
2018-04-19 15:48:48 +00:00
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
|
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
}
|
|
|
|
|
2018-04-25 17:04:39 +00:00
|
|
|
spdk_json_write_array_end(w);
|
2018-04-19 15:48:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 04:51:35 +00:00
|
|
|
void
|
|
|
|
nbd_disconnect(struct spdk_nbd_disk *nbd)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* nbd soft-disconnection to terminate transmission phase.
|
|
|
|
* After receiving this ioctl command, nbd kernel module will send
|
|
|
|
* a NBD_CMD_DISC type io to nbd server in order to inform server.
|
|
|
|
*/
|
|
|
|
ioctl(nbd->dev_fd, NBD_DISCONNECT);
|
|
|
|
}
|
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
static struct nbd_io *
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_get_io(struct spdk_nbd_disk *nbd)
|
2017-12-20 08:50:43 +00:00
|
|
|
{
|
|
|
|
struct nbd_io *io;
|
|
|
|
|
|
|
|
io = calloc(1, sizeof(*io));
|
|
|
|
if (!io) {
|
2018-03-12 22:16:26 +00:00
|
|
|
return NULL;
|
2017-12-20 08:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
io->nbd = nbd;
|
|
|
|
to_be32(&io->resp.magic, NBD_REPLY_MAGIC);
|
|
|
|
|
|
|
|
nbd->io_count++;
|
|
|
|
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_put_io(struct spdk_nbd_disk *nbd, struct nbd_io *io)
|
2017-12-20 08:50:43 +00:00
|
|
|
{
|
|
|
|
if (io->payload) {
|
2019-06-26 09:18:14 +00:00
|
|
|
spdk_free(io->payload);
|
2017-12-20 08:50:43 +00:00
|
|
|
}
|
|
|
|
free(io);
|
|
|
|
|
|
|
|
nbd->io_count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether received nbd_io are all transmitted.
|
|
|
|
*
|
|
|
|
* \return 1 there is still some nbd_io not transmitted.
|
|
|
|
* 0 all nbd_io received are transmitted.
|
|
|
|
*/
|
|
|
|
static int
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_io_xmit_check(struct spdk_nbd_disk *nbd)
|
2017-12-20 08:50:43 +00:00
|
|
|
{
|
|
|
|
if (nbd->io_count == 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (nbd->io_count == 1 && nbd->io_in_recv != NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether received nbd_io are all executed,
|
|
|
|
* and put back executed nbd_io instead of transmitting them
|
|
|
|
*
|
|
|
|
* \return 1 there is still some nbd_io under executing
|
|
|
|
* 0 all nbd_io gotten are freed.
|
|
|
|
*/
|
|
|
|
static int
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_cleanup_io(struct spdk_nbd_disk *nbd)
|
2017-12-20 08:50:43 +00:00
|
|
|
{
|
2021-02-08 11:25:32 +00:00
|
|
|
/* Try to read the remaining nbd commands in the socket */
|
2021-03-26 10:37:31 +00:00
|
|
|
while (nbd_io_recv_internal(nbd) > 0);
|
2021-02-08 11:25:32 +00:00
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
/* free io_in_recv */
|
|
|
|
if (nbd->io_in_recv != NULL) {
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_put_io(nbd, nbd->io_in_recv);
|
2017-12-20 08:50:43 +00:00
|
|
|
nbd->io_in_recv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some nbd_io may be under executing in bdev.
|
|
|
|
* Wait for their done operation.
|
|
|
|
*/
|
|
|
|
if (nbd->io_count != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-05 16:24:01 +00:00
|
|
|
static int
|
|
|
|
_nbd_stop(void *arg)
|
2017-07-14 22:20:19 +00:00
|
|
|
{
|
2021-02-05 16:24:01 +00:00
|
|
|
struct spdk_nbd_disk *nbd = arg;
|
|
|
|
|
2020-09-16 20:33:19 +00:00
|
|
|
if (nbd->nbd_poller) {
|
|
|
|
spdk_poller_unregister(&nbd->nbd_poller);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nbd->intr) {
|
|
|
|
spdk_interrupt_unregister(&nbd->intr);
|
|
|
|
}
|
|
|
|
|
2017-11-21 19:51:57 +00:00
|
|
|
if (nbd->spdk_sp_fd >= 0) {
|
|
|
|
close(nbd->spdk_sp_fd);
|
2021-02-05 16:24:01 +00:00
|
|
|
nbd->spdk_sp_fd = -1;
|
2017-11-21 19:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nbd->kernel_sp_fd >= 0) {
|
|
|
|
close(nbd->kernel_sp_fd);
|
2021-02-05 16:24:01 +00:00
|
|
|
nbd->kernel_sp_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Continue the stop procedure after the exit of nbd_start_kernel pthread */
|
|
|
|
if (nbd->has_nbd_pthread) {
|
|
|
|
if (nbd->retry_poller == NULL) {
|
|
|
|
nbd->retry_count = NBD_STOP_BUSY_WAITING_MS * 1000ULL / NBD_BUSY_POLLING_INTERVAL_US;
|
|
|
|
nbd->retry_poller = SPDK_POLLER_REGISTER(_nbd_stop, nbd,
|
|
|
|
NBD_BUSY_POLLING_INTERVAL_US);
|
|
|
|
return SPDK_POLLER_BUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nbd->retry_count-- > 0) {
|
|
|
|
return SPDK_POLLER_BUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDK_ERRLOG("Failed to wait for returning of NBD_DO_IT ioctl.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nbd->retry_poller) {
|
|
|
|
spdk_poller_unregister(&nbd->retry_poller);
|
2017-08-02 17:40:38 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 12:23:03 +00:00
|
|
|
if (nbd->dev_fd >= 0) {
|
2019-02-14 12:27:08 +00:00
|
|
|
/* Clear nbd device only if it is occupied by SPDK app */
|
2020-05-07 17:27:31 +00:00
|
|
|
if (nbd->nbd_path && nbd_disk_find_by_nbd_path(nbd->nbd_path)) {
|
2019-02-14 12:27:08 +00:00
|
|
|
ioctl(nbd->dev_fd, NBD_CLEAR_QUE);
|
|
|
|
ioctl(nbd->dev_fd, NBD_CLEAR_SOCK);
|
|
|
|
}
|
2018-01-03 12:23:03 +00:00
|
|
|
close(nbd->dev_fd);
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:27:08 +00:00
|
|
|
if (nbd->nbd_path) {
|
|
|
|
free(nbd->nbd_path);
|
|
|
|
}
|
|
|
|
|
2021-02-05 16:24:01 +00:00
|
|
|
if (nbd->ch) {
|
|
|
|
spdk_put_io_channel(nbd->ch);
|
2021-02-05 16:24:01 +00:00
|
|
|
nbd->ch = NULL;
|
2021-02-05 16:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nbd->bdev_desc) {
|
|
|
|
spdk_bdev_close(nbd->bdev_desc);
|
2021-02-05 16:24:01 +00:00
|
|
|
nbd->bdev_desc = NULL;
|
2021-02-05 16:24:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_disk_unregister(nbd);
|
2017-12-04 05:46:13 +00:00
|
|
|
|
2017-08-02 17:40:38 +00:00
|
|
|
free(nbd);
|
2021-02-05 16:24:01 +00:00
|
|
|
|
|
|
|
return 0;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:12:44 +00:00
|
|
|
int
|
2017-11-06 17:51:31 +00:00
|
|
|
spdk_nbd_stop(struct spdk_nbd_disk *nbd)
|
|
|
|
{
|
2020-12-17 17:12:44 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
2017-11-06 17:51:31 +00:00
|
|
|
if (nbd == NULL) {
|
2020-12-17 17:12:44 +00:00
|
|
|
return rc;
|
2017-11-06 17:51:31 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
nbd->state = NBD_DISK_STATE_HARDDISC;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stop action should be called only after all nbd_io are executed.
|
|
|
|
*/
|
2020-12-17 17:12:44 +00:00
|
|
|
|
|
|
|
rc = nbd_cleanup_io(nbd);
|
|
|
|
if (!rc) {
|
2017-11-06 17:51:31 +00:00
|
|
|
_nbd_stop(nbd);
|
|
|
|
}
|
2020-12-17 17:12:44 +00:00
|
|
|
|
|
|
|
return rc;
|
2017-11-06 17:51:31 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 21:37:53 +00:00
|
|
|
static int64_t
|
2020-12-30 14:29:45 +00:00
|
|
|
nbd_socket_rw(int fd, void *buf, size_t length, bool read_op)
|
2017-07-14 22:20:19 +00:00
|
|
|
{
|
2020-12-30 14:29:45 +00:00
|
|
|
ssize_t rc;
|
2017-07-14 22:20:19 +00:00
|
|
|
|
2020-12-30 14:29:45 +00:00
|
|
|
if (read_op) {
|
|
|
|
rc = read(fd, buf, length);
|
2017-07-14 22:20:19 +00:00
|
|
|
} else {
|
2020-12-30 14:29:45 +00:00
|
|
|
rc = write(fd, buf, length);
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 14:29:45 +00:00
|
|
|
if (rc == 0) {
|
2017-08-02 21:37:53 +00:00
|
|
|
return -EIO;
|
2020-12-30 14:29:45 +00:00
|
|
|
} else if (rc == -1) {
|
2021-01-05 11:25:17 +00:00
|
|
|
if (errno != EAGAIN) {
|
2017-08-02 21:37:53 +00:00
|
|
|
return -errno;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} else {
|
2020-12-30 14:29:45 +00:00
|
|
|
return rc;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nbd_io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
|
|
|
|
{
|
|
|
|
struct nbd_io *io = cb_arg;
|
2017-12-18 06:39:15 +00:00
|
|
|
struct spdk_nbd_disk *nbd = io->nbd;
|
2017-07-14 22:20:19 +00:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
io->resp.error = 0;
|
|
|
|
} else {
|
|
|
|
to_be32(&io->resp.error, EIO);
|
|
|
|
}
|
2017-12-18 06:39:15 +00:00
|
|
|
|
|
|
|
memcpy(&io->resp.handle, &io->req.handle, sizeof(io->resp.handle));
|
2020-09-16 20:33:19 +00:00
|
|
|
|
|
|
|
/* When there begins to have executed_io, enable socket writable notice in order to
|
|
|
|
* get it processed in nbd_io_xmit
|
|
|
|
*/
|
2021-01-02 21:52:46 +00:00
|
|
|
if (nbd->interrupt_mode && TAILQ_EMPTY(&nbd->executed_io_list)) {
|
2020-09-16 20:33:19 +00:00
|
|
|
spdk_interrupt_set_event_types(nbd->intr, SPDK_INTERRUPT_EVENT_IN | SPDK_INTERRUPT_EVENT_OUT);
|
|
|
|
}
|
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
TAILQ_INSERT_TAIL(&nbd->executed_io_list, io, tailq);
|
2017-12-18 06:39:15 +00:00
|
|
|
|
2017-07-14 22:20:19 +00:00
|
|
|
if (bdev_io != NULL) {
|
|
|
|
spdk_bdev_free_io(bdev_io);
|
|
|
|
}
|
2017-11-06 17:51:31 +00:00
|
|
|
|
2020-05-07 17:27:31 +00:00
|
|
|
if (nbd->state == NBD_DISK_STATE_HARDDISC && !nbd_cleanup_io(nbd)) {
|
2017-12-18 06:39:15 +00:00
|
|
|
_nbd_stop(nbd);
|
2017-11-06 17:51:31 +00:00
|
|
|
}
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 12:19:17 +00:00
|
|
|
static void
|
|
|
|
nbd_resubmit_io(void *arg)
|
|
|
|
{
|
|
|
|
struct nbd_io *io = (struct nbd_io *)arg;
|
|
|
|
struct spdk_nbd_disk *nbd = io->nbd;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
rc = nbd_submit_bdev_io(nbd, io);
|
|
|
|
if (rc) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_INFOLOG(nbd, "nbd: io resubmit for dev %s , io_type %d, returned %d.\n",
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_disk_get_bdev_name(nbd), from_be32(&io->req.type), rc);
|
2018-09-14 12:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nbd_queue_io(struct nbd_io *io)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct spdk_bdev *bdev = io->nbd->bdev;
|
|
|
|
|
|
|
|
io->bdev_io_wait.bdev = bdev;
|
|
|
|
io->bdev_io_wait.cb_fn = nbd_resubmit_io;
|
|
|
|
io->bdev_io_wait.cb_arg = io;
|
|
|
|
|
|
|
|
rc = spdk_bdev_queue_io_wait(bdev, io->nbd->ch, &io->bdev_io_wait);
|
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("Queue io failed in nbd_queue_io, rc=%d.\n", rc);
|
|
|
|
nbd_io_done(NULL, false, io);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 06:39:15 +00:00
|
|
|
static int
|
|
|
|
nbd_submit_bdev_io(struct spdk_nbd_disk *nbd, struct nbd_io *io)
|
2017-07-14 22:20:19 +00:00
|
|
|
{
|
2017-12-18 06:39:15 +00:00
|
|
|
struct spdk_bdev_desc *desc = nbd->bdev_desc;
|
|
|
|
struct spdk_io_channel *ch = nbd->ch;
|
|
|
|
int rc = 0;
|
2017-07-14 22:20:19 +00:00
|
|
|
|
2017-12-18 06:39:15 +00:00
|
|
|
switch (from_be32(&io->req.type)) {
|
|
|
|
case NBD_CMD_READ:
|
2017-07-14 22:20:19 +00:00
|
|
|
rc = spdk_bdev_read(desc, ch, io->payload, from_be64(&io->req.from),
|
|
|
|
io->payload_size, nbd_io_done, io);
|
|
|
|
break;
|
2017-12-18 06:39:15 +00:00
|
|
|
case NBD_CMD_WRITE:
|
2017-07-14 22:20:19 +00:00
|
|
|
rc = spdk_bdev_write(desc, ch, io->payload, from_be64(&io->req.from),
|
|
|
|
io->payload_size, nbd_io_done, io);
|
|
|
|
break;
|
2017-12-18 06:39:15 +00:00
|
|
|
#ifdef NBD_FLAG_SEND_FLUSH
|
|
|
|
case NBD_CMD_FLUSH:
|
|
|
|
rc = spdk_bdev_flush(desc, ch, 0,
|
|
|
|
spdk_bdev_get_num_blocks(nbd->bdev) * spdk_bdev_get_block_size(nbd->bdev),
|
|
|
|
nbd_io_done, io);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef NBD_FLAG_SEND_TRIM
|
|
|
|
case NBD_CMD_TRIM:
|
2017-07-19 21:32:04 +00:00
|
|
|
rc = spdk_bdev_unmap(desc, ch, from_be64(&io->req.from),
|
2018-06-05 17:38:10 +00:00
|
|
|
from_be32(&io->req.len), nbd_io_done, io);
|
2017-07-14 22:20:19 +00:00
|
|
|
break;
|
2017-12-18 06:39:15 +00:00
|
|
|
#endif
|
|
|
|
case NBD_CMD_DISC:
|
2017-12-20 08:50:43 +00:00
|
|
|
nbd->state = NBD_DISK_STATE_SOFTDISC;
|
2021-01-22 14:10:55 +00:00
|
|
|
rc = spdk_bdev_abort(desc, ch, io, nbd_io_done, io);
|
2020-09-16 20:33:19 +00:00
|
|
|
|
|
|
|
/* when there begins to have executed_io to send, enable socket writable notice */
|
2021-01-02 21:52:46 +00:00
|
|
|
if (nbd->interrupt_mode && TAILQ_EMPTY(&nbd->executed_io_list)) {
|
2020-09-16 20:33:19 +00:00
|
|
|
spdk_interrupt_set_event_types(nbd->intr, SPDK_INTERRUPT_EVENT_IN | SPDK_INTERRUPT_EVENT_OUT);
|
|
|
|
}
|
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
break;
|
2017-07-14 22:20:19 +00:00
|
|
|
default:
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
|
2017-12-18 06:39:15 +00:00
|
|
|
if (rc < 0) {
|
2018-09-14 12:19:17 +00:00
|
|
|
if (rc == -ENOMEM) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_INFOLOG(nbd, "No memory, start to queue io.\n");
|
2018-09-14 12:19:17 +00:00
|
|
|
nbd_queue_io(io);
|
|
|
|
} else {
|
|
|
|
SPDK_ERRLOG("nbd io failed in nbd_queue_io, rc=%d.\n", rc);
|
|
|
|
nbd_io_done(NULL, false, io);
|
|
|
|
}
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
2017-12-18 06:39:15 +00:00
|
|
|
|
|
|
|
return 0;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 21:37:53 +00:00
|
|
|
static int
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_io_exec(struct spdk_nbd_disk *nbd)
|
2017-07-14 22:20:19 +00:00
|
|
|
{
|
2017-12-20 08:50:43 +00:00
|
|
|
struct nbd_io *io, *io_tmp;
|
2020-07-01 09:44:23 +00:00
|
|
|
int io_count = 0;
|
2017-12-18 06:39:15 +00:00
|
|
|
int ret = 0;
|
2017-11-30 10:25:58 +00:00
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
if (!TAILQ_EMPTY(&nbd->received_io_list)) {
|
|
|
|
TAILQ_FOREACH_SAFE(io, &nbd->received_io_list, tailq, io_tmp) {
|
|
|
|
TAILQ_REMOVE(&nbd->received_io_list, io, tailq);
|
|
|
|
ret = nbd_submit_bdev_io(nbd, io);
|
|
|
|
if (ret < 0) {
|
2020-07-01 09:44:23 +00:00
|
|
|
return ret;
|
2017-12-20 08:50:43 +00:00
|
|
|
}
|
2020-07-01 09:44:23 +00:00
|
|
|
|
|
|
|
io_count++;
|
2017-12-20 08:50:43 +00:00
|
|
|
}
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
2017-08-02 21:37:53 +00:00
|
|
|
|
2020-07-01 09:44:23 +00:00
|
|
|
return io_count;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-02 08:59:14 +00:00
|
|
|
static int
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_io_recv_internal(struct spdk_nbd_disk *nbd)
|
2017-07-14 22:20:19 +00:00
|
|
|
{
|
2017-12-20 08:50:43 +00:00
|
|
|
struct nbd_io *io;
|
2017-12-18 06:39:15 +00:00
|
|
|
int ret = 0;
|
2020-07-01 09:44:23 +00:00
|
|
|
int received = 0;
|
2017-07-14 22:20:19 +00:00
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
if (nbd->io_in_recv == NULL) {
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd->io_in_recv = nbd_get_io(nbd);
|
2018-03-12 22:16:26 +00:00
|
|
|
if (!nbd->io_in_recv) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2017-12-20 08:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
io = nbd->io_in_recv;
|
|
|
|
|
2017-12-18 06:39:15 +00:00
|
|
|
if (io->state == NBD_IO_RECV_REQ) {
|
2020-12-30 14:29:45 +00:00
|
|
|
ret = nbd_socket_rw(nbd->spdk_sp_fd, (char *)&io->req + io->offset,
|
|
|
|
sizeof(io->req) - io->offset, true);
|
2017-12-20 08:50:43 +00:00
|
|
|
if (ret < 0) {
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_put_io(nbd, io);
|
2017-12-20 08:50:43 +00:00
|
|
|
nbd->io_in_recv = NULL;
|
2017-08-02 21:37:53 +00:00
|
|
|
return ret;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
2017-12-18 06:39:15 +00:00
|
|
|
|
2017-07-14 22:20:19 +00:00
|
|
|
io->offset += ret;
|
2020-07-01 09:44:23 +00:00
|
|
|
received = ret;
|
2017-12-18 06:39:15 +00:00
|
|
|
|
|
|
|
/* request is fully received */
|
2017-07-14 22:20:19 +00:00
|
|
|
if (io->offset == sizeof(io->req)) {
|
2017-12-18 06:39:15 +00:00
|
|
|
io->offset = 0;
|
|
|
|
|
|
|
|
/* req magic check */
|
|
|
|
if (from_be32(&io->req.magic) != NBD_REQUEST_MAGIC) {
|
|
|
|
SPDK_ERRLOG("invalid request magic\n");
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_put_io(nbd, io);
|
2017-12-20 08:50:43 +00:00
|
|
|
nbd->io_in_recv = NULL;
|
2017-12-18 06:39:15 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* io except read/write should ignore payload */
|
|
|
|
if (from_be32(&io->req.type) == NBD_CMD_WRITE ||
|
|
|
|
from_be32(&io->req.type) == NBD_CMD_READ) {
|
|
|
|
io->payload_size = from_be32(&io->req.len);
|
|
|
|
} else {
|
|
|
|
io->payload_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* io payload allocate */
|
|
|
|
if (io->payload_size) {
|
2019-06-26 09:18:14 +00:00
|
|
|
io->payload = spdk_malloc(io->payload_size, nbd->buf_align, NULL,
|
|
|
|
SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
|
2017-12-18 06:39:15 +00:00
|
|
|
if (io->payload == NULL) {
|
|
|
|
SPDK_ERRLOG("could not allocate io->payload of size %d\n", io->payload_size);
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_put_io(nbd, io);
|
2017-12-20 08:50:43 +00:00
|
|
|
nbd->io_in_recv = NULL;
|
2017-12-18 06:39:15 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
io->payload = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* next io step */
|
|
|
|
if (from_be32(&io->req.type) == NBD_CMD_WRITE) {
|
|
|
|
io->state = NBD_IO_RECV_PAYLOAD;
|
|
|
|
} else {
|
2017-12-20 08:50:43 +00:00
|
|
|
io->state = NBD_IO_XMIT_RESP;
|
2021-02-08 11:25:32 +00:00
|
|
|
if (spdk_likely(nbd->state == NBD_DISK_STATE_RUNNING)) {
|
|
|
|
TAILQ_INSERT_TAIL(&nbd->received_io_list, io, tailq);
|
|
|
|
} else {
|
|
|
|
nbd_io_done(NULL, false, io);
|
|
|
|
}
|
2017-12-20 08:50:43 +00:00
|
|
|
nbd->io_in_recv = NULL;
|
2017-08-02 21:37:53 +00:00
|
|
|
}
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 06:39:15 +00:00
|
|
|
if (io->state == NBD_IO_RECV_PAYLOAD) {
|
2020-12-30 14:29:45 +00:00
|
|
|
ret = nbd_socket_rw(nbd->spdk_sp_fd, io->payload + io->offset, io->payload_size - io->offset, true);
|
2017-12-20 08:50:43 +00:00
|
|
|
if (ret < 0) {
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_put_io(nbd, io);
|
2017-12-20 08:50:43 +00:00
|
|
|
nbd->io_in_recv = NULL;
|
2017-08-02 21:37:53 +00:00
|
|
|
return ret;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
2017-12-18 06:39:15 +00:00
|
|
|
|
2017-07-14 22:20:19 +00:00
|
|
|
io->offset += ret;
|
2020-07-01 09:44:23 +00:00
|
|
|
received += ret;
|
2017-12-18 06:39:15 +00:00
|
|
|
|
|
|
|
/* request payload is fully received */
|
2017-07-14 22:20:19 +00:00
|
|
|
if (io->offset == io->payload_size) {
|
|
|
|
io->offset = 0;
|
2017-12-20 08:50:43 +00:00
|
|
|
io->state = NBD_IO_XMIT_RESP;
|
2021-02-08 11:25:32 +00:00
|
|
|
if (spdk_likely(nbd->state == NBD_DISK_STATE_RUNNING)) {
|
|
|
|
TAILQ_INSERT_TAIL(&nbd->received_io_list, io, tailq);
|
|
|
|
} else {
|
|
|
|
nbd_io_done(NULL, false, io);
|
|
|
|
}
|
2017-12-20 08:50:43 +00:00
|
|
|
nbd->io_in_recv = NULL;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
2017-12-18 06:39:15 +00:00
|
|
|
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 09:44:23 +00:00
|
|
|
return received;
|
2017-12-18 06:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_io_recv(struct spdk_nbd_disk *nbd)
|
2017-12-18 06:39:15 +00:00
|
|
|
{
|
2020-07-01 09:44:23 +00:00
|
|
|
int i, rc, ret = 0;
|
2017-12-20 08:50:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* nbd server should not accept request in both soft and hard
|
|
|
|
* disconnect states.
|
|
|
|
*/
|
|
|
|
if (nbd->state != NBD_DISK_STATE_RUNNING) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < GET_IO_LOOP_COUNT; i++) {
|
2020-07-01 09:44:23 +00:00
|
|
|
rc = nbd_io_recv_internal(nbd);
|
|
|
|
if (rc < 0) {
|
|
|
|
return rc;
|
2017-12-20 08:50:43 +00:00
|
|
|
}
|
2020-07-01 09:44:23 +00:00
|
|
|
ret += rc;
|
2017-12-20 08:50:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 09:44:23 +00:00
|
|
|
return ret;
|
2017-12-20 08:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_io_xmit_internal(struct spdk_nbd_disk *nbd)
|
2017-12-20 08:50:43 +00:00
|
|
|
{
|
|
|
|
struct nbd_io *io;
|
2017-12-18 06:39:15 +00:00
|
|
|
int ret = 0;
|
2020-07-01 09:44:23 +00:00
|
|
|
int sent = 0;
|
2017-12-18 06:39:15 +00:00
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
io = TAILQ_FIRST(&nbd->executed_io_list);
|
|
|
|
if (io == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-01 01:17:43 +00:00
|
|
|
/* Remove IO from list now assuming it will be completed. It will be inserted
|
|
|
|
* back to the head if it cannot be completed. This approach is specifically
|
|
|
|
* taken to work around a scan-build use-after-free mischaracterization.
|
|
|
|
*/
|
|
|
|
TAILQ_REMOVE(&nbd->executed_io_list, io, tailq);
|
|
|
|
|
2017-12-18 06:39:15 +00:00
|
|
|
/* resp error and handler are already set in io_done */
|
|
|
|
|
|
|
|
if (io->state == NBD_IO_XMIT_RESP) {
|
2020-12-30 14:29:45 +00:00
|
|
|
ret = nbd_socket_rw(nbd->spdk_sp_fd, (char *)&io->resp + io->offset,
|
|
|
|
sizeof(io->resp) - io->offset, false);
|
2017-08-02 21:37:53 +00:00
|
|
|
if (ret <= 0) {
|
2018-10-01 07:26:27 +00:00
|
|
|
goto reinsert;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
2017-12-18 06:39:15 +00:00
|
|
|
|
2017-07-14 22:20:19 +00:00
|
|
|
io->offset += ret;
|
2020-07-01 09:44:23 +00:00
|
|
|
sent = ret;
|
2017-12-18 06:39:15 +00:00
|
|
|
|
|
|
|
/* response is fully transmitted */
|
2017-07-14 22:20:19 +00:00
|
|
|
if (io->offset == sizeof(io->resp)) {
|
2017-12-18 06:39:15 +00:00
|
|
|
io->offset = 0;
|
|
|
|
|
|
|
|
/* transmit payload only when NBD_CMD_READ with no resp error */
|
|
|
|
if (from_be32(&io->req.type) != NBD_CMD_READ || io->resp.error != 0) {
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_put_io(nbd, io);
|
2017-12-20 08:50:43 +00:00
|
|
|
return 0;
|
2017-07-14 22:20:19 +00:00
|
|
|
} else {
|
2017-12-18 06:39:15 +00:00
|
|
|
io->state = NBD_IO_XMIT_PAYLOAD;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 06:39:15 +00:00
|
|
|
if (io->state == NBD_IO_XMIT_PAYLOAD) {
|
2020-12-30 14:29:45 +00:00
|
|
|
ret = nbd_socket_rw(nbd->spdk_sp_fd, io->payload + io->offset, io->payload_size - io->offset,
|
|
|
|
false);
|
2017-08-02 21:37:53 +00:00
|
|
|
if (ret <= 0) {
|
2018-10-01 07:26:27 +00:00
|
|
|
goto reinsert;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
2017-12-18 06:39:15 +00:00
|
|
|
|
2017-07-14 22:20:19 +00:00
|
|
|
io->offset += ret;
|
2020-07-01 09:44:23 +00:00
|
|
|
sent += ret;
|
2017-12-18 06:39:15 +00:00
|
|
|
|
|
|
|
/* read payload is fully transmitted */
|
2017-07-14 22:20:19 +00:00
|
|
|
if (io->offset == io->payload_size) {
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_put_io(nbd, io);
|
2020-07-01 09:44:23 +00:00
|
|
|
return sent;
|
2017-12-20 08:50:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 07:26:27 +00:00
|
|
|
reinsert:
|
2018-10-01 01:17:43 +00:00
|
|
|
TAILQ_INSERT_HEAD(&nbd->executed_io_list, io, tailq);
|
2020-07-01 09:44:23 +00:00
|
|
|
return ret < 0 ? ret : sent;
|
2017-12-20 08:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_io_xmit(struct spdk_nbd_disk *nbd)
|
2017-12-20 08:50:43 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2020-07-01 09:44:23 +00:00
|
|
|
int rc;
|
2017-12-20 08:50:43 +00:00
|
|
|
|
|
|
|
while (!TAILQ_EMPTY(&nbd->executed_io_list)) {
|
2020-07-01 09:44:23 +00:00
|
|
|
rc = nbd_io_xmit_internal(nbd);
|
|
|
|
if (rc < 0) {
|
|
|
|
return rc;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
2020-07-01 09:44:23 +00:00
|
|
|
|
|
|
|
ret += rc;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
2017-08-02 21:37:53 +00:00
|
|
|
|
2020-09-16 20:33:19 +00:00
|
|
|
/* When there begins to have no executed_io, disable socket writable notice */
|
2021-01-02 21:52:46 +00:00
|
|
|
if (nbd->interrupt_mode) {
|
2020-09-16 20:33:19 +00:00
|
|
|
spdk_interrupt_set_event_types(nbd->intr, SPDK_INTERRUPT_EVENT_IN);
|
|
|
|
}
|
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
/*
|
|
|
|
* For soft disconnection, nbd server can close connection after all
|
|
|
|
* outstanding request are transmitted.
|
|
|
|
*/
|
2020-05-07 17:27:31 +00:00
|
|
|
if (nbd->state == NBD_DISK_STATE_SOFTDISC && !nbd_io_xmit_check(nbd)) {
|
2017-12-20 08:50:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-07-01 09:44:23 +00:00
|
|
|
return ret;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 06:39:15 +00:00
|
|
|
/**
|
|
|
|
* Poll an NBD instance.
|
|
|
|
*
|
|
|
|
* \return 0 on success or negated errno values on error (e.g. connection closed).
|
|
|
|
*/
|
|
|
|
static int
|
2020-05-07 17:27:31 +00:00
|
|
|
_nbd_poll(struct spdk_nbd_disk *nbd)
|
2017-12-18 06:39:15 +00:00
|
|
|
{
|
2020-07-01 09:44:23 +00:00
|
|
|
int received, sent, executed;
|
2017-12-18 06:39:15 +00:00
|
|
|
|
2017-12-20 08:50:43 +00:00
|
|
|
/* transmit executed io first */
|
2020-07-01 09:44:23 +00:00
|
|
|
sent = nbd_io_xmit(nbd);
|
|
|
|
if (sent < 0) {
|
|
|
|
return sent;
|
2017-12-18 06:39:15 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 09:44:23 +00:00
|
|
|
received = nbd_io_recv(nbd);
|
|
|
|
if (received < 0) {
|
|
|
|
return received;
|
2017-12-20 08:50:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 09:44:23 +00:00
|
|
|
executed = nbd_io_exec(nbd);
|
|
|
|
if (executed < 0) {
|
|
|
|
return executed;
|
|
|
|
}
|
2017-12-18 06:39:15 +00:00
|
|
|
|
2020-07-01 09:44:23 +00:00
|
|
|
return sent + received + executed;
|
2017-12-18 06:39:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:16:47 +00:00
|
|
|
static int
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_poll(void *arg)
|
2017-12-02 08:59:14 +00:00
|
|
|
{
|
|
|
|
struct spdk_nbd_disk *nbd = arg;
|
|
|
|
int rc;
|
|
|
|
|
2020-05-07 17:27:31 +00:00
|
|
|
rc = _nbd_poll(nbd);
|
2017-12-02 08:59:14 +00:00
|
|
|
if (rc < 0) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_INFOLOG(nbd, "nbd_poll() returned %s (%d); closing connection\n",
|
2017-11-07 12:56:52 +00:00
|
|
|
spdk_strerror(-rc), rc);
|
2017-12-02 08:59:14 +00:00
|
|
|
spdk_nbd_stop(nbd);
|
|
|
|
}
|
2018-03-13 00:16:47 +00:00
|
|
|
|
2020-07-01 09:44:23 +00:00
|
|
|
return rc > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
|
2017-12-02 08:59:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 19:52:37 +00:00
|
|
|
static void *
|
|
|
|
nbd_start_kernel(void *arg)
|
2017-07-14 22:20:19 +00:00
|
|
|
{
|
2021-02-05 16:24:01 +00:00
|
|
|
struct spdk_nbd_disk *nbd = arg;
|
2017-07-14 22:20:19 +00:00
|
|
|
|
2017-11-21 19:52:37 +00:00
|
|
|
spdk_unaffinitize_thread();
|
2017-07-14 22:20:19 +00:00
|
|
|
|
2017-11-21 19:52:37 +00:00
|
|
|
/* This will block in the kernel until we close the spdk_sp_fd. */
|
2021-02-05 16:24:01 +00:00
|
|
|
ioctl(nbd->dev_fd, NBD_DO_IT);
|
|
|
|
|
|
|
|
nbd->has_nbd_pthread = false;
|
2017-12-14 05:29:08 +00:00
|
|
|
|
2017-11-21 19:52:37 +00:00
|
|
|
pthread_exit(NULL);
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 08:05:53 +00:00
|
|
|
static void
|
2020-10-14 22:54:33 +00:00
|
|
|
nbd_bdev_hot_remove(struct spdk_nbd_disk *nbd)
|
2017-12-14 08:05:53 +00:00
|
|
|
{
|
|
|
|
spdk_nbd_stop(nbd);
|
|
|
|
}
|
|
|
|
|
2020-10-14 22:54:33 +00:00
|
|
|
static void
|
|
|
|
nbd_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev,
|
|
|
|
void *event_ctx)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case SPDK_BDEV_EVENT_REMOVE:
|
|
|
|
nbd_bdev_hot_remove(event_ctx);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 20:01:57 +00:00
|
|
|
struct spdk_nbd_start_ctx {
|
|
|
|
struct spdk_nbd_disk *nbd;
|
|
|
|
spdk_nbd_start_cb cb_fn;
|
|
|
|
void *cb_arg;
|
|
|
|
};
|
|
|
|
|
2021-01-02 21:52:46 +00:00
|
|
|
static void
|
|
|
|
nbd_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode)
|
|
|
|
{
|
|
|
|
struct spdk_nbd_disk *nbd = cb_arg;
|
|
|
|
|
|
|
|
nbd->interrupt_mode = interrupt_mode;
|
|
|
|
}
|
|
|
|
|
2018-11-19 20:01:57 +00:00
|
|
|
static void
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_start_complete(struct spdk_nbd_start_ctx *ctx)
|
2018-11-19 20:01:57 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
pthread_t tid;
|
2021-02-23 10:29:46 +00:00
|
|
|
unsigned long nbd_flags = 0;
|
2018-11-19 20:01:57 +00:00
|
|
|
|
2019-02-14 12:27:08 +00:00
|
|
|
rc = ioctl(ctx->nbd->dev_fd, NBD_SET_BLKSIZE, spdk_bdev_get_block_size(ctx->nbd->bdev));
|
|
|
|
if (rc == -1) {
|
|
|
|
SPDK_ERRLOG("ioctl(NBD_SET_BLKSIZE) failed: %s\n", spdk_strerror(errno));
|
|
|
|
rc = -errno;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ioctl(ctx->nbd->dev_fd, NBD_SET_SIZE_BLOCKS, spdk_bdev_get_num_blocks(ctx->nbd->bdev));
|
|
|
|
if (rc == -1) {
|
|
|
|
SPDK_ERRLOG("ioctl(NBD_SET_SIZE_BLOCKS) failed: %s\n", spdk_strerror(errno));
|
|
|
|
rc = -errno;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-02-01 13:19:14 +00:00
|
|
|
#ifdef NBD_SET_TIMEOUT
|
|
|
|
rc = ioctl(ctx->nbd->dev_fd, NBD_SET_TIMEOUT, NBD_IO_TIMEOUT_S);
|
|
|
|
if (rc == -1) {
|
|
|
|
SPDK_ERRLOG("ioctl(NBD_SET_TIMEOUT) failed: %s\n", spdk_strerror(errno));
|
|
|
|
rc = -errno;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
SPDK_NOTICELOG("ioctl(NBD_SET_TIMEOUT) is not supported.\n");
|
|
|
|
#endif
|
|
|
|
|
2021-02-23 10:29:46 +00:00
|
|
|
#ifdef NBD_FLAG_SEND_FLUSH
|
|
|
|
nbd_flags |= NBD_FLAG_SEND_FLUSH;
|
|
|
|
#endif
|
2018-11-19 20:01:57 +00:00
|
|
|
#ifdef NBD_FLAG_SEND_TRIM
|
2021-02-23 10:29:46 +00:00
|
|
|
nbd_flags |= NBD_FLAG_SEND_TRIM;
|
2018-11-19 20:01:57 +00:00
|
|
|
#endif
|
2021-02-23 10:29:46 +00:00
|
|
|
if (nbd_flags) {
|
|
|
|
rc = ioctl(ctx->nbd->dev_fd, NBD_SET_FLAGS, nbd_flags);
|
|
|
|
if (rc == -1) {
|
|
|
|
SPDK_ERRLOG("ioctl(NBD_SET_FLAGS, 0x%lx) failed: %s\n", nbd_flags, spdk_strerror(errno));
|
|
|
|
rc = -errno;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2018-11-19 20:01:57 +00:00
|
|
|
|
2021-02-05 16:24:01 +00:00
|
|
|
ctx->nbd->has_nbd_pthread = true;
|
|
|
|
rc = pthread_create(&tid, NULL, nbd_start_kernel, ctx->nbd);
|
2018-11-19 20:01:57 +00:00
|
|
|
if (rc != 0) {
|
2021-02-05 16:24:01 +00:00
|
|
|
ctx->nbd->has_nbd_pthread = false;
|
2018-11-19 20:01:57 +00:00
|
|
|
SPDK_ERRLOG("could not create thread: %s\n", spdk_strerror(rc));
|
2018-11-22 08:46:40 +00:00
|
|
|
rc = -rc;
|
2018-11-19 20:01:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = pthread_detach(tid);
|
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("could not detach thread for nbd kernel: %s\n", spdk_strerror(rc));
|
2018-11-22 08:46:40 +00:00
|
|
|
rc = -rc;
|
2018-11-19 20:01:57 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2020-09-16 20:33:19 +00:00
|
|
|
if (spdk_interrupt_mode_is_enabled()) {
|
|
|
|
ctx->nbd->intr = SPDK_INTERRUPT_REGISTER(ctx->nbd->spdk_sp_fd, nbd_poll, ctx->nbd);
|
|
|
|
}
|
2018-11-19 20:01:57 +00:00
|
|
|
|
2021-01-02 21:52:46 +00:00
|
|
|
ctx->nbd->nbd_poller = SPDK_POLLER_REGISTER(nbd_poll, ctx->nbd, 0);
|
|
|
|
spdk_poller_register_interrupt(ctx->nbd->nbd_poller, nbd_poller_set_interrupt_mode, ctx->nbd);
|
|
|
|
|
2018-11-19 20:01:57 +00:00
|
|
|
if (ctx->cb_fn) {
|
|
|
|
ctx->cb_fn(ctx->cb_arg, ctx->nbd, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ctx);
|
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
spdk_nbd_stop(ctx->nbd);
|
|
|
|
if (ctx->cb_fn) {
|
|
|
|
ctx->cb_fn(ctx->cb_arg, NULL, rc);
|
|
|
|
}
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
2018-11-19 21:03:26 +00:00
|
|
|
static int
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_enable_kernel(void *arg)
|
2018-11-19 21:03:26 +00:00
|
|
|
{
|
|
|
|
struct spdk_nbd_start_ctx *ctx = arg;
|
|
|
|
int rc;
|
|
|
|
|
2019-02-14 12:27:08 +00:00
|
|
|
/* Declare device setup by this process */
|
2018-11-19 21:03:26 +00:00
|
|
|
rc = ioctl(ctx->nbd->dev_fd, NBD_SET_SOCK, ctx->nbd->kernel_sp_fd);
|
2020-12-30 14:45:38 +00:00
|
|
|
|
|
|
|
if (rc) {
|
2021-02-05 12:44:39 +00:00
|
|
|
if (errno == EBUSY) {
|
|
|
|
if (ctx->nbd->retry_poller == NULL) {
|
|
|
|
ctx->nbd->retry_count = NBD_START_BUSY_WAITING_MS * 1000ULL / NBD_BUSY_POLLING_INTERVAL_US;
|
|
|
|
ctx->nbd->retry_poller = SPDK_POLLER_REGISTER(nbd_enable_kernel, ctx,
|
|
|
|
NBD_BUSY_POLLING_INTERVAL_US);
|
|
|
|
return SPDK_POLLER_BUSY;
|
|
|
|
} else if (ctx->nbd->retry_count-- > 0) {
|
|
|
|
/* Repeatedly unregiter and register retry poller to avoid scan-build error */
|
|
|
|
spdk_poller_unregister(&ctx->nbd->retry_poller);
|
|
|
|
ctx->nbd->retry_poller = SPDK_POLLER_REGISTER(nbd_enable_kernel, ctx,
|
|
|
|
NBD_BUSY_POLLING_INTERVAL_US);
|
|
|
|
return SPDK_POLLER_BUSY;
|
2018-11-19 21:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDK_ERRLOG("ioctl(NBD_SET_SOCK) failed: %s\n", spdk_strerror(errno));
|
2021-02-05 12:44:39 +00:00
|
|
|
if (ctx->nbd->retry_poller) {
|
|
|
|
spdk_poller_unregister(&ctx->nbd->retry_poller);
|
2018-11-19 21:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spdk_nbd_stop(ctx->nbd);
|
|
|
|
|
|
|
|
if (ctx->cb_fn) {
|
|
|
|
ctx->cb_fn(ctx->cb_arg, NULL, -errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ctx);
|
2020-05-04 09:51:27 +00:00
|
|
|
return SPDK_POLLER_BUSY;
|
2018-11-19 21:03:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 12:44:39 +00:00
|
|
|
if (ctx->nbd->retry_poller) {
|
|
|
|
spdk_poller_unregister(&ctx->nbd->retry_poller);
|
2018-11-19 21:03:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_start_complete(ctx);
|
2018-11-19 21:03:26 +00:00
|
|
|
|
2020-05-04 09:51:27 +00:00
|
|
|
return SPDK_POLLER_BUSY;
|
2018-11-19 21:03:26 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 20:44:03 +00:00
|
|
|
void
|
|
|
|
spdk_nbd_start(const char *bdev_name, const char *nbd_path,
|
|
|
|
spdk_nbd_start_cb cb_fn, void *cb_arg)
|
2017-07-14 22:20:19 +00:00
|
|
|
{
|
2018-11-19 20:01:57 +00:00
|
|
|
struct spdk_nbd_start_ctx *ctx = NULL;
|
|
|
|
struct spdk_nbd_disk *nbd = NULL;
|
|
|
|
struct spdk_bdev *bdev;
|
|
|
|
int rc;
|
|
|
|
int sp[2];
|
2017-07-14 22:20:19 +00:00
|
|
|
|
2017-08-02 17:40:38 +00:00
|
|
|
nbd = calloc(1, sizeof(*nbd));
|
|
|
|
if (nbd == NULL) {
|
2018-11-19 20:44:03 +00:00
|
|
|
rc = -ENOMEM;
|
|
|
|
goto err;
|
2017-08-02 17:40:38 +00:00
|
|
|
}
|
2017-12-18 06:39:15 +00:00
|
|
|
|
2017-11-21 19:51:57 +00:00
|
|
|
nbd->dev_fd = -1;
|
|
|
|
nbd->spdk_sp_fd = -1;
|
|
|
|
nbd->kernel_sp_fd = -1;
|
2017-08-02 17:40:38 +00:00
|
|
|
|
2018-11-19 20:01:57 +00:00
|
|
|
ctx = calloc(1, sizeof(*ctx));
|
|
|
|
if (ctx == NULL) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->nbd = nbd;
|
|
|
|
ctx->cb_fn = cb_fn;
|
|
|
|
ctx->cb_arg = cb_arg;
|
|
|
|
|
2020-10-14 22:54:33 +00:00
|
|
|
rc = spdk_bdev_open_ext(bdev_name, true, nbd_bdev_event_cb, nbd, &nbd->bdev_desc);
|
2017-07-14 22:20:19 +00:00
|
|
|
if (rc != 0) {
|
2020-10-14 22:54:33 +00:00
|
|
|
SPDK_ERRLOG("could not open bdev %s, error=%d\n", bdev_name, rc);
|
2017-08-02 17:40:38 +00:00
|
|
|
goto err;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 22:54:33 +00:00
|
|
|
bdev = spdk_bdev_desc_get_bdev(nbd->bdev_desc);
|
2017-08-02 17:40:38 +00:00
|
|
|
nbd->bdev = bdev;
|
2017-12-04 05:46:13 +00:00
|
|
|
|
2017-08-02 17:40:38 +00:00
|
|
|
nbd->ch = spdk_bdev_get_io_channel(nbd->bdev_desc);
|
|
|
|
nbd->buf_align = spdk_max(spdk_bdev_get_buf_align(bdev), 64);
|
2017-07-14 22:20:19 +00:00
|
|
|
|
2021-02-19 19:46:25 +00:00
|
|
|
rc = socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, sp);
|
2017-07-14 22:20:19 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("socketpair failed\n");
|
2018-11-22 08:46:40 +00:00
|
|
|
rc = -errno;
|
2017-08-02 17:40:38 +00:00
|
|
|
goto err;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 19:51:57 +00:00
|
|
|
nbd->spdk_sp_fd = sp[0];
|
|
|
|
nbd->kernel_sp_fd = sp[1];
|
2017-12-04 06:56:49 +00:00
|
|
|
nbd->nbd_path = strdup(nbd_path);
|
|
|
|
if (!nbd->nbd_path) {
|
|
|
|
SPDK_ERRLOG("strdup allocation failure\n");
|
2018-11-19 20:44:03 +00:00
|
|
|
rc = -ENOMEM;
|
2017-12-04 06:56:49 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2017-12-20 08:50:43 +00:00
|
|
|
|
|
|
|
TAILQ_INIT(&nbd->received_io_list);
|
|
|
|
TAILQ_INIT(&nbd->executed_io_list);
|
|
|
|
|
2021-01-27 17:46:38 +00:00
|
|
|
/* Add nbd_disk to the end of disk list */
|
|
|
|
nbd_disk_register(ctx->nbd);
|
|
|
|
if (rc != 0) {
|
2017-12-04 05:46:13 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2017-12-04 06:56:49 +00:00
|
|
|
|
2020-08-04 16:09:09 +00:00
|
|
|
nbd->dev_fd = open(nbd_path, O_RDWR | O_DIRECT);
|
2017-11-21 19:51:57 +00:00
|
|
|
if (nbd->dev_fd == -1) {
|
2017-11-07 12:56:52 +00:00
|
|
|
SPDK_ERRLOG("open(\"%s\") failed: %s\n", nbd_path, spdk_strerror(errno));
|
2018-11-19 20:44:03 +00:00
|
|
|
rc = -errno;
|
2017-08-02 17:40:38 +00:00
|
|
|
goto err;
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_INFOLOG(nbd, "Enabling kernel access to bdev %s via %s\n",
|
2020-10-14 22:54:33 +00:00
|
|
|
bdev_name, nbd_path);
|
2017-07-14 22:20:19 +00:00
|
|
|
|
2020-05-07 17:27:31 +00:00
|
|
|
nbd_enable_kernel(ctx);
|
2018-11-19 20:44:03 +00:00
|
|
|
return;
|
2017-08-02 17:40:38 +00:00
|
|
|
|
|
|
|
err:
|
2018-11-19 20:01:57 +00:00
|
|
|
free(ctx);
|
2018-11-19 20:44:03 +00:00
|
|
|
if (nbd) {
|
|
|
|
spdk_nbd_stop(nbd);
|
|
|
|
}
|
2017-08-02 17:40:38 +00:00
|
|
|
|
2018-11-19 20:44:03 +00:00
|
|
|
if (cb_fn) {
|
|
|
|
cb_fn(cb_arg, NULL, rc);
|
|
|
|
}
|
2017-07-14 22:20:19 +00:00
|
|
|
}
|
2017-12-08 19:54:11 +00:00
|
|
|
|
2018-11-19 20:14:56 +00:00
|
|
|
const char *
|
|
|
|
spdk_nbd_get_path(struct spdk_nbd_disk *nbd)
|
|
|
|
{
|
|
|
|
return nbd->nbd_path;
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_LOG_REGISTER_COMPONENT(nbd)
|