Skip to content

Commit 16d36e2

Browse files
Vladimir Sementsov-Ogievskiyebblake
authored andcommitted
block-backend: drop blk_prw, use block-coroutine-wrapper
Let's drop hand-made coroutine wrappers and use coroutine wrapper generation like in block/io.c. Now, blk_foo() functions are written in same way as blk_co_foo() ones, but wrap blk_do_foo() instead of blk_co_do_foo(). Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]> Message-Id: <[email protected]> Reviewed-by: Eric Blake <[email protected]> [eblake: spelling fix] Signed-off-by: Eric Blake <[email protected]>
1 parent 7d55a3b commit 16d36e2

File tree

2 files changed

+88
-93
lines changed

2 files changed

+88
-93
lines changed

block/block-backend.c

Lines changed: 58 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "sysemu/block-backend.h"
1515
#include "block/block_int.h"
1616
#include "block/blockjob.h"
17+
#include "block/coroutines.h"
1718
#include "block/throttle-groups.h"
1819
#include "hw/qdev-core.h"
1920
#include "sysemu/blockdev.h"
@@ -1204,7 +1205,7 @@ static void coroutine_fn blk_wait_while_drained(BlockBackend *blk)
12041205
}
12051206

12061207
/* To be called between exactly one pair of blk_inc/dec_in_flight() */
1207-
static int coroutine_fn
1208+
int coroutine_fn
12081209
blk_co_do_preadv(BlockBackend *blk, int64_t offset, int64_t bytes,
12091210
QEMUIOVector *qiov, BdrvRequestFlags flags)
12101211
{
@@ -1249,7 +1250,7 @@ int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
12491250
}
12501251

12511252
/* To be called between exactly one pair of blk_inc/dec_in_flight() */
1252-
static int coroutine_fn
1253+
int coroutine_fn
12531254
blk_co_do_pwritev_part(BlockBackend *blk, int64_t offset, int64_t bytes,
12541255
QEMUIOVector *qiov, size_t qiov_offset,
12551256
BdrvRequestFlags flags)
@@ -1306,6 +1307,20 @@ int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
13061307
return blk_co_pwritev_part(blk, offset, bytes, qiov, 0, flags);
13071308
}
13081309

1310+
static int coroutine_fn blk_pwritev_part(BlockBackend *blk, int64_t offset,
1311+
int64_t bytes,
1312+
QEMUIOVector *qiov, size_t qiov_offset,
1313+
BdrvRequestFlags flags)
1314+
{
1315+
int ret;
1316+
1317+
blk_inc_in_flight(blk);
1318+
ret = blk_do_pwritev_part(blk, offset, bytes, qiov, qiov_offset, flags);
1319+
blk_dec_in_flight(blk);
1320+
1321+
return ret;
1322+
}
1323+
13091324
typedef struct BlkRwCo {
13101325
BlockBackend *blk;
13111326
int64_t offset;
@@ -1314,58 +1329,11 @@ typedef struct BlkRwCo {
13141329
BdrvRequestFlags flags;
13151330
} BlkRwCo;
13161331

1317-
static void blk_read_entry(void *opaque)
1318-
{
1319-
BlkRwCo *rwco = opaque;
1320-
QEMUIOVector *qiov = rwco->iobuf;
1321-
1322-
rwco->ret = blk_co_do_preadv(rwco->blk, rwco->offset, qiov->size,
1323-
qiov, rwco->flags);
1324-
aio_wait_kick();
1325-
}
1326-
1327-
static void blk_write_entry(void *opaque)
1328-
{
1329-
BlkRwCo *rwco = opaque;
1330-
QEMUIOVector *qiov = rwco->iobuf;
1331-
1332-
rwco->ret = blk_co_do_pwritev_part(rwco->blk, rwco->offset, qiov->size,
1333-
qiov, 0, rwco->flags);
1334-
aio_wait_kick();
1335-
}
1336-
1337-
static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf,
1338-
int64_t bytes, CoroutineEntry co_entry,
1339-
BdrvRequestFlags flags)
1340-
{
1341-
QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
1342-
BlkRwCo rwco = {
1343-
.blk = blk,
1344-
.offset = offset,
1345-
.iobuf = &qiov,
1346-
.flags = flags,
1347-
.ret = NOT_DONE,
1348-
};
1349-
1350-
blk_inc_in_flight(blk);
1351-
if (qemu_in_coroutine()) {
1352-
/* Fast-path if already in coroutine context */
1353-
co_entry(&rwco);
1354-
} else {
1355-
Coroutine *co = qemu_coroutine_create(co_entry, &rwco);
1356-
bdrv_coroutine_enter(blk_bs(blk), co);
1357-
BDRV_POLL_WHILE(blk_bs(blk), rwco.ret == NOT_DONE);
1358-
}
1359-
blk_dec_in_flight(blk);
1360-
1361-
return rwco.ret;
1362-
}
1363-
13641332
int blk_pwrite_zeroes(BlockBackend *blk, int64_t offset,
13651333
int bytes, BdrvRequestFlags flags)
13661334
{
1367-
return blk_prw(blk, offset, NULL, bytes, blk_write_entry,
1368-
flags | BDRV_REQ_ZERO_WRITE);
1335+
return blk_pwritev_part(blk, offset, bytes, NULL, 0,
1336+
flags | BDRV_REQ_ZERO_WRITE);
13691337
}
13701338

13711339
int blk_make_zero(BlockBackend *blk, BdrvRequestFlags flags)
@@ -1510,22 +1478,25 @@ BlockAIOCB *blk_aio_pwrite_zeroes(BlockBackend *blk, int64_t offset,
15101478

15111479
int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
15121480
{
1513-
int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0);
1514-
if (ret < 0) {
1515-
return ret;
1516-
}
1517-
return count;
1481+
int ret;
1482+
QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, count);
1483+
1484+
blk_inc_in_flight(blk);
1485+
ret = blk_do_preadv(blk, offset, count, &qiov, 0);
1486+
blk_dec_in_flight(blk);
1487+
1488+
return ret < 0 ? ret : count;
15181489
}
15191490

15201491
int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count,
15211492
BdrvRequestFlags flags)
15221493
{
1523-
int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
1524-
flags);
1525-
if (ret < 0) {
1526-
return ret;
1527-
}
1528-
return count;
1494+
int ret;
1495+
QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, count);
1496+
1497+
ret = blk_pwritev_part(blk, offset, count, &qiov, 0, flags);
1498+
1499+
return ret < 0 ? ret : count;
15291500
}
15301501

15311502
int64_t blk_getlength(BlockBackend *blk)
@@ -1582,7 +1553,7 @@ void blk_aio_cancel_async(BlockAIOCB *acb)
15821553
}
15831554

15841555
/* To be called between exactly one pair of blk_inc/dec_in_flight() */
1585-
static int coroutine_fn
1556+
int coroutine_fn
15861557
blk_co_do_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
15871558
{
15881559
blk_wait_while_drained(blk);
@@ -1594,18 +1565,15 @@ blk_co_do_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
15941565
return bdrv_co_ioctl(blk_bs(blk), req, buf);
15951566
}
15961567

1597-
static void blk_ioctl_entry(void *opaque)
1568+
int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
15981569
{
1599-
BlkRwCo *rwco = opaque;
1600-
QEMUIOVector *qiov = rwco->iobuf;
1570+
int ret;
16011571

1602-
rwco->ret = blk_co_do_ioctl(rwco->blk, rwco->offset, qiov->iov[0].iov_base);
1603-
aio_wait_kick();
1604-
}
1572+
blk_inc_in_flight(blk);
1573+
ret = blk_do_ioctl(blk, req, buf);
1574+
blk_dec_in_flight(blk);
16051575

1606-
int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
1607-
{
1608-
return blk_prw(blk, req, buf, 0, blk_ioctl_entry, 0);
1576+
return ret;
16091577
}
16101578

16111579
static void blk_aio_ioctl_entry(void *opaque)
@@ -1625,7 +1593,7 @@ BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
16251593
}
16261594

16271595
/* To be called between exactly one pair of blk_inc/dec_in_flight() */
1628-
static int coroutine_fn
1596+
int coroutine_fn
16291597
blk_co_do_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes)
16301598
{
16311599
int ret;
@@ -1669,22 +1637,19 @@ int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset,
16691637
return ret;
16701638
}
16711639

1672-
static void blk_pdiscard_entry(void *opaque)
1640+
int blk_pdiscard(BlockBackend *blk, int64_t offset, int bytes)
16731641
{
1674-
BlkRwCo *rwco = opaque;
1675-
QEMUIOVector *qiov = rwco->iobuf;
1642+
int ret;
16761643

1677-
rwco->ret = blk_co_do_pdiscard(rwco->blk, rwco->offset, qiov->size);
1678-
aio_wait_kick();
1679-
}
1644+
blk_inc_in_flight(blk);
1645+
ret = blk_do_pdiscard(blk, offset, bytes);
1646+
blk_dec_in_flight(blk);
16801647

1681-
int blk_pdiscard(BlockBackend *blk, int64_t offset, int bytes)
1682-
{
1683-
return blk_prw(blk, offset, NULL, bytes, blk_pdiscard_entry, 0);
1648+
return ret;
16841649
}
16851650

16861651
/* To be called between exactly one pair of blk_inc/dec_in_flight() */
1687-
static int coroutine_fn blk_co_do_flush(BlockBackend *blk)
1652+
int coroutine_fn blk_co_do_flush(BlockBackend *blk)
16881653
{
16891654
blk_wait_while_drained(blk);
16901655

@@ -1721,16 +1686,15 @@ int coroutine_fn blk_co_flush(BlockBackend *blk)
17211686
return ret;
17221687
}
17231688

1724-
static void blk_flush_entry(void *opaque)
1725-
{
1726-
BlkRwCo *rwco = opaque;
1727-
rwco->ret = blk_co_do_flush(rwco->blk);
1728-
aio_wait_kick();
1729-
}
1730-
17311689
int blk_flush(BlockBackend *blk)
17321690
{
1733-
return blk_prw(blk, 0, NULL, 0, blk_flush_entry, 0);
1691+
int ret;
1692+
1693+
blk_inc_in_flight(blk);
1694+
ret = blk_do_flush(blk);
1695+
blk_dec_in_flight(blk);
1696+
1697+
return ret;
17341698
}
17351699

17361700
void blk_drain(BlockBackend *blk)
@@ -2224,8 +2188,9 @@ int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
22242188
int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,
22252189
int count)
22262190
{
2227-
return blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
2228-
BDRV_REQ_WRITE_COMPRESSED);
2191+
QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, count);
2192+
return blk_pwritev_part(blk, offset, count, &qiov, 0,
2193+
BDRV_REQ_WRITE_COMPRESSED);
22292194
}
22302195

22312196
int blk_truncate(BlockBackend *blk, int64_t offset, bool exact,

block/coroutines.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,34 @@ int coroutine_fn
7575
nbd_co_do_establish_connection(BlockDriverState *bs, Error **errp);
7676

7777

78+
int generated_co_wrapper
79+
blk_do_preadv(BlockBackend *blk, int64_t offset, int64_t bytes,
80+
QEMUIOVector *qiov, BdrvRequestFlags flags);
81+
int coroutine_fn
82+
blk_co_do_preadv(BlockBackend *blk, int64_t offset, int64_t bytes,
83+
QEMUIOVector *qiov, BdrvRequestFlags flags);
84+
85+
86+
int generated_co_wrapper
87+
blk_do_pwritev_part(BlockBackend *blk, int64_t offset, int64_t bytes,
88+
QEMUIOVector *qiov, size_t qiov_offset,
89+
BdrvRequestFlags flags);
90+
int coroutine_fn
91+
blk_co_do_pwritev_part(BlockBackend *blk, int64_t offset, int64_t bytes,
92+
QEMUIOVector *qiov, size_t qiov_offset,
93+
BdrvRequestFlags flags);
94+
95+
int generated_co_wrapper
96+
blk_do_ioctl(BlockBackend *blk, unsigned long int req, void *buf);
97+
int coroutine_fn
98+
blk_co_do_ioctl(BlockBackend *blk, unsigned long int req, void *buf);
99+
100+
int generated_co_wrapper
101+
blk_do_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes);
102+
int coroutine_fn
103+
blk_co_do_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes);
104+
105+
int generated_co_wrapper blk_do_flush(BlockBackend *blk);
106+
int coroutine_fn blk_co_do_flush(BlockBackend *blk);
107+
78108
#endif /* BLOCK_COROUTINES_INT_H */

0 commit comments

Comments
 (0)