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