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