1 /* 2 * Null block driver 3 * 4 * Authors: 5 * Fam Zheng <famz@redhat.com> 6 * 7 * Copyright (C) 2014 Red Hat, Inc. 8 * 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. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "qapi/error.h" 15 #include "qapi/qmp/qdict.h" 16 #include "qapi/qmp/qstring.h" 17 #include "qemu/option.h" 18 #include "block/block_int.h" 19 20 #define NULL_OPT_LATENCY "latency-ns" 21 #define NULL_OPT_ZEROES "read-zeroes" 22 23 typedef struct { 24 int64_t length; 25 int64_t latency_ns; 26 bool read_zeroes; 27 } BDRVNullState; 28 29 static QemuOptsList runtime_opts = { 30 .name = "null", 31 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 32 .desc = { 33 { 34 .name = BLOCK_OPT_SIZE, 35 .type = QEMU_OPT_SIZE, 36 .help = "size of the null block", 37 }, 38 { 39 .name = NULL_OPT_LATENCY, 40 .type = QEMU_OPT_NUMBER, 41 .help = "nanoseconds (approximated) to wait " 42 "before completing request", 43 }, 44 { 45 .name = NULL_OPT_ZEROES, 46 .type = QEMU_OPT_BOOL, 47 .help = "return zeroes when read", 48 }, 49 { /* end of list */ } 50 }, 51 }; 52 53 static void null_co_parse_filename(const char *filename, QDict *options, 54 Error **errp) 55 { 56 /* This functions only exists so that a null-co:// filename is accepted 57 * with the null-co driver. */ 58 if (strcmp(filename, "null-co://")) { 59 error_setg(errp, "The only allowed filename for this driver is " 60 "'null-co://'"); 61 return; 62 } 63 } 64 65 static void null_aio_parse_filename(const char *filename, QDict *options, 66 Error **errp) 67 { 68 /* This functions only exists so that a null-aio:// filename is accepted 69 * with the null-aio driver. */ 70 if (strcmp(filename, "null-aio://")) { 71 error_setg(errp, "The only allowed filename for this driver is " 72 "'null-aio://'"); 73 return; 74 } 75 } 76 77 static int null_file_open(BlockDriverState *bs, QDict *options, int flags, 78 Error **errp) 79 { 80 QemuOpts *opts; 81 BDRVNullState *s = bs->opaque; 82 int ret = 0; 83 84 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 85 qemu_opts_absorb_qdict(opts, options, &error_abort); 86 s->length = 87 qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30); 88 s->latency_ns = 89 qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0); 90 if (s->latency_ns < 0) { 91 error_setg(errp, "latency-ns is invalid"); 92 ret = -EINVAL; 93 } 94 s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false); 95 qemu_opts_del(opts); 96 bs->supported_write_flags = BDRV_REQ_FUA; 97 return ret; 98 } 99 100 static void null_close(BlockDriverState *bs) 101 { 102 } 103 104 static int64_t null_getlength(BlockDriverState *bs) 105 { 106 BDRVNullState *s = bs->opaque; 107 return s->length; 108 } 109 110 static coroutine_fn int null_co_common(BlockDriverState *bs) 111 { 112 BDRVNullState *s = bs->opaque; 113 114 if (s->latency_ns) { 115 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, s->latency_ns); 116 } 117 return 0; 118 } 119 120 static coroutine_fn int null_co_preadv(BlockDriverState *bs, 121 uint64_t offset, uint64_t bytes, 122 QEMUIOVector *qiov, int flags) 123 { 124 BDRVNullState *s = bs->opaque; 125 126 if (s->read_zeroes) { 127 qemu_iovec_memset(qiov, 0, 0, bytes); 128 } 129 130 return null_co_common(bs); 131 } 132 133 static coroutine_fn int null_co_pwritev(BlockDriverState *bs, 134 uint64_t offset, uint64_t bytes, 135 QEMUIOVector *qiov, int flags) 136 { 137 return null_co_common(bs); 138 } 139 140 static coroutine_fn int null_co_flush(BlockDriverState *bs) 141 { 142 return null_co_common(bs); 143 } 144 145 typedef struct { 146 BlockAIOCB common; 147 QEMUTimer timer; 148 } NullAIOCB; 149 150 static const AIOCBInfo null_aiocb_info = { 151 .aiocb_size = sizeof(NullAIOCB), 152 }; 153 154 static void null_bh_cb(void *opaque) 155 { 156 NullAIOCB *acb = opaque; 157 acb->common.cb(acb->common.opaque, 0); 158 qemu_aio_unref(acb); 159 } 160 161 static void null_timer_cb(void *opaque) 162 { 163 NullAIOCB *acb = opaque; 164 acb->common.cb(acb->common.opaque, 0); 165 timer_deinit(&acb->timer); 166 qemu_aio_unref(acb); 167 } 168 169 static inline BlockAIOCB *null_aio_common(BlockDriverState *bs, 170 BlockCompletionFunc *cb, 171 void *opaque) 172 { 173 NullAIOCB *acb; 174 BDRVNullState *s = bs->opaque; 175 176 acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque); 177 /* Only emulate latency after vcpu is running. */ 178 if (s->latency_ns) { 179 aio_timer_init(bdrv_get_aio_context(bs), &acb->timer, 180 QEMU_CLOCK_REALTIME, SCALE_NS, 181 null_timer_cb, acb); 182 timer_mod_ns(&acb->timer, 183 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns); 184 } else { 185 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), null_bh_cb, acb); 186 } 187 return &acb->common; 188 } 189 190 static BlockAIOCB *null_aio_preadv(BlockDriverState *bs, 191 uint64_t offset, uint64_t bytes, 192 QEMUIOVector *qiov, int flags, 193 BlockCompletionFunc *cb, 194 void *opaque) 195 { 196 BDRVNullState *s = bs->opaque; 197 198 if (s->read_zeroes) { 199 qemu_iovec_memset(qiov, 0, 0, bytes); 200 } 201 202 return null_aio_common(bs, cb, opaque); 203 } 204 205 static BlockAIOCB *null_aio_pwritev(BlockDriverState *bs, 206 uint64_t offset, uint64_t bytes, 207 QEMUIOVector *qiov, int flags, 208 BlockCompletionFunc *cb, 209 void *opaque) 210 { 211 return null_aio_common(bs, cb, opaque); 212 } 213 214 static BlockAIOCB *null_aio_flush(BlockDriverState *bs, 215 BlockCompletionFunc *cb, 216 void *opaque) 217 { 218 return null_aio_common(bs, cb, opaque); 219 } 220 221 static int null_reopen_prepare(BDRVReopenState *reopen_state, 222 BlockReopenQueue *queue, Error **errp) 223 { 224 return 0; 225 } 226 227 static int coroutine_fn null_co_block_status(BlockDriverState *bs, 228 bool want_zero, int64_t offset, 229 int64_t bytes, int64_t *pnum, 230 int64_t *map, 231 BlockDriverState **file) 232 { 233 BDRVNullState *s = bs->opaque; 234 int ret = BDRV_BLOCK_OFFSET_VALID; 235 236 *pnum = bytes; 237 *map = offset; 238 *file = bs; 239 240 if (s->read_zeroes) { 241 ret |= BDRV_BLOCK_ZERO; 242 } 243 return ret; 244 } 245 246 static void null_refresh_filename(BlockDriverState *bs, QDict *opts) 247 { 248 qdict_del(opts, "filename"); 249 250 if (!qdict_size(opts)) { 251 snprintf(bs->exact_filename, sizeof(bs->exact_filename), "%s://", 252 bs->drv->format_name); 253 } 254 255 qdict_put_str(opts, "driver", bs->drv->format_name); 256 bs->full_open_options = qobject_ref(opts); 257 } 258 259 static BlockDriver bdrv_null_co = { 260 .format_name = "null-co", 261 .protocol_name = "null-co", 262 .instance_size = sizeof(BDRVNullState), 263 264 .bdrv_file_open = null_file_open, 265 .bdrv_parse_filename = null_co_parse_filename, 266 .bdrv_close = null_close, 267 .bdrv_getlength = null_getlength, 268 269 .bdrv_co_preadv = null_co_preadv, 270 .bdrv_co_pwritev = null_co_pwritev, 271 .bdrv_co_flush_to_disk = null_co_flush, 272 .bdrv_reopen_prepare = null_reopen_prepare, 273 274 .bdrv_co_block_status = null_co_block_status, 275 276 .bdrv_refresh_filename = null_refresh_filename, 277 }; 278 279 static BlockDriver bdrv_null_aio = { 280 .format_name = "null-aio", 281 .protocol_name = "null-aio", 282 .instance_size = sizeof(BDRVNullState), 283 284 .bdrv_file_open = null_file_open, 285 .bdrv_parse_filename = null_aio_parse_filename, 286 .bdrv_close = null_close, 287 .bdrv_getlength = null_getlength, 288 289 .bdrv_aio_preadv = null_aio_preadv, 290 .bdrv_aio_pwritev = null_aio_pwritev, 291 .bdrv_aio_flush = null_aio_flush, 292 .bdrv_reopen_prepare = null_reopen_prepare, 293 294 .bdrv_co_block_status = null_co_block_status, 295 296 .bdrv_refresh_filename = null_refresh_filename, 297 }; 298 299 static void bdrv_null_init(void) 300 { 301 bdrv_register(&bdrv_null_co); 302 bdrv_register(&bdrv_null_aio); 303 } 304 305 block_init(bdrv_null_init); 306