mirror of https://github.com/xqemu/xqemu.git
nbd: Use BlockBackend internally
With all externally visible functions changed to use BlockBackend, this patch makes nbd use BlockBackend for everything internally as well. While touching them, substitute 512 by BDRV_SECTOR_SIZE in the calls to blk_read(), blk_write() and blk_co_discard(). Signed-off-by: Max Reitz <mreitz@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Message-id: 1416309679-333-6-git-send-email-mreitz@redhat.com Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
e140177d9c
commit
aadf99a792
56
nbd.c
56
nbd.c
|
@ -17,8 +17,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "block/nbd.h"
|
#include "block/nbd.h"
|
||||||
#include "block/block.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
#include "sysemu/block-backend.h"
|
#include "sysemu/block-backend.h"
|
||||||
|
|
||||||
#include "block/coroutine.h"
|
#include "block/coroutine.h"
|
||||||
|
@ -102,7 +100,7 @@ struct NBDExport {
|
||||||
int refcount;
|
int refcount;
|
||||||
void (*close)(NBDExport *exp);
|
void (*close)(NBDExport *exp);
|
||||||
|
|
||||||
BlockDriverState *bs;
|
BlockBackend *blk;
|
||||||
char *name;
|
char *name;
|
||||||
off_t dev_offset;
|
off_t dev_offset;
|
||||||
off_t size;
|
off_t size;
|
||||||
|
@ -930,7 +928,7 @@ static void nbd_request_put(NBDRequest *req)
|
||||||
nbd_client_put(client);
|
nbd_client_put(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void bs_aio_attached(AioContext *ctx, void *opaque)
|
static void blk_aio_attached(AioContext *ctx, void *opaque)
|
||||||
{
|
{
|
||||||
NBDExport *exp = opaque;
|
NBDExport *exp = opaque;
|
||||||
NBDClient *client;
|
NBDClient *client;
|
||||||
|
@ -944,7 +942,7 @@ static void bs_aio_attached(AioContext *ctx, void *opaque)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void bs_aio_detach(void *opaque)
|
static void blk_aio_detach(void *opaque)
|
||||||
{
|
{
|
||||||
NBDExport *exp = opaque;
|
NBDExport *exp = opaque;
|
||||||
NBDClient *client;
|
NBDClient *client;
|
||||||
|
@ -961,24 +959,23 @@ static void bs_aio_detach(void *opaque)
|
||||||
NBDExport *nbd_export_new(BlockBackend *blk, off_t dev_offset, off_t size,
|
NBDExport *nbd_export_new(BlockBackend *blk, off_t dev_offset, off_t size,
|
||||||
uint32_t nbdflags, void (*close)(NBDExport *))
|
uint32_t nbdflags, void (*close)(NBDExport *))
|
||||||
{
|
{
|
||||||
BlockDriverState *bs = blk_bs(blk);
|
|
||||||
NBDExport *exp = g_malloc0(sizeof(NBDExport));
|
NBDExport *exp = g_malloc0(sizeof(NBDExport));
|
||||||
exp->refcount = 1;
|
exp->refcount = 1;
|
||||||
QTAILQ_INIT(&exp->clients);
|
QTAILQ_INIT(&exp->clients);
|
||||||
exp->bs = bs;
|
exp->blk = blk;
|
||||||
exp->dev_offset = dev_offset;
|
exp->dev_offset = dev_offset;
|
||||||
exp->nbdflags = nbdflags;
|
exp->nbdflags = nbdflags;
|
||||||
exp->size = size == -1 ? bdrv_getlength(bs) : size;
|
exp->size = size == -1 ? blk_getlength(blk) : size;
|
||||||
exp->close = close;
|
exp->close = close;
|
||||||
exp->ctx = bdrv_get_aio_context(bs);
|
exp->ctx = blk_get_aio_context(blk);
|
||||||
bdrv_ref(bs);
|
blk_ref(blk);
|
||||||
bdrv_add_aio_context_notifier(bs, bs_aio_attached, bs_aio_detach, exp);
|
blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
|
||||||
/*
|
/*
|
||||||
* NBD exports are used for non-shared storage migration. Make sure
|
* NBD exports are used for non-shared storage migration. Make sure
|
||||||
* that BDRV_O_INCOMING is cleared and the image is ready for write
|
* that BDRV_O_INCOMING is cleared and the image is ready for write
|
||||||
* access since the export could be available before migration handover.
|
* access since the export could be available before migration handover.
|
||||||
*/
|
*/
|
||||||
bdrv_invalidate_cache(bs, NULL);
|
blk_invalidate_cache(blk, NULL);
|
||||||
return exp;
|
return exp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1025,11 +1022,11 @@ void nbd_export_close(NBDExport *exp)
|
||||||
}
|
}
|
||||||
nbd_export_set_name(exp, NULL);
|
nbd_export_set_name(exp, NULL);
|
||||||
nbd_export_put(exp);
|
nbd_export_put(exp);
|
||||||
if (exp->bs) {
|
if (exp->blk) {
|
||||||
bdrv_remove_aio_context_notifier(exp->bs, bs_aio_attached,
|
blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
|
||||||
bs_aio_detach, exp);
|
blk_aio_detach, exp);
|
||||||
bdrv_unref(exp->bs);
|
blk_unref(exp->blk);
|
||||||
exp->bs = NULL;
|
exp->blk = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1059,7 +1056,7 @@ void nbd_export_put(NBDExport *exp)
|
||||||
|
|
||||||
BlockBackend *nbd_export_get_blockdev(NBDExport *exp)
|
BlockBackend *nbd_export_get_blockdev(NBDExport *exp)
|
||||||
{
|
{
|
||||||
return exp->bs->blk;
|
return exp->blk;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nbd_export_close_all(void)
|
void nbd_export_close_all(void)
|
||||||
|
@ -1138,7 +1135,7 @@ static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *reque
|
||||||
|
|
||||||
command = request->type & NBD_CMD_MASK_COMMAND;
|
command = request->type & NBD_CMD_MASK_COMMAND;
|
||||||
if (command == NBD_CMD_READ || command == NBD_CMD_WRITE) {
|
if (command == NBD_CMD_READ || command == NBD_CMD_WRITE) {
|
||||||
req->data = qemu_blockalign(client->exp->bs, request->len);
|
req->data = blk_blockalign(client->exp->blk, request->len);
|
||||||
}
|
}
|
||||||
if (command == NBD_CMD_WRITE) {
|
if (command == NBD_CMD_WRITE) {
|
||||||
TRACE("Reading %u byte(s)", request->len);
|
TRACE("Reading %u byte(s)", request->len);
|
||||||
|
@ -1204,7 +1201,7 @@ static void nbd_trip(void *opaque)
|
||||||
TRACE("Request type is READ");
|
TRACE("Request type is READ");
|
||||||
|
|
||||||
if (request.type & NBD_CMD_FLAG_FUA) {
|
if (request.type & NBD_CMD_FLAG_FUA) {
|
||||||
ret = bdrv_co_flush(exp->bs);
|
ret = blk_co_flush(exp->blk);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("flush failed");
|
LOG("flush failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
|
@ -1212,8 +1209,9 @@ static void nbd_trip(void *opaque)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_read(exp->bs, (request.from + exp->dev_offset) / 512,
|
ret = blk_read(exp->blk,
|
||||||
req->data, request.len / 512);
|
(request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
|
||||||
|
req->data, request.len / BDRV_SECTOR_SIZE);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("reading from file failed");
|
LOG("reading from file failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
|
@ -1235,8 +1233,9 @@ static void nbd_trip(void *opaque)
|
||||||
|
|
||||||
TRACE("Writing to device");
|
TRACE("Writing to device");
|
||||||
|
|
||||||
ret = bdrv_write(exp->bs, (request.from + exp->dev_offset) / 512,
|
ret = blk_write(exp->blk,
|
||||||
req->data, request.len / 512);
|
(request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
|
||||||
|
req->data, request.len / BDRV_SECTOR_SIZE);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("writing to file failed");
|
LOG("writing to file failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
|
@ -1244,7 +1243,7 @@ static void nbd_trip(void *opaque)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.type & NBD_CMD_FLAG_FUA) {
|
if (request.type & NBD_CMD_FLAG_FUA) {
|
||||||
ret = bdrv_co_flush(exp->bs);
|
ret = blk_co_flush(exp->blk);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("flush failed");
|
LOG("flush failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
|
@ -1263,7 +1262,7 @@ static void nbd_trip(void *opaque)
|
||||||
case NBD_CMD_FLUSH:
|
case NBD_CMD_FLUSH:
|
||||||
TRACE("Request type is FLUSH");
|
TRACE("Request type is FLUSH");
|
||||||
|
|
||||||
ret = bdrv_co_flush(exp->bs);
|
ret = blk_co_flush(exp->blk);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("flush failed");
|
LOG("flush failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
|
@ -1274,8 +1273,9 @@ static void nbd_trip(void *opaque)
|
||||||
break;
|
break;
|
||||||
case NBD_CMD_TRIM:
|
case NBD_CMD_TRIM:
|
||||||
TRACE("Request type is TRIM");
|
TRACE("Request type is TRIM");
|
||||||
ret = bdrv_co_discard(exp->bs, (request.from + exp->dev_offset) / 512,
|
ret = blk_co_discard(exp->blk, (request.from + exp->dev_offset)
|
||||||
request.len / 512);
|
/ BDRV_SECTOR_SIZE,
|
||||||
|
request.len / BDRV_SECTOR_SIZE);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("discard failed");
|
LOG("discard failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
|
|
Loading…
Reference in New Issue