mirror of
https://github.com/izzy2lost/xemu.git
synced 2026-07-06 00:20:22 -07:00
Merge tag 'for-upstream' of git://repo.or.cz/qemu/kevin into staging
Block layer patches - Add vduse-blk export - Dirty bitmaps: Fix and improve bitmap merge - gluster: correctly set max_pdiscard - rbd: report a better error when namespace does not exist - aio_wait_kick: add missing memory barrier - Code cleanups # -----BEGIN PGP SIGNATURE----- # # iQJFBAABCAAvFiEE3D3rFZqa+V09dFb+fwmycsiPL9YFAmK10yURHGt3b2xmQHJl # ZGhhdC5jb20ACgkQfwmycsiPL9YnXg/+J9PKsGl7hbJUFVIb4RZ3pMQ0o4xC1TbS # OjA63RRwdJWt1gGsgLNH6kdsgz04FXiXk7XNDTe8MYn21bvznCYGFsI2EFJiit+x # HkOYXJwAGsrfKBViq8QznosBQbwf9fOrDUlOdbnixFH/dx6DXUZM2F9ud5shiuiC # vrP76zjUgSlHhPwL+0xsJSNxxNISv845uN9SYNpiGbZDaYLBtNbEc8NA2GMOYW45 # sCbxJDxNSFNrTT5gaZyo4KGFH9393qJSmYHJ+q53elbhje5dpmcn2c/AkIG7/UM4 # HsEWd7Gj1YbLQV6kvkefJzF4BOuZmN/b8sG+LRDjrEMHLeOw5rh2ETmkQfIdy2H3 # 2U52mHqw9bMGWtds0ocOJFKw3vPZuOCJaA+ql/A0aWGO9Fjivx1cQcOVXn3jKgj5 # 7riSxigK9TCTd92Qhi3j4do86mJi9NpjgawbQ5oOvKqUiq29moOwcv1hit0WVzwB # hJp2qsrM6w00wFseyp+10vHigW7ZMNEIh4TKYLfZ15Gvy6YIG/m/oyCYF4oVM5y5 # knbsnJBRpTjLnggag86Pk8Ji+Lxi0cbv7zrbn++/S9OLQ5VMV5OpxKXHmyX2HyXa # RCV0rMaTC3s4hy8gElDSwSeor6NBaJ9TECcit67uJ/kw1eK+omoa4hKMCgKf6qYC # GY+NRPELJGA= # =OuiN # -----END PGP SIGNATURE----- # gpg: Signature made Fri 24 Jun 2022 08:07:17 AM PDT # gpg: using RSA key DC3DEB159A9AF95D3D7456FE7F09B272C88F2FD6 # gpg: issuer "kwolf@redhat.com" # gpg: Good signature from "Kevin Wolf <kwolf@redhat.com>" [full] * tag 'for-upstream' of git://repo.or.cz/qemu/kevin: vduse-blk: Add name option vduse-blk: Add serial option nbd: Drop dead code spotted by Coverity aio_wait_kick: add missing memory barrier block/gluster: correctly set max_pdiscard block/rbd: report a better error when namespace does not exist qsd: document vduse-blk exports libvduse: Add support for reconnecting vduse-blk: Add vduse-blk resize support vduse-blk: Implement vduse-blk export libvduse: Add VDUSE (vDPA Device in Userspace) library linux-headers: Add vduse.h block/export: Abstract out the logic of virtio-blk I/O process block/export: Fix incorrect length passed to vu_queue_push() block: Support passing NULL ops to blk_set_dev_ops() block: simplify handling of try to merge different sized bitmaps block: improve block_dirty_bitmap_merge(): don't allocate extra bitmap block: block_dirty_bitmap_merge(): fix error path block: get rid of blk->guest_block_size block: drop unused bdrv_co_drain() API Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
@@ -3580,6 +3580,8 @@ M: Coiby Xu <Coiby.Xu@gmail.com>
|
||||
S: Maintained
|
||||
F: block/export/vhost-user-blk-server.c
|
||||
F: block/export/vhost-user-blk-server.h
|
||||
F: block/export/virtio-blk-handler.c
|
||||
F: block/export/virtio-blk-handler.h
|
||||
F: include/qemu/vhost-user-server.h
|
||||
F: tests/qtest/libqos/vhost-user-blk.c
|
||||
F: tests/qtest/libqos/vhost-user-blk.h
|
||||
@@ -3592,6 +3594,13 @@ L: qemu-block@nongnu.org
|
||||
S: Supported
|
||||
F: block/export/fuse.c
|
||||
|
||||
VDUSE library and block device exports
|
||||
M: Xie Yongji <xieyongji@bytedance.com>
|
||||
S: Maintained
|
||||
F: subprojects/libvduse/
|
||||
F: block/export/vduse-blk.c
|
||||
F: block/export/vduse-blk.h
|
||||
|
||||
Replication
|
||||
M: Wen Congyang <wencongyang2@huawei.com>
|
||||
M: Xie Changlong <xiechanglong.d@gmail.com>
|
||||
|
||||
+2
-4
@@ -228,15 +228,13 @@ out:
|
||||
|
||||
static void backup_init_bcs_bitmap(BackupBlockJob *job)
|
||||
{
|
||||
bool ret;
|
||||
uint64_t estimate;
|
||||
BdrvDirtyBitmap *bcs_bitmap = block_copy_dirty_bitmap(job->bcs);
|
||||
|
||||
if (job->sync_mode == MIRROR_SYNC_MODE_BITMAP) {
|
||||
bdrv_clear_dirty_bitmap(bcs_bitmap, NULL);
|
||||
ret = bdrv_dirty_bitmap_merge_internal(bcs_bitmap, job->sync_bitmap,
|
||||
NULL, true);
|
||||
assert(ret);
|
||||
bdrv_dirty_bitmap_merge_internal(bcs_bitmap, job->sync_bitmap, NULL,
|
||||
true);
|
||||
} else if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
|
||||
/*
|
||||
* We can't hog the coroutine to initialize this thoroughly.
|
||||
|
||||
+1
-11
@@ -56,9 +56,6 @@ struct BlockBackend {
|
||||
const BlockDevOps *dev_ops;
|
||||
void *dev_opaque;
|
||||
|
||||
/* the block size for which the guest device expects atomicity */
|
||||
int guest_block_size;
|
||||
|
||||
/* If the BDS tree is removed, some of its options are stored here (which
|
||||
* can be used to restore those options in the new BDS on insert) */
|
||||
BlockBackendRootState root_state;
|
||||
@@ -998,7 +995,6 @@ void blk_detach_dev(BlockBackend *blk, DeviceState *dev)
|
||||
blk->dev = NULL;
|
||||
blk->dev_ops = NULL;
|
||||
blk->dev_opaque = NULL;
|
||||
blk->guest_block_size = 512;
|
||||
blk_set_perm(blk, 0, BLK_PERM_ALL, &error_abort);
|
||||
blk_unref(blk);
|
||||
}
|
||||
@@ -1062,7 +1058,7 @@ void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
|
||||
blk->dev_opaque = opaque;
|
||||
|
||||
/* Are we currently quiesced? Should we enforce this right now? */
|
||||
if (blk->quiesce_counter && ops->drained_begin) {
|
||||
if (blk->quiesce_counter && ops && ops->drained_begin) {
|
||||
ops->drained_begin(opaque);
|
||||
}
|
||||
}
|
||||
@@ -2100,12 +2096,6 @@ int blk_get_max_iov(BlockBackend *blk)
|
||||
return blk->root->bs->bl.max_iov;
|
||||
}
|
||||
|
||||
void blk_set_guest_block_size(BlockBackend *blk, int align)
|
||||
{
|
||||
IO_CODE();
|
||||
blk->guest_block_size = align;
|
||||
}
|
||||
|
||||
void *blk_try_blockalign(BlockBackend *blk, size_t size)
|
||||
{
|
||||
IO_CODE();
|
||||
|
||||
+11
-15
@@ -309,10 +309,7 @@ BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BdrvDirtyBitmap *parent,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
|
||||
error_setg(errp, "Merging of parent and successor bitmap failed");
|
||||
return NULL;
|
||||
}
|
||||
hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap);
|
||||
|
||||
parent->disabled = successor->disabled;
|
||||
parent->busy = false;
|
||||
@@ -912,13 +909,15 @@ bool bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
|
||||
error_setg(errp, "Bitmaps are incompatible and can't be merged");
|
||||
if (bdrv_dirty_bitmap_size(src) != bdrv_dirty_bitmap_size(dest)) {
|
||||
error_setg(errp, "Bitmaps are of different sizes (destination size is %"
|
||||
PRId64 ", source size is %" PRId64 ") and can't be merged",
|
||||
bdrv_dirty_bitmap_size(dest), bdrv_dirty_bitmap_size(src));
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
|
||||
assert(ret);
|
||||
bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
|
||||
ret = true;
|
||||
|
||||
out:
|
||||
bdrv_dirty_bitmaps_unlock(dest->bs);
|
||||
@@ -932,17 +931,16 @@ out:
|
||||
/**
|
||||
* bdrv_dirty_bitmap_merge_internal: merge src into dest.
|
||||
* Does NOT check bitmap permissions; not suitable for use as public API.
|
||||
* @dest, @src and @backup (if not NULL) must have same size.
|
||||
*
|
||||
* @backup: If provided, make a copy of dest here prior to merge.
|
||||
* @lock: If true, lock and unlock bitmaps on the way in/out.
|
||||
* returns true if the merge succeeded; false if unattempted.
|
||||
*/
|
||||
bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
|
||||
void bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
|
||||
const BdrvDirtyBitmap *src,
|
||||
HBitmap **backup,
|
||||
bool lock)
|
||||
{
|
||||
bool ret;
|
||||
IO_CODE();
|
||||
|
||||
assert(!bdrv_dirty_bitmap_readonly(dest));
|
||||
@@ -959,9 +957,9 @@ bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
|
||||
if (backup) {
|
||||
*backup = dest->bitmap;
|
||||
dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
|
||||
ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap);
|
||||
hbitmap_merge(*backup, src->bitmap, dest->bitmap);
|
||||
} else {
|
||||
ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
|
||||
hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
|
||||
}
|
||||
|
||||
if (lock) {
|
||||
@@ -970,6 +968,4 @@ bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
|
||||
bdrv_dirty_bitmaps_unlock(src->bs);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,9 @@
|
||||
#ifdef CONFIG_VHOST_USER_BLK_SERVER
|
||||
#include "vhost-user-blk-server.h"
|
||||
#endif
|
||||
#ifdef CONFIG_VDUSE_BLK_EXPORT
|
||||
#include "vduse-blk.h"
|
||||
#endif
|
||||
|
||||
static const BlockExportDriver *blk_exp_drivers[] = {
|
||||
&blk_exp_nbd,
|
||||
@@ -35,6 +38,9 @@ static const BlockExportDriver *blk_exp_drivers[] = {
|
||||
#ifdef CONFIG_FUSE
|
||||
&blk_exp_fuse,
|
||||
#endif
|
||||
#ifdef CONFIG_VDUSE_BLK_EXPORT
|
||||
&blk_exp_vduse_blk,
|
||||
#endif
|
||||
};
|
||||
|
||||
/* Only accessed from the main thread */
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
blockdev_ss.add(files('export.c'))
|
||||
|
||||
if have_vhost_user_blk_server
|
||||
blockdev_ss.add(files('vhost-user-blk-server.c'))
|
||||
blockdev_ss.add(files('vhost-user-blk-server.c', 'virtio-blk-handler.c'))
|
||||
endif
|
||||
|
||||
blockdev_ss.add(when: fuse, if_true: files('fuse.c'))
|
||||
|
||||
if have_vduse_blk_export
|
||||
blockdev_ss.add(files('vduse-blk.c', 'virtio-blk-handler.c'))
|
||||
blockdev_ss.add(libvduse)
|
||||
endif
|
||||
|
||||
@@ -0,0 +1,374 @@
|
||||
/*
|
||||
* Export QEMU block device via VDUSE
|
||||
*
|
||||
* Copyright (C) 2022 Bytedance Inc. and/or its affiliates. All rights reserved.
|
||||
*
|
||||
* Author:
|
||||
* Xie Yongji <xieyongji@bytedance.com>
|
||||
*
|
||||
* This work is licensed under the terms of the GNU GPL, version 2 or
|
||||
* later. See the COPYING file in the top-level directory.
|
||||
*/
|
||||
|
||||
#include <sys/eventfd.h>
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qapi/error.h"
|
||||
#include "block/export.h"
|
||||
#include "qemu/error-report.h"
|
||||
#include "util/block-helpers.h"
|
||||
#include "subprojects/libvduse/libvduse.h"
|
||||
#include "virtio-blk-handler.h"
|
||||
|
||||
#include "standard-headers/linux/virtio_blk.h"
|
||||
|
||||
#define VDUSE_DEFAULT_NUM_QUEUE 1
|
||||
#define VDUSE_DEFAULT_QUEUE_SIZE 256
|
||||
|
||||
typedef struct VduseBlkExport {
|
||||
BlockExport export;
|
||||
VirtioBlkHandler handler;
|
||||
VduseDev *dev;
|
||||
uint16_t num_queues;
|
||||
char *recon_file;
|
||||
unsigned int inflight;
|
||||
} VduseBlkExport;
|
||||
|
||||
typedef struct VduseBlkReq {
|
||||
VduseVirtqElement elem;
|
||||
VduseVirtq *vq;
|
||||
} VduseBlkReq;
|
||||
|
||||
static void vduse_blk_inflight_inc(VduseBlkExport *vblk_exp)
|
||||
{
|
||||
vblk_exp->inflight++;
|
||||
}
|
||||
|
||||
static void vduse_blk_inflight_dec(VduseBlkExport *vblk_exp)
|
||||
{
|
||||
if (--vblk_exp->inflight == 0) {
|
||||
aio_wait_kick();
|
||||
}
|
||||
}
|
||||
|
||||
static void vduse_blk_req_complete(VduseBlkReq *req, size_t in_len)
|
||||
{
|
||||
vduse_queue_push(req->vq, &req->elem, in_len);
|
||||
vduse_queue_notify(req->vq);
|
||||
|
||||
free(req);
|
||||
}
|
||||
|
||||
static void coroutine_fn vduse_blk_virtio_process_req(void *opaque)
|
||||
{
|
||||
VduseBlkReq *req = opaque;
|
||||
VduseVirtq *vq = req->vq;
|
||||
VduseDev *dev = vduse_queue_get_dev(vq);
|
||||
VduseBlkExport *vblk_exp = vduse_dev_get_priv(dev);
|
||||
VirtioBlkHandler *handler = &vblk_exp->handler;
|
||||
VduseVirtqElement *elem = &req->elem;
|
||||
struct iovec *in_iov = elem->in_sg;
|
||||
struct iovec *out_iov = elem->out_sg;
|
||||
unsigned in_num = elem->in_num;
|
||||
unsigned out_num = elem->out_num;
|
||||
int in_len;
|
||||
|
||||
in_len = virtio_blk_process_req(handler, in_iov,
|
||||
out_iov, in_num, out_num);
|
||||
if (in_len < 0) {
|
||||
free(req);
|
||||
return;
|
||||
}
|
||||
|
||||
vduse_blk_req_complete(req, in_len);
|
||||
vduse_blk_inflight_dec(vblk_exp);
|
||||
}
|
||||
|
||||
static void vduse_blk_vq_handler(VduseDev *dev, VduseVirtq *vq)
|
||||
{
|
||||
VduseBlkExport *vblk_exp = vduse_dev_get_priv(dev);
|
||||
|
||||
while (1) {
|
||||
VduseBlkReq *req;
|
||||
|
||||
req = vduse_queue_pop(vq, sizeof(VduseBlkReq));
|
||||
if (!req) {
|
||||
break;
|
||||
}
|
||||
req->vq = vq;
|
||||
|
||||
Coroutine *co =
|
||||
qemu_coroutine_create(vduse_blk_virtio_process_req, req);
|
||||
|
||||
vduse_blk_inflight_inc(vblk_exp);
|
||||
qemu_coroutine_enter(co);
|
||||
}
|
||||
}
|
||||
|
||||
static void on_vduse_vq_kick(void *opaque)
|
||||
{
|
||||
VduseVirtq *vq = opaque;
|
||||
VduseDev *dev = vduse_queue_get_dev(vq);
|
||||
int fd = vduse_queue_get_fd(vq);
|
||||
eventfd_t kick_data;
|
||||
|
||||
if (eventfd_read(fd, &kick_data) == -1) {
|
||||
error_report("failed to read data from eventfd");
|
||||
return;
|
||||
}
|
||||
|
||||
vduse_blk_vq_handler(dev, vq);
|
||||
}
|
||||
|
||||
static void vduse_blk_enable_queue(VduseDev *dev, VduseVirtq *vq)
|
||||
{
|
||||
VduseBlkExport *vblk_exp = vduse_dev_get_priv(dev);
|
||||
|
||||
aio_set_fd_handler(vblk_exp->export.ctx, vduse_queue_get_fd(vq),
|
||||
true, on_vduse_vq_kick, NULL, NULL, NULL, vq);
|
||||
/* Make sure we don't miss any kick afer reconnecting */
|
||||
eventfd_write(vduse_queue_get_fd(vq), 1);
|
||||
}
|
||||
|
||||
static void vduse_blk_disable_queue(VduseDev *dev, VduseVirtq *vq)
|
||||
{
|
||||
VduseBlkExport *vblk_exp = vduse_dev_get_priv(dev);
|
||||
|
||||
aio_set_fd_handler(vblk_exp->export.ctx, vduse_queue_get_fd(vq),
|
||||
true, NULL, NULL, NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
static const VduseOps vduse_blk_ops = {
|
||||
.enable_queue = vduse_blk_enable_queue,
|
||||
.disable_queue = vduse_blk_disable_queue,
|
||||
};
|
||||
|
||||
static void on_vduse_dev_kick(void *opaque)
|
||||
{
|
||||
VduseDev *dev = opaque;
|
||||
|
||||
vduse_dev_handler(dev);
|
||||
}
|
||||
|
||||
static void vduse_blk_attach_ctx(VduseBlkExport *vblk_exp, AioContext *ctx)
|
||||
{
|
||||
int i;
|
||||
|
||||
aio_set_fd_handler(vblk_exp->export.ctx, vduse_dev_get_fd(vblk_exp->dev),
|
||||
true, on_vduse_dev_kick, NULL, NULL, NULL,
|
||||
vblk_exp->dev);
|
||||
|
||||
for (i = 0; i < vblk_exp->num_queues; i++) {
|
||||
VduseVirtq *vq = vduse_dev_get_queue(vblk_exp->dev, i);
|
||||
int fd = vduse_queue_get_fd(vq);
|
||||
|
||||
if (fd < 0) {
|
||||
continue;
|
||||
}
|
||||
aio_set_fd_handler(vblk_exp->export.ctx, fd, true,
|
||||
on_vduse_vq_kick, NULL, NULL, NULL, vq);
|
||||
}
|
||||
}
|
||||
|
||||
static void vduse_blk_detach_ctx(VduseBlkExport *vblk_exp)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < vblk_exp->num_queues; i++) {
|
||||
VduseVirtq *vq = vduse_dev_get_queue(vblk_exp->dev, i);
|
||||
int fd = vduse_queue_get_fd(vq);
|
||||
|
||||
if (fd < 0) {
|
||||
continue;
|
||||
}
|
||||
aio_set_fd_handler(vblk_exp->export.ctx, fd,
|
||||
true, NULL, NULL, NULL, NULL, NULL);
|
||||
}
|
||||
aio_set_fd_handler(vblk_exp->export.ctx, vduse_dev_get_fd(vblk_exp->dev),
|
||||
true, NULL, NULL, NULL, NULL, NULL);
|
||||
|
||||
AIO_WAIT_WHILE(vblk_exp->export.ctx, vblk_exp->inflight > 0);
|
||||
}
|
||||
|
||||
|
||||
static void blk_aio_attached(AioContext *ctx, void *opaque)
|
||||
{
|
||||
VduseBlkExport *vblk_exp = opaque;
|
||||
|
||||
vblk_exp->export.ctx = ctx;
|
||||
vduse_blk_attach_ctx(vblk_exp, ctx);
|
||||
}
|
||||
|
||||
static void blk_aio_detach(void *opaque)
|
||||
{
|
||||
VduseBlkExport *vblk_exp = opaque;
|
||||
|
||||
vduse_blk_detach_ctx(vblk_exp);
|
||||
vblk_exp->export.ctx = NULL;
|
||||
}
|
||||
|
||||
static void vduse_blk_resize(void *opaque)
|
||||
{
|
||||
BlockExport *exp = opaque;
|
||||
VduseBlkExport *vblk_exp = container_of(exp, VduseBlkExport, export);
|
||||
struct virtio_blk_config config;
|
||||
|
||||
config.capacity =
|
||||
cpu_to_le64(blk_getlength(exp->blk) >> VIRTIO_BLK_SECTOR_BITS);
|
||||
vduse_dev_update_config(vblk_exp->dev, sizeof(config.capacity),
|
||||
offsetof(struct virtio_blk_config, capacity),
|
||||
(char *)&config.capacity);
|
||||
}
|
||||
|
||||
static const BlockDevOps vduse_block_ops = {
|
||||
.resize_cb = vduse_blk_resize,
|
||||
};
|
||||
|
||||
static int vduse_blk_exp_create(BlockExport *exp, BlockExportOptions *opts,
|
||||
Error **errp)
|
||||
{
|
||||
VduseBlkExport *vblk_exp = container_of(exp, VduseBlkExport, export);
|
||||
BlockExportOptionsVduseBlk *vblk_opts = &opts->u.vduse_blk;
|
||||
uint64_t logical_block_size = VIRTIO_BLK_SECTOR_SIZE;
|
||||
uint16_t num_queues = VDUSE_DEFAULT_NUM_QUEUE;
|
||||
uint16_t queue_size = VDUSE_DEFAULT_QUEUE_SIZE;
|
||||
Error *local_err = NULL;
|
||||
struct virtio_blk_config config = { 0 };
|
||||
uint64_t features;
|
||||
int i, ret;
|
||||
|
||||
if (vblk_opts->has_num_queues) {
|
||||
num_queues = vblk_opts->num_queues;
|
||||
if (num_queues == 0) {
|
||||
error_setg(errp, "num-queues must be greater than 0");
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
if (vblk_opts->has_queue_size) {
|
||||
queue_size = vblk_opts->queue_size;
|
||||
if (queue_size <= 2 || !is_power_of_2(queue_size) ||
|
||||
queue_size > VIRTQUEUE_MAX_SIZE) {
|
||||
error_setg(errp, "queue-size is invalid");
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
if (vblk_opts->has_logical_block_size) {
|
||||
logical_block_size = vblk_opts->logical_block_size;
|
||||
check_block_size(exp->id, "logical-block-size", logical_block_size,
|
||||
&local_err);
|
||||
if (local_err) {
|
||||
error_propagate(errp, local_err);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
vblk_exp->num_queues = num_queues;
|
||||
vblk_exp->handler.blk = exp->blk;
|
||||
vblk_exp->handler.serial = g_strdup(vblk_opts->has_serial ?
|
||||
vblk_opts->serial : "");
|
||||
vblk_exp->handler.logical_block_size = logical_block_size;
|
||||
vblk_exp->handler.writable = opts->writable;
|
||||
|
||||
config.capacity =
|
||||
cpu_to_le64(blk_getlength(exp->blk) >> VIRTIO_BLK_SECTOR_BITS);
|
||||
config.seg_max = cpu_to_le32(queue_size - 2);
|
||||
config.min_io_size = cpu_to_le16(1);
|
||||
config.opt_io_size = cpu_to_le32(1);
|
||||
config.num_queues = cpu_to_le16(num_queues);
|
||||
config.blk_size = cpu_to_le32(logical_block_size);
|
||||
config.max_discard_sectors = cpu_to_le32(VIRTIO_BLK_MAX_DISCARD_SECTORS);
|
||||
config.max_discard_seg = cpu_to_le32(1);
|
||||
config.discard_sector_alignment =
|
||||
cpu_to_le32(logical_block_size >> VIRTIO_BLK_SECTOR_BITS);
|
||||
config.max_write_zeroes_sectors =
|
||||
cpu_to_le32(VIRTIO_BLK_MAX_WRITE_ZEROES_SECTORS);
|
||||
config.max_write_zeroes_seg = cpu_to_le32(1);
|
||||
|
||||
features = vduse_get_virtio_features() |
|
||||
(1ULL << VIRTIO_BLK_F_SEG_MAX) |
|
||||
(1ULL << VIRTIO_BLK_F_TOPOLOGY) |
|
||||
(1ULL << VIRTIO_BLK_F_BLK_SIZE) |
|
||||
(1ULL << VIRTIO_BLK_F_FLUSH) |
|
||||
(1ULL << VIRTIO_BLK_F_DISCARD) |
|
||||
(1ULL << VIRTIO_BLK_F_WRITE_ZEROES);
|
||||
|
||||
if (num_queues > 1) {
|
||||
features |= 1ULL << VIRTIO_BLK_F_MQ;
|
||||
}
|
||||
if (!opts->writable) {
|
||||
features |= 1ULL << VIRTIO_BLK_F_RO;
|
||||
}
|
||||
|
||||
vblk_exp->dev = vduse_dev_create(vblk_opts->name, VIRTIO_ID_BLOCK, 0,
|
||||
features, num_queues,
|
||||
sizeof(struct virtio_blk_config),
|
||||
(char *)&config, &vduse_blk_ops,
|
||||
vblk_exp);
|
||||
if (!vblk_exp->dev) {
|
||||
error_setg(errp, "failed to create vduse device");
|
||||
ret = -ENOMEM;
|
||||
goto err_dev;
|
||||
}
|
||||
|
||||
vblk_exp->recon_file = g_strdup_printf("%s/vduse-blk-%s",
|
||||
g_get_tmp_dir(), vblk_opts->name);
|
||||
if (vduse_set_reconnect_log_file(vblk_exp->dev, vblk_exp->recon_file)) {
|
||||
error_setg(errp, "failed to set reconnect log file");
|
||||
ret = -EINVAL;
|
||||
goto err;
|
||||
}
|
||||
|
||||
for (i = 0; i < num_queues; i++) {
|
||||
vduse_dev_setup_queue(vblk_exp->dev, i, queue_size);
|
||||
}
|
||||
|
||||
aio_set_fd_handler(exp->ctx, vduse_dev_get_fd(vblk_exp->dev), true,
|
||||
on_vduse_dev_kick, NULL, NULL, NULL, vblk_exp->dev);
|
||||
|
||||
blk_add_aio_context_notifier(exp->blk, blk_aio_attached, blk_aio_detach,
|
||||
vblk_exp);
|
||||
|
||||
blk_set_dev_ops(exp->blk, &vduse_block_ops, exp);
|
||||
|
||||
return 0;
|
||||
err:
|
||||
vduse_dev_destroy(vblk_exp->dev);
|
||||
g_free(vblk_exp->recon_file);
|
||||
err_dev:
|
||||
g_free(vblk_exp->handler.serial);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void vduse_blk_exp_delete(BlockExport *exp)
|
||||
{
|
||||
VduseBlkExport *vblk_exp = container_of(exp, VduseBlkExport, export);
|
||||
int ret;
|
||||
|
||||
blk_remove_aio_context_notifier(exp->blk, blk_aio_attached, blk_aio_detach,
|
||||
vblk_exp);
|
||||
blk_set_dev_ops(exp->blk, NULL, NULL);
|
||||
ret = vduse_dev_destroy(vblk_exp->dev);
|
||||
if (ret != -EBUSY) {
|
||||
unlink(vblk_exp->recon_file);
|
||||
}
|
||||
g_free(vblk_exp->recon_file);
|
||||
g_free(vblk_exp->handler.serial);
|
||||
}
|
||||
|
||||
static void vduse_blk_exp_request_shutdown(BlockExport *exp)
|
||||
{
|
||||
VduseBlkExport *vblk_exp = container_of(exp, VduseBlkExport, export);
|
||||
|
||||
aio_context_acquire(vblk_exp->export.ctx);
|
||||
vduse_blk_detach_ctx(vblk_exp);
|
||||
aio_context_acquire(vblk_exp->export.ctx);
|
||||
}
|
||||
|
||||
const BlockExportDriver blk_exp_vduse_blk = {
|
||||
.type = BLOCK_EXPORT_TYPE_VDUSE_BLK,
|
||||
.instance_size = sizeof(VduseBlkExport),
|
||||
.create = vduse_blk_exp_create,
|
||||
.delete = vduse_blk_exp_delete,
|
||||
.request_shutdown = vduse_blk_exp_request_shutdown,
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Export QEMU block device via VDUSE
|
||||
*
|
||||
* Copyright (C) 2022 Bytedance Inc. and/or its affiliates. All rights reserved.
|
||||
*
|
||||
* Author:
|
||||
* Xie Yongji <xieyongji@bytedance.com>
|
||||
*
|
||||
* This work is licensed under the terms of the GNU GPL, version 2 or
|
||||
* later. See the COPYING file in the top-level directory.
|
||||
*/
|
||||
|
||||
#ifndef VDUSE_BLK_H
|
||||
#define VDUSE_BLK_H
|
||||
|
||||
#include "block/export.h"
|
||||
|
||||
extern const BlockExportDriver blk_exp_vduse_blk;
|
||||
|
||||
#endif /* VDUSE_BLK_H */
|
||||
@@ -17,31 +17,15 @@
|
||||
#include "vhost-user-blk-server.h"
|
||||
#include "qapi/error.h"
|
||||
#include "qom/object_interfaces.h"
|
||||
#include "sysemu/block-backend.h"
|
||||
#include "util/block-helpers.h"
|
||||
|
||||
/*
|
||||
* Sector units are 512 bytes regardless of the
|
||||
* virtio_blk_config->blk_size value.
|
||||
*/
|
||||
#define VIRTIO_BLK_SECTOR_BITS 9
|
||||
#define VIRTIO_BLK_SECTOR_SIZE (1ull << VIRTIO_BLK_SECTOR_BITS)
|
||||
#include "virtio-blk-handler.h"
|
||||
|
||||
enum {
|
||||
VHOST_USER_BLK_NUM_QUEUES_DEFAULT = 1,
|
||||
VHOST_USER_BLK_MAX_DISCARD_SECTORS = 32768,
|
||||
VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS = 32768,
|
||||
};
|
||||
struct virtio_blk_inhdr {
|
||||
unsigned char status;
|
||||
};
|
||||
|
||||
typedef struct VuBlkReq {
|
||||
VuVirtqElement elem;
|
||||
int64_t sector_num;
|
||||
size_t size;
|
||||
struct virtio_blk_inhdr *in;
|
||||
struct virtio_blk_outhdr out;
|
||||
VuServer *server;
|
||||
struct VuVirtq *vq;
|
||||
} VuBlkReq;
|
||||
@@ -50,248 +34,44 @@ typedef struct VuBlkReq {
|
||||
typedef struct {
|
||||
BlockExport export;
|
||||
VuServer vu_server;
|
||||
uint32_t blk_size;
|
||||
VirtioBlkHandler handler;
|
||||
QIOChannelSocket *sioc;
|
||||
struct virtio_blk_config blkcfg;
|
||||
bool writable;
|
||||
} VuBlkExport;
|
||||
|
||||
static void vu_blk_req_complete(VuBlkReq *req)
|
||||
static void vu_blk_req_complete(VuBlkReq *req, size_t in_len)
|
||||
{
|
||||
VuDev *vu_dev = &req->server->vu_dev;
|
||||
|
||||
/* IO size with 1 extra status byte */
|
||||
vu_queue_push(vu_dev, req->vq, &req->elem, req->size + 1);
|
||||
vu_queue_push(vu_dev, req->vq, &req->elem, in_len);
|
||||
vu_queue_notify(vu_dev, req->vq);
|
||||
|
||||
free(req);
|
||||
}
|
||||
|
||||
static bool vu_blk_sect_range_ok(VuBlkExport *vexp, uint64_t sector,
|
||||
size_t size)
|
||||
{
|
||||
uint64_t nb_sectors;
|
||||
uint64_t total_sectors;
|
||||
|
||||
if (size % VIRTIO_BLK_SECTOR_SIZE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
nb_sectors = size >> VIRTIO_BLK_SECTOR_BITS;
|
||||
|
||||
QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE != VIRTIO_BLK_SECTOR_SIZE);
|
||||
if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
|
||||
return false;
|
||||
}
|
||||
if ((sector << VIRTIO_BLK_SECTOR_BITS) % vexp->blk_size) {
|
||||
return false;
|
||||
}
|
||||
blk_get_geometry(vexp->export.blk, &total_sectors);
|
||||
if (sector > total_sectors || nb_sectors > total_sectors - sector) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int coroutine_fn
|
||||
vu_blk_discard_write_zeroes(VuBlkExport *vexp, struct iovec *iov,
|
||||
uint32_t iovcnt, uint32_t type)
|
||||
{
|
||||
BlockBackend *blk = vexp->export.blk;
|
||||
struct virtio_blk_discard_write_zeroes desc;
|
||||
ssize_t size;
|
||||
uint64_t sector;
|
||||
uint32_t num_sectors;
|
||||
uint32_t max_sectors;
|
||||
uint32_t flags;
|
||||
int bytes;
|
||||
|
||||
/* Only one desc is currently supported */
|
||||
if (unlikely(iov_size(iov, iovcnt) > sizeof(desc))) {
|
||||
return VIRTIO_BLK_S_UNSUPP;
|
||||
}
|
||||
|
||||
size = iov_to_buf(iov, iovcnt, 0, &desc, sizeof(desc));
|
||||
if (unlikely(size != sizeof(desc))) {
|
||||
error_report("Invalid size %zd, expected %zu", size, sizeof(desc));
|
||||
return VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
|
||||
sector = le64_to_cpu(desc.sector);
|
||||
num_sectors = le32_to_cpu(desc.num_sectors);
|
||||
flags = le32_to_cpu(desc.flags);
|
||||
max_sectors = (type == VIRTIO_BLK_T_WRITE_ZEROES) ?
|
||||
VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS :
|
||||
VHOST_USER_BLK_MAX_DISCARD_SECTORS;
|
||||
|
||||
/* This check ensures that 'bytes' fits in an int */
|
||||
if (unlikely(num_sectors > max_sectors)) {
|
||||
return VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
|
||||
bytes = num_sectors << VIRTIO_BLK_SECTOR_BITS;
|
||||
|
||||
if (unlikely(!vu_blk_sect_range_ok(vexp, sector, bytes))) {
|
||||
return VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
|
||||
/*
|
||||
* The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard
|
||||
* and write zeroes commands if any unknown flag is set.
|
||||
*/
|
||||
if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
|
||||
return VIRTIO_BLK_S_UNSUPP;
|
||||
}
|
||||
|
||||
if (type == VIRTIO_BLK_T_WRITE_ZEROES) {
|
||||
int blk_flags = 0;
|
||||
|
||||
if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) {
|
||||
blk_flags |= BDRV_REQ_MAY_UNMAP;
|
||||
}
|
||||
|
||||
if (blk_co_pwrite_zeroes(blk, sector << VIRTIO_BLK_SECTOR_BITS,
|
||||
bytes, blk_flags) == 0) {
|
||||
return VIRTIO_BLK_S_OK;
|
||||
}
|
||||
} else if (type == VIRTIO_BLK_T_DISCARD) {
|
||||
/*
|
||||
* The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for
|
||||
* discard commands if the unmap flag is set.
|
||||
*/
|
||||
if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
|
||||
return VIRTIO_BLK_S_UNSUPP;
|
||||
}
|
||||
|
||||
if (blk_co_pdiscard(blk, sector << VIRTIO_BLK_SECTOR_BITS,
|
||||
bytes) == 0) {
|
||||
return VIRTIO_BLK_S_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
|
||||
/* Called with server refcount increased, must decrease before returning */
|
||||
static void coroutine_fn vu_blk_virtio_process_req(void *opaque)
|
||||
{
|
||||
VuBlkReq *req = opaque;
|
||||
VuServer *server = req->server;
|
||||
VuVirtqElement *elem = &req->elem;
|
||||
uint32_t type;
|
||||
|
||||
VuBlkExport *vexp = container_of(server, VuBlkExport, vu_server);
|
||||
BlockBackend *blk = vexp->export.blk;
|
||||
|
||||
VirtioBlkHandler *handler = &vexp->handler;
|
||||
struct iovec *in_iov = elem->in_sg;
|
||||
struct iovec *out_iov = elem->out_sg;
|
||||
unsigned in_num = elem->in_num;
|
||||
unsigned out_num = elem->out_num;
|
||||
int in_len;
|
||||
|
||||
/* refer to hw/block/virtio_blk.c */
|
||||
if (elem->out_num < 1 || elem->in_num < 1) {
|
||||
error_report("virtio-blk request missing headers");
|
||||
goto err;
|
||||
in_len = virtio_blk_process_req(handler, in_iov, out_iov,
|
||||
in_num, out_num);
|
||||
if (in_len < 0) {
|
||||
free(req);
|
||||
vhost_user_server_unref(server);
|
||||
return;
|
||||
}
|
||||
|
||||
if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out,
|
||||
sizeof(req->out)) != sizeof(req->out))) {
|
||||
error_report("virtio-blk request outhdr too short");
|
||||
goto err;
|
||||
}
|
||||
|
||||
iov_discard_front(&out_iov, &out_num, sizeof(req->out));
|
||||
|
||||
if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) {
|
||||
error_report("virtio-blk request inhdr too short");
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* We always touch the last byte, so just see how big in_iov is. */
|
||||
req->in = (void *)in_iov[in_num - 1].iov_base
|
||||
+ in_iov[in_num - 1].iov_len
|
||||
- sizeof(struct virtio_blk_inhdr);
|
||||
iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr));
|
||||
|
||||
type = le32_to_cpu(req->out.type);
|
||||
switch (type & ~VIRTIO_BLK_T_BARRIER) {
|
||||
case VIRTIO_BLK_T_IN:
|
||||
case VIRTIO_BLK_T_OUT: {
|
||||
QEMUIOVector qiov;
|
||||
int64_t offset;
|
||||
ssize_t ret = 0;
|
||||
bool is_write = type & VIRTIO_BLK_T_OUT;
|
||||
req->sector_num = le64_to_cpu(req->out.sector);
|
||||
|
||||
if (is_write && !vexp->writable) {
|
||||
req->in->status = VIRTIO_BLK_S_IOERR;
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_write) {
|
||||
qemu_iovec_init_external(&qiov, out_iov, out_num);
|
||||
} else {
|
||||
qemu_iovec_init_external(&qiov, in_iov, in_num);
|
||||
}
|
||||
|
||||
if (unlikely(!vu_blk_sect_range_ok(vexp,
|
||||
req->sector_num,
|
||||
qiov.size))) {
|
||||
req->in->status = VIRTIO_BLK_S_IOERR;
|
||||
break;
|
||||
}
|
||||
|
||||
offset = req->sector_num << VIRTIO_BLK_SECTOR_BITS;
|
||||
|
||||
if (is_write) {
|
||||
ret = blk_co_pwritev(blk, offset, qiov.size, &qiov, 0);
|
||||
} else {
|
||||
ret = blk_co_preadv(blk, offset, qiov.size, &qiov, 0);
|
||||
}
|
||||
if (ret >= 0) {
|
||||
req->in->status = VIRTIO_BLK_S_OK;
|
||||
} else {
|
||||
req->in->status = VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case VIRTIO_BLK_T_FLUSH:
|
||||
if (blk_co_flush(blk) == 0) {
|
||||
req->in->status = VIRTIO_BLK_S_OK;
|
||||
} else {
|
||||
req->in->status = VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
break;
|
||||
case VIRTIO_BLK_T_GET_ID: {
|
||||
size_t size = MIN(iov_size(&elem->in_sg[0], in_num),
|
||||
VIRTIO_BLK_ID_BYTES);
|
||||
snprintf(elem->in_sg[0].iov_base, size, "%s", "vhost_user_blk");
|
||||
req->in->status = VIRTIO_BLK_S_OK;
|
||||
req->size = elem->in_sg[0].iov_len;
|
||||
break;
|
||||
}
|
||||
case VIRTIO_BLK_T_DISCARD:
|
||||
case VIRTIO_BLK_T_WRITE_ZEROES: {
|
||||
if (!vexp->writable) {
|
||||
req->in->status = VIRTIO_BLK_S_IOERR;
|
||||
break;
|
||||
}
|
||||
|
||||
req->in->status = vu_blk_discard_write_zeroes(vexp, out_iov, out_num,
|
||||
type);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
req->in->status = VIRTIO_BLK_S_UNSUPP;
|
||||
break;
|
||||
}
|
||||
|
||||
vu_blk_req_complete(req);
|
||||
vhost_user_server_unref(server);
|
||||
return;
|
||||
|
||||
err:
|
||||
free(req);
|
||||
vu_blk_req_complete(req, in_len);
|
||||
vhost_user_server_unref(server);
|
||||
}
|
||||
|
||||
@@ -348,7 +128,7 @@ static uint64_t vu_blk_get_features(VuDev *dev)
|
||||
1ull << VIRTIO_RING_F_EVENT_IDX |
|
||||
1ull << VHOST_USER_F_PROTOCOL_FEATURES;
|
||||
|
||||
if (!vexp->writable) {
|
||||
if (!vexp->handler.writable) {
|
||||
features |= 1ull << VIRTIO_BLK_F_RO;
|
||||
}
|
||||
|
||||
@@ -455,12 +235,12 @@ vu_blk_initialize_config(BlockDriverState *bs,
|
||||
config->opt_io_size = cpu_to_le32(1);
|
||||
config->num_queues = cpu_to_le16(num_queues);
|
||||
config->max_discard_sectors =
|
||||
cpu_to_le32(VHOST_USER_BLK_MAX_DISCARD_SECTORS);
|
||||
cpu_to_le32(VIRTIO_BLK_MAX_DISCARD_SECTORS);
|
||||
config->max_discard_seg = cpu_to_le32(1);
|
||||
config->discard_sector_alignment =
|
||||
cpu_to_le32(blk_size >> VIRTIO_BLK_SECTOR_BITS);
|
||||
config->max_write_zeroes_sectors
|
||||
= cpu_to_le32(VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS);
|
||||
= cpu_to_le32(VIRTIO_BLK_MAX_WRITE_ZEROES_SECTORS);
|
||||
config->max_write_zeroes_seg = cpu_to_le32(1);
|
||||
}
|
||||
|
||||
@@ -480,7 +260,6 @@ static int vu_blk_exp_create(BlockExport *exp, BlockExportOptions *opts,
|
||||
uint64_t logical_block_size;
|
||||
uint16_t num_queues = VHOST_USER_BLK_NUM_QUEUES_DEFAULT;
|
||||
|
||||
vexp->writable = opts->writable;
|
||||
vexp->blkcfg.wce = 0;
|
||||
|
||||
if (vu_opts->has_logical_block_size) {
|
||||
@@ -494,8 +273,6 @@ static int vu_blk_exp_create(BlockExport *exp, BlockExportOptions *opts,
|
||||
error_propagate(errp, local_err);
|
||||
return -EINVAL;
|
||||
}
|
||||
vexp->blk_size = logical_block_size;
|
||||
blk_set_guest_block_size(exp->blk, logical_block_size);
|
||||
|
||||
if (vu_opts->has_num_queues) {
|
||||
num_queues = vu_opts->num_queues;
|
||||
@@ -504,6 +281,10 @@ static int vu_blk_exp_create(BlockExport *exp, BlockExportOptions *opts,
|
||||
error_setg(errp, "num-queues must be greater than 0");
|
||||
return -EINVAL;
|
||||
}
|
||||
vexp->handler.blk = exp->blk;
|
||||
vexp->handler.serial = g_strdup("vhost_user_blk");
|
||||
vexp->handler.logical_block_size = logical_block_size;
|
||||
vexp->handler.writable = opts->writable;
|
||||
|
||||
vu_blk_initialize_config(blk_bs(exp->blk), &vexp->blkcfg,
|
||||
logical_block_size, num_queues);
|
||||
@@ -515,6 +296,7 @@ static int vu_blk_exp_create(BlockExport *exp, BlockExportOptions *opts,
|
||||
num_queues, &vu_blk_iface, errp)) {
|
||||
blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
|
||||
blk_aio_detach, vexp);
|
||||
g_free(vexp->handler.serial);
|
||||
return -EADDRNOTAVAIL;
|
||||
}
|
||||
|
||||
@@ -527,6 +309,7 @@ static void vu_blk_exp_delete(BlockExport *exp)
|
||||
|
||||
blk_remove_aio_context_notifier(exp->blk, blk_aio_attached, blk_aio_detach,
|
||||
vexp);
|
||||
g_free(vexp->handler.serial);
|
||||
}
|
||||
|
||||
const BlockExportDriver blk_exp_vhost_user_blk = {
|
||||
|
||||
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
* Handler for virtio-blk I/O
|
||||
*
|
||||
* Copyright (c) 2020 Red Hat, Inc.
|
||||
* Copyright (C) 2022 Bytedance Inc. and/or its affiliates. All rights reserved.
|
||||
*
|
||||
* Author:
|
||||
* Coiby Xu <coiby.xu@gmail.com>
|
||||
* Xie Yongji <xieyongji@bytedance.com>
|
||||
*
|
||||
* This work is licensed under the terms of the GNU GPL, version 2 or
|
||||
* later. See the COPYING file in the top-level directory.
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu/error-report.h"
|
||||
#include "virtio-blk-handler.h"
|
||||
|
||||
#include "standard-headers/linux/virtio_blk.h"
|
||||
|
||||
struct virtio_blk_inhdr {
|
||||
unsigned char status;
|
||||
};
|
||||
|
||||
static bool virtio_blk_sect_range_ok(BlockBackend *blk, uint32_t block_size,
|
||||
uint64_t sector, size_t size)
|
||||
{
|
||||
uint64_t nb_sectors;
|
||||
uint64_t total_sectors;
|
||||
|
||||
if (size % VIRTIO_BLK_SECTOR_SIZE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
nb_sectors = size >> VIRTIO_BLK_SECTOR_BITS;
|
||||
|
||||
QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE != VIRTIO_BLK_SECTOR_SIZE);
|
||||
if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
|
||||
return false;
|
||||
}
|
||||
if ((sector << VIRTIO_BLK_SECTOR_BITS) % block_size) {
|
||||
return false;
|
||||
}
|
||||
blk_get_geometry(blk, &total_sectors);
|
||||
if (sector > total_sectors || nb_sectors > total_sectors - sector) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int coroutine_fn
|
||||
virtio_blk_discard_write_zeroes(VirtioBlkHandler *handler, struct iovec *iov,
|
||||
uint32_t iovcnt, uint32_t type)
|
||||
{
|
||||
BlockBackend *blk = handler->blk;
|
||||
struct virtio_blk_discard_write_zeroes desc;
|
||||
ssize_t size;
|
||||
uint64_t sector;
|
||||
uint32_t num_sectors;
|
||||
uint32_t max_sectors;
|
||||
uint32_t flags;
|
||||
int bytes;
|
||||
|
||||
/* Only one desc is currently supported */
|
||||
if (unlikely(iov_size(iov, iovcnt) > sizeof(desc))) {
|
||||
return VIRTIO_BLK_S_UNSUPP;
|
||||
}
|
||||
|
||||
size = iov_to_buf(iov, iovcnt, 0, &desc, sizeof(desc));
|
||||
if (unlikely(size != sizeof(desc))) {
|
||||
error_report("Invalid size %zd, expected %zu", size, sizeof(desc));
|
||||
return VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
|
||||
sector = le64_to_cpu(desc.sector);
|
||||
num_sectors = le32_to_cpu(desc.num_sectors);
|
||||
flags = le32_to_cpu(desc.flags);
|
||||
max_sectors = (type == VIRTIO_BLK_T_WRITE_ZEROES) ?
|
||||
VIRTIO_BLK_MAX_WRITE_ZEROES_SECTORS :
|
||||
VIRTIO_BLK_MAX_DISCARD_SECTORS;
|
||||
|
||||
/* This check ensures that 'bytes' fits in an int */
|
||||
if (unlikely(num_sectors > max_sectors)) {
|
||||
return VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
|
||||
bytes = num_sectors << VIRTIO_BLK_SECTOR_BITS;
|
||||
|
||||
if (unlikely(!virtio_blk_sect_range_ok(blk, handler->logical_block_size,
|
||||
sector, bytes))) {
|
||||
return VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
|
||||
/*
|
||||
* The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard
|
||||
* and write zeroes commands if any unknown flag is set.
|
||||
*/
|
||||
if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
|
||||
return VIRTIO_BLK_S_UNSUPP;
|
||||
}
|
||||
|
||||
if (type == VIRTIO_BLK_T_WRITE_ZEROES) {
|
||||
int blk_flags = 0;
|
||||
|
||||
if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) {
|
||||
blk_flags |= BDRV_REQ_MAY_UNMAP;
|
||||
}
|
||||
|
||||
if (blk_co_pwrite_zeroes(blk, sector << VIRTIO_BLK_SECTOR_BITS,
|
||||
bytes, blk_flags) == 0) {
|
||||
return VIRTIO_BLK_S_OK;
|
||||
}
|
||||
} else if (type == VIRTIO_BLK_T_DISCARD) {
|
||||
/*
|
||||
* The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for
|
||||
* discard commands if the unmap flag is set.
|
||||
*/
|
||||
if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
|
||||
return VIRTIO_BLK_S_UNSUPP;
|
||||
}
|
||||
|
||||
if (blk_co_pdiscard(blk, sector << VIRTIO_BLK_SECTOR_BITS,
|
||||
bytes) == 0) {
|
||||
return VIRTIO_BLK_S_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
|
||||
int coroutine_fn virtio_blk_process_req(VirtioBlkHandler *handler,
|
||||
struct iovec *in_iov,
|
||||
struct iovec *out_iov,
|
||||
unsigned int in_num,
|
||||
unsigned int out_num)
|
||||
{
|
||||
BlockBackend *blk = handler->blk;
|
||||
struct virtio_blk_inhdr *in;
|
||||
struct virtio_blk_outhdr out;
|
||||
uint32_t type;
|
||||
int in_len;
|
||||
|
||||
if (out_num < 1 || in_num < 1) {
|
||||
error_report("virtio-blk request missing headers");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (unlikely(iov_to_buf(out_iov, out_num, 0, &out,
|
||||
sizeof(out)) != sizeof(out))) {
|
||||
error_report("virtio-blk request outhdr too short");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
iov_discard_front(&out_iov, &out_num, sizeof(out));
|
||||
|
||||
if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) {
|
||||
error_report("virtio-blk request inhdr too short");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* We always touch the last byte, so just see how big in_iov is. */
|
||||
in_len = iov_size(in_iov, in_num);
|
||||
in = (void *)in_iov[in_num - 1].iov_base
|
||||
+ in_iov[in_num - 1].iov_len
|
||||
- sizeof(struct virtio_blk_inhdr);
|
||||
iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr));
|
||||
|
||||
type = le32_to_cpu(out.type);
|
||||
switch (type & ~VIRTIO_BLK_T_BARRIER) {
|
||||
case VIRTIO_BLK_T_IN:
|
||||
case VIRTIO_BLK_T_OUT: {
|
||||
QEMUIOVector qiov;
|
||||
int64_t offset;
|
||||
ssize_t ret = 0;
|
||||
bool is_write = type & VIRTIO_BLK_T_OUT;
|
||||
int64_t sector_num = le64_to_cpu(out.sector);
|
||||
|
||||
if (is_write && !handler->writable) {
|
||||
in->status = VIRTIO_BLK_S_IOERR;
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_write) {
|
||||
qemu_iovec_init_external(&qiov, out_iov, out_num);
|
||||
} else {
|
||||
qemu_iovec_init_external(&qiov, in_iov, in_num);
|
||||
}
|
||||
|
||||
if (unlikely(!virtio_blk_sect_range_ok(blk,
|
||||
handler->logical_block_size,
|
||||
sector_num, qiov.size))) {
|
||||
in->status = VIRTIO_BLK_S_IOERR;
|
||||
break;
|
||||
}
|
||||
|
||||
offset = sector_num << VIRTIO_BLK_SECTOR_BITS;
|
||||
|
||||
if (is_write) {
|
||||
ret = blk_co_pwritev(blk, offset, qiov.size, &qiov, 0);
|
||||
} else {
|
||||
ret = blk_co_preadv(blk, offset, qiov.size, &qiov, 0);
|
||||
}
|
||||
if (ret >= 0) {
|
||||
in->status = VIRTIO_BLK_S_OK;
|
||||
} else {
|
||||
in->status = VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case VIRTIO_BLK_T_FLUSH:
|
||||
if (blk_co_flush(blk) == 0) {
|
||||
in->status = VIRTIO_BLK_S_OK;
|
||||
} else {
|
||||
in->status = VIRTIO_BLK_S_IOERR;
|
||||
}
|
||||
break;
|
||||
case VIRTIO_BLK_T_GET_ID: {
|
||||
size_t size = MIN(strlen(handler->serial) + 1,
|
||||
MIN(iov_size(in_iov, in_num),
|
||||
VIRTIO_BLK_ID_BYTES));
|
||||
iov_from_buf(in_iov, in_num, 0, handler->serial, size);
|
||||
in->status = VIRTIO_BLK_S_OK;
|
||||
break;
|
||||
}
|
||||
case VIRTIO_BLK_T_DISCARD:
|
||||
case VIRTIO_BLK_T_WRITE_ZEROES:
|
||||
if (!handler->writable) {
|
||||
in->status = VIRTIO_BLK_S_IOERR;
|
||||
break;
|
||||
}
|
||||
in->status = virtio_blk_discard_write_zeroes(handler, out_iov,
|
||||
out_num, type);
|
||||
break;
|
||||
default:
|
||||
in->status = VIRTIO_BLK_S_UNSUPP;
|
||||
break;
|
||||
}
|
||||
|
||||
return in_len;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Handler for virtio-blk I/O
|
||||
*
|
||||
* Copyright (C) 2022 Bytedance Inc. and/or its affiliates. All rights reserved.
|
||||
*
|
||||
* Author:
|
||||
* Xie Yongji <xieyongji@bytedance.com>
|
||||
*
|
||||
* This work is licensed under the terms of the GNU GPL, version 2 or
|
||||
* later. See the COPYING file in the top-level directory.
|
||||
*/
|
||||
|
||||
#ifndef VIRTIO_BLK_HANDLER_H
|
||||
#define VIRTIO_BLK_HANDLER_H
|
||||
|
||||
#include "sysemu/block-backend.h"
|
||||
|
||||
#define VIRTIO_BLK_SECTOR_BITS 9
|
||||
#define VIRTIO_BLK_SECTOR_SIZE (1ULL << VIRTIO_BLK_SECTOR_BITS)
|
||||
|
||||
#define VIRTIO_BLK_MAX_DISCARD_SECTORS 32768
|
||||
#define VIRTIO_BLK_MAX_WRITE_ZEROES_SECTORS 32768
|
||||
|
||||
typedef struct {
|
||||
BlockBackend *blk;
|
||||
char *serial;
|
||||
uint32_t logical_block_size;
|
||||
bool writable;
|
||||
} VirtioBlkHandler;
|
||||
|
||||
int coroutine_fn virtio_blk_process_req(VirtioBlkHandler *handler,
|
||||
struct iovec *in_iov,
|
||||
struct iovec *out_iov,
|
||||
unsigned int in_num,
|
||||
unsigned int out_num);
|
||||
|
||||
#endif /* VIRTIO_BLK_HANDLER_H */
|
||||
+1
-1
@@ -891,7 +891,7 @@ out:
|
||||
static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
|
||||
{
|
||||
bs->bl.max_transfer = GLUSTER_MAX_TRANSFER;
|
||||
bs->bl.max_pdiscard = SIZE_MAX;
|
||||
bs->bl.max_pdiscard = MIN(SIZE_MAX, INT64_MAX);
|
||||
}
|
||||
|
||||
static int qemu_gluster_reopen_prepare(BDRVReopenState *state,
|
||||
|
||||
-15
@@ -588,21 +588,6 @@ void bdrv_unapply_subtree_drain(BdrvChild *child, BlockDriverState *old_parent)
|
||||
BDRV_POLL_WHILE(child->bs, qatomic_read(&drained_end_counter) > 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Wait for pending requests to complete on a single BlockDriverState subtree,
|
||||
* and suspend block driver's internal I/O until next request arrives.
|
||||
*
|
||||
* Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
|
||||
* AioContext.
|
||||
*/
|
||||
void coroutine_fn bdrv_co_drain(BlockDriverState *bs)
|
||||
{
|
||||
IO_OR_GS_CODE();
|
||||
assert(qemu_in_coroutine());
|
||||
bdrv_drained_begin(bs);
|
||||
bdrv_drained_end(bs);
|
||||
}
|
||||
|
||||
void bdrv_drain(BlockDriverState *bs)
|
||||
{
|
||||
IO_OR_GS_CODE();
|
||||
|
||||
@@ -261,8 +261,9 @@ BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *node, const char *target,
|
||||
HBitmap **backup, Error **errp)
|
||||
{
|
||||
BlockDriverState *bs;
|
||||
BdrvDirtyBitmap *dst, *src, *anon;
|
||||
BdrvDirtyBitmap *dst, *src;
|
||||
BlockDirtyBitmapOrStrList *lst;
|
||||
HBitmap *local_backup = NULL;
|
||||
|
||||
GLOBAL_STATE_CODE();
|
||||
|
||||
@@ -271,12 +272,6 @@ BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *node, const char *target,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
anon = bdrv_create_dirty_bitmap(bs, bdrv_dirty_bitmap_granularity(dst),
|
||||
NULL, errp);
|
||||
if (!anon) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (lst = bms; lst; lst = lst->next) {
|
||||
switch (lst->value->type) {
|
||||
const char *name, *node;
|
||||
@@ -285,8 +280,7 @@ BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *node, const char *target,
|
||||
src = bdrv_find_dirty_bitmap(bs, name);
|
||||
if (!src) {
|
||||
error_setg(errp, "Dirty bitmap '%s' not found", name);
|
||||
dst = NULL;
|
||||
goto out;
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
case QTYPE_QDICT:
|
||||
@@ -294,26 +288,36 @@ BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *node, const char *target,
|
||||
name = lst->value->u.external.name;
|
||||
src = block_dirty_bitmap_lookup(node, name, NULL, errp);
|
||||
if (!src) {
|
||||
dst = NULL;
|
||||
goto out;
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
|
||||
if (!bdrv_merge_dirty_bitmap(anon, src, NULL, errp)) {
|
||||
dst = NULL;
|
||||
goto out;
|
||||
/* We do backup only for first merge operation */
|
||||
if (!bdrv_merge_dirty_bitmap(dst, src,
|
||||
local_backup ? NULL : &local_backup,
|
||||
errp))
|
||||
{
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
/* Merge into dst; dst is unchanged on failure. */
|
||||
bdrv_merge_dirty_bitmap(dst, anon, backup, errp);
|
||||
if (backup) {
|
||||
*backup = local_backup;
|
||||
} else {
|
||||
hbitmap_free(local_backup);
|
||||
}
|
||||
|
||||
out:
|
||||
bdrv_release_dirty_bitmap(anon);
|
||||
return dst;
|
||||
|
||||
fail:
|
||||
if (local_backup) {
|
||||
bdrv_restore_dirty_bitmap(dst, local_backup);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void qmp_block_dirty_bitmap_merge(const char *node, const char *target,
|
||||
|
||||
+2
-6
@@ -521,12 +521,8 @@ static int coroutine_fn nbd_co_send_request(BlockDriverState *bs,
|
||||
if (qiov) {
|
||||
qio_channel_set_cork(s->ioc, true);
|
||||
rc = nbd_send_request(s->ioc, request);
|
||||
if (rc >= 0) {
|
||||
if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov,
|
||||
NULL) < 0) {
|
||||
rc = -EIO;
|
||||
}
|
||||
} else if (rc >= 0) {
|
||||
if (rc >= 0 && qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov,
|
||||
NULL) < 0) {
|
||||
rc = -EIO;
|
||||
}
|
||||
qio_channel_set_cork(s->ioc, false);
|
||||
|
||||
+24
@@ -831,6 +831,26 @@ static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
|
||||
error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
|
||||
goto failed_shutdown;
|
||||
}
|
||||
|
||||
#ifdef HAVE_RBD_NAMESPACE_EXISTS
|
||||
if (opts->has_q_namespace && strlen(opts->q_namespace) > 0) {
|
||||
bool exists;
|
||||
|
||||
r = rbd_namespace_exists(*io_ctx, opts->q_namespace, &exists);
|
||||
if (r < 0) {
|
||||
error_setg_errno(errp, -r, "error checking namespace");
|
||||
goto failed_ioctx_destroy;
|
||||
}
|
||||
|
||||
if (!exists) {
|
||||
error_setg(errp, "namespace '%s' does not exist",
|
||||
opts->q_namespace);
|
||||
r = -ENOENT;
|
||||
goto failed_ioctx_destroy;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Set the namespace after opening the io context on the pool,
|
||||
* if nspace == NULL or if nspace == "", it is just as we did nothing
|
||||
@@ -840,6 +860,10 @@ static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
|
||||
r = 0;
|
||||
goto out;
|
||||
|
||||
#ifdef HAVE_RBD_NAMESPACE_EXISTS
|
||||
failed_ioctx_destroy:
|
||||
rados_ioctx_destroy(*io_ctx);
|
||||
#endif
|
||||
failed_shutdown:
|
||||
rados_shutdown(*cluster);
|
||||
out:
|
||||
|
||||
@@ -77,6 +77,7 @@ Standard options:
|
||||
--export [type=]vhost-user-blk,id=<id>,node-name=<node-name>,addr.type=unix,addr.path=<socket-path>[,writable=on|off][,logical-block-size=<block-size>][,num-queues=<num-queues>]
|
||||
--export [type=]vhost-user-blk,id=<id>,node-name=<node-name>,addr.type=fd,addr.str=<fd>[,writable=on|off][,logical-block-size=<block-size>][,num-queues=<num-queues>]
|
||||
--export [type=]fuse,id=<id>,node-name=<node-name>,mountpoint=<file>[,growable=on|off][,writable=on|off][,allow-other=on|off|auto]
|
||||
--export [type=]vduse-blk,id=<id>,node-name=<node-name>,name=<vduse-name>[,writable=on|off][,num-queues=<num-queues>][,queue-size=<queue-size>][,logical-block-size=<block-size>][,serial=<serial-number>]
|
||||
|
||||
is a block export definition. ``node-name`` is the block node that should be
|
||||
exported. ``writable`` determines whether or not the export allows write
|
||||
@@ -110,6 +111,27 @@ Standard options:
|
||||
``allow-other`` to auto (the default) will try enabling this option, and on
|
||||
error fall back to disabling it.
|
||||
|
||||
The ``vduse-blk`` export type takes a ``name`` (must be unique across the host)
|
||||
to create the VDUSE device.
|
||||
``num-queues`` sets the number of virtqueues (the default is 1).
|
||||
``queue-size`` sets the virtqueue descriptor table size (the default is 256).
|
||||
|
||||
The instantiated VDUSE device must then be added to the vDPA bus using the
|
||||
vdpa(8) command from the iproute2 project::
|
||||
|
||||
# vdpa dev add name <id> mgmtdev vduse
|
||||
|
||||
The device can be removed from the vDPA bus later as follows::
|
||||
|
||||
# vdpa dev del <id>
|
||||
|
||||
For more information about attaching vDPA devices to the host with
|
||||
virtio_vdpa.ko or attaching them to guests with vhost_vdpa.ko, see
|
||||
https://vdpa-dev.gitlab.io/.
|
||||
|
||||
For more information about VDUSE, see
|
||||
https://docs.kernel.org/userspace-api/vduse.html.
|
||||
|
||||
.. option:: --monitor MONITORDEF
|
||||
|
||||
is a QMP monitor definition. See the :manpage:`qemu(1)` manual page for
|
||||
|
||||
@@ -1228,7 +1228,6 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
|
||||
|
||||
s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
|
||||
blk_set_dev_ops(s->blk, &virtio_block_ops, s);
|
||||
blk_set_guest_block_size(s->blk, s->conf.conf.logical_block_size);
|
||||
|
||||
blk_iostatus_enable(s->blk);
|
||||
|
||||
|
||||
@@ -243,7 +243,6 @@ static void xen_block_realize(XenDevice *xendev, Error **errp)
|
||||
}
|
||||
|
||||
blk_set_dev_ops(blk, &xen_block_dev_ops, blockdev);
|
||||
blk_set_guest_block_size(blk, conf->logical_block_size);
|
||||
|
||||
if (conf->discard_granularity == -1) {
|
||||
conf->discard_granularity = conf->physical_block_size;
|
||||
|
||||
@@ -2548,7 +2548,6 @@ int ide_init_drive(IDEState *s, BlockBackend *blk, IDEDriveKind kind,
|
||||
s->smart_selftest_count = 0;
|
||||
if (kind == IDE_CD) {
|
||||
blk_set_dev_ops(blk, &ide_cd_block_ops, s);
|
||||
blk_set_guest_block_size(blk, 2048);
|
||||
} else {
|
||||
if (!blk_is_inserted(s->blk)) {
|
||||
error_setg(errp, "Device needs media, but drive is empty");
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user