5 * Fam Zheng <famz@redhat.com>
7 * Copyright (C) 2014 Red Hat, Inc.
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "block/block_int.h"
19 static QemuOptsList runtime_opts
= {
21 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
25 .type
= QEMU_OPT_STRING
,
29 .name
= BLOCK_OPT_SIZE
,
30 .type
= QEMU_OPT_SIZE
,
31 .help
= "size of the null block",
37 static int null_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
41 BDRVNullState
*s
= bs
->opaque
;
43 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
44 qemu_opts_absorb_qdict(opts
, options
, &error_abort
);
46 qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 1 << 30);
51 static void null_close(BlockDriverState
*bs
)
55 static int64_t null_getlength(BlockDriverState
*bs
)
57 BDRVNullState
*s
= bs
->opaque
;
61 static coroutine_fn
int null_co_readv(BlockDriverState
*bs
,
62 int64_t sector_num
, int nb_sectors
,
68 static coroutine_fn
int null_co_writev(BlockDriverState
*bs
,
69 int64_t sector_num
, int nb_sectors
,
75 static coroutine_fn
int null_co_flush(BlockDriverState
*bs
)
81 BlockDriverAIOCB common
;
85 static const AIOCBInfo null_aiocb_info
= {
86 .aiocb_size
= sizeof(NullAIOCB
),
89 static void null_bh_cb(void *opaque
)
91 NullAIOCB
*acb
= opaque
;
92 acb
->common
.cb(acb
->common
.opaque
, 0);
93 qemu_bh_delete(acb
->bh
);
97 static inline BlockDriverAIOCB
*null_aio_common(BlockDriverState
*bs
,
98 BlockDriverCompletionFunc
*cb
,
103 acb
= qemu_aio_get(&null_aiocb_info
, bs
, cb
, opaque
);
104 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
), null_bh_cb
, acb
);
105 qemu_bh_schedule(acb
->bh
);
109 static BlockDriverAIOCB
*null_aio_readv(BlockDriverState
*bs
,
110 int64_t sector_num
, QEMUIOVector
*qiov
,
112 BlockDriverCompletionFunc
*cb
,
115 return null_aio_common(bs
, cb
, opaque
);
118 static BlockDriverAIOCB
*null_aio_writev(BlockDriverState
*bs
,
119 int64_t sector_num
, QEMUIOVector
*qiov
,
121 BlockDriverCompletionFunc
*cb
,
124 return null_aio_common(bs
, cb
, opaque
);
127 static BlockDriverAIOCB
*null_aio_flush(BlockDriverState
*bs
,
128 BlockDriverCompletionFunc
*cb
,
131 return null_aio_common(bs
, cb
, opaque
);
134 static BlockDriver bdrv_null_co
= {
135 .format_name
= "null-co",
136 .protocol_name
= "null-co",
137 .instance_size
= sizeof(BDRVNullState
),
139 .bdrv_file_open
= null_file_open
,
140 .bdrv_close
= null_close
,
141 .bdrv_getlength
= null_getlength
,
143 .bdrv_co_readv
= null_co_readv
,
144 .bdrv_co_writev
= null_co_writev
,
145 .bdrv_co_flush_to_disk
= null_co_flush
,
148 static BlockDriver bdrv_null_aio
= {
149 .format_name
= "null-aio",
150 .protocol_name
= "null-aio",
151 .instance_size
= sizeof(BDRVNullState
),
153 .bdrv_file_open
= null_file_open
,
154 .bdrv_close
= null_close
,
155 .bdrv_getlength
= null_getlength
,
157 .bdrv_aio_readv
= null_aio_readv
,
158 .bdrv_aio_writev
= null_aio_writev
,
159 .bdrv_aio_flush
= null_aio_flush
,
162 static void bdrv_null_init(void)
164 bdrv_register(&bdrv_null_co
);
165 bdrv_register(&bdrv_null_aio
);
168 block_init(bdrv_null_init
);