1 /* 2 * Block protocol for block driver correctness testing 3 * 4 * Copyright (C) 2010 IBM, Corp. 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or later. 7 * See the COPYING file in the top-level directory. 8 */ 9 10 #include <stdarg.h> 11 #include "qemu/sockets.h" /* for EINPROGRESS on Windows */ 12 #include "block/block_int.h" 13 #include "qapi/qmp/qdict.h" 14 #include "qapi/qmp/qstring.h" 15 16 typedef struct { 17 BlockDriverState *test_file; 18 } BDRVBlkverifyState; 19 20 typedef struct BlkverifyAIOCB BlkverifyAIOCB; 21 struct BlkverifyAIOCB { 22 BlockDriverAIOCB common; 23 QEMUBH *bh; 24 25 /* Request metadata */ 26 bool is_write; 27 int64_t sector_num; 28 int nb_sectors; 29 30 int ret; /* first completed request's result */ 31 unsigned int done; /* completion counter */ 32 bool *finished; /* completion signal for cancel */ 33 34 QEMUIOVector *qiov; /* user I/O vector */ 35 QEMUIOVector raw_qiov; /* cloned I/O vector for raw file */ 36 void *buf; /* buffer for raw file I/O */ 37 38 void (*verify)(BlkverifyAIOCB *acb); 39 }; 40 41 static void blkverify_aio_cancel(BlockDriverAIOCB *blockacb) 42 { 43 BlkverifyAIOCB *acb = (BlkverifyAIOCB *)blockacb; 44 AioContext *aio_context = bdrv_get_aio_context(blockacb->bs); 45 bool finished = false; 46 47 /* Wait until request completes, invokes its callback, and frees itself */ 48 acb->finished = &finished; 49 while (!finished) { 50 aio_poll(aio_context, true); 51 } 52 } 53 54 static const AIOCBInfo blkverify_aiocb_info = { 55 .aiocb_size = sizeof(BlkverifyAIOCB), 56 .cancel = blkverify_aio_cancel, 57 }; 58 59 static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb, 60 const char *fmt, ...) 61 { 62 va_list ap; 63 64 va_start(ap, fmt); 65 fprintf(stderr, "blkverify: %s sector_num=%" PRId64 " nb_sectors=%d ", 66 acb->is_write ? "write" : "read", acb->sector_num, 67 acb->nb_sectors); 68 vfprintf(stderr, fmt, ap); 69 fprintf(stderr, "\n"); 70 va_end(ap); 71 exit(1); 72 } 73 74 /* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */ 75 static void blkverify_parse_filename(const char *filename, QDict *options, 76 Error **errp) 77 { 78 const char *c; 79 QString *raw_path; 80 81 82 /* Parse the blkverify: prefix */ 83 if (!strstart(filename, "blkverify:", &filename)) { 84 /* There was no prefix; therefore, all options have to be already 85 present in the QDict (except for the filename) */ 86 qdict_put(options, "x-image", qstring_from_str(filename)); 87 return; 88 } 89 90 /* Parse the raw image filename */ 91 c = strchr(filename, ':'); 92 if (c == NULL) { 93 error_setg(errp, "blkverify requires raw copy and original image path"); 94 return; 95 } 96 97 /* TODO Implement option pass-through and set raw.filename here */ 98 raw_path = qstring_from_substr(filename, 0, c - filename - 1); 99 qdict_put(options, "x-raw", raw_path); 100 101 /* TODO Allow multi-level nesting and set file.filename here */ 102 filename = c + 1; 103 qdict_put(options, "x-image", qstring_from_str(filename)); 104 } 105 106 static QemuOptsList runtime_opts = { 107 .name = "blkverify", 108 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 109 .desc = { 110 { 111 .name = "x-raw", 112 .type = QEMU_OPT_STRING, 113 .help = "[internal use only, will be removed]", 114 }, 115 { 116 .name = "x-image", 117 .type = QEMU_OPT_STRING, 118 .help = "[internal use only, will be removed]", 119 }, 120 { /* end of list */ } 121 }, 122 }; 123 124 static int blkverify_open(BlockDriverState *bs, QDict *options, int flags, 125 Error **errp) 126 { 127 BDRVBlkverifyState *s = bs->opaque; 128 QemuOpts *opts; 129 Error *local_err = NULL; 130 int ret; 131 132 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 133 qemu_opts_absorb_qdict(opts, options, &local_err); 134 if (local_err) { 135 error_propagate(errp, local_err); 136 ret = -EINVAL; 137 goto fail; 138 } 139 140 /* Open the raw file */ 141 assert(bs->file == NULL); 142 ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-raw"), options, 143 "raw", flags | BDRV_O_PROTOCOL, false, &local_err); 144 if (ret < 0) { 145 error_propagate(errp, local_err); 146 goto fail; 147 } 148 149 /* Open the test file */ 150 assert(s->test_file == NULL); 151 ret = bdrv_open_image(&s->test_file, qemu_opt_get(opts, "x-image"), options, 152 "test", flags, false, &local_err); 153 if (ret < 0) { 154 error_propagate(errp, local_err); 155 s->test_file = NULL; 156 goto fail; 157 } 158 159 ret = 0; 160 fail: 161 return ret; 162 } 163 164 static void blkverify_close(BlockDriverState *bs) 165 { 166 BDRVBlkverifyState *s = bs->opaque; 167 168 bdrv_unref(s->test_file); 169 s->test_file = NULL; 170 } 171 172 static int64_t blkverify_getlength(BlockDriverState *bs) 173 { 174 BDRVBlkverifyState *s = bs->opaque; 175 176 return bdrv_getlength(s->test_file); 177 } 178 179 static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write, 180 int64_t sector_num, QEMUIOVector *qiov, 181 int nb_sectors, 182 BlockDriverCompletionFunc *cb, 183 void *opaque) 184 { 185 BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque); 186 187 acb->bh = NULL; 188 acb->is_write = is_write; 189 acb->sector_num = sector_num; 190 acb->nb_sectors = nb_sectors; 191 acb->ret = -EINPROGRESS; 192 acb->done = 0; 193 acb->qiov = qiov; 194 acb->buf = NULL; 195 acb->verify = NULL; 196 acb->finished = NULL; 197 return acb; 198 } 199 200 static void blkverify_aio_bh(void *opaque) 201 { 202 BlkverifyAIOCB *acb = opaque; 203 204 qemu_bh_delete(acb->bh); 205 if (acb->buf) { 206 qemu_iovec_destroy(&acb->raw_qiov); 207 qemu_vfree(acb->buf); 208 } 209 acb->common.cb(acb->common.opaque, acb->ret); 210 if (acb->finished) { 211 *acb->finished = true; 212 } 213 qemu_aio_release(acb); 214 } 215 216 static void blkverify_aio_cb(void *opaque, int ret) 217 { 218 BlkverifyAIOCB *acb = opaque; 219 220 switch (++acb->done) { 221 case 1: 222 acb->ret = ret; 223 break; 224 225 case 2: 226 if (acb->ret != ret) { 227 blkverify_err(acb, "return value mismatch %d != %d", acb->ret, ret); 228 } 229 230 if (acb->verify) { 231 acb->verify(acb); 232 } 233 234 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs), 235 blkverify_aio_bh, acb); 236 qemu_bh_schedule(acb->bh); 237 break; 238 } 239 } 240 241 static void blkverify_verify_readv(BlkverifyAIOCB *acb) 242 { 243 ssize_t offset = qemu_iovec_compare(acb->qiov, &acb->raw_qiov); 244 if (offset != -1) { 245 blkverify_err(acb, "contents mismatch in sector %" PRId64, 246 acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE)); 247 } 248 } 249 250 static BlockDriverAIOCB *blkverify_aio_readv(BlockDriverState *bs, 251 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 252 BlockDriverCompletionFunc *cb, void *opaque) 253 { 254 BDRVBlkverifyState *s = bs->opaque; 255 BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov, 256 nb_sectors, cb, opaque); 257 258 acb->verify = blkverify_verify_readv; 259 acb->buf = qemu_blockalign(bs->file, qiov->size); 260 qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov); 261 qemu_iovec_clone(&acb->raw_qiov, qiov, acb->buf); 262 263 bdrv_aio_readv(s->test_file, sector_num, qiov, nb_sectors, 264 blkverify_aio_cb, acb); 265 bdrv_aio_readv(bs->file, sector_num, &acb->raw_qiov, nb_sectors, 266 blkverify_aio_cb, acb); 267 return &acb->common; 268 } 269 270 static BlockDriverAIOCB *blkverify_aio_writev(BlockDriverState *bs, 271 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 272 BlockDriverCompletionFunc *cb, void *opaque) 273 { 274 BDRVBlkverifyState *s = bs->opaque; 275 BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov, 276 nb_sectors, cb, opaque); 277 278 bdrv_aio_writev(s->test_file, sector_num, qiov, nb_sectors, 279 blkverify_aio_cb, acb); 280 bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, 281 blkverify_aio_cb, acb); 282 return &acb->common; 283 } 284 285 static BlockDriverAIOCB *blkverify_aio_flush(BlockDriverState *bs, 286 BlockDriverCompletionFunc *cb, 287 void *opaque) 288 { 289 BDRVBlkverifyState *s = bs->opaque; 290 291 /* Only flush test file, the raw file is not important */ 292 return bdrv_aio_flush(s->test_file, cb, opaque); 293 } 294 295 static bool blkverify_recurse_is_first_non_filter(BlockDriverState *bs, 296 BlockDriverState *candidate) 297 { 298 BDRVBlkverifyState *s = bs->opaque; 299 300 bool perm = bdrv_recurse_is_first_non_filter(bs->file, candidate); 301 302 if (perm) { 303 return true; 304 } 305 306 return bdrv_recurse_is_first_non_filter(s->test_file, candidate); 307 } 308 309 /* Propagate AioContext changes to ->test_file */ 310 static void blkverify_detach_aio_context(BlockDriverState *bs) 311 { 312 BDRVBlkverifyState *s = bs->opaque; 313 314 bdrv_detach_aio_context(s->test_file); 315 } 316 317 static void blkverify_attach_aio_context(BlockDriverState *bs, 318 AioContext *new_context) 319 { 320 BDRVBlkverifyState *s = bs->opaque; 321 322 bdrv_attach_aio_context(s->test_file, new_context); 323 } 324 325 static void blkverify_refresh_filename(BlockDriverState *bs) 326 { 327 BDRVBlkverifyState *s = bs->opaque; 328 329 /* bs->file has already been refreshed */ 330 bdrv_refresh_filename(s->test_file); 331 332 if (bs->file->full_open_options && s->test_file->full_open_options) { 333 QDict *opts = qdict_new(); 334 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkverify"))); 335 336 QINCREF(bs->file->full_open_options); 337 qdict_put_obj(opts, "raw", QOBJECT(bs->file->full_open_options)); 338 QINCREF(s->test_file->full_open_options); 339 qdict_put_obj(opts, "test", QOBJECT(s->test_file->full_open_options)); 340 341 bs->full_open_options = opts; 342 } 343 344 if (bs->file->exact_filename[0] && s->test_file->exact_filename[0]) { 345 snprintf(bs->exact_filename, sizeof(bs->exact_filename), 346 "blkverify:%s:%s", 347 bs->file->exact_filename, s->test_file->exact_filename); 348 } 349 } 350 351 static BlockDriver bdrv_blkverify = { 352 .format_name = "blkverify", 353 .protocol_name = "blkverify", 354 .instance_size = sizeof(BDRVBlkverifyState), 355 356 .bdrv_parse_filename = blkverify_parse_filename, 357 .bdrv_file_open = blkverify_open, 358 .bdrv_close = blkverify_close, 359 .bdrv_getlength = blkverify_getlength, 360 .bdrv_refresh_filename = blkverify_refresh_filename, 361 362 .bdrv_aio_readv = blkverify_aio_readv, 363 .bdrv_aio_writev = blkverify_aio_writev, 364 .bdrv_aio_flush = blkverify_aio_flush, 365 366 .bdrv_attach_aio_context = blkverify_attach_aio_context, 367 .bdrv_detach_aio_context = blkverify_detach_aio_context, 368 369 .is_filter = true, 370 .bdrv_recurse_is_first_non_filter = blkverify_recurse_is_first_non_filter, 371 }; 372 373 static void bdrv_blkverify_init(void) 374 { 375 bdrv_register(&bdrv_blkverify); 376 } 377 378 block_init(bdrv_blkverify_init); 379