| 1 | /* |
| 2 | * QEMU Enhanced Disk Format |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2010 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> |
| 8 | * Anthony Liguori <aliguori@us.ibm.com> |
| 9 | * |
| 10 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. |
| 11 | * See the COPYING.LIB file in the top-level directory. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include "qemu/osdep.h" |
| 16 | #include "block/qdict.h" |
| 17 | #include "qapi/error.h" |
| 18 | #include "qemu/timer.h" |
| 19 | #include "qemu/bswap.h" |
| 20 | #include "qemu/main-loop.h" |
| 21 | #include "qemu/module.h" |
| 22 | #include "qemu/option.h" |
| 23 | #include "qemu/memalign.h" |
| 24 | #include "trace.h" |
| 25 | #include "qed.h" |
| 26 | #include "system/block-backend.h" |
| 27 | #include "qobject/qdict.h" |
| 28 | #include "qapi/qobject-input-visitor.h" |
| 29 | #include "qapi/qapi-visit-block-core.h" |
| 30 | |
| 31 | static QemuOptsList qed_create_opts; |
| 32 | |
| 33 | static int bdrv_qed_probe(const uint8_t *buf, int buf_size, |
| 34 | const char *filename) |
| 35 | { |
| 36 | const QEDHeader *header = (const QEDHeader *)buf; |
| 37 | |
| 38 | if (buf_size < sizeof(*header)) { |
| 39 | return 0; |
| 40 | } |
| 41 | if (le32_to_cpu(header->magic) != QED_MAGIC) { |
| 42 | return 0; |
| 43 | } |
| 44 | return 100; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Check whether an image format is raw |
| 49 | * |
| 50 | * @fmt: Backing file format, may be NULL |
| 51 | */ |
| 52 | static bool qed_fmt_is_raw(const char *fmt) |
| 53 | { |
| 54 | return fmt && strcmp(fmt, "raw") == 0; |
| 55 | } |
| 56 | |
| 57 | static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu) |
| 58 | { |
| 59 | cpu->magic = le32_to_cpu(le->magic); |
| 60 | cpu->cluster_size = le32_to_cpu(le->cluster_size); |
| 61 | cpu->table_size = le32_to_cpu(le->table_size); |
| 62 | cpu->header_size = le32_to_cpu(le->header_size); |
| 63 | cpu->features = le64_to_cpu(le->features); |
| 64 | cpu->compat_features = le64_to_cpu(le->compat_features); |
| 65 | cpu->autoclear_features = le64_to_cpu(le->autoclear_features); |
| 66 | cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset); |
| 67 | cpu->image_size = le64_to_cpu(le->image_size); |
| 68 | cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset); |
| 69 | cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size); |
| 70 | } |
| 71 | |
| 72 | static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le) |
| 73 | { |
| 74 | le->magic = cpu_to_le32(cpu->magic); |
| 75 | le->cluster_size = cpu_to_le32(cpu->cluster_size); |
| 76 | le->table_size = cpu_to_le32(cpu->table_size); |
| 77 | le->header_size = cpu_to_le32(cpu->header_size); |
| 78 | le->features = cpu_to_le64(cpu->features); |
| 79 | le->compat_features = cpu_to_le64(cpu->compat_features); |
| 80 | le->autoclear_features = cpu_to_le64(cpu->autoclear_features); |
| 81 | le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset); |
| 82 | le->image_size = cpu_to_le64(cpu->image_size); |
| 83 | le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset); |
| 84 | le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size); |
| 85 | } |
| 86 | |
| 87 | int qed_write_header_sync(BDRVQEDState *s) |
| 88 | { |
| 89 | QEDHeader le; |
| 90 | |
| 91 | qed_header_cpu_to_le(&s->header, &le); |
| 92 | return bdrv_pwrite(s->bs->file, 0, sizeof(le), &le, 0); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Update header in-place (does not rewrite backing filename or other strings) |
| 97 | * |
| 98 | * This function only updates known header fields in-place and does not affect |
| 99 | * extra data after the QED header. |
| 100 | * |
| 101 | * No new allocating reqs can start while this function runs. |
| 102 | */ |
| 103 | static int coroutine_fn GRAPH_RDLOCK qed_write_header(BDRVQEDState *s) |
| 104 | { |
| 105 | /* We must write full sectors for O_DIRECT but cannot necessarily generate |
| 106 | * the data following the header if an unrecognized compat feature is |
| 107 | * active. Therefore, first read the sectors containing the header, update |
| 108 | * them, and write back. |
| 109 | */ |
| 110 | |
| 111 | int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE); |
| 112 | size_t len = nsectors * BDRV_SECTOR_SIZE; |
| 113 | uint8_t *buf; |
| 114 | int ret; |
| 115 | |
| 116 | assert(s->allocating_acb || s->allocating_write_reqs_plugged); |
| 117 | |
| 118 | buf = qemu_blockalign(s->bs, len); |
| 119 | |
| 120 | ret = bdrv_co_pread(s->bs->file, 0, len, buf, 0); |
| 121 | if (ret < 0) { |
| 122 | goto out; |
| 123 | } |
| 124 | |
| 125 | /* Update header */ |
| 126 | qed_header_cpu_to_le(&s->header, (QEDHeader *) buf); |
| 127 | |
| 128 | ret = bdrv_co_pwrite(s->bs->file, 0, len, buf, 0); |
| 129 | if (ret < 0) { |
| 130 | goto out; |
| 131 | } |
| 132 | |
| 133 | ret = 0; |
| 134 | out: |
| 135 | qemu_vfree(buf); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) |
| 140 | { |
| 141 | uint64_t table_entries; |
| 142 | uint64_t l2_size; |
| 143 | |
| 144 | table_entries = (table_size * cluster_size) / sizeof(uint64_t); |
| 145 | l2_size = table_entries * cluster_size; |
| 146 | |
| 147 | return l2_size * table_entries; |
| 148 | } |
| 149 | |
| 150 | static bool qed_is_cluster_size_valid(uint32_t cluster_size) |
| 151 | { |
| 152 | if (cluster_size < QED_MIN_CLUSTER_SIZE || |
| 153 | cluster_size > QED_MAX_CLUSTER_SIZE) { |
| 154 | return false; |
| 155 | } |
| 156 | if (cluster_size & (cluster_size - 1)) { |
| 157 | return false; /* not power of 2 */ |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | static bool qed_is_table_size_valid(uint32_t table_size) |
| 163 | { |
| 164 | if (table_size < QED_MIN_TABLE_SIZE || |
| 165 | table_size > QED_MAX_TABLE_SIZE) { |
| 166 | return false; |
| 167 | } |
| 168 | if (table_size & (table_size - 1)) { |
| 169 | return false; /* not power of 2 */ |
| 170 | } |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, |
| 175 | uint32_t table_size) |
| 176 | { |
| 177 | if (image_size % BDRV_SECTOR_SIZE != 0) { |
| 178 | return false; /* not multiple of sector size */ |
| 179 | } |
| 180 | if (image_size > qed_max_image_size(cluster_size, table_size)) { |
| 181 | return false; /* image is too large */ |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Read a string of known length from the image file |
| 188 | * |
| 189 | * @file: Image file |
| 190 | * @offset: File offset to start of string, in bytes |
| 191 | * @n: String length in bytes |
| 192 | * @buf: Destination buffer |
| 193 | * @buflen: Destination buffer length in bytes |
| 194 | * @ret: 0 on success, -errno on failure |
| 195 | * |
| 196 | * The string is NUL-terminated. |
| 197 | */ |
| 198 | static int coroutine_fn GRAPH_RDLOCK |
| 199 | qed_read_string(BdrvChild *file, uint64_t offset, |
| 200 | size_t n, char *buf, size_t buflen) |
| 201 | { |
| 202 | int ret; |
| 203 | if (n >= buflen) { |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | ret = bdrv_co_pread(file, offset, n, buf, 0); |
| 207 | if (ret < 0) { |
| 208 | return ret; |
| 209 | } |
| 210 | buf[n] = '\0'; |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Allocate new clusters |
| 216 | * |
| 217 | * @s: QED state |
| 218 | * @n: Number of contiguous clusters to allocate |
| 219 | * @ret: Offset of first allocated cluster |
| 220 | * |
| 221 | * This function only produces the offset where the new clusters should be |
| 222 | * written. It updates BDRVQEDState but does not make any changes to the image |
| 223 | * file. |
| 224 | * |
| 225 | * Called with table_lock held. |
| 226 | */ |
| 227 | static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) |
| 228 | { |
| 229 | uint64_t offset = s->file_size; |
| 230 | s->file_size += n * s->header.cluster_size; |
| 231 | return offset; |
| 232 | } |
| 233 | |
| 234 | QEDTable *qed_alloc_table(BDRVQEDState *s) |
| 235 | { |
| 236 | /* Honor O_DIRECT memory alignment requirements */ |
| 237 | return qemu_blockalign(s->bs, |
| 238 | s->header.cluster_size * s->header.table_size); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Allocate a new zeroed L2 table |
| 243 | * |
| 244 | * Called with table_lock held. |
| 245 | */ |
| 246 | static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) |
| 247 | { |
| 248 | CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); |
| 249 | |
| 250 | l2_table->table = qed_alloc_table(s); |
| 251 | l2_table->offset = qed_alloc_clusters(s, s->header.table_size); |
| 252 | |
| 253 | memset(l2_table->table->offsets, 0, |
| 254 | s->header.cluster_size * s->header.table_size); |
| 255 | return l2_table; |
| 256 | } |
| 257 | |
| 258 | static bool coroutine_fn qed_plug_allocating_write_reqs(BDRVQEDState *s) |
| 259 | { |
| 260 | qemu_co_mutex_lock(&s->table_lock); |
| 261 | |
| 262 | /* No reentrancy is allowed. */ |
| 263 | assert(!s->allocating_write_reqs_plugged); |
| 264 | if (s->allocating_acb != NULL) { |
| 265 | /* Another allocating write came concurrently. This cannot happen |
| 266 | * from bdrv_qed_drain_begin, but it can happen when the timer runs. |
| 267 | */ |
| 268 | qemu_co_mutex_unlock(&s->table_lock); |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | s->allocating_write_reqs_plugged = true; |
| 273 | qemu_co_mutex_unlock(&s->table_lock); |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | static void coroutine_fn qed_unplug_allocating_write_reqs(BDRVQEDState *s) |
| 278 | { |
| 279 | qemu_co_mutex_lock(&s->table_lock); |
| 280 | assert(s->allocating_write_reqs_plugged); |
| 281 | s->allocating_write_reqs_plugged = false; |
| 282 | qemu_co_queue_next(&s->allocating_write_reqs); |
| 283 | qemu_co_mutex_unlock(&s->table_lock); |
| 284 | } |
| 285 | |
| 286 | static void coroutine_fn GRAPH_RDLOCK qed_need_check_timer(BDRVQEDState *s) |
| 287 | { |
| 288 | int ret; |
| 289 | |
| 290 | trace_qed_need_check_timer_cb(s); |
| 291 | assert_bdrv_graph_readable(); |
| 292 | |
| 293 | if (!qed_plug_allocating_write_reqs(s)) { |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | /* Ensure writes are on disk before clearing flag */ |
| 298 | ret = bdrv_co_flush(s->bs->file->bs); |
| 299 | if (ret < 0) { |
| 300 | qed_unplug_allocating_write_reqs(s); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | s->header.features &= ~QED_F_NEED_CHECK; |
| 305 | ret = qed_write_header(s); |
| 306 | (void) ret; |
| 307 | |
| 308 | qed_unplug_allocating_write_reqs(s); |
| 309 | |
| 310 | ret = bdrv_co_flush(s->bs); |
| 311 | (void) ret; |
| 312 | } |
| 313 | |
| 314 | static void coroutine_fn qed_need_check_timer_entry(void *opaque) |
| 315 | { |
| 316 | BDRVQEDState *s = opaque; |
| 317 | GRAPH_RDLOCK_GUARD(); |
| 318 | |
| 319 | qed_need_check_timer(opaque); |
| 320 | bdrv_dec_in_flight(s->bs); |
| 321 | } |
| 322 | |
| 323 | static void qed_need_check_timer_cb(void *opaque) |
| 324 | { |
| 325 | BDRVQEDState *s = opaque; |
| 326 | Coroutine *co = qemu_coroutine_create(qed_need_check_timer_entry, opaque); |
| 327 | |
| 328 | bdrv_inc_in_flight(s->bs); |
| 329 | qemu_coroutine_enter(co); |
| 330 | } |
| 331 | |
| 332 | static void qed_start_need_check_timer(BDRVQEDState *s) |
| 333 | { |
| 334 | trace_qed_start_need_check_timer(s); |
| 335 | |
| 336 | /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for |
| 337 | * migration. |
| 338 | */ |
| 339 | timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
| 340 | NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT); |
| 341 | } |
| 342 | |
| 343 | /* It's okay to call this multiple times or when no timer is started */ |
| 344 | static void qed_cancel_need_check_timer(BDRVQEDState *s) |
| 345 | { |
| 346 | trace_qed_cancel_need_check_timer(s); |
| 347 | timer_del(s->need_check_timer); |
| 348 | } |
| 349 | |
| 350 | static void bdrv_qed_detach_aio_context(BlockDriverState *bs) |
| 351 | { |
| 352 | BDRVQEDState *s = bs->opaque; |
| 353 | |
| 354 | if (s->need_check_timer) { |
| 355 | qed_cancel_need_check_timer(s); |
| 356 | timer_free(s->need_check_timer); |
| 357 | s->need_check_timer = NULL; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static void GRAPH_RDLOCK bdrv_qed_attach_aio_context(BlockDriverState *bs, |
| 362 | AioContext *new_context) |
| 363 | { |
| 364 | BDRVQEDState *s = bs->opaque; |
| 365 | |
| 366 | if (bdrv_is_inactive(bs)) { |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | s->need_check_timer = aio_timer_new(new_context, |
| 371 | QEMU_CLOCK_VIRTUAL, SCALE_NS, |
| 372 | qed_need_check_timer_cb, s); |
| 373 | if (s->header.features & QED_F_NEED_CHECK) { |
| 374 | qed_start_need_check_timer(s); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | static void bdrv_qed_drain_begin(BlockDriverState *bs) |
| 379 | { |
| 380 | BDRVQEDState *s = bs->opaque; |
| 381 | |
| 382 | /* Fire the timer immediately in order to start doing I/O as soon as the |
| 383 | * header is flushed. |
| 384 | */ |
| 385 | if (s->need_check_timer && timer_pending(s->need_check_timer)) { |
| 386 | Coroutine *co; |
| 387 | |
| 388 | qed_cancel_need_check_timer(s); |
| 389 | co = qemu_coroutine_create(qed_need_check_timer_entry, s); |
| 390 | bdrv_inc_in_flight(bs); |
| 391 | aio_co_enter(bdrv_get_aio_context(bs), co); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | static void bdrv_qed_init_state(BlockDriverState *bs) |
| 396 | { |
| 397 | BDRVQEDState *s = bs->opaque; |
| 398 | |
| 399 | memset(s, 0, sizeof(BDRVQEDState)); |
| 400 | s->bs = bs; |
| 401 | qemu_co_mutex_init(&s->table_lock); |
| 402 | qemu_co_queue_init(&s->allocating_write_reqs); |
| 403 | } |
| 404 | |
| 405 | /* Called with table_lock held. */ |
| 406 | static int coroutine_fn GRAPH_RDLOCK |
| 407 | bdrv_qed_do_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) |
| 408 | { |
| 409 | BDRVQEDState *s = bs->opaque; |
| 410 | QEDHeader le_header; |
| 411 | int64_t file_size; |
| 412 | int ret; |
| 413 | |
| 414 | ret = bdrv_co_pread(bs->file, 0, sizeof(le_header), &le_header, 0); |
| 415 | if (ret < 0) { |
| 416 | error_setg(errp, "Failed to read QED header"); |
| 417 | return ret; |
| 418 | } |
| 419 | qed_header_le_to_cpu(&le_header, &s->header); |
| 420 | |
| 421 | if (s->header.magic != QED_MAGIC) { |
| 422 | error_setg(errp, "Image not in QED format"); |
| 423 | return -EINVAL; |
| 424 | } |
| 425 | if (s->header.features & ~QED_FEATURE_MASK) { |
| 426 | /* image uses unsupported feature bits */ |
| 427 | error_setg(errp, "Unsupported QED features: %" PRIx64, |
| 428 | s->header.features & ~QED_FEATURE_MASK); |
| 429 | return -ENOTSUP; |
| 430 | } |
| 431 | if (!qed_is_cluster_size_valid(s->header.cluster_size)) { |
| 432 | error_setg(errp, "QED cluster size is invalid"); |
| 433 | return -EINVAL; |
| 434 | } |
| 435 | |
| 436 | /* Round down file size to the last cluster */ |
| 437 | file_size = bdrv_co_getlength(bs->file->bs); |
| 438 | if (file_size < 0) { |
| 439 | error_setg(errp, "Failed to get file length"); |
| 440 | return file_size; |
| 441 | } |
| 442 | s->file_size = qed_start_of_cluster(s, file_size); |
| 443 | |
| 444 | if (!qed_is_table_size_valid(s->header.table_size)) { |
| 445 | error_setg(errp, "QED table size is invalid"); |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | if (!qed_is_image_size_valid(s->header.image_size, |
| 449 | s->header.cluster_size, |
| 450 | s->header.table_size)) { |
| 451 | error_setg(errp, "QED image size is invalid"); |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | if (!qed_check_table_offset(s, s->header.l1_table_offset)) { |
| 455 | error_setg(errp, "QED table offset is invalid"); |
| 456 | return -EINVAL; |
| 457 | } |
| 458 | |
| 459 | s->table_nelems = (s->header.cluster_size * s->header.table_size) / |
| 460 | sizeof(uint64_t); |
| 461 | s->l2_shift = ctz32(s->header.cluster_size); |
| 462 | s->l2_mask = s->table_nelems - 1; |
| 463 | s->l1_shift = s->l2_shift + ctz32(s->table_nelems); |
| 464 | |
| 465 | /* Header size calculation must not overflow uint32_t */ |
| 466 | if (s->header.header_size > UINT32_MAX / s->header.cluster_size) { |
| 467 | error_setg(errp, "QED header size is too large"); |
| 468 | return -EINVAL; |
| 469 | } |
| 470 | |
| 471 | if ((s->header.features & QED_F_BACKING_FILE)) { |
| 472 | g_autofree char *backing_file_str = NULL; |
| 473 | |
| 474 | if ((uint64_t)s->header.backing_filename_offset + |
| 475 | s->header.backing_filename_size > |
| 476 | s->header.cluster_size * s->header.header_size) { |
| 477 | error_setg(errp, "QED backing filename offset is invalid"); |
| 478 | return -EINVAL; |
| 479 | } |
| 480 | |
| 481 | backing_file_str = g_malloc(sizeof(bs->backing_file)); |
| 482 | ret = qed_read_string(bs->file, s->header.backing_filename_offset, |
| 483 | s->header.backing_filename_size, |
| 484 | backing_file_str, sizeof(bs->backing_file)); |
| 485 | if (ret < 0) { |
| 486 | error_setg(errp, "Failed to read backing filename"); |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | if (!g_str_equal(backing_file_str, bs->backing_file)) { |
| 491 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), |
| 492 | backing_file_str); |
| 493 | pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file), |
| 494 | backing_file_str); |
| 495 | } |
| 496 | |
| 497 | if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { |
| 498 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /* Reset unknown autoclear feature bits. This is a backwards |
| 503 | * compatibility mechanism that allows images to be opened by older |
| 504 | * programs, which "knock out" unknown feature bits. When an image is |
| 505 | * opened by a newer program again it can detect that the autoclear |
| 506 | * feature is no longer valid. |
| 507 | */ |
| 508 | if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && |
| 509 | !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) { |
| 510 | s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; |
| 511 | |
| 512 | ret = qed_write_header_sync(s); |
| 513 | if (ret) { |
| 514 | error_setg(errp, "Failed to update header"); |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | /* From here on only known autoclear feature bits are valid */ |
| 519 | bdrv_co_flush(bs->file->bs); |
| 520 | } |
| 521 | |
| 522 | s->l1_table = qed_alloc_table(s); |
| 523 | qed_init_l2_cache(&s->l2_cache); |
| 524 | |
| 525 | ret = qed_read_l1_table_sync(s); |
| 526 | if (ret) { |
| 527 | error_setg(errp, "Failed to read L1 table"); |
| 528 | goto out; |
| 529 | } |
| 530 | |
| 531 | /* If image was not closed cleanly, check consistency */ |
| 532 | if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) { |
| 533 | /* Read-only images cannot be fixed. There is no risk of corruption |
| 534 | * since write operations are not possible. Therefore, allow |
| 535 | * potentially inconsistent images to be opened read-only. This can |
| 536 | * aid data recovery from an otherwise inconsistent image. |
| 537 | */ |
| 538 | if (!bdrv_is_read_only(bs->file->bs) && |
| 539 | !(flags & BDRV_O_INACTIVE)) { |
| 540 | BdrvCheckResult result = {0}; |
| 541 | |
| 542 | ret = qed_check(s, &result, true); |
| 543 | if (ret) { |
| 544 | error_setg(errp, "Image corrupted"); |
| 545 | goto out; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs)); |
| 551 | |
| 552 | out: |
| 553 | if (ret) { |
| 554 | qed_free_l2_cache(&s->l2_cache); |
| 555 | qemu_vfree(s->l1_table); |
| 556 | } |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | typedef struct QEDOpenCo { |
| 561 | BlockDriverState *bs; |
| 562 | QDict *options; |
| 563 | int flags; |
| 564 | Error **errp; |
| 565 | int ret; |
| 566 | } QEDOpenCo; |
| 567 | |
| 568 | static void coroutine_fn bdrv_qed_open_entry(void *opaque) |
| 569 | { |
| 570 | QEDOpenCo *qoc = opaque; |
| 571 | BDRVQEDState *s = qoc->bs->opaque; |
| 572 | |
| 573 | GRAPH_RDLOCK_GUARD(); |
| 574 | |
| 575 | qemu_co_mutex_lock(&s->table_lock); |
| 576 | qoc->ret = bdrv_qed_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp); |
| 577 | qemu_co_mutex_unlock(&s->table_lock); |
| 578 | } |
| 579 | |
| 580 | static int coroutine_mixed_fn bdrv_qed_open(BlockDriverState *bs, QDict *options, |
| 581 | int flags, Error **errp) |
| 582 | { |
| 583 | QEDOpenCo qoc = { |
| 584 | .bs = bs, |
| 585 | .options = options, |
| 586 | .flags = flags, |
| 587 | .errp = errp, |
| 588 | .ret = -EINPROGRESS |
| 589 | }; |
| 590 | int ret; |
| 591 | |
| 592 | ret = bdrv_open_file_child(NULL, options, "file", bs, errp); |
| 593 | if (ret < 0) { |
| 594 | return ret; |
| 595 | } |
| 596 | |
| 597 | bdrv_qed_init_state(bs); |
| 598 | assert(!qemu_in_coroutine()); |
| 599 | assert(qemu_get_current_aio_context() == qemu_get_aio_context()); |
| 600 | qemu_coroutine_enter(qemu_coroutine_create(bdrv_qed_open_entry, &qoc)); |
| 601 | BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS); |
| 602 | |
| 603 | return qoc.ret; |
| 604 | } |
| 605 | |
| 606 | static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp) |
| 607 | { |
| 608 | BDRVQEDState *s = bs->opaque; |
| 609 | |
| 610 | bs->bl.pwrite_zeroes_alignment = s->header.cluster_size; |
| 611 | bs->bl.max_pwrite_zeroes = QEMU_ALIGN_DOWN(INT_MAX, s->header.cluster_size); |
| 612 | } |
| 613 | |
| 614 | /* We have nothing to do for QED reopen, stubs just return |
| 615 | * success */ |
| 616 | static int bdrv_qed_reopen_prepare(BDRVReopenState *state, |
| 617 | BlockReopenQueue *queue, Error **errp) |
| 618 | { |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static void GRAPH_RDLOCK bdrv_qed_do_close(BlockDriverState *bs) |
| 623 | { |
| 624 | BDRVQEDState *s = bs->opaque; |
| 625 | |
| 626 | bdrv_qed_detach_aio_context(bs); |
| 627 | |
| 628 | /* Ensure writes reach stable storage */ |
| 629 | bdrv_flush(bs->file->bs); |
| 630 | |
| 631 | /* Clean shutdown, no check required on next open */ |
| 632 | if (s->header.features & QED_F_NEED_CHECK) { |
| 633 | s->header.features &= ~QED_F_NEED_CHECK; |
| 634 | qed_write_header_sync(s); |
| 635 | } |
| 636 | |
| 637 | qed_free_l2_cache(&s->l2_cache); |
| 638 | qemu_vfree(s->l1_table); |
| 639 | } |
| 640 | |
| 641 | static void GRAPH_UNLOCKED bdrv_qed_close(BlockDriverState *bs) |
| 642 | { |
| 643 | GLOBAL_STATE_CODE(); |
| 644 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 645 | |
| 646 | bdrv_qed_do_close(bs); |
| 647 | } |
| 648 | |
| 649 | static int coroutine_fn GRAPH_UNLOCKED |
| 650 | bdrv_qed_co_create(BlockdevCreateOptions *opts, Error **errp) |
| 651 | { |
| 652 | BlockdevCreateOptionsQed *qed_opts; |
| 653 | BlockBackend *blk = NULL; |
| 654 | BlockDriverState *bs = NULL; |
| 655 | |
| 656 | QEDHeader header; |
| 657 | QEDHeader le_header; |
| 658 | uint8_t *l1_table = NULL; |
| 659 | size_t l1_size; |
| 660 | int ret = 0; |
| 661 | |
| 662 | assert(opts->driver == BLOCKDEV_DRIVER_QED); |
| 663 | qed_opts = &opts->u.qed; |
| 664 | |
| 665 | /* Validate options and set default values */ |
| 666 | if (!qed_opts->has_cluster_size) { |
| 667 | qed_opts->cluster_size = QED_DEFAULT_CLUSTER_SIZE; |
| 668 | } |
| 669 | if (!qed_opts->has_table_size) { |
| 670 | qed_opts->table_size = QED_DEFAULT_TABLE_SIZE; |
| 671 | } |
| 672 | |
| 673 | if (!qed_is_cluster_size_valid(qed_opts->cluster_size)) { |
| 674 | error_setg(errp, "QED cluster size must be within range [%u, %u] " |
| 675 | "and power of 2", |
| 676 | QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); |
| 677 | return -EINVAL; |
| 678 | } |
| 679 | if (!qed_is_table_size_valid(qed_opts->table_size)) { |
| 680 | error_setg(errp, "QED table size must be within range [%u, %u] " |
| 681 | "and power of 2", |
| 682 | QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); |
| 683 | return -EINVAL; |
| 684 | } |
| 685 | if (!qed_is_image_size_valid(qed_opts->size, qed_opts->cluster_size, |
| 686 | qed_opts->table_size)) |
| 687 | { |
| 688 | error_setg(errp, "QED image size must be a non-zero multiple of " |
| 689 | "cluster size and less than %" PRIu64 " bytes", |
| 690 | qed_max_image_size(qed_opts->cluster_size, |
| 691 | qed_opts->table_size)); |
| 692 | return -EINVAL; |
| 693 | } |
| 694 | |
| 695 | /* Create BlockBackend to write to the image */ |
| 696 | bs = bdrv_co_open_blockdev_ref(qed_opts->file, errp); |
| 697 | if (bs == NULL) { |
| 698 | return -EIO; |
| 699 | } |
| 700 | |
| 701 | blk = blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL, |
| 702 | errp); |
| 703 | if (!blk) { |
| 704 | ret = -EPERM; |
| 705 | goto out; |
| 706 | } |
| 707 | blk_set_allow_write_beyond_eof(blk, true); |
| 708 | |
| 709 | /* Prepare image format */ |
| 710 | header = (QEDHeader) { |
| 711 | .magic = QED_MAGIC, |
| 712 | .cluster_size = qed_opts->cluster_size, |
| 713 | .table_size = qed_opts->table_size, |
| 714 | .header_size = 1, |
| 715 | .features = 0, |
| 716 | .compat_features = 0, |
| 717 | .l1_table_offset = qed_opts->cluster_size, |
| 718 | .image_size = qed_opts->size, |
| 719 | }; |
| 720 | |
| 721 | l1_size = header.cluster_size * header.table_size; |
| 722 | |
| 723 | /* |
| 724 | * The QED format associates file length with allocation status, |
| 725 | * so a new file (which is empty) must have a length of 0. |
| 726 | */ |
| 727 | ret = blk_co_truncate(blk, 0, true, PREALLOC_MODE_OFF, 0, errp); |
| 728 | if (ret < 0) { |
| 729 | goto out; |
| 730 | } |
| 731 | |
| 732 | if (qed_opts->backing_file) { |
| 733 | header.features |= QED_F_BACKING_FILE; |
| 734 | header.backing_filename_offset = sizeof(le_header); |
| 735 | header.backing_filename_size = strlen(qed_opts->backing_file); |
| 736 | |
| 737 | if (qed_opts->has_backing_fmt) { |
| 738 | const char *backing_fmt = BlockdevDriver_str(qed_opts->backing_fmt); |
| 739 | if (qed_fmt_is_raw(backing_fmt)) { |
| 740 | header.features |= QED_F_BACKING_FORMAT_NO_PROBE; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | qed_header_cpu_to_le(&header, &le_header); |
| 746 | ret = blk_co_pwrite(blk, 0, sizeof(le_header), &le_header, 0); |
| 747 | if (ret < 0) { |
| 748 | goto out; |
| 749 | } |
| 750 | ret = blk_co_pwrite(blk, sizeof(le_header), header.backing_filename_size, |
| 751 | qed_opts->backing_file, 0); |
| 752 | if (ret < 0) { |
| 753 | goto out; |
| 754 | } |
| 755 | |
| 756 | l1_table = g_malloc0(l1_size); |
| 757 | ret = blk_co_pwrite(blk, header.l1_table_offset, l1_size, l1_table, 0); |
| 758 | if (ret < 0) { |
| 759 | goto out; |
| 760 | } |
| 761 | |
| 762 | ret = 0; /* success */ |
| 763 | out: |
| 764 | g_free(l1_table); |
| 765 | blk_co_unref(blk); |
| 766 | bdrv_co_unref(bs); |
| 767 | return ret; |
| 768 | } |
| 769 | |
| 770 | static int coroutine_fn GRAPH_UNLOCKED |
| 771 | bdrv_qed_co_create_opts(BlockDriver *drv, const char *filename, |
| 772 | QemuOpts *opts, Error **errp) |
| 773 | { |
| 774 | BlockdevCreateOptions *create_options = NULL; |
| 775 | QDict *qdict; |
| 776 | Visitor *v; |
| 777 | BlockDriverState *bs = NULL; |
| 778 | int ret; |
| 779 | |
| 780 | static const QDictRenames opt_renames[] = { |
| 781 | { BLOCK_OPT_BACKING_FILE, "backing-file" }, |
| 782 | { BLOCK_OPT_BACKING_FMT, "backing-fmt" }, |
| 783 | { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" }, |
| 784 | { BLOCK_OPT_TABLE_SIZE, "table-size" }, |
| 785 | { NULL, NULL }, |
| 786 | }; |
| 787 | |
| 788 | /* Parse options and convert legacy syntax */ |
| 789 | qdict = qemu_opts_to_qdict_filtered(opts, NULL, &qed_create_opts, true); |
| 790 | |
| 791 | if (!qdict_rename_keys(qdict, opt_renames, errp)) { |
| 792 | ret = -EINVAL; |
| 793 | goto fail; |
| 794 | } |
| 795 | |
| 796 | /* Create and open the file (protocol layer) */ |
| 797 | ret = bdrv_co_create_file(filename, opts, true, errp); |
| 798 | if (ret < 0) { |
| 799 | goto fail; |
| 800 | } |
| 801 | |
| 802 | bs = bdrv_co_open(filename, NULL, NULL, |
| 803 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); |
| 804 | if (bs == NULL) { |
| 805 | ret = -EIO; |
| 806 | goto fail; |
| 807 | } |
| 808 | |
| 809 | /* Now get the QAPI type BlockdevCreateOptions */ |
| 810 | qdict_put_str(qdict, "driver", "qed"); |
| 811 | qdict_put_str(qdict, "file", bs->node_name); |
| 812 | |
| 813 | v = qobject_input_visitor_new_flat_confused(qdict, errp); |
| 814 | if (!v) { |
| 815 | ret = -EINVAL; |
| 816 | goto fail; |
| 817 | } |
| 818 | |
| 819 | visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp); |
| 820 | visit_free(v); |
| 821 | if (!create_options) { |
| 822 | ret = -EINVAL; |
| 823 | goto fail; |
| 824 | } |
| 825 | |
| 826 | /* Silently round up size */ |
| 827 | assert(create_options->driver == BLOCKDEV_DRIVER_QED); |
| 828 | create_options->u.qed.size = |
| 829 | ROUND_UP(create_options->u.qed.size, BDRV_SECTOR_SIZE); |
| 830 | |
| 831 | /* Create the qed image (format layer) */ |
| 832 | ret = bdrv_qed_co_create(create_options, errp); |
| 833 | |
| 834 | fail: |
| 835 | qobject_unref(qdict); |
| 836 | bdrv_co_unref(bs); |
| 837 | qapi_free_BlockdevCreateOptions(create_options); |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | static int coroutine_fn GRAPH_RDLOCK |
| 842 | bdrv_qed_co_block_status(BlockDriverState *bs, unsigned int mode, |
| 843 | int64_t pos, int64_t bytes, int64_t *pnum, |
| 844 | int64_t *map, BlockDriverState **file) |
| 845 | { |
| 846 | BDRVQEDState *s = bs->opaque; |
| 847 | size_t len = MIN(bytes, SIZE_MAX); |
| 848 | int status; |
| 849 | QEDRequest request = { .l2_table = NULL }; |
| 850 | uint64_t offset; |
| 851 | int ret; |
| 852 | |
| 853 | qemu_co_mutex_lock(&s->table_lock); |
| 854 | ret = qed_find_cluster(s, &request, pos, &len, &offset); |
| 855 | |
| 856 | *pnum = len; |
| 857 | switch (ret) { |
| 858 | case QED_CLUSTER_FOUND: |
| 859 | *map = offset | qed_offset_into_cluster(s, pos); |
| 860 | status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; |
| 861 | *file = bs->file->bs; |
| 862 | break; |
| 863 | case QED_CLUSTER_ZERO: |
| 864 | status = BDRV_BLOCK_ZERO; |
| 865 | break; |
| 866 | case QED_CLUSTER_L2: |
| 867 | case QED_CLUSTER_L1: |
| 868 | status = 0; |
| 869 | break; |
| 870 | default: |
| 871 | assert(ret < 0); |
| 872 | status = ret; |
| 873 | break; |
| 874 | } |
| 875 | |
| 876 | qed_unref_l2_cache_entry(request.l2_table); |
| 877 | qemu_co_mutex_unlock(&s->table_lock); |
| 878 | |
| 879 | return status; |
| 880 | } |
| 881 | |
| 882 | static BDRVQEDState *acb_to_s(QEDAIOCB *acb) |
| 883 | { |
| 884 | return acb->bs->opaque; |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * Read from the backing file or zero-fill if no backing file |
| 889 | * |
| 890 | * @s: QED state |
| 891 | * @pos: Byte position in device |
| 892 | * @qiov: Destination I/O vector |
| 893 | * |
| 894 | * This function reads qiov->size bytes starting at pos from the backing file. |
| 895 | * If there is no backing file then zeroes are read. |
| 896 | */ |
| 897 | static int coroutine_fn GRAPH_RDLOCK |
| 898 | qed_read_backing_file(BDRVQEDState *s, uint64_t pos, QEMUIOVector *qiov) |
| 899 | { |
| 900 | if (s->bs->backing) { |
| 901 | BLKDBG_CO_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO); |
| 902 | return bdrv_co_preadv(s->bs->backing, pos, qiov->size, qiov, 0); |
| 903 | } |
| 904 | qemu_iovec_memset(qiov, 0, 0, qiov->size); |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * Copy data from backing file into the image |
| 910 | * |
| 911 | * @s: QED state |
| 912 | * @pos: Byte position in device |
| 913 | * @len: Number of bytes |
| 914 | * @offset: Byte offset in image file |
| 915 | */ |
| 916 | static int coroutine_fn GRAPH_RDLOCK |
| 917 | qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, uint64_t len, |
| 918 | uint64_t offset) |
| 919 | { |
| 920 | QEMUIOVector qiov; |
| 921 | int ret; |
| 922 | |
| 923 | /* Skip copy entirely if there is no work to do */ |
| 924 | if (len == 0) { |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | qemu_iovec_init_buf(&qiov, qemu_blockalign(s->bs, len), len); |
| 929 | |
| 930 | ret = qed_read_backing_file(s, pos, &qiov); |
| 931 | |
| 932 | if (ret) { |
| 933 | goto out; |
| 934 | } |
| 935 | |
| 936 | BLKDBG_CO_EVENT(s->bs->file, BLKDBG_COW_WRITE); |
| 937 | ret = bdrv_co_pwritev(s->bs->file, offset, qiov.size, &qiov, 0); |
| 938 | if (ret < 0) { |
| 939 | goto out; |
| 940 | } |
| 941 | ret = 0; |
| 942 | out: |
| 943 | qemu_vfree(qemu_iovec_buf(&qiov)); |
| 944 | return ret; |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * Link one or more contiguous clusters into a table |
| 949 | * |
| 950 | * @s: QED state |
| 951 | * @table: L2 table |
| 952 | * @index: First cluster index |
| 953 | * @n: Number of contiguous clusters |
| 954 | * @cluster: First cluster offset |
| 955 | * |
| 956 | * The cluster offset may be an allocated byte offset in the image file, the |
| 957 | * zero cluster marker, or the unallocated cluster marker. |
| 958 | * |
| 959 | * Called with table_lock held. |
| 960 | */ |
| 961 | static void coroutine_fn qed_update_l2_table(BDRVQEDState *s, QEDTable *table, |
| 962 | int index, unsigned int n, |
| 963 | uint64_t cluster) |
| 964 | { |
| 965 | int i; |
| 966 | for (i = index; i < index + n; i++) { |
| 967 | table->offsets[i] = cluster; |
| 968 | if (!qed_offset_is_unalloc_cluster(cluster) && |
| 969 | !qed_offset_is_zero_cluster(cluster)) { |
| 970 | cluster += s->header.cluster_size; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | /* Called with table_lock held. */ |
| 976 | static void coroutine_fn qed_aio_complete(QEDAIOCB *acb) |
| 977 | { |
| 978 | BDRVQEDState *s = acb_to_s(acb); |
| 979 | |
| 980 | /* Free resources */ |
| 981 | qemu_iovec_destroy(&acb->cur_qiov); |
| 982 | qed_unref_l2_cache_entry(acb->request.l2_table); |
| 983 | |
| 984 | /* Free the buffer we may have allocated for zero writes */ |
| 985 | if (acb->flags & QED_AIOCB_ZERO) { |
| 986 | qemu_vfree(acb->qiov->iov[0].iov_base); |
| 987 | acb->qiov->iov[0].iov_base = NULL; |
| 988 | } |
| 989 | |
| 990 | /* Start next allocating write request waiting behind this one. Note that |
| 991 | * requests enqueue themselves when they first hit an unallocated cluster |
| 992 | * but they wait until the entire request is finished before waking up the |
| 993 | * next request in the queue. This ensures that we don't cycle through |
| 994 | * requests multiple times but rather finish one at a time completely. |
| 995 | */ |
| 996 | if (acb == s->allocating_acb) { |
| 997 | s->allocating_acb = NULL; |
| 998 | if (!qemu_co_queue_empty(&s->allocating_write_reqs)) { |
| 999 | qemu_co_queue_next(&s->allocating_write_reqs); |
| 1000 | } else if (s->header.features & QED_F_NEED_CHECK) { |
| 1001 | qed_start_need_check_timer(s); |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * Update L1 table with new L2 table offset and write it out |
| 1008 | * |
| 1009 | * Called with table_lock held. |
| 1010 | */ |
| 1011 | static int coroutine_fn GRAPH_RDLOCK qed_aio_write_l1_update(QEDAIOCB *acb) |
| 1012 | { |
| 1013 | BDRVQEDState *s = acb_to_s(acb); |
| 1014 | CachedL2Table *l2_table = acb->request.l2_table; |
| 1015 | uint64_t l2_offset = l2_table->offset; |
| 1016 | int index, ret; |
| 1017 | |
| 1018 | index = qed_l1_index(s, acb->cur_pos); |
| 1019 | s->l1_table->offsets[index] = l2_table->offset; |
| 1020 | |
| 1021 | ret = qed_write_l1_table(s, index, 1); |
| 1022 | |
| 1023 | /* Commit the current L2 table to the cache */ |
| 1024 | qed_commit_l2_cache_entry(&s->l2_cache, l2_table); |
| 1025 | |
| 1026 | /* This is guaranteed to succeed because we just committed the entry to the |
| 1027 | * cache. |
| 1028 | */ |
| 1029 | acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); |
| 1030 | assert(acb->request.l2_table != NULL); |
| 1031 | |
| 1032 | return ret; |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | /** |
| 1037 | * Update L2 table with new cluster offsets and write them out |
| 1038 | * |
| 1039 | * Called with table_lock held. |
| 1040 | */ |
| 1041 | static int coroutine_fn GRAPH_RDLOCK |
| 1042 | qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset) |
| 1043 | { |
| 1044 | BDRVQEDState *s = acb_to_s(acb); |
| 1045 | bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; |
| 1046 | int index, ret; |
| 1047 | |
| 1048 | if (need_alloc) { |
| 1049 | qed_unref_l2_cache_entry(acb->request.l2_table); |
| 1050 | acb->request.l2_table = qed_new_l2_table(s); |
| 1051 | } |
| 1052 | |
| 1053 | index = qed_l2_index(s, acb->cur_pos); |
| 1054 | qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, |
| 1055 | offset); |
| 1056 | |
| 1057 | if (need_alloc) { |
| 1058 | /* Write out the whole new L2 table */ |
| 1059 | ret = qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true); |
| 1060 | if (ret) { |
| 1061 | return ret; |
| 1062 | } |
| 1063 | return qed_aio_write_l1_update(acb); |
| 1064 | } else { |
| 1065 | /* Write out only the updated part of the L2 table */ |
| 1066 | ret = qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, |
| 1067 | false); |
| 1068 | if (ret) { |
| 1069 | return ret; |
| 1070 | } |
| 1071 | } |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | /** |
| 1076 | * Write data to the image file |
| 1077 | * |
| 1078 | * Called with table_lock *not* held. |
| 1079 | */ |
| 1080 | static int coroutine_fn GRAPH_RDLOCK qed_aio_write_main(QEDAIOCB *acb) |
| 1081 | { |
| 1082 | BDRVQEDState *s = acb_to_s(acb); |
| 1083 | uint64_t offset = acb->cur_cluster + |
| 1084 | qed_offset_into_cluster(s, acb->cur_pos); |
| 1085 | |
| 1086 | trace_qed_aio_write_main(s, acb, 0, offset, acb->cur_qiov.size); |
| 1087 | |
| 1088 | BLKDBG_CO_EVENT(s->bs->file, BLKDBG_WRITE_AIO); |
| 1089 | return bdrv_co_pwritev(s->bs->file, offset, acb->cur_qiov.size, |
| 1090 | &acb->cur_qiov, 0); |
| 1091 | } |
| 1092 | |
| 1093 | /** |
| 1094 | * Populate untouched regions of new data cluster |
| 1095 | * |
| 1096 | * Called with table_lock held. |
| 1097 | */ |
| 1098 | static int coroutine_fn GRAPH_RDLOCK qed_aio_write_cow(QEDAIOCB *acb) |
| 1099 | { |
| 1100 | BDRVQEDState *s = acb_to_s(acb); |
| 1101 | uint64_t start, len, offset; |
| 1102 | int ret; |
| 1103 | |
| 1104 | qemu_co_mutex_unlock(&s->table_lock); |
| 1105 | |
| 1106 | /* Populate front untouched region of new data cluster */ |
| 1107 | start = qed_start_of_cluster(s, acb->cur_pos); |
| 1108 | len = qed_offset_into_cluster(s, acb->cur_pos); |
| 1109 | |
| 1110 | trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); |
| 1111 | ret = qed_copy_from_backing_file(s, start, len, acb->cur_cluster); |
| 1112 | if (ret < 0) { |
| 1113 | goto out; |
| 1114 | } |
| 1115 | |
| 1116 | /* Populate back untouched region of new data cluster */ |
| 1117 | start = acb->cur_pos + acb->cur_qiov.size; |
| 1118 | len = qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; |
| 1119 | offset = acb->cur_cluster + |
| 1120 | qed_offset_into_cluster(s, acb->cur_pos) + |
| 1121 | acb->cur_qiov.size; |
| 1122 | |
| 1123 | trace_qed_aio_write_postfill(s, acb, start, len, offset); |
| 1124 | ret = qed_copy_from_backing_file(s, start, len, offset); |
| 1125 | if (ret < 0) { |
| 1126 | goto out; |
| 1127 | } |
| 1128 | |
| 1129 | ret = qed_aio_write_main(acb); |
| 1130 | if (ret < 0) { |
| 1131 | goto out; |
| 1132 | } |
| 1133 | |
| 1134 | if (s->bs->backing) { |
| 1135 | /* |
| 1136 | * Flush new data clusters before updating the L2 table |
| 1137 | * |
| 1138 | * This flush is necessary when a backing file is in use. A crash |
| 1139 | * during an allocating write could result in empty clusters in the |
| 1140 | * image. If the write only touched a subregion of the cluster, |
| 1141 | * then backing image sectors have been lost in the untouched |
| 1142 | * region. The solution is to flush after writing a new data |
| 1143 | * cluster and before updating the L2 table. |
| 1144 | */ |
| 1145 | ret = bdrv_co_flush(s->bs->file->bs); |
| 1146 | } |
| 1147 | |
| 1148 | out: |
| 1149 | qemu_co_mutex_lock(&s->table_lock); |
| 1150 | return ret; |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * Check if the QED_F_NEED_CHECK bit should be set during allocating write |
| 1155 | */ |
| 1156 | static bool GRAPH_RDLOCK qed_should_set_need_check(BDRVQEDState *s) |
| 1157 | { |
| 1158 | /* The flush before L2 update path ensures consistency */ |
| 1159 | if (s->bs->backing) { |
| 1160 | return false; |
| 1161 | } |
| 1162 | |
| 1163 | return !(s->header.features & QED_F_NEED_CHECK); |
| 1164 | } |
| 1165 | |
| 1166 | /** |
| 1167 | * Write new data cluster |
| 1168 | * |
| 1169 | * @acb: Write request |
| 1170 | * @len: Length in bytes |
| 1171 | * |
| 1172 | * This path is taken when writing to previously unallocated clusters. |
| 1173 | * |
| 1174 | * Called with table_lock held. |
| 1175 | */ |
| 1176 | static int coroutine_fn GRAPH_RDLOCK |
| 1177 | qed_aio_write_alloc(QEDAIOCB *acb, size_t len) |
| 1178 | { |
| 1179 | BDRVQEDState *s = acb_to_s(acb); |
| 1180 | int ret; |
| 1181 | |
| 1182 | /* Cancel timer when the first allocating request comes in */ |
| 1183 | if (s->allocating_acb == NULL) { |
| 1184 | qed_cancel_need_check_timer(s); |
| 1185 | } |
| 1186 | |
| 1187 | /* Freeze this request if another allocating write is in progress */ |
| 1188 | if (s->allocating_acb != acb || s->allocating_write_reqs_plugged) { |
| 1189 | if (s->allocating_acb != NULL) { |
| 1190 | qemu_co_queue_wait(&s->allocating_write_reqs, &s->table_lock); |
| 1191 | assert(s->allocating_acb == NULL); |
| 1192 | } |
| 1193 | s->allocating_acb = acb; |
| 1194 | return -EAGAIN; /* start over with looking up table entries */ |
| 1195 | } |
| 1196 | |
| 1197 | acb->cur_nclusters = qed_bytes_to_clusters(s, |
| 1198 | qed_offset_into_cluster(s, acb->cur_pos) + len); |
| 1199 | qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
| 1200 | |
| 1201 | if (acb->flags & QED_AIOCB_ZERO) { |
| 1202 | /* Skip ahead if the clusters are already zero */ |
| 1203 | if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { |
| 1204 | return 0; |
| 1205 | } |
| 1206 | acb->cur_cluster = 1; |
| 1207 | } else { |
| 1208 | acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); |
| 1209 | } |
| 1210 | |
| 1211 | if (qed_should_set_need_check(s)) { |
| 1212 | s->header.features |= QED_F_NEED_CHECK; |
| 1213 | ret = qed_write_header(s); |
| 1214 | if (ret < 0) { |
| 1215 | return ret; |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | if (!(acb->flags & QED_AIOCB_ZERO)) { |
| 1220 | ret = qed_aio_write_cow(acb); |
| 1221 | if (ret < 0) { |
| 1222 | return ret; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | return qed_aio_write_l2_update(acb, acb->cur_cluster); |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * Write data cluster in place |
| 1231 | * |
| 1232 | * @acb: Write request |
| 1233 | * @offset: Cluster offset in bytes |
| 1234 | * @len: Length in bytes |
| 1235 | * |
| 1236 | * This path is taken when writing to already allocated clusters. |
| 1237 | * |
| 1238 | * Called with table_lock held. |
| 1239 | */ |
| 1240 | static int coroutine_fn GRAPH_RDLOCK |
| 1241 | qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) |
| 1242 | { |
| 1243 | BDRVQEDState *s = acb_to_s(acb); |
| 1244 | int r; |
| 1245 | |
| 1246 | qemu_co_mutex_unlock(&s->table_lock); |
| 1247 | |
| 1248 | /* Allocate buffer for zero writes */ |
| 1249 | if (acb->flags & QED_AIOCB_ZERO) { |
| 1250 | struct iovec *iov = acb->qiov->iov; |
| 1251 | |
| 1252 | if (!iov->iov_base) { |
| 1253 | iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len); |
| 1254 | if (iov->iov_base == NULL) { |
| 1255 | r = -ENOMEM; |
| 1256 | goto out; |
| 1257 | } |
| 1258 | memset(iov->iov_base, 0, iov->iov_len); |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | /* Calculate the I/O vector */ |
| 1263 | acb->cur_cluster = offset; |
| 1264 | qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
| 1265 | |
| 1266 | /* Do the actual write. */ |
| 1267 | r = qed_aio_write_main(acb); |
| 1268 | out: |
| 1269 | qemu_co_mutex_lock(&s->table_lock); |
| 1270 | return r; |
| 1271 | } |
| 1272 | |
| 1273 | /** |
| 1274 | * Write data cluster |
| 1275 | * |
| 1276 | * @opaque: Write request |
| 1277 | * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 |
| 1278 | * @offset: Cluster offset in bytes |
| 1279 | * @len: Length in bytes |
| 1280 | * |
| 1281 | * Called with table_lock held. |
| 1282 | */ |
| 1283 | static int coroutine_fn GRAPH_RDLOCK |
| 1284 | qed_aio_write_data(void *opaque, int ret, uint64_t offset, size_t len) |
| 1285 | { |
| 1286 | QEDAIOCB *acb = opaque; |
| 1287 | |
| 1288 | trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); |
| 1289 | |
| 1290 | acb->find_cluster_ret = ret; |
| 1291 | |
| 1292 | switch (ret) { |
| 1293 | case QED_CLUSTER_FOUND: |
| 1294 | return qed_aio_write_inplace(acb, offset, len); |
| 1295 | |
| 1296 | case QED_CLUSTER_L2: |
| 1297 | case QED_CLUSTER_L1: |
| 1298 | case QED_CLUSTER_ZERO: |
| 1299 | return qed_aio_write_alloc(acb, len); |
| 1300 | |
| 1301 | default: |
| 1302 | g_assert_not_reached(); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | /** |
| 1307 | * Read data cluster |
| 1308 | * |
| 1309 | * @opaque: Read request |
| 1310 | * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 |
| 1311 | * @offset: Cluster offset in bytes |
| 1312 | * @len: Length in bytes |
| 1313 | * |
| 1314 | * Called with table_lock held. |
| 1315 | */ |
| 1316 | static int coroutine_fn GRAPH_RDLOCK |
| 1317 | qed_aio_read_data(void *opaque, int ret, uint64_t offset, size_t len) |
| 1318 | { |
| 1319 | QEDAIOCB *acb = opaque; |
| 1320 | BDRVQEDState *s = acb_to_s(acb); |
| 1321 | BlockDriverState *bs = acb->bs; |
| 1322 | int r; |
| 1323 | |
| 1324 | qemu_co_mutex_unlock(&s->table_lock); |
| 1325 | |
| 1326 | /* Adjust offset into cluster */ |
| 1327 | offset += qed_offset_into_cluster(s, acb->cur_pos); |
| 1328 | |
| 1329 | trace_qed_aio_read_data(s, acb, ret, offset, len); |
| 1330 | |
| 1331 | qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
| 1332 | |
| 1333 | /* Handle zero cluster and backing file reads, otherwise read |
| 1334 | * data cluster directly. |
| 1335 | */ |
| 1336 | if (ret == QED_CLUSTER_ZERO) { |
| 1337 | qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); |
| 1338 | r = 0; |
| 1339 | } else if (ret != QED_CLUSTER_FOUND) { |
| 1340 | r = qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov); |
| 1341 | } else { |
| 1342 | BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_AIO); |
| 1343 | r = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size, |
| 1344 | &acb->cur_qiov, 0); |
| 1345 | } |
| 1346 | |
| 1347 | qemu_co_mutex_lock(&s->table_lock); |
| 1348 | return r; |
| 1349 | } |
| 1350 | |
| 1351 | /** |
| 1352 | * Begin next I/O or complete the request |
| 1353 | */ |
| 1354 | static int coroutine_fn GRAPH_RDLOCK qed_aio_next_io(QEDAIOCB *acb) |
| 1355 | { |
| 1356 | BDRVQEDState *s = acb_to_s(acb); |
| 1357 | uint64_t offset; |
| 1358 | size_t len; |
| 1359 | int ret; |
| 1360 | |
| 1361 | qemu_co_mutex_lock(&s->table_lock); |
| 1362 | while (1) { |
| 1363 | trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size); |
| 1364 | |
| 1365 | acb->qiov_offset += acb->cur_qiov.size; |
| 1366 | acb->cur_pos += acb->cur_qiov.size; |
| 1367 | qemu_iovec_reset(&acb->cur_qiov); |
| 1368 | |
| 1369 | /* Complete request */ |
| 1370 | if (acb->cur_pos >= acb->end_pos) { |
| 1371 | ret = 0; |
| 1372 | break; |
| 1373 | } |
| 1374 | |
| 1375 | /* Find next cluster and start I/O */ |
| 1376 | len = acb->end_pos - acb->cur_pos; |
| 1377 | ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset); |
| 1378 | if (ret < 0) { |
| 1379 | break; |
| 1380 | } |
| 1381 | |
| 1382 | if (acb->flags & QED_AIOCB_WRITE) { |
| 1383 | ret = qed_aio_write_data(acb, ret, offset, len); |
| 1384 | } else { |
| 1385 | ret = qed_aio_read_data(acb, ret, offset, len); |
| 1386 | } |
| 1387 | |
| 1388 | if (ret < 0 && ret != -EAGAIN) { |
| 1389 | break; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | trace_qed_aio_complete(s, acb, ret); |
| 1394 | qed_aio_complete(acb); |
| 1395 | qemu_co_mutex_unlock(&s->table_lock); |
| 1396 | return ret; |
| 1397 | } |
| 1398 | |
| 1399 | static int coroutine_fn GRAPH_RDLOCK |
| 1400 | qed_co_request(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov, |
| 1401 | int nb_sectors, int flags) |
| 1402 | { |
| 1403 | QEDAIOCB acb = { |
| 1404 | .bs = bs, |
| 1405 | .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE, |
| 1406 | .end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE, |
| 1407 | .qiov = qiov, |
| 1408 | .flags = flags, |
| 1409 | }; |
| 1410 | qemu_iovec_init(&acb.cur_qiov, qiov->niov); |
| 1411 | |
| 1412 | trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags); |
| 1413 | |
| 1414 | /* Start request */ |
| 1415 | return qed_aio_next_io(&acb); |
| 1416 | } |
| 1417 | |
| 1418 | static int coroutine_fn GRAPH_RDLOCK |
| 1419 | bdrv_qed_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
| 1420 | QEMUIOVector *qiov) |
| 1421 | { |
| 1422 | return qed_co_request(bs, sector_num, qiov, nb_sectors, 0); |
| 1423 | } |
| 1424 | |
| 1425 | static int coroutine_fn GRAPH_RDLOCK |
| 1426 | bdrv_qed_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
| 1427 | QEMUIOVector *qiov, int flags) |
| 1428 | { |
| 1429 | return qed_co_request(bs, sector_num, qiov, nb_sectors, QED_AIOCB_WRITE); |
| 1430 | } |
| 1431 | |
| 1432 | static int coroutine_fn GRAPH_RDLOCK |
| 1433 | bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, |
| 1434 | BdrvRequestFlags flags) |
| 1435 | { |
| 1436 | BDRVQEDState *s = bs->opaque; |
| 1437 | |
| 1438 | /* |
| 1439 | * Zero writes start without an I/O buffer. If a buffer becomes necessary |
| 1440 | * then it will be allocated during request processing. |
| 1441 | */ |
| 1442 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, bytes); |
| 1443 | |
| 1444 | /* |
| 1445 | * QED is not prepared for 63bit write-zero requests, so rely on |
| 1446 | * max_pwrite_zeroes. |
| 1447 | */ |
| 1448 | assert(bytes <= INT_MAX); |
| 1449 | |
| 1450 | /* Fall back if the request is not aligned */ |
| 1451 | if (qed_offset_into_cluster(s, offset) || |
| 1452 | qed_offset_into_cluster(s, bytes)) { |
| 1453 | return -ENOTSUP; |
| 1454 | } |
| 1455 | |
| 1456 | return qed_co_request(bs, offset >> BDRV_SECTOR_BITS, &qiov, |
| 1457 | bytes >> BDRV_SECTOR_BITS, |
| 1458 | QED_AIOCB_WRITE | QED_AIOCB_ZERO); |
| 1459 | } |
| 1460 | |
| 1461 | static int coroutine_fn GRAPH_RDLOCK |
| 1462 | bdrv_qed_co_truncate(BlockDriverState *bs, int64_t offset, bool exact, |
| 1463 | PreallocMode prealloc, BdrvRequestFlags flags, |
| 1464 | Error **errp) |
| 1465 | { |
| 1466 | BDRVQEDState *s = bs->opaque; |
| 1467 | uint64_t old_image_size; |
| 1468 | int ret; |
| 1469 | |
| 1470 | if (prealloc != PREALLOC_MODE_OFF) { |
| 1471 | error_setg(errp, "Unsupported preallocation mode '%s'", |
| 1472 | PreallocMode_str(prealloc)); |
| 1473 | return -ENOTSUP; |
| 1474 | } |
| 1475 | |
| 1476 | if (!qed_is_image_size_valid(offset, s->header.cluster_size, |
| 1477 | s->header.table_size)) { |
| 1478 | error_setg(errp, "Invalid image size specified"); |
| 1479 | return -EINVAL; |
| 1480 | } |
| 1481 | |
| 1482 | if ((uint64_t)offset < s->header.image_size) { |
| 1483 | error_setg(errp, "Shrinking images is currently not supported"); |
| 1484 | return -ENOTSUP; |
| 1485 | } |
| 1486 | |
| 1487 | old_image_size = s->header.image_size; |
| 1488 | s->header.image_size = offset; |
| 1489 | ret = qed_write_header_sync(s); |
| 1490 | if (ret < 0) { |
| 1491 | s->header.image_size = old_image_size; |
| 1492 | error_setg_errno(errp, -ret, "Failed to update the image size"); |
| 1493 | } |
| 1494 | return ret; |
| 1495 | } |
| 1496 | |
| 1497 | static int64_t coroutine_fn bdrv_qed_co_getlength(BlockDriverState *bs) |
| 1498 | { |
| 1499 | BDRVQEDState *s = bs->opaque; |
| 1500 | return s->header.image_size; |
| 1501 | } |
| 1502 | |
| 1503 | static int coroutine_fn |
| 1504 | bdrv_qed_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 1505 | { |
| 1506 | BDRVQEDState *s = bs->opaque; |
| 1507 | |
| 1508 | memset(bdi, 0, sizeof(*bdi)); |
| 1509 | bdi->cluster_size = s->header.cluster_size; |
| 1510 | bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | static int coroutine_fn GRAPH_RDLOCK |
| 1515 | bdrv_qed_co_change_backing_file(BlockDriverState *bs, const char *backing_file, |
| 1516 | const char *backing_fmt) |
| 1517 | { |
| 1518 | BDRVQEDState *s = bs->opaque; |
| 1519 | QEDHeader new_header, le_header; |
| 1520 | void *buffer; |
| 1521 | size_t buffer_len, backing_file_len; |
| 1522 | int ret; |
| 1523 | |
| 1524 | /* Refuse to set backing filename if unknown compat feature bits are |
| 1525 | * active. If the image uses an unknown compat feature then we may not |
| 1526 | * know the layout of data following the header structure and cannot safely |
| 1527 | * add a new string. |
| 1528 | */ |
| 1529 | if (backing_file && (s->header.compat_features & |
| 1530 | ~QED_COMPAT_FEATURE_MASK)) { |
| 1531 | return -ENOTSUP; |
| 1532 | } |
| 1533 | |
| 1534 | memcpy(&new_header, &s->header, sizeof(new_header)); |
| 1535 | |
| 1536 | new_header.features &= ~(QED_F_BACKING_FILE | |
| 1537 | QED_F_BACKING_FORMAT_NO_PROBE); |
| 1538 | |
| 1539 | /* Adjust feature flags */ |
| 1540 | if (backing_file) { |
| 1541 | new_header.features |= QED_F_BACKING_FILE; |
| 1542 | |
| 1543 | if (qed_fmt_is_raw(backing_fmt)) { |
| 1544 | new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | /* Calculate new header size */ |
| 1549 | backing_file_len = 0; |
| 1550 | |
| 1551 | if (backing_file) { |
| 1552 | backing_file_len = strlen(backing_file); |
| 1553 | } |
| 1554 | |
| 1555 | buffer_len = sizeof(new_header); |
| 1556 | new_header.backing_filename_offset = buffer_len; |
| 1557 | new_header.backing_filename_size = backing_file_len; |
| 1558 | buffer_len += backing_file_len; |
| 1559 | |
| 1560 | /* Make sure we can rewrite header without failing */ |
| 1561 | if (buffer_len > new_header.header_size * new_header.cluster_size) { |
| 1562 | return -ENOSPC; |
| 1563 | } |
| 1564 | |
| 1565 | /* Prepare new header */ |
| 1566 | buffer = g_malloc(buffer_len); |
| 1567 | |
| 1568 | qed_header_cpu_to_le(&new_header, &le_header); |
| 1569 | memcpy(buffer, &le_header, sizeof(le_header)); |
| 1570 | buffer_len = sizeof(le_header); |
| 1571 | |
| 1572 | if (backing_file) { |
| 1573 | memcpy(buffer + buffer_len, backing_file, backing_file_len); |
| 1574 | buffer_len += backing_file_len; |
| 1575 | } |
| 1576 | |
| 1577 | /* Write new header */ |
| 1578 | ret = bdrv_co_pwrite_sync(bs->file, 0, buffer_len, buffer, 0); |
| 1579 | g_free(buffer); |
| 1580 | if (ret == 0) { |
| 1581 | memcpy(&s->header, &new_header, sizeof(new_header)); |
| 1582 | } |
| 1583 | return ret; |
| 1584 | } |
| 1585 | |
| 1586 | static void coroutine_fn GRAPH_RDLOCK |
| 1587 | bdrv_qed_co_invalidate_cache(BlockDriverState *bs, Error **errp) |
| 1588 | { |
| 1589 | ERRP_GUARD(); |
| 1590 | BDRVQEDState *s = bs->opaque; |
| 1591 | int ret; |
| 1592 | |
| 1593 | bdrv_qed_do_close(bs); |
| 1594 | |
| 1595 | bdrv_qed_init_state(bs); |
| 1596 | qemu_co_mutex_lock(&s->table_lock); |
| 1597 | ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, errp); |
| 1598 | qemu_co_mutex_unlock(&s->table_lock); |
| 1599 | if (ret < 0) { |
| 1600 | error_prepend(errp, "Could not reopen qed layer: "); |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | static int coroutine_fn GRAPH_RDLOCK |
| 1605 | bdrv_qed_co_check(BlockDriverState *bs, BdrvCheckResult *result, |
| 1606 | BdrvCheckMode fix) |
| 1607 | { |
| 1608 | BDRVQEDState *s = bs->opaque; |
| 1609 | int ret; |
| 1610 | |
| 1611 | qemu_co_mutex_lock(&s->table_lock); |
| 1612 | ret = qed_check(s, result, !!fix); |
| 1613 | qemu_co_mutex_unlock(&s->table_lock); |
| 1614 | |
| 1615 | return ret; |
| 1616 | } |
| 1617 | |
| 1618 | static QemuOptsList qed_create_opts = { |
| 1619 | .name = "qed-create-opts", |
| 1620 | .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head), |
| 1621 | .desc = { |
| 1622 | { |
| 1623 | .name = BLOCK_OPT_SIZE, |
| 1624 | .type = QEMU_OPT_SIZE, |
| 1625 | .help = "Virtual disk size" |
| 1626 | }, |
| 1627 | { |
| 1628 | .name = BLOCK_OPT_BACKING_FILE, |
| 1629 | .type = QEMU_OPT_STRING, |
| 1630 | .help = "File name of a base image" |
| 1631 | }, |
| 1632 | { |
| 1633 | .name = BLOCK_OPT_BACKING_FMT, |
| 1634 | .type = QEMU_OPT_STRING, |
| 1635 | .help = "Image format of the base image" |
| 1636 | }, |
| 1637 | { |
| 1638 | .name = BLOCK_OPT_CLUSTER_SIZE, |
| 1639 | .type = QEMU_OPT_SIZE, |
| 1640 | .help = "Cluster size (in bytes)", |
| 1641 | .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE) |
| 1642 | }, |
| 1643 | { |
| 1644 | .name = BLOCK_OPT_TABLE_SIZE, |
| 1645 | .type = QEMU_OPT_SIZE, |
| 1646 | .help = "L1/L2 table size (in clusters)" |
| 1647 | }, |
| 1648 | { /* end of list */ } |
| 1649 | } |
| 1650 | }; |
| 1651 | |
| 1652 | static BlockDriver bdrv_qed = { |
| 1653 | .format_name = "qed", |
| 1654 | .instance_size = sizeof(BDRVQEDState), |
| 1655 | .create_opts = &qed_create_opts, |
| 1656 | .is_format = true, |
| 1657 | .supports_backing = true, |
| 1658 | |
| 1659 | .bdrv_probe = bdrv_qed_probe, |
| 1660 | .bdrv_open = bdrv_qed_open, |
| 1661 | .bdrv_close = bdrv_qed_close, |
| 1662 | .bdrv_reopen_prepare = bdrv_qed_reopen_prepare, |
| 1663 | .bdrv_child_perm = bdrv_default_perms, |
| 1664 | .bdrv_co_create = bdrv_qed_co_create, |
| 1665 | .bdrv_co_create_opts = bdrv_qed_co_create_opts, |
| 1666 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
| 1667 | .bdrv_co_block_status = bdrv_qed_co_block_status, |
| 1668 | .bdrv_co_readv = bdrv_qed_co_readv, |
| 1669 | .bdrv_co_writev = bdrv_qed_co_writev, |
| 1670 | .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes, |
| 1671 | .bdrv_co_truncate = bdrv_qed_co_truncate, |
| 1672 | .bdrv_co_getlength = bdrv_qed_co_getlength, |
| 1673 | .bdrv_co_get_info = bdrv_qed_co_get_info, |
| 1674 | .bdrv_refresh_limits = bdrv_qed_refresh_limits, |
| 1675 | .bdrv_co_change_backing_file = bdrv_qed_co_change_backing_file, |
| 1676 | .bdrv_co_invalidate_cache = bdrv_qed_co_invalidate_cache, |
| 1677 | .bdrv_co_check = bdrv_qed_co_check, |
| 1678 | .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, |
| 1679 | .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, |
| 1680 | .bdrv_drain_begin = bdrv_qed_drain_begin, |
| 1681 | }; |
| 1682 | |
| 1683 | static void bdrv_qed_init(void) |
| 1684 | { |
| 1685 | bdrv_register(&bdrv_qed); |
| 1686 | } |
| 1687 | |
| 1688 | block_init(bdrv_qed_init); |