block.c (68485420187094c26f86faee5c7f68b5d6a03603) block.c (f9f05dc58c50d19ad762e6c1ce6b5def9814a4ed)
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 50 unchanged lines hidden (view full) ---

59static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
60 const uint8_t *buf, int nb_sectors);
61static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs,
62 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
63 BlockDriverCompletionFunc *cb, void *opaque);
64static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs,
65 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
66 BlockDriverCompletionFunc *cb, void *opaque);
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 50 unchanged lines hidden (view full) ---

59static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
60 const uint8_t *buf, int nb_sectors);
61static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs,
62 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
63 BlockDriverCompletionFunc *cb, void *opaque);
64static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs,
65 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
66 BlockDriverCompletionFunc *cb, void *opaque);
67static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
68 int64_t sector_num, int nb_sectors,
69 QEMUIOVector *iov);
70static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
71 int64_t sector_num, int nb_sectors,
72 QEMUIOVector *iov);
67
68static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
69 QTAILQ_HEAD_INITIALIZER(bdrv_states);
70
71static QLIST_HEAD(, BlockDriver) bdrv_drivers =
72 QLIST_HEAD_INITIALIZER(bdrv_drivers);
73
74/* The device to use for VM snapshots */

--- 102 unchanged lines hidden (view full) ---

177void bdrv_register(BlockDriver *bdrv)
178{
179 if (bdrv->bdrv_co_readv) {
180 /* Emulate AIO by coroutines, and sync by AIO */
181 bdrv->bdrv_aio_readv = bdrv_co_aio_readv_em;
182 bdrv->bdrv_aio_writev = bdrv_co_aio_writev_em;
183 bdrv->bdrv_read = bdrv_read_em;
184 bdrv->bdrv_write = bdrv_write_em;
73
74static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
75 QTAILQ_HEAD_INITIALIZER(bdrv_states);
76
77static QLIST_HEAD(, BlockDriver) bdrv_drivers =
78 QLIST_HEAD_INITIALIZER(bdrv_drivers);
79
80/* The device to use for VM snapshots */

--- 102 unchanged lines hidden (view full) ---

183void bdrv_register(BlockDriver *bdrv)
184{
185 if (bdrv->bdrv_co_readv) {
186 /* Emulate AIO by coroutines, and sync by AIO */
187 bdrv->bdrv_aio_readv = bdrv_co_aio_readv_em;
188 bdrv->bdrv_aio_writev = bdrv_co_aio_writev_em;
189 bdrv->bdrv_read = bdrv_read_em;
190 bdrv->bdrv_write = bdrv_write_em;
185 } else if (!bdrv->bdrv_aio_readv) {
186 /* add AIO emulation layer */
187 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
188 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
189 } else if (!bdrv->bdrv_read) {
190 /* add synchronous IO emulation layer */
191 bdrv->bdrv_read = bdrv_read_em;
192 bdrv->bdrv_write = bdrv_write_em;
191 } else {
192 bdrv->bdrv_co_readv = bdrv_co_readv_em;
193 bdrv->bdrv_co_writev = bdrv_co_writev_em;
194
195 if (!bdrv->bdrv_aio_readv) {
196 /* add AIO emulation layer */
197 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
198 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
199 } else if (!bdrv->bdrv_read) {
200 /* add synchronous IO emulation layer */
201 bdrv->bdrv_read = bdrv_read_em;
202 bdrv->bdrv_write = bdrv_write_em;
203 }
193 }
194
195 if (!bdrv->bdrv_aio_flush)
196 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
197
198 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
199}
200

--- 2650 unchanged lines hidden (view full) ---

2851{
2852 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2853 AIOPool *pool = acb->pool;
2854 acb->next = pool->free_aiocb;
2855 pool->free_aiocb = acb;
2856}
2857
2858/**************************************************************/
204 }
205
206 if (!bdrv->bdrv_aio_flush)
207 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
208
209 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
210}
211

--- 2650 unchanged lines hidden (view full) ---

2862{
2863 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2864 AIOPool *pool = acb->pool;
2865 acb->next = pool->free_aiocb;
2866 pool->free_aiocb = acb;
2867}
2868
2869/**************************************************************/
2870/* Coroutine block device emulation */
2871
2872typedef struct CoroutineIOCompletion {
2873 Coroutine *coroutine;
2874 int ret;
2875} CoroutineIOCompletion;
2876
2877static void bdrv_co_io_em_complete(void *opaque, int ret)
2878{
2879 CoroutineIOCompletion *co = opaque;
2880
2881 co->ret = ret;
2882 qemu_coroutine_enter(co->coroutine, NULL);
2883}
2884
2885static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
2886 int nb_sectors, QEMUIOVector *iov,
2887 bool is_write)
2888{
2889 CoroutineIOCompletion co = {
2890 .coroutine = qemu_coroutine_self(),
2891 };
2892 BlockDriverAIOCB *acb;
2893
2894 if (is_write) {
2895 acb = bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
2896 bdrv_co_io_em_complete, &co);
2897 } else {
2898 acb = bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
2899 bdrv_co_io_em_complete, &co);
2900 }
2901
2902 trace_bdrv_co_io(is_write, acb);
2903 if (!acb) {
2904 return -EIO;
2905 }
2906 qemu_coroutine_yield();
2907
2908 return co.ret;
2909}
2910
2911static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
2912 int64_t sector_num, int nb_sectors,
2913 QEMUIOVector *iov)
2914{
2915 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
2916}
2917
2918static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
2919 int64_t sector_num, int nb_sectors,
2920 QEMUIOVector *iov)
2921{
2922 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
2923}
2924
2925/**************************************************************/
2859/* removable device support */
2860
2861/**
2862 * Return TRUE if the media is present
2863 */
2864int bdrv_is_inserted(BlockDriverState *bs)
2865{
2866 BlockDriver *drv = bs->drv;

--- 294 unchanged lines hidden ---
2926/* removable device support */
2927
2928/**
2929 * Return TRUE if the media is present
2930 */
2931int bdrv_is_inserted(BlockDriverState *bs)
2932{
2933 BlockDriver *drv = bs->drv;

--- 294 unchanged lines hidden ---