Skip to content

Commit 61124f0

Browse files
bonziniFam Zheng
authored and
Fam Zheng
committedJul 17, 2017
block: invoke .bdrv_drain callback in coroutine context and from AioContext
This will let the callback take a CoMutex in the next patch. Reviewed-by: Stefan Hajnoczi <[email protected]> Reviewed-by: Fam Zheng <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> Message-Id: <[email protected]> Signed-off-by: Fam Zheng <[email protected]>
1 parent e7569c1 commit 61124f0

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed
 

‎block/io.c

+33-9
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,46 @@ bool bdrv_requests_pending(BlockDriverState *bs)
149149
return false;
150150
}
151151

152+
typedef struct {
153+
Coroutine *co;
154+
BlockDriverState *bs;
155+
bool done;
156+
} BdrvCoDrainData;
157+
158+
static void coroutine_fn bdrv_drain_invoke_entry(void *opaque)
159+
{
160+
BdrvCoDrainData *data = opaque;
161+
BlockDriverState *bs = data->bs;
162+
163+
bs->drv->bdrv_co_drain(bs);
164+
165+
/* Set data->done before reading bs->wakeup. */
166+
atomic_mb_set(&data->done, true);
167+
bdrv_wakeup(bs);
168+
}
169+
170+
static void bdrv_drain_invoke(BlockDriverState *bs)
171+
{
172+
BdrvCoDrainData data = { .bs = bs, .done = false };
173+
174+
if (!bs->drv || !bs->drv->bdrv_co_drain) {
175+
return;
176+
}
177+
178+
data.co = qemu_coroutine_create(bdrv_drain_invoke_entry, &data);
179+
bdrv_coroutine_enter(bs, data.co);
180+
BDRV_POLL_WHILE(bs, !data.done);
181+
}
182+
152183
static bool bdrv_drain_recurse(BlockDriverState *bs)
153184
{
154185
BdrvChild *child, *tmp;
155186
bool waited;
156187

157188
waited = BDRV_POLL_WHILE(bs, atomic_read(&bs->in_flight) > 0);
158189

159-
if (bs->drv && bs->drv->bdrv_drain) {
160-
bs->drv->bdrv_drain(bs);
161-
}
190+
/* Ensure any pending metadata writes are submitted to bs->file. */
191+
bdrv_drain_invoke(bs);
162192

163193
QLIST_FOREACH_SAFE(child, &bs->children, next, tmp) {
164194
BlockDriverState *bs = child->bs;
@@ -184,12 +214,6 @@ static bool bdrv_drain_recurse(BlockDriverState *bs)
184214
return waited;
185215
}
186216

187-
typedef struct {
188-
Coroutine *co;
189-
BlockDriverState *bs;
190-
bool done;
191-
} BdrvCoDrainData;
192-
193217
static void bdrv_co_drain_bh_cb(void *opaque)
194218
{
195219
BdrvCoDrainData *data = opaque;

‎block/qed.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ static void bdrv_qed_attach_aio_context(BlockDriverState *bs,
350350
}
351351
}
352352

353-
static void bdrv_qed_drain(BlockDriverState *bs)
353+
static void coroutine_fn bdrv_qed_co_drain(BlockDriverState *bs)
354354
{
355355
BDRVQEDState *s = bs->opaque;
356356

@@ -359,7 +359,7 @@ static void bdrv_qed_drain(BlockDriverState *bs)
359359
*/
360360
if (s->need_check_timer && timer_pending(s->need_check_timer)) {
361361
qed_cancel_need_check_timer(s);
362-
qed_need_check_timer_cb(s);
362+
qed_need_check_timer_entry(s);
363363
}
364364
}
365365

@@ -1548,7 +1548,7 @@ static BlockDriver bdrv_qed = {
15481548
.bdrv_check = bdrv_qed_check,
15491549
.bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
15501550
.bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
1551-
.bdrv_drain = bdrv_qed_drain,
1551+
.bdrv_co_drain = bdrv_qed_co_drain,
15521552
};
15531553

15541554
static void bdrv_qed_init(void)

‎include/block/block_int.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ struct BlockDriver {
324324
* Drain and stop any internal sources of requests in the driver, and
325325
* remain so until next I/O callback (e.g. bdrv_co_writev) is called.
326326
*/
327-
void (*bdrv_drain)(BlockDriverState *bs);
327+
void coroutine_fn (*bdrv_co_drain)(BlockDriverState *bs);
328328

329329
void (*bdrv_add_child)(BlockDriverState *parent, BlockDriverState *child,
330330
Error **errp);

0 commit comments

Comments
 (0)
Please sign in to comment.