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 #include "qemu/cutils.h" 17 #include "qemu/module.h" 18 #include "qemu/option.h" 19 #include "qemu/memalign.h" 20 21 typedef struct { 22 BdrvChild *test_file; 23 } BDRVBlkverifyState; 24 25 typedef struct BlkverifyRequest { 26 Coroutine *co; 27 BlockDriverState *bs; 28 29 /* Request metadata */ 30 bool is_write; 31 uint64_t offset; 32 uint64_t bytes; 33 int flags; 34 35 int (*request_fn)(BdrvChild *, int64_t, int64_t, QEMUIOVector *, 36 BdrvRequestFlags); 37 38 int ret; /* test image result */ 39 int raw_ret; /* raw image result */ 40 41 unsigned int done; /* completion counter */ 42 43 QEMUIOVector *qiov; /* user I/O vector */ 44 QEMUIOVector *raw_qiov; /* cloned I/O vector for raw file */ 45 } BlkverifyRequest; 46 47 static void G_GNUC_PRINTF(2, 3) blkverify_err(BlkverifyRequest *r, 48 const char *fmt, ...) 49 { 50 va_list ap; 51 52 va_start(ap, fmt); 53 fprintf(stderr, "blkverify: %s offset=%" PRId64 " bytes=%" PRId64 " ", 54 r->is_write ? "write" : "read", r->offset, r->bytes); 55 vfprintf(stderr, fmt, ap); 56 fprintf(stderr, "\n"); 57 va_end(ap); 58 exit(1); 59 } 60 61 /* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */ 62 static void blkverify_parse_filename(const char *filename, QDict *options, 63 Error **errp) 64 { 65 const char *c; 66 QString *raw_path; 67 68 69 /* Parse the blkverify: prefix */ 70 if (!strstart(filename, "blkverify:", &filename)) { 71 /* There was no prefix; therefore, all options have to be already 72 present in the QDict (except for the filename) */ 73 qdict_put_str(options, "x-image", filename); 74 return; 75 } 76 77 /* Parse the raw image filename */ 78 c = strchr(filename, ':'); 79 if (c == NULL) { 80 error_setg(errp, "blkverify requires raw copy and original image path"); 81 return; 82 } 83 84 /* TODO Implement option pass-through and set raw.filename here */ 85 raw_path = qstring_from_substr(filename, 0, c - filename); 86 qdict_put(options, "x-raw", raw_path); 87 88 /* TODO Allow multi-level nesting and set file.filename here */ 89 filename = c + 1; 90 qdict_put_str(options, "x-image", filename); 91 } 92 93 static QemuOptsList runtime_opts = { 94 .name = "blkverify", 95 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 96 .desc = { 97 { 98 .name = "x-raw", 99 .type = QEMU_OPT_STRING, 100 .help = "[internal use only, will be removed]", 101 }, 102 { 103 .name = "x-image", 104 .type = QEMU_OPT_STRING, 105 .help = "[internal use only, will be removed]", 106 }, 107 { /* end of list */ } 108 }, 109 }; 110 111 static int blkverify_open(BlockDriverState *bs, QDict *options, int flags, 112 Error **errp) 113 { 114 BDRVBlkverifyState *s = bs->opaque; 115 QemuOpts *opts; 116 int ret; 117 118 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 119 if (!qemu_opts_absorb_qdict(opts, options, errp)) { 120 ret = -EINVAL; 121 goto fail; 122 } 123 124 /* Open the raw file */ 125 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-raw"), options, "raw", 126 bs, &child_of_bds, 127 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, 128 false, errp); 129 if (!bs->file) { 130 ret = -EINVAL; 131 goto fail; 132 } 133 134 /* Open the test file */ 135 s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, 136 "test", bs, &child_of_bds, BDRV_CHILD_DATA, 137 false, errp); 138 if (!s->test_file) { 139 ret = -EINVAL; 140 goto fail; 141 } 142 143 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED; 144 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED; 145 146 ret = 0; 147 fail: 148 qemu_opts_del(opts); 149 return ret; 150 } 151 152 static void blkverify_close(BlockDriverState *bs) 153 { 154 BDRVBlkverifyState *s = bs->opaque; 155 156 bdrv_unref_child(bs, s->test_file); 157 s->test_file = NULL; 158 } 159 160 static int64_t blkverify_getlength(BlockDriverState *bs) 161 { 162 BDRVBlkverifyState *s = bs->opaque; 163 164 return bdrv_getlength(s->test_file->bs); 165 } 166 167 static void coroutine_fn blkverify_do_test_req(void *opaque) 168 { 169 BlkverifyRequest *r = opaque; 170 BDRVBlkverifyState *s = r->bs->opaque; 171 172 r->ret = r->request_fn(s->test_file, r->offset, r->bytes, r->qiov, 173 r->flags); 174 r->done++; 175 qemu_coroutine_enter_if_inactive(r->co); 176 } 177 178 static void coroutine_fn blkverify_do_raw_req(void *opaque) 179 { 180 BlkverifyRequest *r = opaque; 181 182 r->raw_ret = r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_qiov, 183 r->flags); 184 r->done++; 185 qemu_coroutine_enter_if_inactive(r->co); 186 } 187 188 static int coroutine_fn 189 blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offset, 190 uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qiov, 191 int flags, bool is_write) 192 { 193 Coroutine *co_a, *co_b; 194 195 *r = (BlkverifyRequest) { 196 .co = qemu_coroutine_self(), 197 .bs = bs, 198 .offset = offset, 199 .bytes = bytes, 200 .qiov = qiov, 201 .raw_qiov = raw_qiov, 202 .flags = flags, 203 .is_write = is_write, 204 .request_fn = is_write ? bdrv_co_pwritev : bdrv_co_preadv, 205 }; 206 207 co_a = qemu_coroutine_create(blkverify_do_test_req, r); 208 co_b = qemu_coroutine_create(blkverify_do_raw_req, r); 209 210 qemu_coroutine_enter(co_a); 211 qemu_coroutine_enter(co_b); 212 213 while (r->done < 2) { 214 qemu_coroutine_yield(); 215 } 216 217 if (r->ret != r->raw_ret) { 218 blkverify_err(r, "return value mismatch %d != %d", r->ret, r->raw_ret); 219 } 220 221 return r->ret; 222 } 223 224 static int coroutine_fn 225 blkverify_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, 226 QEMUIOVector *qiov, BdrvRequestFlags flags) 227 { 228 BlkverifyRequest r; 229 QEMUIOVector raw_qiov; 230 void *buf; 231 ssize_t cmp_offset; 232 int ret; 233 234 buf = qemu_blockalign(bs->file->bs, qiov->size); 235 qemu_iovec_init(&raw_qiov, qiov->niov); 236 qemu_iovec_clone(&raw_qiov, qiov, buf); 237 238 ret = blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov, flags, 239 false); 240 241 cmp_offset = qemu_iovec_compare(qiov, &raw_qiov); 242 if (cmp_offset != -1) { 243 blkverify_err(&r, "contents mismatch at offset %" PRId64, 244 offset + cmp_offset); 245 } 246 247 qemu_iovec_destroy(&raw_qiov); 248 qemu_vfree(buf); 249 250 return ret; 251 } 252 253 static int coroutine_fn 254 blkverify_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, 255 QEMUIOVector *qiov, BdrvRequestFlags flags) 256 { 257 BlkverifyRequest r; 258 return blkverify_co_prwv(bs, &r, offset, bytes, qiov, qiov, flags, true); 259 } 260 261 static int blkverify_co_flush(BlockDriverState *bs) 262 { 263 BDRVBlkverifyState *s = bs->opaque; 264 265 /* Only flush test file, the raw file is not important */ 266 return bdrv_co_flush(s->test_file->bs); 267 } 268 269 static bool blkverify_recurse_can_replace(BlockDriverState *bs, 270 BlockDriverState *to_replace) 271 { 272 BDRVBlkverifyState *s = bs->opaque; 273 274 /* 275 * blkverify quits the whole qemu process if there is a mismatch 276 * between bs->file->bs and s->test_file->bs. Therefore, we know 277 * know that both must match bs and we can recurse down to either. 278 */ 279 return bdrv_recurse_can_replace(bs->file->bs, to_replace) || 280 bdrv_recurse_can_replace(s->test_file->bs, to_replace); 281 } 282 283 static void blkverify_refresh_filename(BlockDriverState *bs) 284 { 285 BDRVBlkverifyState *s = bs->opaque; 286 287 if (bs->file->bs->exact_filename[0] 288 && s->test_file->bs->exact_filename[0]) 289 { 290 int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 291 "blkverify:%s:%s", 292 bs->file->bs->exact_filename, 293 s->test_file->bs->exact_filename); 294 if (ret >= sizeof(bs->exact_filename)) { 295 /* An overflow makes the filename unusable, so do not report any */ 296 bs->exact_filename[0] = 0; 297 } 298 } 299 } 300 301 static char *blkverify_dirname(BlockDriverState *bs, Error **errp) 302 { 303 /* In general, there are two BDSs with different dirnames below this one; 304 * so there is no unique dirname we could return (unless both are equal by 305 * chance). Therefore, to be consistent, just always return NULL. */ 306 error_setg(errp, "Cannot generate a base directory for blkverify nodes"); 307 return NULL; 308 } 309 310 static BlockDriver bdrv_blkverify = { 311 .format_name = "blkverify", 312 .protocol_name = "blkverify", 313 .instance_size = sizeof(BDRVBlkverifyState), 314 315 .bdrv_parse_filename = blkverify_parse_filename, 316 .bdrv_file_open = blkverify_open, 317 .bdrv_close = blkverify_close, 318 .bdrv_child_perm = bdrv_default_perms, 319 .bdrv_getlength = blkverify_getlength, 320 .bdrv_refresh_filename = blkverify_refresh_filename, 321 .bdrv_dirname = blkverify_dirname, 322 323 .bdrv_co_preadv = blkverify_co_preadv, 324 .bdrv_co_pwritev = blkverify_co_pwritev, 325 .bdrv_co_flush = blkverify_co_flush, 326 327 .is_filter = true, 328 .bdrv_recurse_can_replace = blkverify_recurse_can_replace, 329 }; 330 331 static void bdrv_blkverify_init(void) 332 { 333 bdrv_register(&bdrv_blkverify); 334 } 335 336 block_init(bdrv_blkverify_init); 337