Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 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 | |
Stefan Hajnoczi | 6f321e9 | 2011-05-09 16:45:40 +0100 | [diff] [blame] | 15 | #include "qemu-timer.h" |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 16 | #include "trace.h" |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 17 | #include "qed.h" |
Kevin Wolf | 10b758e | 2011-02-09 11:13:26 +0100 | [diff] [blame] | 18 | #include "qerror.h" |
Anthony Liguori | 1ed520c | 2011-11-14 15:09:47 -0600 | [diff] [blame] | 19 | #include "migration.h" |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 20 | |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 21 | static void qed_aio_cancel(BlockDriverAIOCB *blockacb) |
| 22 | { |
| 23 | QEDAIOCB *acb = (QEDAIOCB *)blockacb; |
| 24 | bool finished = false; |
| 25 | |
| 26 | /* Wait for the request to finish */ |
| 27 | acb->finished = &finished; |
| 28 | while (!finished) { |
| 29 | qemu_aio_wait(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | static AIOPool qed_aio_pool = { |
| 34 | .aiocb_size = sizeof(QEDAIOCB), |
| 35 | .cancel = qed_aio_cancel, |
| 36 | }; |
| 37 | |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 38 | static int bdrv_qed_probe(const uint8_t *buf, int buf_size, |
| 39 | const char *filename) |
| 40 | { |
| 41 | const QEDHeader *header = (const QEDHeader *)buf; |
| 42 | |
| 43 | if (buf_size < sizeof(*header)) { |
| 44 | return 0; |
| 45 | } |
| 46 | if (le32_to_cpu(header->magic) != QED_MAGIC) { |
| 47 | return 0; |
| 48 | } |
| 49 | return 100; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Check whether an image format is raw |
| 54 | * |
| 55 | * @fmt: Backing file format, may be NULL |
| 56 | */ |
| 57 | static bool qed_fmt_is_raw(const char *fmt) |
| 58 | { |
| 59 | return fmt && strcmp(fmt, "raw") == 0; |
| 60 | } |
| 61 | |
| 62 | static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu) |
| 63 | { |
| 64 | cpu->magic = le32_to_cpu(le->magic); |
| 65 | cpu->cluster_size = le32_to_cpu(le->cluster_size); |
| 66 | cpu->table_size = le32_to_cpu(le->table_size); |
| 67 | cpu->header_size = le32_to_cpu(le->header_size); |
| 68 | cpu->features = le64_to_cpu(le->features); |
| 69 | cpu->compat_features = le64_to_cpu(le->compat_features); |
| 70 | cpu->autoclear_features = le64_to_cpu(le->autoclear_features); |
| 71 | cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset); |
| 72 | cpu->image_size = le64_to_cpu(le->image_size); |
| 73 | cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset); |
| 74 | cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size); |
| 75 | } |
| 76 | |
| 77 | static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le) |
| 78 | { |
| 79 | le->magic = cpu_to_le32(cpu->magic); |
| 80 | le->cluster_size = cpu_to_le32(cpu->cluster_size); |
| 81 | le->table_size = cpu_to_le32(cpu->table_size); |
| 82 | le->header_size = cpu_to_le32(cpu->header_size); |
| 83 | le->features = cpu_to_le64(cpu->features); |
| 84 | le->compat_features = cpu_to_le64(cpu->compat_features); |
| 85 | le->autoclear_features = cpu_to_le64(cpu->autoclear_features); |
| 86 | le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset); |
| 87 | le->image_size = cpu_to_le64(cpu->image_size); |
| 88 | le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset); |
| 89 | le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size); |
| 90 | } |
| 91 | |
| 92 | static int qed_write_header_sync(BDRVQEDState *s) |
| 93 | { |
| 94 | QEDHeader le; |
| 95 | int ret; |
| 96 | |
| 97 | qed_header_cpu_to_le(&s->header, &le); |
| 98 | ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le)); |
| 99 | if (ret != sizeof(le)) { |
| 100 | return ret; |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 105 | typedef struct { |
| 106 | GenericCB gencb; |
| 107 | BDRVQEDState *s; |
| 108 | struct iovec iov; |
| 109 | QEMUIOVector qiov; |
| 110 | int nsectors; |
| 111 | uint8_t *buf; |
| 112 | } QEDWriteHeaderCB; |
| 113 | |
| 114 | static void qed_write_header_cb(void *opaque, int ret) |
| 115 | { |
| 116 | QEDWriteHeaderCB *write_header_cb = opaque; |
| 117 | |
| 118 | qemu_vfree(write_header_cb->buf); |
| 119 | gencb_complete(write_header_cb, ret); |
| 120 | } |
| 121 | |
| 122 | static void qed_write_header_read_cb(void *opaque, int ret) |
| 123 | { |
| 124 | QEDWriteHeaderCB *write_header_cb = opaque; |
| 125 | BDRVQEDState *s = write_header_cb->s; |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 126 | |
| 127 | if (ret) { |
| 128 | qed_write_header_cb(write_header_cb, ret); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | /* Update header */ |
| 133 | qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf); |
| 134 | |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 135 | bdrv_aio_writev(s->bs->file, 0, &write_header_cb->qiov, |
| 136 | write_header_cb->nsectors, qed_write_header_cb, |
| 137 | write_header_cb); |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Update header in-place (does not rewrite backing filename or other strings) |
| 142 | * |
| 143 | * This function only updates known header fields in-place and does not affect |
| 144 | * extra data after the QED header. |
| 145 | */ |
| 146 | static void qed_write_header(BDRVQEDState *s, BlockDriverCompletionFunc cb, |
| 147 | void *opaque) |
| 148 | { |
| 149 | /* We must write full sectors for O_DIRECT but cannot necessarily generate |
| 150 | * the data following the header if an unrecognized compat feature is |
| 151 | * active. Therefore, first read the sectors containing the header, update |
| 152 | * them, and write back. |
| 153 | */ |
| 154 | |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 155 | int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) / |
| 156 | BDRV_SECTOR_SIZE; |
| 157 | size_t len = nsectors * BDRV_SECTOR_SIZE; |
| 158 | QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb), |
| 159 | cb, opaque); |
| 160 | |
| 161 | write_header_cb->s = s; |
| 162 | write_header_cb->nsectors = nsectors; |
| 163 | write_header_cb->buf = qemu_blockalign(s->bs, len); |
| 164 | write_header_cb->iov.iov_base = write_header_cb->buf; |
| 165 | write_header_cb->iov.iov_len = len; |
| 166 | qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1); |
| 167 | |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 168 | bdrv_aio_readv(s->bs->file, 0, &write_header_cb->qiov, nsectors, |
| 169 | qed_write_header_read_cb, write_header_cb); |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 172 | static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) |
| 173 | { |
| 174 | uint64_t table_entries; |
| 175 | uint64_t l2_size; |
| 176 | |
| 177 | table_entries = (table_size * cluster_size) / sizeof(uint64_t); |
| 178 | l2_size = table_entries * cluster_size; |
| 179 | |
| 180 | return l2_size * table_entries; |
| 181 | } |
| 182 | |
| 183 | static bool qed_is_cluster_size_valid(uint32_t cluster_size) |
| 184 | { |
| 185 | if (cluster_size < QED_MIN_CLUSTER_SIZE || |
| 186 | cluster_size > QED_MAX_CLUSTER_SIZE) { |
| 187 | return false; |
| 188 | } |
| 189 | if (cluster_size & (cluster_size - 1)) { |
| 190 | return false; /* not power of 2 */ |
| 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | static bool qed_is_table_size_valid(uint32_t table_size) |
| 196 | { |
| 197 | if (table_size < QED_MIN_TABLE_SIZE || |
| 198 | table_size > QED_MAX_TABLE_SIZE) { |
| 199 | return false; |
| 200 | } |
| 201 | if (table_size & (table_size - 1)) { |
| 202 | return false; /* not power of 2 */ |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, |
| 208 | uint32_t table_size) |
| 209 | { |
| 210 | if (image_size % BDRV_SECTOR_SIZE != 0) { |
| 211 | return false; /* not multiple of sector size */ |
| 212 | } |
| 213 | if (image_size > qed_max_image_size(cluster_size, table_size)) { |
| 214 | return false; /* image is too large */ |
| 215 | } |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Read a string of known length from the image file |
| 221 | * |
| 222 | * @file: Image file |
| 223 | * @offset: File offset to start of string, in bytes |
| 224 | * @n: String length in bytes |
| 225 | * @buf: Destination buffer |
| 226 | * @buflen: Destination buffer length in bytes |
| 227 | * @ret: 0 on success, -errno on failure |
| 228 | * |
| 229 | * The string is NUL-terminated. |
| 230 | */ |
| 231 | static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n, |
| 232 | char *buf, size_t buflen) |
| 233 | { |
| 234 | int ret; |
| 235 | if (n >= buflen) { |
| 236 | return -EINVAL; |
| 237 | } |
| 238 | ret = bdrv_pread(file, offset, buf, n); |
| 239 | if (ret < 0) { |
| 240 | return ret; |
| 241 | } |
| 242 | buf[n] = '\0'; |
| 243 | return 0; |
| 244 | } |
| 245 | |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 246 | /** |
| 247 | * Allocate new clusters |
| 248 | * |
| 249 | * @s: QED state |
| 250 | * @n: Number of contiguous clusters to allocate |
| 251 | * @ret: Offset of first allocated cluster |
| 252 | * |
| 253 | * This function only produces the offset where the new clusters should be |
| 254 | * written. It updates BDRVQEDState but does not make any changes to the image |
| 255 | * file. |
| 256 | */ |
| 257 | static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) |
| 258 | { |
| 259 | uint64_t offset = s->file_size; |
| 260 | s->file_size += n * s->header.cluster_size; |
| 261 | return offset; |
| 262 | } |
| 263 | |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 264 | QEDTable *qed_alloc_table(BDRVQEDState *s) |
| 265 | { |
| 266 | /* Honor O_DIRECT memory alignment requirements */ |
| 267 | return qemu_blockalign(s->bs, |
| 268 | s->header.cluster_size * s->header.table_size); |
| 269 | } |
| 270 | |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 271 | /** |
| 272 | * Allocate a new zeroed L2 table |
| 273 | */ |
| 274 | static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) |
| 275 | { |
| 276 | CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); |
| 277 | |
| 278 | l2_table->table = qed_alloc_table(s); |
| 279 | l2_table->offset = qed_alloc_clusters(s, s->header.table_size); |
| 280 | |
| 281 | memset(l2_table->table->offsets, 0, |
| 282 | s->header.cluster_size * s->header.table_size); |
| 283 | return l2_table; |
| 284 | } |
| 285 | |
| 286 | static void qed_aio_next_io(void *opaque, int ret); |
| 287 | |
Stefan Hajnoczi | 6f321e9 | 2011-05-09 16:45:40 +0100 | [diff] [blame] | 288 | static void qed_plug_allocating_write_reqs(BDRVQEDState *s) |
| 289 | { |
| 290 | assert(!s->allocating_write_reqs_plugged); |
| 291 | |
| 292 | s->allocating_write_reqs_plugged = true; |
| 293 | } |
| 294 | |
| 295 | static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) |
| 296 | { |
| 297 | QEDAIOCB *acb; |
| 298 | |
| 299 | assert(s->allocating_write_reqs_plugged); |
| 300 | |
| 301 | s->allocating_write_reqs_plugged = false; |
| 302 | |
| 303 | acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); |
| 304 | if (acb) { |
| 305 | qed_aio_next_io(acb, 0); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static void qed_finish_clear_need_check(void *opaque, int ret) |
| 310 | { |
| 311 | /* Do nothing */ |
| 312 | } |
| 313 | |
| 314 | static void qed_flush_after_clear_need_check(void *opaque, int ret) |
| 315 | { |
| 316 | BDRVQEDState *s = opaque; |
| 317 | |
| 318 | bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s); |
| 319 | |
| 320 | /* No need to wait until flush completes */ |
| 321 | qed_unplug_allocating_write_reqs(s); |
| 322 | } |
| 323 | |
| 324 | static void qed_clear_need_check(void *opaque, int ret) |
| 325 | { |
| 326 | BDRVQEDState *s = opaque; |
| 327 | |
| 328 | if (ret) { |
| 329 | qed_unplug_allocating_write_reqs(s); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | s->header.features &= ~QED_F_NEED_CHECK; |
| 334 | qed_write_header(s, qed_flush_after_clear_need_check, s); |
| 335 | } |
| 336 | |
| 337 | static void qed_need_check_timer_cb(void *opaque) |
| 338 | { |
| 339 | BDRVQEDState *s = opaque; |
| 340 | |
| 341 | /* The timer should only fire when allocating writes have drained */ |
| 342 | assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs)); |
| 343 | |
| 344 | trace_qed_need_check_timer_cb(s); |
| 345 | |
| 346 | qed_plug_allocating_write_reqs(s); |
| 347 | |
| 348 | /* Ensure writes are on disk before clearing flag */ |
| 349 | bdrv_aio_flush(s->bs, qed_clear_need_check, s); |
| 350 | } |
| 351 | |
| 352 | static void qed_start_need_check_timer(BDRVQEDState *s) |
| 353 | { |
| 354 | trace_qed_start_need_check_timer(s); |
| 355 | |
| 356 | /* Use vm_clock so we don't alter the image file while suspended for |
| 357 | * migration. |
| 358 | */ |
| 359 | qemu_mod_timer(s->need_check_timer, qemu_get_clock_ns(vm_clock) + |
| 360 | get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT); |
| 361 | } |
| 362 | |
| 363 | /* It's okay to call this multiple times or when no timer is started */ |
| 364 | static void qed_cancel_need_check_timer(BDRVQEDState *s) |
| 365 | { |
| 366 | trace_qed_cancel_need_check_timer(s); |
| 367 | qemu_del_timer(s->need_check_timer); |
| 368 | } |
| 369 | |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 370 | static int bdrv_qed_open(BlockDriverState *bs, int flags) |
| 371 | { |
| 372 | BDRVQEDState *s = bs->opaque; |
| 373 | QEDHeader le_header; |
| 374 | int64_t file_size; |
| 375 | int ret; |
| 376 | |
| 377 | s->bs = bs; |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 378 | QSIMPLEQ_INIT(&s->allocating_write_reqs); |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 379 | |
| 380 | ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header)); |
| 381 | if (ret < 0) { |
| 382 | return ret; |
| 383 | } |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 384 | qed_header_le_to_cpu(&le_header, &s->header); |
| 385 | |
| 386 | if (s->header.magic != QED_MAGIC) { |
| 387 | return -EINVAL; |
| 388 | } |
| 389 | if (s->header.features & ~QED_FEATURE_MASK) { |
Kevin Wolf | 10b758e | 2011-02-09 11:13:26 +0100 | [diff] [blame] | 390 | /* image uses unsupported feature bits */ |
| 391 | char buf[64]; |
| 392 | snprintf(buf, sizeof(buf), "%" PRIx64, |
| 393 | s->header.features & ~QED_FEATURE_MASK); |
| 394 | qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, |
| 395 | bs->device_name, "QED", buf); |
| 396 | return -ENOTSUP; |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 397 | } |
| 398 | if (!qed_is_cluster_size_valid(s->header.cluster_size)) { |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | |
| 402 | /* Round down file size to the last cluster */ |
| 403 | file_size = bdrv_getlength(bs->file); |
| 404 | if (file_size < 0) { |
| 405 | return file_size; |
| 406 | } |
| 407 | s->file_size = qed_start_of_cluster(s, file_size); |
| 408 | |
| 409 | if (!qed_is_table_size_valid(s->header.table_size)) { |
| 410 | return -EINVAL; |
| 411 | } |
| 412 | if (!qed_is_image_size_valid(s->header.image_size, |
| 413 | s->header.cluster_size, |
| 414 | s->header.table_size)) { |
| 415 | return -EINVAL; |
| 416 | } |
| 417 | if (!qed_check_table_offset(s, s->header.l1_table_offset)) { |
| 418 | return -EINVAL; |
| 419 | } |
| 420 | |
| 421 | s->table_nelems = (s->header.cluster_size * s->header.table_size) / |
| 422 | sizeof(uint64_t); |
| 423 | s->l2_shift = ffs(s->header.cluster_size) - 1; |
| 424 | s->l2_mask = s->table_nelems - 1; |
| 425 | s->l1_shift = s->l2_shift + ffs(s->table_nelems) - 1; |
| 426 | |
| 427 | if ((s->header.features & QED_F_BACKING_FILE)) { |
| 428 | if ((uint64_t)s->header.backing_filename_offset + |
| 429 | s->header.backing_filename_size > |
| 430 | s->header.cluster_size * s->header.header_size) { |
| 431 | return -EINVAL; |
| 432 | } |
| 433 | |
| 434 | ret = qed_read_string(bs->file, s->header.backing_filename_offset, |
| 435 | s->header.backing_filename_size, bs->backing_file, |
| 436 | sizeof(bs->backing_file)); |
| 437 | if (ret < 0) { |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { |
| 442 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /* Reset unknown autoclear feature bits. This is a backwards |
| 447 | * compatibility mechanism that allows images to be opened by older |
| 448 | * programs, which "knock out" unknown feature bits. When an image is |
| 449 | * opened by a newer program again it can detect that the autoclear |
| 450 | * feature is no longer valid. |
| 451 | */ |
| 452 | if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && |
Benoît Canet | 2d1f3c2 | 2012-03-23 08:36:53 +0100 | [diff] [blame] | 453 | !bdrv_is_read_only(bs->file) && !(flags & BDRV_O_INCOMING)) { |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 454 | s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; |
| 455 | |
| 456 | ret = qed_write_header_sync(s); |
| 457 | if (ret) { |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | /* From here on only known autoclear feature bits are valid */ |
| 462 | bdrv_flush(bs->file); |
| 463 | } |
| 464 | |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 465 | s->l1_table = qed_alloc_table(s); |
| 466 | qed_init_l2_cache(&s->l2_cache); |
| 467 | |
| 468 | ret = qed_read_l1_table_sync(s); |
| 469 | if (ret) { |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 470 | goto out; |
| 471 | } |
| 472 | |
| 473 | /* If image was not closed cleanly, check consistency */ |
| 474 | if (s->header.features & QED_F_NEED_CHECK) { |
| 475 | /* Read-only images cannot be fixed. There is no risk of corruption |
| 476 | * since write operations are not possible. Therefore, allow |
| 477 | * potentially inconsistent images to be opened read-only. This can |
| 478 | * aid data recovery from an otherwise inconsistent image. |
| 479 | */ |
Benoît Canet | 2d1f3c2 | 2012-03-23 08:36:53 +0100 | [diff] [blame] | 480 | if (!bdrv_is_read_only(bs->file) && |
| 481 | !(flags & BDRV_O_INCOMING)) { |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 482 | BdrvCheckResult result = {0}; |
| 483 | |
| 484 | ret = qed_check(s, &result, true); |
Stefan Hajnoczi | 6f321e9 | 2011-05-09 16:45:40 +0100 | [diff] [blame] | 485 | if (ret) { |
| 486 | goto out; |
| 487 | } |
| 488 | if (!result.corruptions && !result.check_errors) { |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 489 | /* Ensure fixes reach storage before clearing check bit */ |
| 490 | bdrv_flush(s->bs); |
| 491 | |
| 492 | s->header.features &= ~QED_F_NEED_CHECK; |
| 493 | qed_write_header_sync(s); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
Stefan Hajnoczi | 6f321e9 | 2011-05-09 16:45:40 +0100 | [diff] [blame] | 498 | s->need_check_timer = qemu_new_timer_ns(vm_clock, |
| 499 | qed_need_check_timer_cb, s); |
| 500 | |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 501 | out: |
| 502 | if (ret) { |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 503 | qed_free_l2_cache(&s->l2_cache); |
| 504 | qemu_vfree(s->l1_table); |
| 505 | } |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | static void bdrv_qed_close(BlockDriverState *bs) |
| 510 | { |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 511 | BDRVQEDState *s = bs->opaque; |
| 512 | |
Stefan Hajnoczi | 6f321e9 | 2011-05-09 16:45:40 +0100 | [diff] [blame] | 513 | qed_cancel_need_check_timer(s); |
| 514 | qemu_free_timer(s->need_check_timer); |
| 515 | |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 516 | /* Ensure writes reach stable storage */ |
| 517 | bdrv_flush(bs->file); |
| 518 | |
| 519 | /* Clean shutdown, no check required on next open */ |
| 520 | if (s->header.features & QED_F_NEED_CHECK) { |
| 521 | s->header.features &= ~QED_F_NEED_CHECK; |
| 522 | qed_write_header_sync(s); |
| 523 | } |
| 524 | |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 525 | qed_free_l2_cache(&s->l2_cache); |
| 526 | qemu_vfree(s->l1_table); |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 529 | static int qed_create(const char *filename, uint32_t cluster_size, |
| 530 | uint64_t image_size, uint32_t table_size, |
| 531 | const char *backing_file, const char *backing_fmt) |
| 532 | { |
| 533 | QEDHeader header = { |
| 534 | .magic = QED_MAGIC, |
| 535 | .cluster_size = cluster_size, |
| 536 | .table_size = table_size, |
| 537 | .header_size = 1, |
| 538 | .features = 0, |
| 539 | .compat_features = 0, |
| 540 | .l1_table_offset = cluster_size, |
| 541 | .image_size = image_size, |
| 542 | }; |
| 543 | QEDHeader le_header; |
| 544 | uint8_t *l1_table = NULL; |
| 545 | size_t l1_size = header.cluster_size * header.table_size; |
| 546 | int ret = 0; |
| 547 | BlockDriverState *bs = NULL; |
| 548 | |
| 549 | ret = bdrv_create_file(filename, NULL); |
| 550 | if (ret < 0) { |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR | BDRV_O_CACHE_WB); |
| 555 | if (ret < 0) { |
| 556 | return ret; |
| 557 | } |
| 558 | |
Stefan Hajnoczi | c743849 | 2011-01-14 22:44:33 +0000 | [diff] [blame] | 559 | /* File must start empty and grow, check truncate is supported */ |
| 560 | ret = bdrv_truncate(bs, 0); |
| 561 | if (ret < 0) { |
| 562 | goto out; |
| 563 | } |
| 564 | |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 565 | if (backing_file) { |
| 566 | header.features |= QED_F_BACKING_FILE; |
| 567 | header.backing_filename_offset = sizeof(le_header); |
| 568 | header.backing_filename_size = strlen(backing_file); |
| 569 | |
| 570 | if (qed_fmt_is_raw(backing_fmt)) { |
| 571 | header.features |= QED_F_BACKING_FORMAT_NO_PROBE; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | qed_header_cpu_to_le(&header, &le_header); |
| 576 | ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header)); |
| 577 | if (ret < 0) { |
| 578 | goto out; |
| 579 | } |
| 580 | ret = bdrv_pwrite(bs, sizeof(le_header), backing_file, |
| 581 | header.backing_filename_size); |
| 582 | if (ret < 0) { |
| 583 | goto out; |
| 584 | } |
| 585 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 586 | l1_table = g_malloc0(l1_size); |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 587 | ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size); |
| 588 | if (ret < 0) { |
| 589 | goto out; |
| 590 | } |
| 591 | |
| 592 | ret = 0; /* success */ |
| 593 | out: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 594 | g_free(l1_table); |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 595 | bdrv_delete(bs); |
| 596 | return ret; |
| 597 | } |
| 598 | |
| 599 | static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options) |
| 600 | { |
| 601 | uint64_t image_size = 0; |
| 602 | uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; |
| 603 | uint32_t table_size = QED_DEFAULT_TABLE_SIZE; |
| 604 | const char *backing_file = NULL; |
| 605 | const char *backing_fmt = NULL; |
| 606 | |
| 607 | while (options && options->name) { |
| 608 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { |
| 609 | image_size = options->value.n; |
| 610 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { |
| 611 | backing_file = options->value.s; |
| 612 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { |
| 613 | backing_fmt = options->value.s; |
| 614 | } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { |
| 615 | if (options->value.n) { |
| 616 | cluster_size = options->value.n; |
| 617 | } |
| 618 | } else if (!strcmp(options->name, BLOCK_OPT_TABLE_SIZE)) { |
| 619 | if (options->value.n) { |
| 620 | table_size = options->value.n; |
| 621 | } |
| 622 | } |
| 623 | options++; |
| 624 | } |
| 625 | |
| 626 | if (!qed_is_cluster_size_valid(cluster_size)) { |
| 627 | fprintf(stderr, "QED cluster size must be within range [%u, %u] and power of 2\n", |
| 628 | QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); |
| 629 | return -EINVAL; |
| 630 | } |
| 631 | if (!qed_is_table_size_valid(table_size)) { |
| 632 | fprintf(stderr, "QED table size must be within range [%u, %u] and power of 2\n", |
| 633 | QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); |
| 634 | return -EINVAL; |
| 635 | } |
| 636 | if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { |
| 637 | fprintf(stderr, "QED image size must be a non-zero multiple of " |
| 638 | "cluster size and less than %" PRIu64 " bytes\n", |
| 639 | qed_max_image_size(cluster_size, table_size)); |
| 640 | return -EINVAL; |
| 641 | } |
| 642 | |
| 643 | return qed_create(filename, cluster_size, image_size, table_size, |
| 644 | backing_file, backing_fmt); |
| 645 | } |
| 646 | |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 647 | typedef struct { |
Stefan Hajnoczi | b7d5a5b | 2011-11-14 12:44:20 +0000 | [diff] [blame] | 648 | Coroutine *co; |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 649 | int is_allocated; |
| 650 | int *pnum; |
| 651 | } QEDIsAllocatedCB; |
| 652 | |
| 653 | static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) |
| 654 | { |
| 655 | QEDIsAllocatedCB *cb = opaque; |
| 656 | *cb->pnum = len / BDRV_SECTOR_SIZE; |
Anthony Liguori | 21df65b | 2010-12-17 15:58:22 +0000 | [diff] [blame] | 657 | cb->is_allocated = (ret == QED_CLUSTER_FOUND || ret == QED_CLUSTER_ZERO); |
Stefan Hajnoczi | b7d5a5b | 2011-11-14 12:44:20 +0000 | [diff] [blame] | 658 | if (cb->co) { |
| 659 | qemu_coroutine_enter(cb->co, NULL); |
| 660 | } |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Stefan Hajnoczi | b7d5a5b | 2011-11-14 12:44:20 +0000 | [diff] [blame] | 663 | static int coroutine_fn bdrv_qed_co_is_allocated(BlockDriverState *bs, |
| 664 | int64_t sector_num, |
| 665 | int nb_sectors, int *pnum) |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 666 | { |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 667 | BDRVQEDState *s = bs->opaque; |
| 668 | uint64_t pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; |
| 669 | size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; |
| 670 | QEDIsAllocatedCB cb = { |
| 671 | .is_allocated = -1, |
| 672 | .pnum = pnum, |
| 673 | }; |
| 674 | QEDRequest request = { .l2_table = NULL }; |
| 675 | |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 676 | qed_find_cluster(s, &request, pos, len, qed_is_allocated_cb, &cb); |
| 677 | |
Stefan Hajnoczi | b7d5a5b | 2011-11-14 12:44:20 +0000 | [diff] [blame] | 678 | /* Now sleep if the callback wasn't invoked immediately */ |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 679 | while (cb.is_allocated == -1) { |
Stefan Hajnoczi | b7d5a5b | 2011-11-14 12:44:20 +0000 | [diff] [blame] | 680 | cb.co = qemu_coroutine_self(); |
| 681 | qemu_coroutine_yield(); |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Stefan Hajnoczi | 298800c | 2010-12-06 16:08:01 +0000 | [diff] [blame] | 684 | qed_unref_l2_cache_entry(request.l2_table); |
| 685 | |
| 686 | return cb.is_allocated; |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | static int bdrv_qed_make_empty(BlockDriverState *bs) |
| 690 | { |
| 691 | return -ENOTSUP; |
| 692 | } |
| 693 | |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 694 | static BDRVQEDState *acb_to_s(QEDAIOCB *acb) |
| 695 | { |
| 696 | return acb->common.bs->opaque; |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * Read from the backing file or zero-fill if no backing file |
| 701 | * |
| 702 | * @s: QED state |
| 703 | * @pos: Byte position in device |
| 704 | * @qiov: Destination I/O vector |
| 705 | * @cb: Completion function |
| 706 | * @opaque: User data for completion function |
| 707 | * |
| 708 | * This function reads qiov->size bytes starting at pos from the backing file. |
| 709 | * If there is no backing file then zeroes are read. |
| 710 | */ |
| 711 | static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos, |
| 712 | QEMUIOVector *qiov, |
| 713 | BlockDriverCompletionFunc *cb, void *opaque) |
| 714 | { |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 715 | uint64_t backing_length = 0; |
| 716 | size_t size; |
| 717 | |
| 718 | /* If there is a backing file, get its length. Treat the absence of a |
| 719 | * backing file like a zero length backing file. |
| 720 | */ |
| 721 | if (s->bs->backing_hd) { |
| 722 | int64_t l = bdrv_getlength(s->bs->backing_hd); |
| 723 | if (l < 0) { |
| 724 | cb(opaque, l); |
| 725 | return; |
| 726 | } |
| 727 | backing_length = l; |
| 728 | } |
| 729 | |
| 730 | /* Zero all sectors if reading beyond the end of the backing file */ |
| 731 | if (pos >= backing_length || |
| 732 | pos + qiov->size > backing_length) { |
| 733 | qemu_iovec_memset(qiov, 0, qiov->size); |
| 734 | } |
| 735 | |
| 736 | /* Complete now if there are no backing file sectors to read */ |
| 737 | if (pos >= backing_length) { |
| 738 | cb(opaque, 0); |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | /* If the read straddles the end of the backing file, shorten it */ |
| 743 | size = MIN((uint64_t)backing_length - pos, qiov->size); |
| 744 | |
| 745 | BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING); |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 746 | bdrv_aio_readv(s->bs->backing_hd, pos / BDRV_SECTOR_SIZE, |
| 747 | qiov, size / BDRV_SECTOR_SIZE, cb, opaque); |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | typedef struct { |
| 751 | GenericCB gencb; |
| 752 | BDRVQEDState *s; |
| 753 | QEMUIOVector qiov; |
| 754 | struct iovec iov; |
| 755 | uint64_t offset; |
| 756 | } CopyFromBackingFileCB; |
| 757 | |
| 758 | static void qed_copy_from_backing_file_cb(void *opaque, int ret) |
| 759 | { |
| 760 | CopyFromBackingFileCB *copy_cb = opaque; |
| 761 | qemu_vfree(copy_cb->iov.iov_base); |
| 762 | gencb_complete(©_cb->gencb, ret); |
| 763 | } |
| 764 | |
| 765 | static void qed_copy_from_backing_file_write(void *opaque, int ret) |
| 766 | { |
| 767 | CopyFromBackingFileCB *copy_cb = opaque; |
| 768 | BDRVQEDState *s = copy_cb->s; |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 769 | |
| 770 | if (ret) { |
| 771 | qed_copy_from_backing_file_cb(copy_cb, ret); |
| 772 | return; |
| 773 | } |
| 774 | |
| 775 | BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 776 | bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE, |
| 777 | ©_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE, |
| 778 | qed_copy_from_backing_file_cb, copy_cb); |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | /** |
| 782 | * Copy data from backing file into the image |
| 783 | * |
| 784 | * @s: QED state |
| 785 | * @pos: Byte position in device |
| 786 | * @len: Number of bytes |
| 787 | * @offset: Byte offset in image file |
| 788 | * @cb: Completion function |
| 789 | * @opaque: User data for completion function |
| 790 | */ |
| 791 | static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, |
| 792 | uint64_t len, uint64_t offset, |
| 793 | BlockDriverCompletionFunc *cb, |
| 794 | void *opaque) |
| 795 | { |
| 796 | CopyFromBackingFileCB *copy_cb; |
| 797 | |
| 798 | /* Skip copy entirely if there is no work to do */ |
| 799 | if (len == 0) { |
| 800 | cb(opaque, 0); |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque); |
| 805 | copy_cb->s = s; |
| 806 | copy_cb->offset = offset; |
| 807 | copy_cb->iov.iov_base = qemu_blockalign(s->bs, len); |
| 808 | copy_cb->iov.iov_len = len; |
| 809 | qemu_iovec_init_external(©_cb->qiov, ©_cb->iov, 1); |
| 810 | |
| 811 | qed_read_backing_file(s, pos, ©_cb->qiov, |
| 812 | qed_copy_from_backing_file_write, copy_cb); |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * Link one or more contiguous clusters into a table |
| 817 | * |
| 818 | * @s: QED state |
| 819 | * @table: L2 table |
| 820 | * @index: First cluster index |
| 821 | * @n: Number of contiguous clusters |
Anthony Liguori | 21df65b | 2010-12-17 15:58:22 +0000 | [diff] [blame] | 822 | * @cluster: First cluster offset |
| 823 | * |
| 824 | * The cluster offset may be an allocated byte offset in the image file, the |
| 825 | * zero cluster marker, or the unallocated cluster marker. |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 826 | */ |
| 827 | static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index, |
| 828 | unsigned int n, uint64_t cluster) |
| 829 | { |
| 830 | int i; |
| 831 | for (i = index; i < index + n; i++) { |
| 832 | table->offsets[i] = cluster; |
Anthony Liguori | 21df65b | 2010-12-17 15:58:22 +0000 | [diff] [blame] | 833 | if (!qed_offset_is_unalloc_cluster(cluster) && |
| 834 | !qed_offset_is_zero_cluster(cluster)) { |
| 835 | cluster += s->header.cluster_size; |
| 836 | } |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
| 840 | static void qed_aio_complete_bh(void *opaque) |
| 841 | { |
| 842 | QEDAIOCB *acb = opaque; |
| 843 | BlockDriverCompletionFunc *cb = acb->common.cb; |
| 844 | void *user_opaque = acb->common.opaque; |
| 845 | int ret = acb->bh_ret; |
| 846 | bool *finished = acb->finished; |
| 847 | |
| 848 | qemu_bh_delete(acb->bh); |
| 849 | qemu_aio_release(acb); |
| 850 | |
| 851 | /* Invoke callback */ |
| 852 | cb(user_opaque, ret); |
| 853 | |
| 854 | /* Signal cancel completion */ |
| 855 | if (finished) { |
| 856 | *finished = true; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | static void qed_aio_complete(QEDAIOCB *acb, int ret) |
| 861 | { |
| 862 | BDRVQEDState *s = acb_to_s(acb); |
| 863 | |
| 864 | trace_qed_aio_complete(s, acb, ret); |
| 865 | |
| 866 | /* Free resources */ |
| 867 | qemu_iovec_destroy(&acb->cur_qiov); |
| 868 | qed_unref_l2_cache_entry(acb->request.l2_table); |
| 869 | |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 870 | /* Free the buffer we may have allocated for zero writes */ |
| 871 | if (acb->flags & QED_AIOCB_ZERO) { |
| 872 | qemu_vfree(acb->qiov->iov[0].iov_base); |
| 873 | acb->qiov->iov[0].iov_base = NULL; |
| 874 | } |
| 875 | |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 876 | /* Arrange for a bh to invoke the completion function */ |
| 877 | acb->bh_ret = ret; |
| 878 | acb->bh = qemu_bh_new(qed_aio_complete_bh, acb); |
| 879 | qemu_bh_schedule(acb->bh); |
| 880 | |
| 881 | /* Start next allocating write request waiting behind this one. Note that |
| 882 | * requests enqueue themselves when they first hit an unallocated cluster |
| 883 | * but they wait until the entire request is finished before waking up the |
| 884 | * next request in the queue. This ensures that we don't cycle through |
| 885 | * requests multiple times but rather finish one at a time completely. |
| 886 | */ |
| 887 | if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { |
| 888 | QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next); |
| 889 | acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); |
| 890 | if (acb) { |
| 891 | qed_aio_next_io(acb, 0); |
Stefan Hajnoczi | 6f321e9 | 2011-05-09 16:45:40 +0100 | [diff] [blame] | 892 | } else if (s->header.features & QED_F_NEED_CHECK) { |
| 893 | qed_start_need_check_timer(s); |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * Commit the current L2 table to the cache |
| 900 | */ |
| 901 | static void qed_commit_l2_update(void *opaque, int ret) |
| 902 | { |
| 903 | QEDAIOCB *acb = opaque; |
| 904 | BDRVQEDState *s = acb_to_s(acb); |
| 905 | CachedL2Table *l2_table = acb->request.l2_table; |
Stefan Hajnoczi | e4fc878 | 2011-09-30 11:39:11 +0100 | [diff] [blame] | 906 | uint64_t l2_offset = l2_table->offset; |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 907 | |
| 908 | qed_commit_l2_cache_entry(&s->l2_cache, l2_table); |
| 909 | |
| 910 | /* This is guaranteed to succeed because we just committed the entry to the |
| 911 | * cache. |
| 912 | */ |
Stefan Hajnoczi | e4fc878 | 2011-09-30 11:39:11 +0100 | [diff] [blame] | 913 | acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 914 | assert(acb->request.l2_table != NULL); |
| 915 | |
| 916 | qed_aio_next_io(opaque, ret); |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * Update L1 table with new L2 table offset and write it out |
| 921 | */ |
| 922 | static void qed_aio_write_l1_update(void *opaque, int ret) |
| 923 | { |
| 924 | QEDAIOCB *acb = opaque; |
| 925 | BDRVQEDState *s = acb_to_s(acb); |
| 926 | int index; |
| 927 | |
| 928 | if (ret) { |
| 929 | qed_aio_complete(acb, ret); |
| 930 | return; |
| 931 | } |
| 932 | |
| 933 | index = qed_l1_index(s, acb->cur_pos); |
| 934 | s->l1_table->offsets[index] = acb->request.l2_table->offset; |
| 935 | |
| 936 | qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb); |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Update L2 table with new cluster offsets and write them out |
| 941 | */ |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 942 | static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset) |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 943 | { |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 944 | BDRVQEDState *s = acb_to_s(acb); |
| 945 | bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; |
| 946 | int index; |
| 947 | |
| 948 | if (ret) { |
| 949 | goto err; |
| 950 | } |
| 951 | |
| 952 | if (need_alloc) { |
| 953 | qed_unref_l2_cache_entry(acb->request.l2_table); |
| 954 | acb->request.l2_table = qed_new_l2_table(s); |
| 955 | } |
| 956 | |
| 957 | index = qed_l2_index(s, acb->cur_pos); |
| 958 | qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 959 | offset); |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 960 | |
| 961 | if (need_alloc) { |
| 962 | /* Write out the whole new L2 table */ |
| 963 | qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true, |
| 964 | qed_aio_write_l1_update, acb); |
| 965 | } else { |
| 966 | /* Write out only the updated part of the L2 table */ |
| 967 | qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false, |
| 968 | qed_aio_next_io, acb); |
| 969 | } |
| 970 | return; |
| 971 | |
| 972 | err: |
| 973 | qed_aio_complete(acb, ret); |
| 974 | } |
| 975 | |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 976 | static void qed_aio_write_l2_update_cb(void *opaque, int ret) |
| 977 | { |
| 978 | QEDAIOCB *acb = opaque; |
| 979 | qed_aio_write_l2_update(acb, ret, acb->cur_cluster); |
| 980 | } |
| 981 | |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 982 | /** |
| 983 | * Flush new data clusters before updating the L2 table |
| 984 | * |
| 985 | * This flush is necessary when a backing file is in use. A crash during an |
| 986 | * allocating write could result in empty clusters in the image. If the write |
| 987 | * only touched a subregion of the cluster, then backing image sectors have |
| 988 | * been lost in the untouched region. The solution is to flush after writing a |
| 989 | * new data cluster and before updating the L2 table. |
| 990 | */ |
| 991 | static void qed_aio_write_flush_before_l2_update(void *opaque, int ret) |
| 992 | { |
| 993 | QEDAIOCB *acb = opaque; |
| 994 | BDRVQEDState *s = acb_to_s(acb); |
| 995 | |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 996 | if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update_cb, opaque)) { |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 997 | qed_aio_complete(acb, -EIO); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | /** |
| 1002 | * Write data to the image file |
| 1003 | */ |
| 1004 | static void qed_aio_write_main(void *opaque, int ret) |
| 1005 | { |
| 1006 | QEDAIOCB *acb = opaque; |
| 1007 | BDRVQEDState *s = acb_to_s(acb); |
| 1008 | uint64_t offset = acb->cur_cluster + |
| 1009 | qed_offset_into_cluster(s, acb->cur_pos); |
| 1010 | BlockDriverCompletionFunc *next_fn; |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1011 | |
| 1012 | trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size); |
| 1013 | |
| 1014 | if (ret) { |
| 1015 | qed_aio_complete(acb, ret); |
| 1016 | return; |
| 1017 | } |
| 1018 | |
| 1019 | if (acb->find_cluster_ret == QED_CLUSTER_FOUND) { |
| 1020 | next_fn = qed_aio_next_io; |
| 1021 | } else { |
| 1022 | if (s->bs->backing_hd) { |
| 1023 | next_fn = qed_aio_write_flush_before_l2_update; |
| 1024 | } else { |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 1025 | next_fn = qed_aio_write_l2_update_cb; |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 1030 | bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE, |
| 1031 | &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, |
| 1032 | next_fn, acb); |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /** |
| 1036 | * Populate back untouched region of new data cluster |
| 1037 | */ |
| 1038 | static void qed_aio_write_postfill(void *opaque, int ret) |
| 1039 | { |
| 1040 | QEDAIOCB *acb = opaque; |
| 1041 | BDRVQEDState *s = acb_to_s(acb); |
| 1042 | uint64_t start = acb->cur_pos + acb->cur_qiov.size; |
| 1043 | uint64_t len = |
| 1044 | qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; |
| 1045 | uint64_t offset = acb->cur_cluster + |
| 1046 | qed_offset_into_cluster(s, acb->cur_pos) + |
| 1047 | acb->cur_qiov.size; |
| 1048 | |
| 1049 | if (ret) { |
| 1050 | qed_aio_complete(acb, ret); |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | trace_qed_aio_write_postfill(s, acb, start, len, offset); |
| 1055 | qed_copy_from_backing_file(s, start, len, offset, |
| 1056 | qed_aio_write_main, acb); |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * Populate front untouched region of new data cluster |
| 1061 | */ |
| 1062 | static void qed_aio_write_prefill(void *opaque, int ret) |
| 1063 | { |
| 1064 | QEDAIOCB *acb = opaque; |
| 1065 | BDRVQEDState *s = acb_to_s(acb); |
| 1066 | uint64_t start = qed_start_of_cluster(s, acb->cur_pos); |
| 1067 | uint64_t len = qed_offset_into_cluster(s, acb->cur_pos); |
| 1068 | |
| 1069 | trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); |
| 1070 | qed_copy_from_backing_file(s, start, len, acb->cur_cluster, |
| 1071 | qed_aio_write_postfill, acb); |
| 1072 | } |
| 1073 | |
| 1074 | /** |
Stefan Hajnoczi | 0d09c79 | 2011-01-28 17:11:59 +0000 | [diff] [blame] | 1075 | * Check if the QED_F_NEED_CHECK bit should be set during allocating write |
| 1076 | */ |
| 1077 | static bool qed_should_set_need_check(BDRVQEDState *s) |
| 1078 | { |
| 1079 | /* The flush before L2 update path ensures consistency */ |
| 1080 | if (s->bs->backing_hd) { |
| 1081 | return false; |
| 1082 | } |
| 1083 | |
| 1084 | return !(s->header.features & QED_F_NEED_CHECK); |
| 1085 | } |
| 1086 | |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 1087 | static void qed_aio_write_zero_cluster(void *opaque, int ret) |
| 1088 | { |
| 1089 | QEDAIOCB *acb = opaque; |
| 1090 | |
| 1091 | if (ret) { |
| 1092 | qed_aio_complete(acb, ret); |
| 1093 | return; |
| 1094 | } |
| 1095 | |
| 1096 | qed_aio_write_l2_update(acb, 0, 1); |
| 1097 | } |
| 1098 | |
Stefan Hajnoczi | 0d09c79 | 2011-01-28 17:11:59 +0000 | [diff] [blame] | 1099 | /** |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1100 | * Write new data cluster |
| 1101 | * |
| 1102 | * @acb: Write request |
| 1103 | * @len: Length in bytes |
| 1104 | * |
| 1105 | * This path is taken when writing to previously unallocated clusters. |
| 1106 | */ |
| 1107 | static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) |
| 1108 | { |
| 1109 | BDRVQEDState *s = acb_to_s(acb); |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 1110 | BlockDriverCompletionFunc *cb; |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1111 | |
Stefan Hajnoczi | 6f321e9 | 2011-05-09 16:45:40 +0100 | [diff] [blame] | 1112 | /* Cancel timer when the first allocating request comes in */ |
| 1113 | if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) { |
| 1114 | qed_cancel_need_check_timer(s); |
| 1115 | } |
| 1116 | |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1117 | /* Freeze this request if another allocating write is in progress */ |
| 1118 | if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { |
| 1119 | QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next); |
| 1120 | } |
Stefan Hajnoczi | 6f321e9 | 2011-05-09 16:45:40 +0100 | [diff] [blame] | 1121 | if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) || |
| 1122 | s->allocating_write_reqs_plugged) { |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1123 | return; /* wait for existing request to finish */ |
| 1124 | } |
| 1125 | |
| 1126 | acb->cur_nclusters = qed_bytes_to_clusters(s, |
| 1127 | qed_offset_into_cluster(s, acb->cur_pos) + len); |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1128 | qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
| 1129 | |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 1130 | if (acb->flags & QED_AIOCB_ZERO) { |
| 1131 | /* Skip ahead if the clusters are already zero */ |
| 1132 | if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { |
| 1133 | qed_aio_next_io(acb, 0); |
| 1134 | return; |
| 1135 | } |
| 1136 | |
| 1137 | cb = qed_aio_write_zero_cluster; |
| 1138 | } else { |
| 1139 | cb = qed_aio_write_prefill; |
| 1140 | acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); |
| 1141 | } |
| 1142 | |
Stefan Hajnoczi | 0d09c79 | 2011-01-28 17:11:59 +0000 | [diff] [blame] | 1143 | if (qed_should_set_need_check(s)) { |
| 1144 | s->header.features |= QED_F_NEED_CHECK; |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 1145 | qed_write_header(s, cb, acb); |
Stefan Hajnoczi | 0d09c79 | 2011-01-28 17:11:59 +0000 | [diff] [blame] | 1146 | } else { |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 1147 | cb(acb, 0); |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 1148 | } |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * Write data cluster in place |
| 1153 | * |
| 1154 | * @acb: Write request |
| 1155 | * @offset: Cluster offset in bytes |
| 1156 | * @len: Length in bytes |
| 1157 | * |
| 1158 | * This path is taken when writing to already allocated clusters. |
| 1159 | */ |
| 1160 | static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) |
| 1161 | { |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 1162 | /* Allocate buffer for zero writes */ |
| 1163 | if (acb->flags & QED_AIOCB_ZERO) { |
| 1164 | struct iovec *iov = acb->qiov->iov; |
| 1165 | |
| 1166 | if (!iov->iov_base) { |
| 1167 | iov->iov_base = qemu_blockalign(acb->common.bs, iov->iov_len); |
| 1168 | memset(iov->iov_base, 0, iov->iov_len); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1172 | /* Calculate the I/O vector */ |
| 1173 | acb->cur_cluster = offset; |
| 1174 | qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
| 1175 | |
| 1176 | /* Do the actual write */ |
| 1177 | qed_aio_write_main(acb, 0); |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * Write data cluster |
| 1182 | * |
| 1183 | * @opaque: Write request |
| 1184 | * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, |
| 1185 | * or -errno |
| 1186 | * @offset: Cluster offset in bytes |
| 1187 | * @len: Length in bytes |
| 1188 | * |
| 1189 | * Callback from qed_find_cluster(). |
| 1190 | */ |
| 1191 | static void qed_aio_write_data(void *opaque, int ret, |
| 1192 | uint64_t offset, size_t len) |
| 1193 | { |
| 1194 | QEDAIOCB *acb = opaque; |
| 1195 | |
| 1196 | trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); |
| 1197 | |
| 1198 | acb->find_cluster_ret = ret; |
| 1199 | |
| 1200 | switch (ret) { |
| 1201 | case QED_CLUSTER_FOUND: |
| 1202 | qed_aio_write_inplace(acb, offset, len); |
| 1203 | break; |
| 1204 | |
| 1205 | case QED_CLUSTER_L2: |
| 1206 | case QED_CLUSTER_L1: |
Anthony Liguori | 21df65b | 2010-12-17 15:58:22 +0000 | [diff] [blame] | 1207 | case QED_CLUSTER_ZERO: |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1208 | qed_aio_write_alloc(acb, len); |
| 1209 | break; |
| 1210 | |
| 1211 | default: |
| 1212 | qed_aio_complete(acb, ret); |
| 1213 | break; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | /** |
| 1218 | * Read data cluster |
| 1219 | * |
| 1220 | * @opaque: Read request |
| 1221 | * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, |
| 1222 | * or -errno |
| 1223 | * @offset: Cluster offset in bytes |
| 1224 | * @len: Length in bytes |
| 1225 | * |
| 1226 | * Callback from qed_find_cluster(). |
| 1227 | */ |
| 1228 | static void qed_aio_read_data(void *opaque, int ret, |
| 1229 | uint64_t offset, size_t len) |
| 1230 | { |
| 1231 | QEDAIOCB *acb = opaque; |
| 1232 | BDRVQEDState *s = acb_to_s(acb); |
| 1233 | BlockDriverState *bs = acb->common.bs; |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1234 | |
| 1235 | /* Adjust offset into cluster */ |
| 1236 | offset += qed_offset_into_cluster(s, acb->cur_pos); |
| 1237 | |
| 1238 | trace_qed_aio_read_data(s, acb, ret, offset, len); |
| 1239 | |
| 1240 | if (ret < 0) { |
| 1241 | goto err; |
| 1242 | } |
| 1243 | |
| 1244 | qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
| 1245 | |
Anthony Liguori | 21df65b | 2010-12-17 15:58:22 +0000 | [diff] [blame] | 1246 | /* Handle zero cluster and backing file reads */ |
| 1247 | if (ret == QED_CLUSTER_ZERO) { |
| 1248 | qemu_iovec_memset(&acb->cur_qiov, 0, acb->cur_qiov.size); |
| 1249 | qed_aio_next_io(acb, 0); |
| 1250 | return; |
| 1251 | } else if (ret != QED_CLUSTER_FOUND) { |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1252 | qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, |
| 1253 | qed_aio_next_io, acb); |
| 1254 | return; |
| 1255 | } |
| 1256 | |
| 1257 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 1258 | bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE, |
| 1259 | &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, |
| 1260 | qed_aio_next_io, acb); |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1261 | return; |
| 1262 | |
| 1263 | err: |
| 1264 | qed_aio_complete(acb, ret); |
| 1265 | } |
| 1266 | |
| 1267 | /** |
| 1268 | * Begin next I/O or complete the request |
| 1269 | */ |
| 1270 | static void qed_aio_next_io(void *opaque, int ret) |
| 1271 | { |
| 1272 | QEDAIOCB *acb = opaque; |
| 1273 | BDRVQEDState *s = acb_to_s(acb); |
Stefan Hajnoczi | 6e4f59b | 2012-02-07 13:27:27 +0000 | [diff] [blame] | 1274 | QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ? |
| 1275 | qed_aio_write_data : qed_aio_read_data; |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1276 | |
| 1277 | trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size); |
| 1278 | |
| 1279 | /* Handle I/O error */ |
| 1280 | if (ret) { |
| 1281 | qed_aio_complete(acb, ret); |
| 1282 | return; |
| 1283 | } |
| 1284 | |
| 1285 | acb->qiov_offset += acb->cur_qiov.size; |
| 1286 | acb->cur_pos += acb->cur_qiov.size; |
| 1287 | qemu_iovec_reset(&acb->cur_qiov); |
| 1288 | |
| 1289 | /* Complete request */ |
| 1290 | if (acb->cur_pos >= acb->end_pos) { |
| 1291 | qed_aio_complete(acb, 0); |
| 1292 | return; |
| 1293 | } |
| 1294 | |
| 1295 | /* Find next cluster and start I/O */ |
| 1296 | qed_find_cluster(s, &acb->request, |
| 1297 | acb->cur_pos, acb->end_pos - acb->cur_pos, |
| 1298 | io_fn, acb); |
| 1299 | } |
| 1300 | |
| 1301 | static BlockDriverAIOCB *qed_aio_setup(BlockDriverState *bs, |
| 1302 | int64_t sector_num, |
| 1303 | QEMUIOVector *qiov, int nb_sectors, |
| 1304 | BlockDriverCompletionFunc *cb, |
Stefan Hajnoczi | 6e4f59b | 2012-02-07 13:27:27 +0000 | [diff] [blame] | 1305 | void *opaque, int flags) |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1306 | { |
| 1307 | QEDAIOCB *acb = qemu_aio_get(&qed_aio_pool, bs, cb, opaque); |
| 1308 | |
| 1309 | trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors, |
Stefan Hajnoczi | 6e4f59b | 2012-02-07 13:27:27 +0000 | [diff] [blame] | 1310 | opaque, flags); |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1311 | |
Stefan Hajnoczi | 6e4f59b | 2012-02-07 13:27:27 +0000 | [diff] [blame] | 1312 | acb->flags = flags; |
Stefan Hajnoczi | eabba58 | 2010-12-06 16:08:02 +0000 | [diff] [blame] | 1313 | acb->finished = NULL; |
| 1314 | acb->qiov = qiov; |
| 1315 | acb->qiov_offset = 0; |
| 1316 | acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; |
| 1317 | acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE; |
| 1318 | acb->request.l2_table = NULL; |
| 1319 | qemu_iovec_init(&acb->cur_qiov, qiov->niov); |
| 1320 | |
| 1321 | /* Start request */ |
| 1322 | qed_aio_next_io(acb, 0); |
| 1323 | return &acb->common; |
| 1324 | } |
| 1325 | |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1326 | static BlockDriverAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs, |
| 1327 | int64_t sector_num, |
| 1328 | QEMUIOVector *qiov, int nb_sectors, |
| 1329 | BlockDriverCompletionFunc *cb, |
| 1330 | void *opaque) |
| 1331 | { |
Stefan Hajnoczi | 6e4f59b | 2012-02-07 13:27:27 +0000 | [diff] [blame] | 1332 | return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | static BlockDriverAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs, |
| 1336 | int64_t sector_num, |
| 1337 | QEMUIOVector *qiov, int nb_sectors, |
| 1338 | BlockDriverCompletionFunc *cb, |
| 1339 | void *opaque) |
| 1340 | { |
Stefan Hajnoczi | 6e4f59b | 2012-02-07 13:27:27 +0000 | [diff] [blame] | 1341 | return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, |
| 1342 | opaque, QED_AIOCB_WRITE); |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 1345 | typedef struct { |
| 1346 | Coroutine *co; |
| 1347 | int ret; |
| 1348 | bool done; |
| 1349 | } QEDWriteZeroesCB; |
| 1350 | |
| 1351 | static void coroutine_fn qed_co_write_zeroes_cb(void *opaque, int ret) |
| 1352 | { |
| 1353 | QEDWriteZeroesCB *cb = opaque; |
| 1354 | |
| 1355 | cb->done = true; |
| 1356 | cb->ret = ret; |
| 1357 | if (cb->co) { |
| 1358 | qemu_coroutine_enter(cb->co, NULL); |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | static int coroutine_fn bdrv_qed_co_write_zeroes(BlockDriverState *bs, |
| 1363 | int64_t sector_num, |
| 1364 | int nb_sectors) |
| 1365 | { |
| 1366 | BlockDriverAIOCB *blockacb; |
| 1367 | QEDWriteZeroesCB cb = { .done = false }; |
| 1368 | QEMUIOVector qiov; |
| 1369 | struct iovec iov; |
| 1370 | |
| 1371 | /* Zero writes start without an I/O buffer. If a buffer becomes necessary |
| 1372 | * then it will be allocated during request processing. |
| 1373 | */ |
| 1374 | iov.iov_base = NULL, |
| 1375 | iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE, |
| 1376 | |
| 1377 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 1378 | blockacb = qed_aio_setup(bs, sector_num, &qiov, nb_sectors, |
| 1379 | qed_co_write_zeroes_cb, &cb, |
| 1380 | QED_AIOCB_WRITE | QED_AIOCB_ZERO); |
| 1381 | if (!blockacb) { |
| 1382 | return -EIO; |
| 1383 | } |
| 1384 | if (!cb.done) { |
| 1385 | cb.co = qemu_coroutine_self(); |
| 1386 | qemu_coroutine_yield(); |
| 1387 | } |
| 1388 | assert(cb.done); |
| 1389 | return cb.ret; |
| 1390 | } |
| 1391 | |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1392 | static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset) |
| 1393 | { |
Stefan Hajnoczi | 77a5a00 | 2011-05-16 13:56:53 +0100 | [diff] [blame] | 1394 | BDRVQEDState *s = bs->opaque; |
| 1395 | uint64_t old_image_size; |
| 1396 | int ret; |
| 1397 | |
| 1398 | if (!qed_is_image_size_valid(offset, s->header.cluster_size, |
| 1399 | s->header.table_size)) { |
| 1400 | return -EINVAL; |
| 1401 | } |
| 1402 | |
| 1403 | /* Shrinking is currently not supported */ |
| 1404 | if ((uint64_t)offset < s->header.image_size) { |
| 1405 | return -ENOTSUP; |
| 1406 | } |
| 1407 | |
| 1408 | old_image_size = s->header.image_size; |
| 1409 | s->header.image_size = offset; |
| 1410 | ret = qed_write_header_sync(s); |
| 1411 | if (ret < 0) { |
| 1412 | s->header.image_size = old_image_size; |
| 1413 | } |
| 1414 | return ret; |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | static int64_t bdrv_qed_getlength(BlockDriverState *bs) |
| 1418 | { |
| 1419 | BDRVQEDState *s = bs->opaque; |
| 1420 | return s->header.image_size; |
| 1421 | } |
| 1422 | |
| 1423 | static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 1424 | { |
| 1425 | BDRVQEDState *s = bs->opaque; |
| 1426 | |
| 1427 | memset(bdi, 0, sizeof(*bdi)); |
| 1428 | bdi->cluster_size = s->header.cluster_size; |
Dong Xu Wang | d68dbee | 2012-03-15 20:13:34 +0800 | [diff] [blame] | 1429 | bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1430 | return 0; |
| 1431 | } |
| 1432 | |
| 1433 | static int bdrv_qed_change_backing_file(BlockDriverState *bs, |
| 1434 | const char *backing_file, |
| 1435 | const char *backing_fmt) |
| 1436 | { |
| 1437 | BDRVQEDState *s = bs->opaque; |
| 1438 | QEDHeader new_header, le_header; |
| 1439 | void *buffer; |
| 1440 | size_t buffer_len, backing_file_len; |
| 1441 | int ret; |
| 1442 | |
| 1443 | /* Refuse to set backing filename if unknown compat feature bits are |
| 1444 | * active. If the image uses an unknown compat feature then we may not |
| 1445 | * know the layout of data following the header structure and cannot safely |
| 1446 | * add a new string. |
| 1447 | */ |
| 1448 | if (backing_file && (s->header.compat_features & |
| 1449 | ~QED_COMPAT_FEATURE_MASK)) { |
| 1450 | return -ENOTSUP; |
| 1451 | } |
| 1452 | |
| 1453 | memcpy(&new_header, &s->header, sizeof(new_header)); |
| 1454 | |
| 1455 | new_header.features &= ~(QED_F_BACKING_FILE | |
| 1456 | QED_F_BACKING_FORMAT_NO_PROBE); |
| 1457 | |
| 1458 | /* Adjust feature flags */ |
| 1459 | if (backing_file) { |
| 1460 | new_header.features |= QED_F_BACKING_FILE; |
| 1461 | |
| 1462 | if (qed_fmt_is_raw(backing_fmt)) { |
| 1463 | new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | /* Calculate new header size */ |
| 1468 | backing_file_len = 0; |
| 1469 | |
| 1470 | if (backing_file) { |
| 1471 | backing_file_len = strlen(backing_file); |
| 1472 | } |
| 1473 | |
| 1474 | buffer_len = sizeof(new_header); |
| 1475 | new_header.backing_filename_offset = buffer_len; |
| 1476 | new_header.backing_filename_size = backing_file_len; |
| 1477 | buffer_len += backing_file_len; |
| 1478 | |
| 1479 | /* Make sure we can rewrite header without failing */ |
| 1480 | if (buffer_len > new_header.header_size * new_header.cluster_size) { |
| 1481 | return -ENOSPC; |
| 1482 | } |
| 1483 | |
| 1484 | /* Prepare new header */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1485 | buffer = g_malloc(buffer_len); |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1486 | |
| 1487 | qed_header_cpu_to_le(&new_header, &le_header); |
| 1488 | memcpy(buffer, &le_header, sizeof(le_header)); |
| 1489 | buffer_len = sizeof(le_header); |
| 1490 | |
Pavel Borzenkov | feba23b | 2011-10-18 21:17:35 +0400 | [diff] [blame] | 1491 | if (backing_file) { |
| 1492 | memcpy(buffer + buffer_len, backing_file, backing_file_len); |
| 1493 | buffer_len += backing_file_len; |
| 1494 | } |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1495 | |
| 1496 | /* Write new header */ |
| 1497 | ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1498 | g_free(buffer); |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1499 | if (ret == 0) { |
| 1500 | memcpy(&s->header, &new_header, sizeof(new_header)); |
| 1501 | } |
| 1502 | return ret; |
| 1503 | } |
| 1504 | |
Benoît Canet | c82954e | 2012-03-23 08:36:51 +0100 | [diff] [blame] | 1505 | static void bdrv_qed_invalidate_cache(BlockDriverState *bs) |
| 1506 | { |
| 1507 | BDRVQEDState *s = bs->opaque; |
| 1508 | |
| 1509 | bdrv_qed_close(bs); |
| 1510 | memset(s, 0, sizeof(BDRVQEDState)); |
| 1511 | bdrv_qed_open(bs, bs->open_flags); |
| 1512 | } |
| 1513 | |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1514 | static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result) |
| 1515 | { |
Stefan Hajnoczi | 01979a9 | 2010-12-06 16:08:03 +0000 | [diff] [blame] | 1516 | BDRVQEDState *s = bs->opaque; |
| 1517 | |
| 1518 | return qed_check(s, result, false); |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | static QEMUOptionParameter qed_create_options[] = { |
| 1522 | { |
| 1523 | .name = BLOCK_OPT_SIZE, |
| 1524 | .type = OPT_SIZE, |
| 1525 | .help = "Virtual disk size (in bytes)" |
| 1526 | }, { |
| 1527 | .name = BLOCK_OPT_BACKING_FILE, |
| 1528 | .type = OPT_STRING, |
| 1529 | .help = "File name of a base image" |
| 1530 | }, { |
| 1531 | .name = BLOCK_OPT_BACKING_FMT, |
| 1532 | .type = OPT_STRING, |
| 1533 | .help = "Image format of the base image" |
| 1534 | }, { |
| 1535 | .name = BLOCK_OPT_CLUSTER_SIZE, |
| 1536 | .type = OPT_SIZE, |
Kevin Wolf | 99cce9f | 2011-05-31 15:01:46 +0200 | [diff] [blame] | 1537 | .help = "Cluster size (in bytes)", |
| 1538 | .value = { .n = QED_DEFAULT_CLUSTER_SIZE }, |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1539 | }, { |
| 1540 | .name = BLOCK_OPT_TABLE_SIZE, |
| 1541 | .type = OPT_SIZE, |
| 1542 | .help = "L1/L2 table size (in clusters)" |
| 1543 | }, |
| 1544 | { /* end of list */ } |
| 1545 | }; |
| 1546 | |
| 1547 | static BlockDriver bdrv_qed = { |
| 1548 | .format_name = "qed", |
| 1549 | .instance_size = sizeof(BDRVQEDState), |
| 1550 | .create_options = qed_create_options, |
| 1551 | |
| 1552 | .bdrv_probe = bdrv_qed_probe, |
| 1553 | .bdrv_open = bdrv_qed_open, |
| 1554 | .bdrv_close = bdrv_qed_close, |
| 1555 | .bdrv_create = bdrv_qed_create, |
Stefan Hajnoczi | b7d5a5b | 2011-11-14 12:44:20 +0000 | [diff] [blame] | 1556 | .bdrv_co_is_allocated = bdrv_qed_co_is_allocated, |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1557 | .bdrv_make_empty = bdrv_qed_make_empty, |
| 1558 | .bdrv_aio_readv = bdrv_qed_aio_readv, |
| 1559 | .bdrv_aio_writev = bdrv_qed_aio_writev, |
Stefan Hajnoczi | 0e71be1 | 2012-02-07 13:27:28 +0000 | [diff] [blame] | 1560 | .bdrv_co_write_zeroes = bdrv_qed_co_write_zeroes, |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1561 | .bdrv_truncate = bdrv_qed_truncate, |
| 1562 | .bdrv_getlength = bdrv_qed_getlength, |
| 1563 | .bdrv_get_info = bdrv_qed_get_info, |
| 1564 | .bdrv_change_backing_file = bdrv_qed_change_backing_file, |
Benoît Canet | c82954e | 2012-03-23 08:36:51 +0100 | [diff] [blame] | 1565 | .bdrv_invalidate_cache = bdrv_qed_invalidate_cache, |
Stefan Hajnoczi | 75411d2 | 2010-12-06 16:08:00 +0000 | [diff] [blame] | 1566 | .bdrv_check = bdrv_qed_check, |
| 1567 | }; |
| 1568 | |
| 1569 | static void bdrv_qed_init(void) |
| 1570 | { |
| 1571 | bdrv_register(&bdrv_qed); |
| 1572 | } |
| 1573 | |
| 1574 | block_init(bdrv_qed_init); |