| 1 | /* |
| 2 | * Block layer I/O functions |
| 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "qemu/osdep.h" |
| 26 | #include "trace.h" |
| 27 | #include "system/block-backend.h" |
| 28 | #include "qemu/aio-wait.h" |
| 29 | #include "block/blockjob.h" |
| 30 | #include "block/blockjob_int.h" |
| 31 | #include "block/block_int.h" |
| 32 | #include "block/coroutines.h" |
| 33 | #include "block/dirty-bitmap.h" |
| 34 | #include "block/write-threshold.h" |
| 35 | #include "qemu/cutils.h" |
| 36 | #include "qemu/memalign.h" |
| 37 | #include "qapi/error.h" |
| 38 | #include "qemu/error-report.h" |
| 39 | #include "qemu/main-loop.h" |
| 40 | #include "system/replay.h" |
| 41 | #include "qemu/units.h" |
| 42 | #include "qemu/atomic.h" |
| 43 | |
| 44 | /* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */ |
| 45 | #define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS) |
| 46 | |
| 47 | /* Maximum read size for checking if data reads as zero, in bytes */ |
| 48 | #define MAX_ZERO_CHECK_BUFFER (128 * KiB) |
| 49 | |
| 50 | static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs, |
| 51 | int64_t offset, int64_t bytes, BdrvRequestFlags flags); |
| 52 | |
| 53 | static void GRAPH_RDLOCK |
| 54 | bdrv_parent_drained_begin(BlockDriverState *bs, BdrvChild *ignore) |
| 55 | { |
| 56 | BdrvChild *c, *next; |
| 57 | IO_OR_GS_CODE(); |
| 58 | assert_bdrv_graph_readable(); |
| 59 | |
| 60 | QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) { |
| 61 | if (c == ignore) { |
| 62 | continue; |
| 63 | } |
| 64 | bdrv_parent_drained_begin_single(c); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void bdrv_parent_drained_end_single(BdrvChild *c) |
| 69 | { |
| 70 | GLOBAL_STATE_CODE(); |
| 71 | |
| 72 | assert(c->quiesced_parent); |
| 73 | c->quiesced_parent = false; |
| 74 | |
| 75 | if (c->klass->drained_end) { |
| 76 | c->klass->drained_end(c); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static void GRAPH_RDLOCK |
| 81 | bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore) |
| 82 | { |
| 83 | BdrvChild *c; |
| 84 | IO_OR_GS_CODE(); |
| 85 | assert_bdrv_graph_readable(); |
| 86 | |
| 87 | QLIST_FOREACH(c, &bs->parents, next_parent) { |
| 88 | if (c == ignore) { |
| 89 | continue; |
| 90 | } |
| 91 | bdrv_parent_drained_end_single(c); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | bool bdrv_parent_drained_poll_single(BdrvChild *c) |
| 96 | { |
| 97 | IO_OR_GS_CODE(); |
| 98 | |
| 99 | if (c->klass->drained_poll) { |
| 100 | return c->klass->drained_poll(c); |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | static bool GRAPH_RDLOCK |
| 106 | bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore, |
| 107 | bool ignore_bds_parents) |
| 108 | { |
| 109 | BdrvChild *c, *next; |
| 110 | bool busy = false; |
| 111 | IO_OR_GS_CODE(); |
| 112 | assert_bdrv_graph_readable(); |
| 113 | |
| 114 | QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) { |
| 115 | if (c == ignore || (ignore_bds_parents && c->klass->parent_is_bds)) { |
| 116 | continue; |
| 117 | } |
| 118 | busy |= bdrv_parent_drained_poll_single(c); |
| 119 | } |
| 120 | |
| 121 | return busy; |
| 122 | } |
| 123 | |
| 124 | void bdrv_parent_drained_begin_single(BdrvChild *c) |
| 125 | { |
| 126 | GLOBAL_STATE_CODE(); |
| 127 | |
| 128 | assert(!c->quiesced_parent); |
| 129 | c->quiesced_parent = true; |
| 130 | |
| 131 | if (c->klass->drained_begin) { |
| 132 | /* called with rdlock taken, but it doesn't really need it. */ |
| 133 | c->klass->drained_begin(c); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void bdrv_merge_limits(BlockLimits *dst, const BlockLimits *src) |
| 138 | { |
| 139 | dst->pdiscard_alignment = MAX(dst->pdiscard_alignment, |
| 140 | src->pdiscard_alignment); |
| 141 | dst->opt_transfer = MAX(dst->opt_transfer, src->opt_transfer); |
| 142 | dst->max_transfer = MIN_NON_ZERO(dst->max_transfer, src->max_transfer); |
| 143 | dst->max_hw_transfer = MIN_NON_ZERO(dst->max_hw_transfer, |
| 144 | src->max_hw_transfer); |
| 145 | dst->opt_mem_alignment = MAX(dst->opt_mem_alignment, |
| 146 | src->opt_mem_alignment); |
| 147 | dst->min_mem_alignment = MAX(dst->min_mem_alignment, |
| 148 | src->min_mem_alignment); |
| 149 | dst->max_iov = MIN_NON_ZERO(dst->max_iov, src->max_iov); |
| 150 | dst->max_hw_iov = MIN_NON_ZERO(dst->max_hw_iov, src->max_hw_iov); |
| 151 | } |
| 152 | |
| 153 | typedef struct BdrvRefreshLimitsState { |
| 154 | BlockDriverState *bs; |
| 155 | BlockLimits old_bl; |
| 156 | } BdrvRefreshLimitsState; |
| 157 | |
| 158 | static void bdrv_refresh_limits_abort(void *opaque) |
| 159 | { |
| 160 | BdrvRefreshLimitsState *s = opaque; |
| 161 | |
| 162 | s->bs->bl = s->old_bl; |
| 163 | } |
| 164 | |
| 165 | static TransactionActionDrv bdrv_refresh_limits_drv = { |
| 166 | .abort = bdrv_refresh_limits_abort, |
| 167 | .clean = g_free, |
| 168 | }; |
| 169 | |
| 170 | /* @tran is allowed to be NULL, in this case no rollback is possible. */ |
| 171 | void bdrv_refresh_limits(BlockDriverState *bs, Transaction *tran, Error **errp) |
| 172 | { |
| 173 | ERRP_GUARD(); |
| 174 | BlockDriver *drv = bs->drv; |
| 175 | BdrvChild *c; |
| 176 | bool have_limits; |
| 177 | |
| 178 | GLOBAL_STATE_CODE(); |
| 179 | |
| 180 | if (tran) { |
| 181 | BdrvRefreshLimitsState *s = g_new(BdrvRefreshLimitsState, 1); |
| 182 | *s = (BdrvRefreshLimitsState) { |
| 183 | .bs = bs, |
| 184 | .old_bl = bs->bl, |
| 185 | }; |
| 186 | tran_add(tran, &bdrv_refresh_limits_drv, s); |
| 187 | } |
| 188 | |
| 189 | memset(&bs->bl, 0, sizeof(bs->bl)); |
| 190 | |
| 191 | if (!drv) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | /* Default alignment based on whether driver has byte interface */ |
| 196 | bs->bl.request_alignment = (drv->bdrv_co_preadv || |
| 197 | drv->bdrv_aio_preadv || |
| 198 | drv->bdrv_co_preadv_part) ? 1 : 512; |
| 199 | |
| 200 | /* Take some limits from the children as a default */ |
| 201 | have_limits = false; |
| 202 | QLIST_FOREACH(c, &bs->children, next) { |
| 203 | if (c->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED | BDRV_CHILD_COW)) |
| 204 | { |
| 205 | bdrv_merge_limits(&bs->bl, &c->bs->bl); |
| 206 | have_limits = true; |
| 207 | } |
| 208 | |
| 209 | if (c->role & BDRV_CHILD_FILTERED) { |
| 210 | bs->bl.has_variable_length |= c->bs->bl.has_variable_length; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (!have_limits) { |
| 215 | bs->bl.min_mem_alignment = 512; |
| 216 | bs->bl.opt_mem_alignment = qemu_real_host_page_size(); |
| 217 | |
| 218 | /* Safe default since most protocols use readv()/writev()/etc */ |
| 219 | bs->bl.max_iov = IOV_MAX; |
| 220 | } |
| 221 | |
| 222 | /* Then let the driver override it */ |
| 223 | if (drv->bdrv_refresh_limits) { |
| 224 | drv->bdrv_refresh_limits(bs, errp); |
| 225 | if (*errp) { |
| 226 | return; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (bs->bl.request_alignment > BDRV_MAX_ALIGNMENT) { |
| 231 | error_setg(errp, "Driver requires too large request alignment"); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * The copy-on-read flag is actually a reference count so multiple users may |
| 237 | * use the feature without worrying about clobbering its previous state. |
| 238 | * Copy-on-read stays enabled until all users have called to disable it. |
| 239 | */ |
| 240 | void bdrv_enable_copy_on_read(BlockDriverState *bs) |
| 241 | { |
| 242 | IO_CODE(); |
| 243 | qatomic_inc(&bs->copy_on_read); |
| 244 | } |
| 245 | |
| 246 | void bdrv_disable_copy_on_read(BlockDriverState *bs) |
| 247 | { |
| 248 | int old = qatomic_fetch_dec(&bs->copy_on_read); |
| 249 | IO_CODE(); |
| 250 | assert(old >= 1); |
| 251 | } |
| 252 | |
| 253 | typedef struct { |
| 254 | Coroutine *co; |
| 255 | BlockDriverState *bs; |
| 256 | bool done; |
| 257 | bool begin; |
| 258 | bool poll; |
| 259 | BdrvChild *parent; |
| 260 | } BdrvCoDrainData; |
| 261 | |
| 262 | /* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */ |
| 263 | bool bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent, |
| 264 | bool ignore_bds_parents) |
| 265 | { |
| 266 | GLOBAL_STATE_CODE(); |
| 267 | |
| 268 | if (bdrv_parent_drained_poll(bs, ignore_parent, ignore_bds_parents)) { |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | if (qatomic_read(&bs->in_flight)) { |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | static bool bdrv_drain_poll_top_level(BlockDriverState *bs, |
| 280 | BdrvChild *ignore_parent) |
| 281 | { |
| 282 | GLOBAL_STATE_CODE(); |
| 283 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 284 | |
| 285 | return bdrv_drain_poll(bs, ignore_parent, false); |
| 286 | } |
| 287 | |
| 288 | static void bdrv_do_drained_begin(BlockDriverState *bs, BdrvChild *parent, |
| 289 | bool poll); |
| 290 | static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent); |
| 291 | |
| 292 | static void bdrv_co_drain_bh_cb(void *opaque) |
| 293 | { |
| 294 | BdrvCoDrainData *data = opaque; |
| 295 | Coroutine *co = data->co; |
| 296 | BlockDriverState *bs = data->bs; |
| 297 | |
| 298 | if (bs) { |
| 299 | bdrv_dec_in_flight(bs); |
| 300 | if (data->begin) { |
| 301 | bdrv_do_drained_begin(bs, data->parent, data->poll); |
| 302 | } else { |
| 303 | assert(!data->poll); |
| 304 | bdrv_do_drained_end(bs, data->parent); |
| 305 | } |
| 306 | } else { |
| 307 | assert(data->begin); |
| 308 | bdrv_drain_all_begin(); |
| 309 | } |
| 310 | |
| 311 | data->done = true; |
| 312 | aio_co_wake(co); |
| 313 | } |
| 314 | |
| 315 | static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs, |
| 316 | bool begin, |
| 317 | BdrvChild *parent, |
| 318 | bool poll) |
| 319 | { |
| 320 | BdrvCoDrainData data; |
| 321 | Coroutine *self = qemu_coroutine_self(); |
| 322 | |
| 323 | /* Calling bdrv_drain() from a BH ensures the current coroutine yields and |
| 324 | * other coroutines run if they were queued by aio_co_enter(). */ |
| 325 | |
| 326 | assert(qemu_in_coroutine()); |
| 327 | data = (BdrvCoDrainData) { |
| 328 | .co = self, |
| 329 | .bs = bs, |
| 330 | .done = false, |
| 331 | .begin = begin, |
| 332 | .parent = parent, |
| 333 | .poll = poll, |
| 334 | }; |
| 335 | |
| 336 | if (bs) { |
| 337 | bdrv_inc_in_flight(bs); |
| 338 | } |
| 339 | |
| 340 | replay_bh_schedule_oneshot_event(qemu_get_aio_context(), |
| 341 | bdrv_co_drain_bh_cb, &data); |
| 342 | |
| 343 | qemu_coroutine_yield(); |
| 344 | /* If we are resumed from some other event (such as an aio completion or a |
| 345 | * timer callback), it is a bug in the caller that should be fixed. */ |
| 346 | assert(data.done); |
| 347 | } |
| 348 | |
| 349 | static void bdrv_do_drained_begin(BlockDriverState *bs, BdrvChild *parent, |
| 350 | bool poll) |
| 351 | { |
| 352 | IO_OR_GS_CODE(); |
| 353 | |
| 354 | if (qemu_in_coroutine()) { |
| 355 | bdrv_co_yield_to_drain(bs, true, parent, poll); |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | GLOBAL_STATE_CODE(); |
| 360 | |
| 361 | /* Stop things in parent-to-child order */ |
| 362 | if (bs->quiesce_counter++ == 0) { |
| 363 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 364 | bdrv_parent_drained_begin(bs, parent); |
| 365 | if (bs->drv && bs->drv->bdrv_drain_begin) { |
| 366 | bs->drv->bdrv_drain_begin(bs); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Wait for drained requests to finish. |
| 372 | * |
| 373 | * Calling BDRV_POLL_WHILE() only once for the top-level node is okay: The |
| 374 | * call is needed so things in this AioContext can make progress even |
| 375 | * though we don't return to the main AioContext loop - this automatically |
| 376 | * includes other nodes in the same AioContext and therefore all child |
| 377 | * nodes. |
| 378 | */ |
| 379 | if (poll) { |
| 380 | BDRV_POLL_WHILE(bs, bdrv_drain_poll_top_level(bs, parent)); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, BdrvChild *parent) |
| 385 | { |
| 386 | bdrv_do_drained_begin(bs, parent, false); |
| 387 | } |
| 388 | |
| 389 | void coroutine_mixed_fn |
| 390 | bdrv_drained_begin(BlockDriverState *bs) |
| 391 | { |
| 392 | IO_OR_GS_CODE(); |
| 393 | bdrv_do_drained_begin(bs, NULL, true); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * This function does not poll, nor must any of its recursively called |
| 398 | * functions. |
| 399 | */ |
| 400 | static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent) |
| 401 | { |
| 402 | IO_OR_GS_CODE(); |
| 403 | |
| 404 | if (qemu_in_coroutine()) { |
| 405 | bdrv_co_yield_to_drain(bs, false, parent, false); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | /* At this point, we should be always running in the main loop. */ |
| 410 | GLOBAL_STATE_CODE(); |
| 411 | assert(bs->quiesce_counter > 0); |
| 412 | |
| 413 | /* Re-enable things in child-to-parent order */ |
| 414 | if (--bs->quiesce_counter == 0) { |
| 415 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 416 | if (bs->drv && bs->drv->bdrv_drain_end) { |
| 417 | bs->drv->bdrv_drain_end(bs); |
| 418 | } |
| 419 | bdrv_parent_drained_end(bs, parent); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | void bdrv_drained_end(BlockDriverState *bs) |
| 424 | { |
| 425 | IO_OR_GS_CODE(); |
| 426 | bdrv_do_drained_end(bs, NULL); |
| 427 | } |
| 428 | |
| 429 | void bdrv_drain(BlockDriverState *bs) |
| 430 | { |
| 431 | IO_OR_GS_CODE(); |
| 432 | bdrv_drained_begin(bs); |
| 433 | bdrv_drained_end(bs); |
| 434 | } |
| 435 | |
| 436 | static void bdrv_drain_assert_idle(BlockDriverState *bs) |
| 437 | { |
| 438 | BdrvChild *child, *next; |
| 439 | GLOBAL_STATE_CODE(); |
| 440 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 441 | |
| 442 | assert(qatomic_read(&bs->in_flight) == 0); |
| 443 | QLIST_FOREACH_SAFE(child, &bs->children, next, next) { |
| 444 | bdrv_drain_assert_idle(child->bs); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | unsigned int bdrv_drain_all_count = 0; |
| 449 | |
| 450 | static bool bdrv_drain_all_poll(void) |
| 451 | { |
| 452 | BlockDriverState *bs = NULL; |
| 453 | bool result = false; |
| 454 | |
| 455 | GLOBAL_STATE_CODE(); |
| 456 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 457 | |
| 458 | /* |
| 459 | * bdrv_drain_poll() can't make changes to the graph and we hold the BQL, |
| 460 | * so iterating bdrv_next_all_states() is safe. |
| 461 | */ |
| 462 | while ((bs = bdrv_next_all_states(bs))) { |
| 463 | result |= bdrv_drain_poll(bs, NULL, true); |
| 464 | } |
| 465 | |
| 466 | return result; |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Wait for pending requests to complete across all BlockDriverStates |
| 471 | * |
| 472 | * This function does not flush data to disk, use bdrv_flush_all() for that |
| 473 | * after calling this function. |
| 474 | * |
| 475 | * This pauses all block jobs and disables external clients. It must |
| 476 | * be paired with bdrv_drain_all_end(). |
| 477 | * |
| 478 | * NOTE: no new block jobs or BlockDriverStates can be created between |
| 479 | * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls. |
| 480 | */ |
| 481 | void bdrv_drain_all_begin_nopoll(void) |
| 482 | { |
| 483 | BlockDriverState *bs = NULL; |
| 484 | GLOBAL_STATE_CODE(); |
| 485 | |
| 486 | /* |
| 487 | * bdrv queue is managed by record/replay, |
| 488 | * waiting for finishing the I/O requests may |
| 489 | * be infinite |
| 490 | */ |
| 491 | if (replay_events_enabled()) { |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | /* AIO_WAIT_WHILE() with a NULL context can only be called from the main |
| 496 | * loop AioContext, so make sure we're in the main context. */ |
| 497 | assert(qemu_get_current_aio_context() == qemu_get_aio_context()); |
| 498 | assert(bdrv_drain_all_count < INT_MAX); |
| 499 | bdrv_drain_all_count++; |
| 500 | |
| 501 | /* Quiesce all nodes, without polling in-flight requests yet. The graph |
| 502 | * cannot change during this loop. */ |
| 503 | while ((bs = bdrv_next_all_states(bs))) { |
| 504 | bdrv_do_drained_begin(bs, NULL, false); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | void coroutine_mixed_fn bdrv_drain_all_begin(void) |
| 509 | { |
| 510 | BlockDriverState *bs = NULL; |
| 511 | |
| 512 | if (qemu_in_coroutine()) { |
| 513 | bdrv_co_yield_to_drain(NULL, true, NULL, true); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * bdrv queue is managed by record/replay, |
| 519 | * waiting for finishing the I/O requests may |
| 520 | * be infinite |
| 521 | */ |
| 522 | if (replay_events_enabled()) { |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | bdrv_drain_all_begin_nopoll(); |
| 527 | |
| 528 | /* Now poll the in-flight requests */ |
| 529 | AIO_WAIT_WHILE_UNLOCKED(NULL, bdrv_drain_all_poll()); |
| 530 | |
| 531 | while ((bs = bdrv_next_all_states(bs))) { |
| 532 | bdrv_drain_assert_idle(bs); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | void bdrv_drain_all_end_quiesce(BlockDriverState *bs) |
| 537 | { |
| 538 | GLOBAL_STATE_CODE(); |
| 539 | |
| 540 | g_assert(bs->quiesce_counter > 0); |
| 541 | g_assert(!bs->refcnt); |
| 542 | |
| 543 | while (bs->quiesce_counter) { |
| 544 | bdrv_do_drained_end(bs, NULL); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | void bdrv_drain_all_end(void) |
| 549 | { |
| 550 | BlockDriverState *bs = NULL; |
| 551 | GLOBAL_STATE_CODE(); |
| 552 | |
| 553 | /* |
| 554 | * bdrv queue is managed by record/replay, |
| 555 | * waiting for finishing the I/O requests may |
| 556 | * be endless |
| 557 | */ |
| 558 | if (replay_events_enabled()) { |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | while ((bs = bdrv_next_all_states(bs))) { |
| 563 | bdrv_do_drained_end(bs, NULL); |
| 564 | } |
| 565 | |
| 566 | assert(qemu_get_current_aio_context() == qemu_get_aio_context()); |
| 567 | assert(bdrv_drain_all_count > 0); |
| 568 | bdrv_drain_all_count--; |
| 569 | } |
| 570 | |
| 571 | void bdrv_drain_all(void) |
| 572 | { |
| 573 | GLOBAL_STATE_CODE(); |
| 574 | bdrv_drain_all_begin(); |
| 575 | bdrv_drain_all_end(); |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Remove an active request from the tracked requests list |
| 580 | * |
| 581 | * This function should be called when a tracked request is completing. |
| 582 | */ |
| 583 | static void coroutine_fn tracked_request_end(BdrvTrackedRequest *req) |
| 584 | { |
| 585 | if (req->serialising) { |
| 586 | qatomic_dec(&req->bs->serialising_in_flight); |
| 587 | } |
| 588 | |
| 589 | qemu_mutex_lock(&req->bs->reqs_lock); |
| 590 | QLIST_REMOVE(req, list); |
| 591 | qemu_mutex_unlock(&req->bs->reqs_lock); |
| 592 | |
| 593 | /* |
| 594 | * At this point qemu_co_queue_wait(&req->wait_queue, ...) won't be called |
| 595 | * anymore because the request has been removed from the list, so it's safe |
| 596 | * to restart the queue outside reqs_lock to minimize the critical section. |
| 597 | */ |
| 598 | qemu_co_queue_restart_all(&req->wait_queue); |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Add an active request to the tracked requests list |
| 603 | */ |
| 604 | static void coroutine_fn tracked_request_begin(BdrvTrackedRequest *req, |
| 605 | BlockDriverState *bs, |
| 606 | int64_t offset, |
| 607 | int64_t bytes, |
| 608 | enum BdrvTrackedRequestType type) |
| 609 | { |
| 610 | bdrv_check_request(offset, bytes, &error_abort); |
| 611 | |
| 612 | *req = (BdrvTrackedRequest){ |
| 613 | .bs = bs, |
| 614 | .offset = offset, |
| 615 | .bytes = bytes, |
| 616 | .type = type, |
| 617 | .co = qemu_coroutine_self(), |
| 618 | .serialising = false, |
| 619 | .overlap_offset = offset, |
| 620 | .overlap_bytes = bytes, |
| 621 | }; |
| 622 | |
| 623 | qemu_co_queue_init(&req->wait_queue); |
| 624 | |
| 625 | qemu_mutex_lock(&bs->reqs_lock); |
| 626 | QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); |
| 627 | qemu_mutex_unlock(&bs->reqs_lock); |
| 628 | } |
| 629 | |
| 630 | static bool tracked_request_overlaps(BdrvTrackedRequest *req, |
| 631 | int64_t offset, int64_t bytes) |
| 632 | { |
| 633 | bdrv_check_request(offset, bytes, &error_abort); |
| 634 | |
| 635 | /* aaaa bbbb */ |
| 636 | if (offset >= req->overlap_offset + req->overlap_bytes) { |
| 637 | return false; |
| 638 | } |
| 639 | /* bbbb aaaa */ |
| 640 | if (req->overlap_offset >= offset + bytes) { |
| 641 | return false; |
| 642 | } |
| 643 | return true; |
| 644 | } |
| 645 | |
| 646 | /* Called with self->bs->reqs_lock held */ |
| 647 | static coroutine_fn BdrvTrackedRequest * |
| 648 | bdrv_find_conflicting_request(BdrvTrackedRequest *self) |
| 649 | { |
| 650 | BdrvTrackedRequest *req; |
| 651 | |
| 652 | QLIST_FOREACH(req, &self->bs->tracked_requests, list) { |
| 653 | if (req == self || (!req->serialising && !self->serialising)) { |
| 654 | continue; |
| 655 | } |
| 656 | if (tracked_request_overlaps(req, self->overlap_offset, |
| 657 | self->overlap_bytes)) |
| 658 | { |
| 659 | /* |
| 660 | * Hitting this means there was a reentrant request, for |
| 661 | * example, a block driver issuing nested requests. This must |
| 662 | * never happen since it means deadlock. |
| 663 | */ |
| 664 | assert(qemu_coroutine_self() != req->co); |
| 665 | |
| 666 | /* |
| 667 | * If the request is already (indirectly) waiting for us, or |
| 668 | * will wait for us as soon as it wakes up, then just go on |
| 669 | * (instead of producing a deadlock in the former case). |
| 670 | */ |
| 671 | if (!req->waiting_for) { |
| 672 | return req; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | return NULL; |
| 678 | } |
| 679 | |
| 680 | /* Called with self->bs->reqs_lock held */ |
| 681 | static void coroutine_fn |
| 682 | bdrv_wait_serialising_requests_locked(BdrvTrackedRequest *self) |
| 683 | { |
| 684 | BdrvTrackedRequest *req; |
| 685 | |
| 686 | while ((req = bdrv_find_conflicting_request(self))) { |
| 687 | self->waiting_for = req; |
| 688 | qemu_co_queue_wait(&req->wait_queue, &self->bs->reqs_lock); |
| 689 | self->waiting_for = NULL; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /* Called with req->bs->reqs_lock held */ |
| 694 | static void tracked_request_set_serialising(BdrvTrackedRequest *req, |
| 695 | uint64_t align) |
| 696 | { |
| 697 | int64_t overlap_offset = req->offset & ~(align - 1); |
| 698 | int64_t overlap_bytes = |
| 699 | ROUND_UP(req->offset + req->bytes, align) - overlap_offset; |
| 700 | |
| 701 | bdrv_check_request(req->offset, req->bytes, &error_abort); |
| 702 | |
| 703 | if (!req->serialising) { |
| 704 | qatomic_inc(&req->bs->serialising_in_flight); |
| 705 | req->serialising = true; |
| 706 | } |
| 707 | |
| 708 | req->overlap_offset = MIN(req->overlap_offset, overlap_offset); |
| 709 | req->overlap_bytes = MAX(req->overlap_bytes, overlap_bytes); |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Return the tracked request on @bs for the current coroutine, or |
| 714 | * NULL if there is none. |
| 715 | */ |
| 716 | BdrvTrackedRequest *coroutine_fn bdrv_co_get_self_request(BlockDriverState *bs) |
| 717 | { |
| 718 | BdrvTrackedRequest *req; |
| 719 | Coroutine *self = qemu_coroutine_self(); |
| 720 | IO_CODE(); |
| 721 | |
| 722 | QEMU_LOCK_GUARD(&bs->reqs_lock); |
| 723 | QLIST_FOREACH(req, &bs->tracked_requests, list) { |
| 724 | if (req->co == self) { |
| 725 | return req; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | return NULL; |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * Round a region to subcluster (if supported) or cluster boundaries |
| 734 | */ |
| 735 | void coroutine_fn GRAPH_RDLOCK |
| 736 | bdrv_round_to_subclusters(BlockDriverState *bs, int64_t offset, int64_t bytes, |
| 737 | int64_t *align_offset, int64_t *align_bytes) |
| 738 | { |
| 739 | BlockDriverInfo bdi; |
| 740 | IO_CODE(); |
| 741 | if (bdrv_co_get_info(bs, &bdi) < 0 || bdi.subcluster_size == 0) { |
| 742 | *align_offset = offset; |
| 743 | *align_bytes = bytes; |
| 744 | } else { |
| 745 | int64_t c = bdi.subcluster_size; |
| 746 | *align_offset = QEMU_ALIGN_DOWN(offset, c); |
| 747 | *align_bytes = QEMU_ALIGN_UP(offset - *align_offset + bytes, c); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | static int coroutine_fn GRAPH_RDLOCK bdrv_get_cluster_size(BlockDriverState *bs) |
| 752 | { |
| 753 | BlockDriverInfo bdi; |
| 754 | int ret; |
| 755 | |
| 756 | ret = bdrv_co_get_info(bs, &bdi); |
| 757 | if (ret < 0 || bdi.cluster_size == 0) { |
| 758 | return bs->bl.request_alignment; |
| 759 | } else { |
| 760 | return bdi.cluster_size; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | void bdrv_inc_in_flight(BlockDriverState *bs) |
| 765 | { |
| 766 | IO_CODE(); |
| 767 | qatomic_inc(&bs->in_flight); |
| 768 | } |
| 769 | |
| 770 | void bdrv_wakeup(BlockDriverState *bs) |
| 771 | { |
| 772 | IO_CODE(); |
| 773 | aio_wait_kick(); |
| 774 | } |
| 775 | |
| 776 | void bdrv_dec_in_flight(BlockDriverState *bs) |
| 777 | { |
| 778 | IO_CODE(); |
| 779 | qatomic_dec(&bs->in_flight); |
| 780 | bdrv_wakeup(bs); |
| 781 | } |
| 782 | |
| 783 | static void coroutine_fn |
| 784 | bdrv_wait_serialising_requests(BdrvTrackedRequest *self) |
| 785 | { |
| 786 | BlockDriverState *bs = self->bs; |
| 787 | |
| 788 | if (!qatomic_read(&bs->serialising_in_flight)) { |
| 789 | return; |
| 790 | } |
| 791 | |
| 792 | qemu_mutex_lock(&bs->reqs_lock); |
| 793 | bdrv_wait_serialising_requests_locked(self); |
| 794 | qemu_mutex_unlock(&bs->reqs_lock); |
| 795 | } |
| 796 | |
| 797 | void coroutine_fn bdrv_make_request_serialising(BdrvTrackedRequest *req, |
| 798 | uint64_t align) |
| 799 | { |
| 800 | IO_CODE(); |
| 801 | |
| 802 | qemu_mutex_lock(&req->bs->reqs_lock); |
| 803 | |
| 804 | tracked_request_set_serialising(req, align); |
| 805 | bdrv_wait_serialising_requests_locked(req); |
| 806 | |
| 807 | qemu_mutex_unlock(&req->bs->reqs_lock); |
| 808 | } |
| 809 | |
| 810 | int bdrv_check_qiov_request(int64_t offset, int64_t bytes, |
| 811 | QEMUIOVector *qiov, size_t qiov_offset, |
| 812 | Error **errp) |
| 813 | { |
| 814 | /* |
| 815 | * Check generic offset/bytes correctness |
| 816 | */ |
| 817 | |
| 818 | if (offset < 0) { |
| 819 | error_setg(errp, "offset is negative: %" PRIi64, offset); |
| 820 | return -EIO; |
| 821 | } |
| 822 | |
| 823 | if (bytes < 0) { |
| 824 | error_setg(errp, "bytes is negative: %" PRIi64, bytes); |
| 825 | return -EIO; |
| 826 | } |
| 827 | |
| 828 | if (bytes > BDRV_MAX_LENGTH) { |
| 829 | error_setg(errp, "bytes(%" PRIi64 ") exceeds maximum(%" PRIi64 ")", |
| 830 | bytes, BDRV_MAX_LENGTH); |
| 831 | return -EIO; |
| 832 | } |
| 833 | |
| 834 | if (offset > BDRV_MAX_LENGTH) { |
| 835 | error_setg(errp, "offset(%" PRIi64 ") exceeds maximum(%" PRIi64 ")", |
| 836 | offset, BDRV_MAX_LENGTH); |
| 837 | return -EIO; |
| 838 | } |
| 839 | |
| 840 | if (offset > BDRV_MAX_LENGTH - bytes) { |
| 841 | error_setg(errp, "sum of offset(%" PRIi64 ") and bytes(%" PRIi64 ") " |
| 842 | "exceeds maximum(%" PRIi64 ")", offset, bytes, |
| 843 | BDRV_MAX_LENGTH); |
| 844 | return -EIO; |
| 845 | } |
| 846 | |
| 847 | if (!qiov) { |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * Check qiov and qiov_offset |
| 853 | */ |
| 854 | |
| 855 | if (qiov_offset > qiov->size) { |
| 856 | error_setg(errp, "qiov_offset(%zu) overflow io vector size(%zu)", |
| 857 | qiov_offset, qiov->size); |
| 858 | return -EIO; |
| 859 | } |
| 860 | |
| 861 | if (bytes > qiov->size - qiov_offset) { |
| 862 | error_setg(errp, "bytes(%" PRIi64 ") + qiov_offset(%zu) overflow io " |
| 863 | "vector size(%zu)", bytes, qiov_offset, qiov->size); |
| 864 | return -EIO; |
| 865 | } |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | int bdrv_check_request(int64_t offset, int64_t bytes, Error **errp) |
| 871 | { |
| 872 | return bdrv_check_qiov_request(offset, bytes, NULL, 0, errp); |
| 873 | } |
| 874 | |
| 875 | static int bdrv_check_request32(int64_t offset, int64_t bytes, |
| 876 | QEMUIOVector *qiov, size_t qiov_offset) |
| 877 | { |
| 878 | int ret = bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, NULL); |
| 879 | if (ret < 0) { |
| 880 | return ret; |
| 881 | } |
| 882 | |
| 883 | if (bytes > BDRV_REQUEST_MAX_BYTES) { |
| 884 | return -EIO; |
| 885 | } |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Completely zero out a block device with the help of bdrv_pwrite_zeroes. |
| 892 | * The operation is sped up by checking the block status and only writing |
| 893 | * zeroes to the device if they currently do not return zeroes. Optional |
| 894 | * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP, |
| 895 | * BDRV_REQ_FUA). |
| 896 | * |
| 897 | * Returns < 0 on error, 0 on success. For error codes see bdrv_pwrite(). |
| 898 | */ |
| 899 | int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags) |
| 900 | { |
| 901 | int ret; |
| 902 | int64_t target_size, bytes, offset = 0; |
| 903 | BlockDriverState *bs = child->bs; |
| 904 | IO_CODE(); |
| 905 | |
| 906 | target_size = bdrv_getlength(bs); |
| 907 | if (target_size < 0) { |
| 908 | return target_size; |
| 909 | } |
| 910 | |
| 911 | for (;;) { |
| 912 | bytes = MIN(target_size - offset, BDRV_REQUEST_MAX_BYTES); |
| 913 | if (bytes <= 0) { |
| 914 | return 0; |
| 915 | } |
| 916 | ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL); |
| 917 | if (ret < 0) { |
| 918 | return ret; |
| 919 | } |
| 920 | if (ret & BDRV_BLOCK_ZERO) { |
| 921 | offset += bytes; |
| 922 | continue; |
| 923 | } |
| 924 | ret = bdrv_pwrite_zeroes(child, offset, bytes, flags); |
| 925 | if (ret < 0) { |
| 926 | return ret; |
| 927 | } |
| 928 | offset += bytes; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Writes to the file and ensures that no writes are reordered across this |
| 934 | * request (acts as a barrier) |
| 935 | * |
| 936 | * Returns 0 on success, -errno in error cases. |
| 937 | */ |
| 938 | int coroutine_fn bdrv_co_pwrite_sync(BdrvChild *child, int64_t offset, |
| 939 | int64_t bytes, const void *buf, |
| 940 | BdrvRequestFlags flags) |
| 941 | { |
| 942 | int ret; |
| 943 | IO_CODE(); |
| 944 | assert_bdrv_graph_readable(); |
| 945 | |
| 946 | ret = bdrv_co_pwrite(child, offset, bytes, buf, flags); |
| 947 | if (ret < 0) { |
| 948 | return ret; |
| 949 | } |
| 950 | |
| 951 | ret = bdrv_co_flush(child->bs); |
| 952 | if (ret < 0) { |
| 953 | return ret; |
| 954 | } |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | typedef struct CoroutineIOCompletion { |
| 960 | Coroutine *coroutine; |
| 961 | int ret; |
| 962 | } CoroutineIOCompletion; |
| 963 | |
| 964 | static void bdrv_co_io_em_complete(void *opaque, int ret) |
| 965 | { |
| 966 | CoroutineIOCompletion *co = opaque; |
| 967 | |
| 968 | co->ret = ret; |
| 969 | aio_co_wake(co->coroutine); |
| 970 | } |
| 971 | |
| 972 | static int coroutine_fn GRAPH_RDLOCK |
| 973 | bdrv_driver_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, |
| 974 | QEMUIOVector *qiov, size_t qiov_offset, int flags) |
| 975 | { |
| 976 | BlockDriver *drv = bs->drv; |
| 977 | int64_t sector_num; |
| 978 | unsigned int nb_sectors; |
| 979 | QEMUIOVector local_qiov; |
| 980 | int ret; |
| 981 | assert_bdrv_graph_readable(); |
| 982 | |
| 983 | bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort); |
| 984 | assert(!(flags & ~bs->supported_read_flags)); |
| 985 | |
| 986 | if (!drv) { |
| 987 | return -ENOMEDIUM; |
| 988 | } |
| 989 | |
| 990 | if (drv->bdrv_co_preadv_part) { |
| 991 | return drv->bdrv_co_preadv_part(bs, offset, bytes, qiov, qiov_offset, |
| 992 | flags); |
| 993 | } |
| 994 | |
| 995 | if (qiov_offset > 0 || bytes != qiov->size) { |
| 996 | qemu_iovec_init_slice(&local_qiov, qiov, qiov_offset, bytes); |
| 997 | qiov = &local_qiov; |
| 998 | } |
| 999 | |
| 1000 | if (drv->bdrv_co_preadv) { |
| 1001 | ret = drv->bdrv_co_preadv(bs, offset, bytes, qiov, flags); |
| 1002 | goto out; |
| 1003 | } |
| 1004 | |
| 1005 | if (drv->bdrv_aio_preadv) { |
| 1006 | BlockAIOCB *acb; |
| 1007 | CoroutineIOCompletion co = { |
| 1008 | .coroutine = qemu_coroutine_self(), |
| 1009 | }; |
| 1010 | |
| 1011 | acb = drv->bdrv_aio_preadv(bs, offset, bytes, qiov, flags, |
| 1012 | bdrv_co_io_em_complete, &co); |
| 1013 | if (acb == NULL) { |
| 1014 | ret = -EIO; |
| 1015 | goto out; |
| 1016 | } else { |
| 1017 | qemu_coroutine_yield(); |
| 1018 | ret = co.ret; |
| 1019 | goto out; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | sector_num = offset >> BDRV_SECTOR_BITS; |
| 1024 | nb_sectors = bytes >> BDRV_SECTOR_BITS; |
| 1025 | |
| 1026 | assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)); |
| 1027 | assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE)); |
| 1028 | assert(bytes <= BDRV_REQUEST_MAX_BYTES); |
| 1029 | assert(drv->bdrv_co_readv); |
| 1030 | |
| 1031 | ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); |
| 1032 | |
| 1033 | out: |
| 1034 | if (qiov == &local_qiov) { |
| 1035 | qemu_iovec_destroy(&local_qiov); |
| 1036 | } |
| 1037 | |
| 1038 | return ret; |
| 1039 | } |
| 1040 | |
| 1041 | static int coroutine_fn GRAPH_RDLOCK |
| 1042 | bdrv_driver_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, |
| 1043 | QEMUIOVector *qiov, size_t qiov_offset, |
| 1044 | BdrvRequestFlags flags) |
| 1045 | { |
| 1046 | BlockDriver *drv = bs->drv; |
| 1047 | bool emulate_fua = false; |
| 1048 | int64_t sector_num; |
| 1049 | unsigned int nb_sectors; |
| 1050 | QEMUIOVector local_qiov; |
| 1051 | int ret; |
| 1052 | assert_bdrv_graph_readable(); |
| 1053 | |
| 1054 | bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort); |
| 1055 | |
| 1056 | if (!drv) { |
| 1057 | return -ENOMEDIUM; |
| 1058 | } |
| 1059 | |
| 1060 | if (bs->open_flags & BDRV_O_NO_FLUSH) { |
| 1061 | flags &= ~BDRV_REQ_FUA; |
| 1062 | } |
| 1063 | |
| 1064 | if ((flags & BDRV_REQ_FUA) && |
| 1065 | (~bs->supported_write_flags & BDRV_REQ_FUA)) { |
| 1066 | flags &= ~BDRV_REQ_FUA; |
| 1067 | emulate_fua = true; |
| 1068 | } |
| 1069 | |
| 1070 | flags &= bs->supported_write_flags; |
| 1071 | |
| 1072 | if (drv->bdrv_co_pwritev_part) { |
| 1073 | ret = drv->bdrv_co_pwritev_part(bs, offset, bytes, qiov, qiov_offset, |
| 1074 | flags); |
| 1075 | goto emulate_flags; |
| 1076 | } |
| 1077 | |
| 1078 | if (qiov_offset > 0 || bytes != qiov->size) { |
| 1079 | qemu_iovec_init_slice(&local_qiov, qiov, qiov_offset, bytes); |
| 1080 | qiov = &local_qiov; |
| 1081 | } |
| 1082 | |
| 1083 | if (drv->bdrv_co_pwritev) { |
| 1084 | ret = drv->bdrv_co_pwritev(bs, offset, bytes, qiov, flags); |
| 1085 | goto emulate_flags; |
| 1086 | } |
| 1087 | |
| 1088 | if (drv->bdrv_aio_pwritev) { |
| 1089 | BlockAIOCB *acb; |
| 1090 | CoroutineIOCompletion co = { |
| 1091 | .coroutine = qemu_coroutine_self(), |
| 1092 | }; |
| 1093 | |
| 1094 | acb = drv->bdrv_aio_pwritev(bs, offset, bytes, qiov, flags, |
| 1095 | bdrv_co_io_em_complete, &co); |
| 1096 | if (acb == NULL) { |
| 1097 | ret = -EIO; |
| 1098 | } else { |
| 1099 | qemu_coroutine_yield(); |
| 1100 | ret = co.ret; |
| 1101 | } |
| 1102 | goto emulate_flags; |
| 1103 | } |
| 1104 | |
| 1105 | sector_num = offset >> BDRV_SECTOR_BITS; |
| 1106 | nb_sectors = bytes >> BDRV_SECTOR_BITS; |
| 1107 | |
| 1108 | assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)); |
| 1109 | assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE)); |
| 1110 | assert(bytes <= BDRV_REQUEST_MAX_BYTES); |
| 1111 | |
| 1112 | assert(drv->bdrv_co_writev); |
| 1113 | ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov, flags); |
| 1114 | |
| 1115 | emulate_flags: |
| 1116 | if (ret == 0 && emulate_fua) { |
| 1117 | ret = bdrv_co_flush(bs); |
| 1118 | } |
| 1119 | |
| 1120 | if (qiov == &local_qiov) { |
| 1121 | qemu_iovec_destroy(&local_qiov); |
| 1122 | } |
| 1123 | |
| 1124 | return ret; |
| 1125 | } |
| 1126 | |
| 1127 | static int coroutine_fn GRAPH_RDLOCK |
| 1128 | bdrv_driver_pwritev_compressed(BlockDriverState *bs, int64_t offset, |
| 1129 | int64_t bytes, QEMUIOVector *qiov, |
| 1130 | size_t qiov_offset) |
| 1131 | { |
| 1132 | BlockDriver *drv = bs->drv; |
| 1133 | QEMUIOVector local_qiov; |
| 1134 | int ret; |
| 1135 | assert_bdrv_graph_readable(); |
| 1136 | |
| 1137 | bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort); |
| 1138 | |
| 1139 | if (!drv) { |
| 1140 | return -ENOMEDIUM; |
| 1141 | } |
| 1142 | |
| 1143 | if (!block_driver_can_compress(drv)) { |
| 1144 | return -ENOTSUP; |
| 1145 | } |
| 1146 | |
| 1147 | if (drv->bdrv_co_pwritev_compressed_part) { |
| 1148 | return drv->bdrv_co_pwritev_compressed_part(bs, offset, bytes, |
| 1149 | qiov, qiov_offset); |
| 1150 | } |
| 1151 | |
| 1152 | if (qiov_offset == 0) { |
| 1153 | return drv->bdrv_co_pwritev_compressed(bs, offset, bytes, qiov); |
| 1154 | } |
| 1155 | |
| 1156 | qemu_iovec_init_slice(&local_qiov, qiov, qiov_offset, bytes); |
| 1157 | ret = drv->bdrv_co_pwritev_compressed(bs, offset, bytes, &local_qiov); |
| 1158 | qemu_iovec_destroy(&local_qiov); |
| 1159 | |
| 1160 | return ret; |
| 1161 | } |
| 1162 | |
| 1163 | static int coroutine_fn GRAPH_RDLOCK |
| 1164 | bdrv_co_do_copy_on_readv(BdrvChild *child, int64_t offset, int64_t bytes, |
| 1165 | QEMUIOVector *qiov, size_t qiov_offset, int flags) |
| 1166 | { |
| 1167 | BlockDriverState *bs = child->bs; |
| 1168 | |
| 1169 | /* Perform I/O through a temporary buffer so that users who scribble over |
| 1170 | * their read buffer while the operation is in progress do not end up |
| 1171 | * modifying the image file. This is critical for zero-copy guest I/O |
| 1172 | * where anything might happen inside guest memory. |
| 1173 | */ |
| 1174 | void *bounce_buffer = NULL; |
| 1175 | |
| 1176 | BlockDriver *drv = bs->drv; |
| 1177 | int64_t align_offset; |
| 1178 | int64_t align_bytes; |
| 1179 | int64_t skip_bytes; |
| 1180 | int ret; |
| 1181 | int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer, |
| 1182 | BDRV_REQUEST_MAX_BYTES); |
| 1183 | int64_t progress = 0; |
| 1184 | bool skip_write; |
| 1185 | |
| 1186 | bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort); |
| 1187 | |
| 1188 | if (!drv) { |
| 1189 | return -ENOMEDIUM; |
| 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | * Do not write anything when the BDS is inactive. That is not |
| 1194 | * allowed, and it would not help. |
| 1195 | */ |
| 1196 | skip_write = (bs->open_flags & BDRV_O_INACTIVE); |
| 1197 | |
| 1198 | /* FIXME We cannot require callers to have write permissions when all they |
| 1199 | * are doing is a read request. If we did things right, write permissions |
| 1200 | * would be obtained anyway, but internally by the copy-on-read code. As |
| 1201 | * long as it is implemented here rather than in a separate filter driver, |
| 1202 | * the copy-on-read code doesn't have its own BdrvChild, however, for which |
| 1203 | * it could request permissions. Therefore we have to bypass the permission |
| 1204 | * system for the moment. */ |
| 1205 | // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE)); |
| 1206 | |
| 1207 | /* Cover entire cluster so no additional backing file I/O is required when |
| 1208 | * allocating cluster in the image file. Note that this value may exceed |
| 1209 | * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which |
| 1210 | * is one reason we loop rather than doing it all at once. |
| 1211 | */ |
| 1212 | bdrv_round_to_subclusters(bs, offset, bytes, &align_offset, &align_bytes); |
| 1213 | skip_bytes = offset - align_offset; |
| 1214 | |
| 1215 | trace_bdrv_co_do_copy_on_readv(bs, offset, bytes, |
| 1216 | align_offset, align_bytes); |
| 1217 | |
| 1218 | while (align_bytes) { |
| 1219 | int64_t pnum; |
| 1220 | |
| 1221 | if (skip_write) { |
| 1222 | ret = 1; /* "already allocated", so nothing will be copied */ |
| 1223 | pnum = MIN(align_bytes, max_transfer); |
| 1224 | } else { |
| 1225 | ret = bdrv_co_is_allocated(bs, align_offset, |
| 1226 | MIN(align_bytes, max_transfer), &pnum); |
| 1227 | if (ret < 0) { |
| 1228 | /* |
| 1229 | * Safe to treat errors in querying allocation as if |
| 1230 | * unallocated; we'll probably fail again soon on the |
| 1231 | * read, but at least that will set a decent errno. |
| 1232 | */ |
| 1233 | pnum = MIN(align_bytes, max_transfer); |
| 1234 | } |
| 1235 | |
| 1236 | /* Stop at EOF if the image ends in the middle of the cluster */ |
| 1237 | if (ret == 0 && pnum == 0) { |
| 1238 | assert(progress >= bytes); |
| 1239 | break; |
| 1240 | } |
| 1241 | |
| 1242 | assert(skip_bytes < pnum); |
| 1243 | } |
| 1244 | |
| 1245 | if (ret <= 0) { |
| 1246 | QEMUIOVector local_qiov; |
| 1247 | |
| 1248 | /* Must copy-on-read; use the bounce buffer */ |
| 1249 | pnum = MIN(pnum, MAX_BOUNCE_BUFFER); |
| 1250 | if (!bounce_buffer) { |
| 1251 | int64_t max_we_need = MAX(pnum, align_bytes - pnum); |
| 1252 | int64_t max_allowed = MIN(max_transfer, MAX_BOUNCE_BUFFER); |
| 1253 | int64_t bounce_buffer_len = MIN(max_we_need, max_allowed); |
| 1254 | |
| 1255 | bounce_buffer = qemu_try_blockalign(bs, bounce_buffer_len); |
| 1256 | if (!bounce_buffer) { |
| 1257 | ret = -ENOMEM; |
| 1258 | goto err; |
| 1259 | } |
| 1260 | } |
| 1261 | qemu_iovec_init_buf(&local_qiov, bounce_buffer, pnum); |
| 1262 | |
| 1263 | ret = bdrv_driver_preadv(bs, align_offset, pnum, |
| 1264 | &local_qiov, 0, 0); |
| 1265 | if (ret < 0) { |
| 1266 | goto err; |
| 1267 | } |
| 1268 | |
| 1269 | bdrv_co_debug_event(bs, BLKDBG_COR_WRITE); |
| 1270 | if (drv->bdrv_co_pwrite_zeroes && |
| 1271 | buffer_is_zero(bounce_buffer, pnum)) { |
| 1272 | /* FIXME: Should we (perhaps conditionally) be setting |
| 1273 | * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy |
| 1274 | * that still correctly reads as zero? */ |
| 1275 | ret = bdrv_co_do_pwrite_zeroes(bs, align_offset, pnum, |
| 1276 | BDRV_REQ_WRITE_UNCHANGED); |
| 1277 | } else { |
| 1278 | /* This does not change the data on the disk, it is not |
| 1279 | * necessary to flush even in cache=writethrough mode. |
| 1280 | */ |
| 1281 | ret = bdrv_driver_pwritev(bs, align_offset, pnum, |
| 1282 | &local_qiov, 0, |
| 1283 | BDRV_REQ_WRITE_UNCHANGED); |
| 1284 | } |
| 1285 | |
| 1286 | if (ret < 0) { |
| 1287 | /* It might be okay to ignore write errors for guest |
| 1288 | * requests. If this is a deliberate copy-on-read |
| 1289 | * then we don't want to ignore the error. Simply |
| 1290 | * report it in all cases. |
| 1291 | */ |
| 1292 | goto err; |
| 1293 | } |
| 1294 | |
| 1295 | if (!(flags & BDRV_REQ_PREFETCH)) { |
| 1296 | qemu_iovec_from_buf(qiov, qiov_offset + progress, |
| 1297 | bounce_buffer + skip_bytes, |
| 1298 | MIN(pnum - skip_bytes, bytes - progress)); |
| 1299 | } |
| 1300 | } else if (!(flags & BDRV_REQ_PREFETCH)) { |
| 1301 | /* Read directly into the destination */ |
| 1302 | ret = bdrv_driver_preadv(bs, offset + progress, |
| 1303 | MIN(pnum - skip_bytes, bytes - progress), |
| 1304 | qiov, qiov_offset + progress, 0); |
| 1305 | if (ret < 0) { |
| 1306 | goto err; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | align_offset += pnum; |
| 1311 | align_bytes -= pnum; |
| 1312 | progress += pnum - skip_bytes; |
| 1313 | skip_bytes = 0; |
| 1314 | } |
| 1315 | ret = 0; |
| 1316 | |
| 1317 | err: |
| 1318 | qemu_vfree(bounce_buffer); |
| 1319 | return ret; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
| 1323 | * Forwards an already correctly aligned request to the BlockDriver. This |
| 1324 | * handles copy on read, zeroing after EOF, and fragmentation of large |
| 1325 | * reads; any other features must be implemented by the caller. |
| 1326 | */ |
| 1327 | static int coroutine_fn GRAPH_RDLOCK |
| 1328 | bdrv_aligned_preadv(BdrvChild *child, BdrvTrackedRequest *req, |
| 1329 | int64_t offset, int64_t bytes, int64_t align, |
| 1330 | QEMUIOVector *qiov, size_t qiov_offset, int flags) |
| 1331 | { |
| 1332 | BlockDriverState *bs = child->bs; |
| 1333 | int64_t total_bytes, max_bytes; |
| 1334 | int ret = 0; |
| 1335 | int64_t bytes_remaining = bytes; |
| 1336 | int max_transfer; |
| 1337 | |
| 1338 | bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort); |
| 1339 | assert(is_power_of_2(align)); |
| 1340 | assert((offset & (align - 1)) == 0); |
| 1341 | assert((bytes & (align - 1)) == 0); |
| 1342 | assert((bs->open_flags & BDRV_O_NO_IO) == 0); |
| 1343 | max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX), |
| 1344 | align); |
| 1345 | |
| 1346 | /* |
| 1347 | * TODO: We would need a per-BDS .supported_read_flags and |
| 1348 | * potential fallback support, if we ever implement any read flags |
| 1349 | * to pass through to drivers. For now, there aren't any |
| 1350 | * passthrough flags except the BDRV_REQ_REGISTERED_BUF optimization hint. |
| 1351 | */ |
| 1352 | assert(!(flags & ~(BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH | |
| 1353 | BDRV_REQ_REGISTERED_BUF))); |
| 1354 | |
| 1355 | /* Handle Copy on Read and associated serialisation */ |
| 1356 | if (flags & BDRV_REQ_COPY_ON_READ) { |
| 1357 | /* If we touch the same cluster it counts as an overlap. This |
| 1358 | * guarantees that allocating writes will be serialized and not race |
| 1359 | * with each other for the same cluster. For example, in copy-on-read |
| 1360 | * it ensures that the CoR read and write operations are atomic and |
| 1361 | * guest writes cannot interleave between them. */ |
| 1362 | bdrv_make_request_serialising(req, bdrv_get_cluster_size(bs)); |
| 1363 | } else { |
| 1364 | bdrv_wait_serialising_requests(req); |
| 1365 | } |
| 1366 | |
| 1367 | if (flags & BDRV_REQ_COPY_ON_READ) { |
| 1368 | int64_t pnum; |
| 1369 | |
| 1370 | /* The flag BDRV_REQ_COPY_ON_READ has reached its addressee */ |
| 1371 | flags &= ~BDRV_REQ_COPY_ON_READ; |
| 1372 | |
| 1373 | ret = bdrv_co_is_allocated(bs, offset, bytes, &pnum); |
| 1374 | if (ret < 0) { |
| 1375 | goto out; |
| 1376 | } |
| 1377 | |
| 1378 | if (!ret || pnum != bytes) { |
| 1379 | ret = bdrv_co_do_copy_on_readv(child, offset, bytes, |
| 1380 | qiov, qiov_offset, flags); |
| 1381 | goto out; |
| 1382 | } else if (flags & BDRV_REQ_PREFETCH) { |
| 1383 | goto out; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | /* Forward the request to the BlockDriver, possibly fragmenting it */ |
| 1388 | total_bytes = bdrv_co_getlength(bs); |
| 1389 | if (total_bytes < 0) { |
| 1390 | ret = total_bytes; |
| 1391 | goto out; |
| 1392 | } |
| 1393 | |
| 1394 | assert(!(flags & ~(bs->supported_read_flags | BDRV_REQ_REGISTERED_BUF))); |
| 1395 | |
| 1396 | max_bytes = ROUND_UP(MAX(0, total_bytes - offset), align); |
| 1397 | if (bytes <= max_bytes && bytes <= max_transfer) { |
| 1398 | ret = bdrv_driver_preadv(bs, offset, bytes, qiov, qiov_offset, flags); |
| 1399 | goto out; |
| 1400 | } |
| 1401 | |
| 1402 | while (bytes_remaining) { |
| 1403 | int64_t num; |
| 1404 | |
| 1405 | if (max_bytes) { |
| 1406 | num = MIN(bytes_remaining, MIN(max_bytes, max_transfer)); |
| 1407 | assert(num); |
| 1408 | |
| 1409 | ret = bdrv_driver_preadv(bs, offset + bytes - bytes_remaining, |
| 1410 | num, qiov, |
| 1411 | qiov_offset + bytes - bytes_remaining, |
| 1412 | flags); |
| 1413 | max_bytes -= num; |
| 1414 | } else { |
| 1415 | num = bytes_remaining; |
| 1416 | ret = qemu_iovec_memset(qiov, qiov_offset + bytes - bytes_remaining, |
| 1417 | 0, bytes_remaining); |
| 1418 | } |
| 1419 | if (ret < 0) { |
| 1420 | goto out; |
| 1421 | } |
| 1422 | bytes_remaining -= num; |
| 1423 | } |
| 1424 | |
| 1425 | out: |
| 1426 | return ret < 0 ? ret : 0; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * Request padding |
| 1431 | * |
| 1432 | * |<---- align ----->| |<----- align ---->| |
| 1433 | * |<- head ->|<------------- bytes ------------->|<-- tail -->| |
| 1434 | * | | | | | | |
| 1435 | * -*----------$-------*-------- ... --------*-----$------------*--- |
| 1436 | * | | | | | | |
| 1437 | * | offset | | end | |
| 1438 | * ALIGN_DOWN(offset) ALIGN_UP(offset) ALIGN_DOWN(end) ALIGN_UP(end) |
| 1439 | * [buf ... ) [tail_buf ) |
| 1440 | * |
| 1441 | * @buf is an aligned allocation needed to store @head and @tail paddings. @head |
| 1442 | * is placed at the beginning of @buf and @tail at the @end. |
| 1443 | * |
| 1444 | * @tail_buf is a pointer to sub-buffer, corresponding to align-sized chunk |
| 1445 | * around tail, if tail exists. |
| 1446 | * |
| 1447 | * @merge_reads is true for small requests, |
| 1448 | * if @buf_len == @head + bytes + @tail. In this case it is possible that both |
| 1449 | * head and tail exist but @buf_len == align and @tail_buf == @buf. |
| 1450 | * |
| 1451 | * @write is true for write requests, false for read requests. |
| 1452 | * |
| 1453 | * If padding makes the vector too long (exceeding IOV_MAX), then we need to |
| 1454 | * merge existing vector elements into a single one. @collapse_bounce_buf acts |
| 1455 | * as the bounce buffer in such cases. @pre_collapse_qiov has the pre-collapse |
| 1456 | * I/O vector elements so for read requests, the data can be copied back after |
| 1457 | * the read is done. |
| 1458 | */ |
| 1459 | typedef struct BdrvRequestPadding { |
| 1460 | uint8_t *buf; |
| 1461 | size_t buf_len; |
| 1462 | uint8_t *tail_buf; |
| 1463 | size_t head; |
| 1464 | size_t tail; |
| 1465 | bool merge_reads; |
| 1466 | bool write; |
| 1467 | QEMUIOVector local_qiov; |
| 1468 | |
| 1469 | uint8_t *collapse_bounce_buf; |
| 1470 | size_t collapse_len; |
| 1471 | QEMUIOVector pre_collapse_qiov; |
| 1472 | } BdrvRequestPadding; |
| 1473 | |
| 1474 | static bool bdrv_init_padding(BlockDriverState *bs, |
| 1475 | int64_t offset, int64_t bytes, |
| 1476 | bool write, |
| 1477 | BdrvRequestPadding *pad) |
| 1478 | { |
| 1479 | int64_t align = bs->bl.request_alignment; |
| 1480 | int64_t sum; |
| 1481 | |
| 1482 | bdrv_check_request(offset, bytes, &error_abort); |
| 1483 | assert(align <= INT_MAX); /* documented in block/block_int.h */ |
| 1484 | assert(align <= SIZE_MAX / 2); /* so we can allocate the buffer */ |
| 1485 | |
| 1486 | memset(pad, 0, sizeof(*pad)); |
| 1487 | |
| 1488 | pad->head = offset & (align - 1); |
| 1489 | pad->tail = ((offset + bytes) & (align - 1)); |
| 1490 | if (pad->tail) { |
| 1491 | pad->tail = align - pad->tail; |
| 1492 | } |
| 1493 | |
| 1494 | if (!pad->head && !pad->tail) { |
| 1495 | return false; |
| 1496 | } |
| 1497 | |
| 1498 | assert(bytes); /* Nothing good in aligning zero-length requests */ |
| 1499 | |
| 1500 | sum = pad->head + bytes + pad->tail; |
| 1501 | pad->buf_len = (sum > align && pad->head && pad->tail) ? 2 * align : align; |
| 1502 | pad->buf = qemu_blockalign(bs, pad->buf_len); |
| 1503 | pad->merge_reads = sum == pad->buf_len; |
| 1504 | if (pad->tail) { |
| 1505 | pad->tail_buf = pad->buf + pad->buf_len - align; |
| 1506 | } |
| 1507 | |
| 1508 | pad->write = write; |
| 1509 | |
| 1510 | return true; |
| 1511 | } |
| 1512 | |
| 1513 | static int coroutine_fn GRAPH_RDLOCK |
| 1514 | bdrv_padding_rmw_read(BdrvChild *child, BdrvTrackedRequest *req, |
| 1515 | BdrvRequestPadding *pad, bool zero_middle) |
| 1516 | { |
| 1517 | QEMUIOVector local_qiov; |
| 1518 | BlockDriverState *bs = child->bs; |
| 1519 | uint64_t align = bs->bl.request_alignment; |
| 1520 | int ret; |
| 1521 | |
| 1522 | assert(req->serialising && pad->buf); |
| 1523 | |
| 1524 | if (pad->head || pad->merge_reads) { |
| 1525 | int64_t bytes = pad->merge_reads ? pad->buf_len : align; |
| 1526 | |
| 1527 | qemu_iovec_init_buf(&local_qiov, pad->buf, bytes); |
| 1528 | |
| 1529 | if (pad->head) { |
| 1530 | bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD); |
| 1531 | } |
| 1532 | if (pad->merge_reads && pad->tail) { |
| 1533 | bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL); |
| 1534 | } |
| 1535 | ret = bdrv_aligned_preadv(child, req, req->overlap_offset, bytes, |
| 1536 | align, &local_qiov, 0, 0); |
| 1537 | if (ret < 0) { |
| 1538 | return ret; |
| 1539 | } |
| 1540 | if (pad->head) { |
| 1541 | bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD); |
| 1542 | } |
| 1543 | if (pad->merge_reads && pad->tail) { |
| 1544 | bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL); |
| 1545 | } |
| 1546 | |
| 1547 | if (pad->merge_reads) { |
| 1548 | goto zero_mem; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | if (pad->tail) { |
| 1553 | qemu_iovec_init_buf(&local_qiov, pad->tail_buf, align); |
| 1554 | |
| 1555 | bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL); |
| 1556 | ret = bdrv_aligned_preadv( |
| 1557 | child, req, |
| 1558 | req->overlap_offset + req->overlap_bytes - align, |
| 1559 | align, align, &local_qiov, 0, 0); |
| 1560 | if (ret < 0) { |
| 1561 | return ret; |
| 1562 | } |
| 1563 | bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL); |
| 1564 | } |
| 1565 | |
| 1566 | zero_mem: |
| 1567 | if (zero_middle) { |
| 1568 | memset(pad->buf + pad->head, 0, pad->buf_len - pad->head - pad->tail); |
| 1569 | } |
| 1570 | |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | /** |
| 1575 | * Free *pad's associated buffers, and perform any necessary finalization steps. |
| 1576 | */ |
| 1577 | static void bdrv_padding_finalize(BdrvRequestPadding *pad) |
| 1578 | { |
| 1579 | if (pad->collapse_bounce_buf) { |
| 1580 | if (!pad->write) { |
| 1581 | /* |
| 1582 | * If padding required elements in the vector to be collapsed into a |
| 1583 | * bounce buffer, copy the bounce buffer content back |
| 1584 | */ |
| 1585 | qemu_iovec_from_buf(&pad->pre_collapse_qiov, 0, |
| 1586 | pad->collapse_bounce_buf, pad->collapse_len); |
| 1587 | } |
| 1588 | qemu_vfree(pad->collapse_bounce_buf); |
| 1589 | qemu_iovec_destroy(&pad->pre_collapse_qiov); |
| 1590 | } |
| 1591 | if (pad->buf) { |
| 1592 | qemu_vfree(pad->buf); |
| 1593 | qemu_iovec_destroy(&pad->local_qiov); |
| 1594 | } |
| 1595 | memset(pad, 0, sizeof(*pad)); |
| 1596 | } |
| 1597 | |
| 1598 | /* |
| 1599 | * Create pad->local_qiov by wrapping @iov in the padding head and tail, while |
| 1600 | * ensuring that the resulting vector will not exceed IOV_MAX elements. |
| 1601 | * |
| 1602 | * To ensure this, when necessary, the first two or three elements of @iov are |
| 1603 | * merged into pad->collapse_bounce_buf and replaced by a reference to that |
| 1604 | * bounce buffer in pad->local_qiov. |
| 1605 | * |
| 1606 | * After performing a read request, the data from the bounce buffer must be |
| 1607 | * copied back into pad->pre_collapse_qiov (e.g. by bdrv_padding_finalize()). |
| 1608 | */ |
| 1609 | static int bdrv_create_padded_qiov(BlockDriverState *bs, |
| 1610 | BdrvRequestPadding *pad, |
| 1611 | struct iovec *iov, int niov, |
| 1612 | size_t iov_offset, size_t bytes) |
| 1613 | { |
| 1614 | int padded_niov, surplus_count, collapse_count; |
| 1615 | |
| 1616 | /* Assert this invariant */ |
| 1617 | assert(niov <= IOV_MAX); |
| 1618 | |
| 1619 | /* |
| 1620 | * Cannot pad if resulting length would exceed SIZE_MAX. Returning an error |
| 1621 | * to the guest is not ideal, but there is little else we can do. At least |
| 1622 | * this will practically never happen on 64-bit systems. |
| 1623 | */ |
| 1624 | if (SIZE_MAX - pad->head < bytes || |
| 1625 | SIZE_MAX - pad->head - bytes < pad->tail) |
| 1626 | { |
| 1627 | return -EINVAL; |
| 1628 | } |
| 1629 | |
| 1630 | /* Length of the resulting IOV if we just concatenated everything */ |
| 1631 | padded_niov = !!pad->head + niov + !!pad->tail; |
| 1632 | |
| 1633 | qemu_iovec_init(&pad->local_qiov, MIN(padded_niov, IOV_MAX)); |
| 1634 | |
| 1635 | if (pad->head) { |
| 1636 | qemu_iovec_add(&pad->local_qiov, pad->buf, pad->head); |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * If padded_niov > IOV_MAX, we cannot just concatenate everything. |
| 1641 | * Instead, merge the first two or three elements of @iov to reduce the |
| 1642 | * number of vector elements as necessary. |
| 1643 | */ |
| 1644 | if (padded_niov > IOV_MAX) { |
| 1645 | /* |
| 1646 | * Only head and tail can have lead to the number of entries exceeding |
| 1647 | * IOV_MAX, so we can exceed it by the head and tail at most. We need |
| 1648 | * to reduce the number of elements by `surplus_count`, so we merge that |
| 1649 | * many elements plus one into one element. |
| 1650 | */ |
| 1651 | surplus_count = padded_niov - IOV_MAX; |
| 1652 | assert(surplus_count <= !!pad->head + !!pad->tail); |
| 1653 | collapse_count = surplus_count + 1; |
| 1654 | |
| 1655 | /* |
| 1656 | * Move the elements to collapse into `pad->pre_collapse_qiov`, then |
| 1657 | * advance `iov` (and associated variables) by those elements. |
| 1658 | */ |
| 1659 | qemu_iovec_init(&pad->pre_collapse_qiov, collapse_count); |
| 1660 | qemu_iovec_concat_iov(&pad->pre_collapse_qiov, iov, |
| 1661 | collapse_count, iov_offset, SIZE_MAX); |
| 1662 | iov += collapse_count; |
| 1663 | iov_offset = 0; |
| 1664 | niov -= collapse_count; |
| 1665 | bytes -= pad->pre_collapse_qiov.size; |
| 1666 | |
| 1667 | /* |
| 1668 | * Construct the bounce buffer to match the length of the to-collapse |
| 1669 | * vector elements, and for write requests, initialize it with the data |
| 1670 | * from those elements. Then add it to `pad->local_qiov`. |
| 1671 | */ |
| 1672 | pad->collapse_len = pad->pre_collapse_qiov.size; |
| 1673 | pad->collapse_bounce_buf = qemu_blockalign(bs, pad->collapse_len); |
| 1674 | if (pad->write) { |
| 1675 | qemu_iovec_to_buf(&pad->pre_collapse_qiov, 0, |
| 1676 | pad->collapse_bounce_buf, pad->collapse_len); |
| 1677 | } |
| 1678 | qemu_iovec_add(&pad->local_qiov, |
| 1679 | pad->collapse_bounce_buf, pad->collapse_len); |
| 1680 | } |
| 1681 | |
| 1682 | qemu_iovec_concat_iov(&pad->local_qiov, iov, niov, iov_offset, bytes); |
| 1683 | |
| 1684 | if (pad->tail) { |
| 1685 | qemu_iovec_add(&pad->local_qiov, |
| 1686 | pad->buf + pad->buf_len - pad->tail, pad->tail); |
| 1687 | } |
| 1688 | |
| 1689 | assert(pad->local_qiov.niov == MIN(padded_niov, IOV_MAX)); |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
| 1693 | /* |
| 1694 | * bdrv_pad_request |
| 1695 | * |
| 1696 | * Exchange request parameters with padded request if needed. Don't include RMW |
| 1697 | * read of padding, bdrv_padding_rmw_read() should be called separately if |
| 1698 | * needed. |
| 1699 | * |
| 1700 | * @write is true for write requests, false for read requests. |
| 1701 | * |
| 1702 | * Request parameters (@qiov, &qiov_offset, &offset, &bytes) are in-out: |
| 1703 | * - on function start they represent original request |
| 1704 | * - on failure or when padding is not needed they are unchanged |
| 1705 | * - on success when padding is needed they represent padded request |
| 1706 | */ |
| 1707 | static int bdrv_pad_request(BlockDriverState *bs, |
| 1708 | QEMUIOVector **qiov, size_t *qiov_offset, |
| 1709 | int64_t *offset, int64_t *bytes, |
| 1710 | bool write, |
| 1711 | BdrvRequestPadding *pad, bool *padded, |
| 1712 | BdrvRequestFlags *flags) |
| 1713 | { |
| 1714 | int ret; |
| 1715 | struct iovec *sliced_iov; |
| 1716 | int sliced_niov; |
| 1717 | size_t sliced_head, sliced_tail; |
| 1718 | |
| 1719 | /* Should have been checked by the caller already */ |
| 1720 | ret = bdrv_check_request32(*offset, *bytes, *qiov, *qiov_offset); |
| 1721 | if (ret < 0) { |
| 1722 | return ret; |
| 1723 | } |
| 1724 | |
| 1725 | if (!bdrv_init_padding(bs, *offset, *bytes, write, pad)) { |
| 1726 | if (padded) { |
| 1727 | *padded = false; |
| 1728 | } |
| 1729 | return 0; |
| 1730 | } |
| 1731 | |
| 1732 | /* |
| 1733 | * For prefetching in stream_populate(), no qiov is passed along, because |
| 1734 | * only copy-on-read matters. |
| 1735 | */ |
| 1736 | if (*qiov) { |
| 1737 | sliced_iov = qemu_iovec_slice(*qiov, *qiov_offset, *bytes, |
| 1738 | &sliced_head, &sliced_tail, |
| 1739 | &sliced_niov); |
| 1740 | |
| 1741 | /* Guaranteed by bdrv_check_request32() */ |
| 1742 | assert(*bytes <= SIZE_MAX); |
| 1743 | ret = bdrv_create_padded_qiov(bs, pad, sliced_iov, sliced_niov, |
| 1744 | sliced_head, *bytes); |
| 1745 | if (ret < 0) { |
| 1746 | bdrv_padding_finalize(pad); |
| 1747 | return ret; |
| 1748 | } |
| 1749 | *qiov = &pad->local_qiov; |
| 1750 | *qiov_offset = 0; |
| 1751 | } |
| 1752 | |
| 1753 | *bytes += pad->head + pad->tail; |
| 1754 | *offset -= pad->head; |
| 1755 | if (padded) { |
| 1756 | *padded = true; |
| 1757 | } |
| 1758 | if (flags) { |
| 1759 | /* Can't use optimization hint with bounce buffer */ |
| 1760 | *flags &= ~BDRV_REQ_REGISTERED_BUF; |
| 1761 | } |
| 1762 | |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
| 1766 | int coroutine_fn bdrv_co_preadv(BdrvChild *child, |
| 1767 | int64_t offset, int64_t bytes, QEMUIOVector *qiov, |
| 1768 | BdrvRequestFlags flags) |
| 1769 | { |
| 1770 | IO_CODE(); |
| 1771 | return bdrv_co_preadv_part(child, offset, bytes, qiov, 0, flags); |
| 1772 | } |
| 1773 | |
| 1774 | int coroutine_fn bdrv_co_preadv_part(BdrvChild *child, |
| 1775 | int64_t offset, int64_t bytes, |
| 1776 | QEMUIOVector *qiov, size_t qiov_offset, |
| 1777 | BdrvRequestFlags flags) |
| 1778 | { |
| 1779 | BlockDriverState *bs = child->bs; |
| 1780 | BdrvTrackedRequest req; |
| 1781 | BdrvRequestPadding pad; |
| 1782 | int ret; |
| 1783 | IO_CODE(); |
| 1784 | |
| 1785 | trace_bdrv_co_preadv_part(bs, offset, bytes, flags); |
| 1786 | |
| 1787 | if (!bdrv_co_is_inserted(bs)) { |
| 1788 | return -ENOMEDIUM; |
| 1789 | } |
| 1790 | |
| 1791 | ret = bdrv_check_request32(offset, bytes, qiov, qiov_offset); |
| 1792 | if (ret < 0) { |
| 1793 | return ret; |
| 1794 | } |
| 1795 | |
| 1796 | if (bytes == 0 && !QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)) { |
| 1797 | /* |
| 1798 | * Aligning zero request is nonsense. Even if driver has special meaning |
| 1799 | * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass |
| 1800 | * it to driver due to request_alignment. |
| 1801 | * |
| 1802 | * Still, no reason to return an error if someone do unaligned |
| 1803 | * zero-length read occasionally. |
| 1804 | */ |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
| 1808 | bdrv_inc_in_flight(bs); |
| 1809 | |
| 1810 | /* Don't do copy-on-read if we read data before write operation */ |
| 1811 | if (qatomic_read(&bs->copy_on_read)) { |
| 1812 | flags |= BDRV_REQ_COPY_ON_READ; |
| 1813 | } |
| 1814 | |
| 1815 | ret = bdrv_pad_request(bs, &qiov, &qiov_offset, &offset, &bytes, false, |
| 1816 | &pad, NULL, &flags); |
| 1817 | if (ret < 0) { |
| 1818 | goto fail; |
| 1819 | } |
| 1820 | |
| 1821 | tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_READ); |
| 1822 | ret = bdrv_aligned_preadv(child, &req, offset, bytes, |
| 1823 | bs->bl.request_alignment, |
| 1824 | qiov, qiov_offset, flags); |
| 1825 | tracked_request_end(&req); |
| 1826 | bdrv_padding_finalize(&pad); |
| 1827 | |
| 1828 | fail: |
| 1829 | bdrv_dec_in_flight(bs); |
| 1830 | |
| 1831 | return ret; |
| 1832 | } |
| 1833 | |
| 1834 | static int coroutine_fn GRAPH_RDLOCK |
| 1835 | bdrv_co_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, |
| 1836 | BdrvRequestFlags flags) |
| 1837 | { |
| 1838 | BlockDriver *drv = bs->drv; |
| 1839 | QEMUIOVector qiov; |
| 1840 | void *buf = NULL; |
| 1841 | int ret = 0; |
| 1842 | bool need_flush = false; |
| 1843 | int head = 0; |
| 1844 | int tail = 0; |
| 1845 | |
| 1846 | int64_t max_write_zeroes = MIN_NON_ZERO(bs->bl.max_pwrite_zeroes, |
| 1847 | INT64_MAX); |
| 1848 | int alignment = MAX(bs->bl.pwrite_zeroes_alignment, |
| 1849 | bs->bl.request_alignment); |
| 1850 | int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer, MAX_BOUNCE_BUFFER); |
| 1851 | |
| 1852 | assert_bdrv_graph_readable(); |
| 1853 | bdrv_check_request(offset, bytes, &error_abort); |
| 1854 | |
| 1855 | if (!drv) { |
| 1856 | return -ENOMEDIUM; |
| 1857 | } |
| 1858 | |
| 1859 | if ((flags & ~bs->supported_zero_flags) & BDRV_REQ_NO_FALLBACK) { |
| 1860 | return -ENOTSUP; |
| 1861 | } |
| 1862 | |
| 1863 | /* By definition there is no user buffer so this flag doesn't make sense */ |
| 1864 | if (flags & BDRV_REQ_REGISTERED_BUF) { |
| 1865 | return -EINVAL; |
| 1866 | } |
| 1867 | |
| 1868 | /* If opened with discard=off we should never unmap. */ |
| 1869 | if (!(bs->open_flags & BDRV_O_UNMAP)) { |
| 1870 | flags &= ~BDRV_REQ_MAY_UNMAP; |
| 1871 | } |
| 1872 | |
| 1873 | /* Invalidate the cached block-status data range if this write overlaps */ |
| 1874 | bdrv_bsc_invalidate_range(bs, offset, bytes); |
| 1875 | |
| 1876 | assert(alignment % bs->bl.request_alignment == 0); |
| 1877 | head = offset % alignment; |
| 1878 | tail = (offset + bytes) % alignment; |
| 1879 | max_write_zeroes = QEMU_ALIGN_DOWN(max_write_zeroes, alignment); |
| 1880 | assert(max_write_zeroes >= bs->bl.request_alignment); |
| 1881 | |
| 1882 | while (bytes > 0 && !ret) { |
| 1883 | int64_t num = bytes; |
| 1884 | |
| 1885 | /* Align request. Block drivers can expect the "bulk" of the request |
| 1886 | * to be aligned, and that unaligned requests do not cross cluster |
| 1887 | * boundaries. |
| 1888 | */ |
| 1889 | if (head) { |
| 1890 | /* Make a small request up to the first aligned sector. For |
| 1891 | * convenience, limit this request to max_transfer even if |
| 1892 | * we don't need to fall back to writes. */ |
| 1893 | num = MIN(MIN(bytes, max_transfer), alignment - head); |
| 1894 | head = (head + num) % alignment; |
| 1895 | assert(num < max_write_zeroes); |
| 1896 | } else if (tail && num > alignment) { |
| 1897 | /* Shorten the request to the last aligned sector. */ |
| 1898 | num -= tail; |
| 1899 | } |
| 1900 | |
| 1901 | /* limit request size */ |
| 1902 | if (num > max_write_zeroes) { |
| 1903 | num = max_write_zeroes; |
| 1904 | } |
| 1905 | |
| 1906 | ret = -ENOTSUP; |
| 1907 | /* First try the efficient write zeroes operation */ |
| 1908 | if (drv->bdrv_co_pwrite_zeroes) { |
| 1909 | ret = drv->bdrv_co_pwrite_zeroes(bs, offset, num, |
| 1910 | flags & bs->supported_zero_flags); |
| 1911 | if (ret != -ENOTSUP && (flags & BDRV_REQ_FUA) && |
| 1912 | !(bs->supported_zero_flags & BDRV_REQ_FUA)) { |
| 1913 | need_flush = true; |
| 1914 | } |
| 1915 | } else { |
| 1916 | assert(!bs->supported_zero_flags); |
| 1917 | } |
| 1918 | |
| 1919 | /* |
| 1920 | * TODO The ret == -EINVAL && num < alignment case is a workaround for |
| 1921 | * when request_alignment is 1 on files with cache=writeback. The Linux |
| 1922 | * ioctl(BLKZEROOUT) requires block alignment and will fail with |
| 1923 | * EINVAL. The block layer should align the request to |
| 1924 | * write_zeroes_alignment instead of trying the syscall, failing, and |
| 1925 | * falling back to a bounce buffer. Doing that is not easy so for now |
| 1926 | * we use a bounce buffer: |
| 1927 | * https://lore.kernel.org/qemu-devel/20260109120837.2772961-1-f.ebner@proxmox.com/ |
| 1928 | */ |
| 1929 | if ((ret == -ENOTSUP || (ret == -EINVAL && num < alignment)) && |
| 1930 | !(flags & BDRV_REQ_NO_FALLBACK)) { |
| 1931 | /* Fall back to bounce buffer if write zeroes is unsupported */ |
| 1932 | BdrvRequestFlags write_flags = flags & ~BDRV_REQ_ZERO_WRITE; |
| 1933 | |
| 1934 | if ((flags & BDRV_REQ_FUA) && |
| 1935 | !(bs->supported_write_flags & BDRV_REQ_FUA)) { |
| 1936 | /* No need for bdrv_driver_pwrite() to do a fallback |
| 1937 | * flush on each chunk; use just one at the end */ |
| 1938 | write_flags &= ~BDRV_REQ_FUA; |
| 1939 | need_flush = true; |
| 1940 | } |
| 1941 | num = MIN(num, max_transfer); |
| 1942 | if (buf == NULL) { |
| 1943 | buf = qemu_try_blockalign0(bs, num); |
| 1944 | if (buf == NULL) { |
| 1945 | ret = -ENOMEM; |
| 1946 | goto fail; |
| 1947 | } |
| 1948 | } |
| 1949 | qemu_iovec_init_buf(&qiov, buf, num); |
| 1950 | |
| 1951 | ret = bdrv_driver_pwritev(bs, offset, num, &qiov, 0, write_flags); |
| 1952 | |
| 1953 | /* Keep bounce buffer around if it is big enough for all |
| 1954 | * all future requests. |
| 1955 | */ |
| 1956 | if (num < max_transfer) { |
| 1957 | qemu_vfree(buf); |
| 1958 | buf = NULL; |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | offset += num; |
| 1963 | bytes -= num; |
| 1964 | } |
| 1965 | |
| 1966 | fail: |
| 1967 | if (ret == 0 && need_flush) { |
| 1968 | ret = bdrv_co_flush(bs); |
| 1969 | } |
| 1970 | qemu_vfree(buf); |
| 1971 | return ret; |
| 1972 | } |
| 1973 | |
| 1974 | static inline int coroutine_fn GRAPH_RDLOCK |
| 1975 | bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, int64_t bytes, |
| 1976 | BdrvTrackedRequest *req, int flags) |
| 1977 | { |
| 1978 | BlockDriverState *bs = child->bs; |
| 1979 | |
| 1980 | bdrv_check_request(offset, bytes, &error_abort); |
| 1981 | |
| 1982 | if (bdrv_is_read_only(bs)) { |
| 1983 | return -EPERM; |
| 1984 | } |
| 1985 | |
| 1986 | assert(!(bs->open_flags & BDRV_O_INACTIVE)); |
| 1987 | assert((bs->open_flags & BDRV_O_NO_IO) == 0); |
| 1988 | assert(!(flags & ~BDRV_REQ_MASK)); |
| 1989 | assert(!((flags & BDRV_REQ_NO_WAIT) && !(flags & BDRV_REQ_SERIALISING))); |
| 1990 | |
| 1991 | if (flags & BDRV_REQ_SERIALISING) { |
| 1992 | QEMU_LOCK_GUARD(&bs->reqs_lock); |
| 1993 | |
| 1994 | tracked_request_set_serialising(req, bdrv_get_cluster_size(bs)); |
| 1995 | |
| 1996 | if ((flags & BDRV_REQ_NO_WAIT) && bdrv_find_conflicting_request(req)) { |
| 1997 | return -EBUSY; |
| 1998 | } |
| 1999 | |
| 2000 | bdrv_wait_serialising_requests_locked(req); |
| 2001 | } else { |
| 2002 | bdrv_wait_serialising_requests(req); |
| 2003 | } |
| 2004 | |
| 2005 | assert(req->overlap_offset <= offset); |
| 2006 | assert(offset + bytes <= req->overlap_offset + req->overlap_bytes); |
| 2007 | assert(offset + bytes <= bs->total_sectors * BDRV_SECTOR_SIZE || |
| 2008 | child->perm & BLK_PERM_RESIZE); |
| 2009 | |
| 2010 | switch (req->type) { |
| 2011 | case BDRV_TRACKED_WRITE: |
| 2012 | case BDRV_TRACKED_DISCARD: |
| 2013 | if (flags & BDRV_REQ_WRITE_UNCHANGED) { |
| 2014 | assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE)); |
| 2015 | } else { |
| 2016 | assert(child->perm & BLK_PERM_WRITE); |
| 2017 | } |
| 2018 | bdrv_write_threshold_check_write(bs, offset, bytes); |
| 2019 | return 0; |
| 2020 | case BDRV_TRACKED_TRUNCATE: |
| 2021 | assert(child->perm & BLK_PERM_RESIZE); |
| 2022 | return 0; |
| 2023 | default: |
| 2024 | abort(); |
| 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | static inline void coroutine_fn GRAPH_RDLOCK |
| 2029 | bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, int64_t bytes, |
| 2030 | BdrvTrackedRequest *req, int ret) |
| 2031 | { |
| 2032 | int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE); |
| 2033 | BlockDriverState *bs = child->bs; |
| 2034 | |
| 2035 | bdrv_check_request(offset, bytes, &error_abort); |
| 2036 | |
| 2037 | qatomic_inc(&bs->write_gen); |
| 2038 | |
| 2039 | /* |
| 2040 | * Discard cannot extend the image, but in error handling cases, such as |
| 2041 | * when reverting a qcow2 cluster allocation, the discarded range can pass |
| 2042 | * the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD |
| 2043 | * here. Instead, just skip it, since semantically a discard request |
| 2044 | * beyond EOF cannot expand the image anyway. |
| 2045 | */ |
| 2046 | if (ret == 0 && |
| 2047 | (req->type == BDRV_TRACKED_TRUNCATE || |
| 2048 | end_sector > bs->total_sectors) && |
| 2049 | req->type != BDRV_TRACKED_DISCARD) { |
| 2050 | bs->total_sectors = end_sector; |
| 2051 | bdrv_co_parent_cb_resize(bs); |
| 2052 | bdrv_dirty_bitmap_truncate(bs, end_sector << BDRV_SECTOR_BITS); |
| 2053 | } |
| 2054 | if (req->bytes) { |
| 2055 | switch (req->type) { |
| 2056 | case BDRV_TRACKED_WRITE: |
| 2057 | { |
| 2058 | uint64_t new = offset + bytes; |
| 2059 | uint64_t old = qatomic_read(&bs->wr_highest_offset); |
| 2060 | |
| 2061 | while (old < new) { |
| 2062 | old = qatomic_cmpxchg(&bs->wr_highest_offset, old, new); |
| 2063 | } |
| 2064 | } |
| 2065 | /* fall through, to set dirty bits */ |
| 2066 | case BDRV_TRACKED_DISCARD: |
| 2067 | bdrv_set_dirty(bs, offset, bytes); |
| 2068 | break; |
| 2069 | default: |
| 2070 | break; |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | /* |
| 2076 | * Forwards an already correctly aligned write request to the BlockDriver, |
| 2077 | * after possibly fragmenting it. |
| 2078 | */ |
| 2079 | static int coroutine_fn GRAPH_RDLOCK |
| 2080 | bdrv_aligned_pwritev(BdrvChild *child, BdrvTrackedRequest *req, |
| 2081 | int64_t offset, int64_t bytes, int64_t align, |
| 2082 | QEMUIOVector *qiov, size_t qiov_offset, |
| 2083 | BdrvRequestFlags flags) |
| 2084 | { |
| 2085 | BlockDriverState *bs = child->bs; |
| 2086 | BlockDriver *drv = bs->drv; |
| 2087 | int ret; |
| 2088 | |
| 2089 | int64_t bytes_remaining = bytes; |
| 2090 | int max_transfer; |
| 2091 | |
| 2092 | bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort); |
| 2093 | |
| 2094 | if (!drv) { |
| 2095 | return -ENOMEDIUM; |
| 2096 | } |
| 2097 | |
| 2098 | if (bdrv_has_readonly_bitmaps(bs)) { |
| 2099 | return -EPERM; |
| 2100 | } |
| 2101 | |
| 2102 | assert(is_power_of_2(align)); |
| 2103 | assert((offset & (align - 1)) == 0); |
| 2104 | assert((bytes & (align - 1)) == 0); |
| 2105 | max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX), |
| 2106 | align); |
| 2107 | |
| 2108 | ret = bdrv_co_write_req_prepare(child, offset, bytes, req, flags); |
| 2109 | |
| 2110 | if (!ret && bs->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF && |
| 2111 | !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_pwrite_zeroes && |
| 2112 | qemu_iovec_is_zero(qiov, qiov_offset, bytes)) { |
| 2113 | flags |= BDRV_REQ_ZERO_WRITE; |
| 2114 | if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP) { |
| 2115 | flags |= BDRV_REQ_MAY_UNMAP; |
| 2116 | } |
| 2117 | |
| 2118 | /* Can't use optimization hint with bufferless zero write */ |
| 2119 | flags &= ~BDRV_REQ_REGISTERED_BUF; |
| 2120 | } |
| 2121 | |
| 2122 | if (ret < 0) { |
| 2123 | /* Do nothing, write notifier decided to fail this request */ |
| 2124 | } else if (flags & BDRV_REQ_ZERO_WRITE) { |
| 2125 | bdrv_co_debug_event(bs, BLKDBG_PWRITEV_ZERO); |
| 2126 | ret = bdrv_co_do_pwrite_zeroes(bs, offset, bytes, flags); |
| 2127 | } else if (flags & BDRV_REQ_WRITE_COMPRESSED) { |
| 2128 | ret = bdrv_driver_pwritev_compressed(bs, offset, bytes, |
| 2129 | qiov, qiov_offset); |
| 2130 | } else if (bytes <= max_transfer) { |
| 2131 | bdrv_co_debug_event(bs, BLKDBG_PWRITEV); |
| 2132 | ret = bdrv_driver_pwritev(bs, offset, bytes, qiov, qiov_offset, flags); |
| 2133 | } else { |
| 2134 | bdrv_co_debug_event(bs, BLKDBG_PWRITEV); |
| 2135 | while (bytes_remaining) { |
| 2136 | int num = MIN(bytes_remaining, max_transfer); |
| 2137 | int local_flags = flags; |
| 2138 | |
| 2139 | assert(num); |
| 2140 | if (num < bytes_remaining && (flags & BDRV_REQ_FUA) && |
| 2141 | !(bs->supported_write_flags & BDRV_REQ_FUA)) { |
| 2142 | /* If FUA is going to be emulated by flush, we only |
| 2143 | * need to flush on the last iteration */ |
| 2144 | local_flags &= ~BDRV_REQ_FUA; |
| 2145 | } |
| 2146 | |
| 2147 | ret = bdrv_driver_pwritev(bs, offset + bytes - bytes_remaining, |
| 2148 | num, qiov, |
| 2149 | qiov_offset + bytes - bytes_remaining, |
| 2150 | local_flags); |
| 2151 | if (ret < 0) { |
| 2152 | break; |
| 2153 | } |
| 2154 | bytes_remaining -= num; |
| 2155 | } |
| 2156 | } |
| 2157 | bdrv_co_debug_event(bs, BLKDBG_PWRITEV_DONE); |
| 2158 | |
| 2159 | if (ret >= 0) { |
| 2160 | ret = 0; |
| 2161 | } |
| 2162 | bdrv_co_write_req_finish(child, offset, bytes, req, ret); |
| 2163 | |
| 2164 | return ret; |
| 2165 | } |
| 2166 | |
| 2167 | static int coroutine_fn GRAPH_RDLOCK |
| 2168 | bdrv_co_do_zero_pwritev(BdrvChild *child, int64_t offset, int64_t bytes, |
| 2169 | BdrvRequestFlags flags, BdrvTrackedRequest *req) |
| 2170 | { |
| 2171 | BlockDriverState *bs = child->bs; |
| 2172 | QEMUIOVector local_qiov; |
| 2173 | uint64_t align = bs->bl.request_alignment; |
| 2174 | int ret = 0; |
| 2175 | bool padding; |
| 2176 | BdrvRequestPadding pad; |
| 2177 | |
| 2178 | /* This flag doesn't make sense for padding or zero writes */ |
| 2179 | flags &= ~BDRV_REQ_REGISTERED_BUF; |
| 2180 | |
| 2181 | padding = bdrv_init_padding(bs, offset, bytes, true, &pad); |
| 2182 | if (padding) { |
| 2183 | assert(!(flags & BDRV_REQ_NO_WAIT)); |
| 2184 | bdrv_make_request_serialising(req, align); |
| 2185 | |
| 2186 | bdrv_padding_rmw_read(child, req, &pad, true); |
| 2187 | |
| 2188 | if (pad.head || pad.merge_reads) { |
| 2189 | int64_t aligned_offset = offset & ~(align - 1); |
| 2190 | int64_t write_bytes = pad.merge_reads ? pad.buf_len : align; |
| 2191 | |
| 2192 | qemu_iovec_init_buf(&local_qiov, pad.buf, write_bytes); |
| 2193 | ret = bdrv_aligned_pwritev(child, req, aligned_offset, write_bytes, |
| 2194 | align, &local_qiov, 0, |
| 2195 | flags & ~BDRV_REQ_ZERO_WRITE); |
| 2196 | if (ret < 0 || pad.merge_reads) { |
| 2197 | /* Error or all work is done */ |
| 2198 | goto out; |
| 2199 | } |
| 2200 | offset += write_bytes - pad.head; |
| 2201 | bytes -= write_bytes - pad.head; |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | assert(!bytes || (offset & (align - 1)) == 0); |
| 2206 | if (bytes >= align) { |
| 2207 | /* Write the aligned part in the middle. */ |
| 2208 | int64_t aligned_bytes = bytes & ~(align - 1); |
| 2209 | ret = bdrv_aligned_pwritev(child, req, offset, aligned_bytes, align, |
| 2210 | NULL, 0, flags); |
| 2211 | if (ret < 0) { |
| 2212 | goto out; |
| 2213 | } |
| 2214 | bytes -= aligned_bytes; |
| 2215 | offset += aligned_bytes; |
| 2216 | } |
| 2217 | |
| 2218 | assert(!bytes || (offset & (align - 1)) == 0); |
| 2219 | if (bytes) { |
| 2220 | assert(align == pad.tail + bytes); |
| 2221 | |
| 2222 | qemu_iovec_init_buf(&local_qiov, pad.tail_buf, align); |
| 2223 | ret = bdrv_aligned_pwritev(child, req, offset, align, align, |
| 2224 | &local_qiov, 0, |
| 2225 | flags & ~BDRV_REQ_ZERO_WRITE); |
| 2226 | } |
| 2227 | |
| 2228 | out: |
| 2229 | bdrv_padding_finalize(&pad); |
| 2230 | |
| 2231 | return ret; |
| 2232 | } |
| 2233 | |
| 2234 | /* |
| 2235 | * Handle a write request in coroutine context |
| 2236 | */ |
| 2237 | int coroutine_fn bdrv_co_pwritev(BdrvChild *child, |
| 2238 | int64_t offset, int64_t bytes, QEMUIOVector *qiov, |
| 2239 | BdrvRequestFlags flags) |
| 2240 | { |
| 2241 | IO_CODE(); |
| 2242 | return bdrv_co_pwritev_part(child, offset, bytes, qiov, 0, flags); |
| 2243 | } |
| 2244 | |
| 2245 | int coroutine_fn bdrv_co_pwritev_part(BdrvChild *child, |
| 2246 | int64_t offset, int64_t bytes, QEMUIOVector *qiov, size_t qiov_offset, |
| 2247 | BdrvRequestFlags flags) |
| 2248 | { |
| 2249 | BlockDriverState *bs = child->bs; |
| 2250 | BdrvTrackedRequest req; |
| 2251 | uint64_t align = bs->bl.request_alignment; |
| 2252 | BdrvRequestPadding pad; |
| 2253 | int ret; |
| 2254 | bool padded = false; |
| 2255 | IO_CODE(); |
| 2256 | |
| 2257 | trace_bdrv_co_pwritev_part(child->bs, offset, bytes, flags); |
| 2258 | |
| 2259 | if (!bdrv_co_is_inserted(bs)) { |
| 2260 | return -ENOMEDIUM; |
| 2261 | } |
| 2262 | |
| 2263 | if (flags & BDRV_REQ_ZERO_WRITE) { |
| 2264 | ret = bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, NULL); |
| 2265 | } else { |
| 2266 | ret = bdrv_check_request32(offset, bytes, qiov, qiov_offset); |
| 2267 | } |
| 2268 | if (ret < 0) { |
| 2269 | return ret; |
| 2270 | } |
| 2271 | |
| 2272 | /* If the request is misaligned then we can't make it efficient */ |
| 2273 | if ((flags & BDRV_REQ_NO_FALLBACK) && |
| 2274 | !QEMU_IS_ALIGNED(offset | bytes, align)) |
| 2275 | { |
| 2276 | return -ENOTSUP; |
| 2277 | } |
| 2278 | |
| 2279 | if (bytes == 0 && !QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)) { |
| 2280 | /* |
| 2281 | * Aligning zero request is nonsense. Even if driver has special meaning |
| 2282 | * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass |
| 2283 | * it to driver due to request_alignment. |
| 2284 | * |
| 2285 | * Still, no reason to return an error if someone do unaligned |
| 2286 | * zero-length write occasionally. |
| 2287 | */ |
| 2288 | return 0; |
| 2289 | } |
| 2290 | |
| 2291 | if (!(flags & BDRV_REQ_ZERO_WRITE)) { |
| 2292 | /* |
| 2293 | * Pad request for following read-modify-write cycle. |
| 2294 | * bdrv_co_do_zero_pwritev() does aligning by itself, so, we do |
| 2295 | * alignment only if there is no ZERO flag. |
| 2296 | */ |
| 2297 | ret = bdrv_pad_request(bs, &qiov, &qiov_offset, &offset, &bytes, true, |
| 2298 | &pad, &padded, &flags); |
| 2299 | if (ret < 0) { |
| 2300 | return ret; |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | bdrv_inc_in_flight(bs); |
| 2305 | tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE); |
| 2306 | |
| 2307 | if (flags & BDRV_REQ_ZERO_WRITE) { |
| 2308 | assert(!padded); |
| 2309 | ret = bdrv_co_do_zero_pwritev(child, offset, bytes, flags, &req); |
| 2310 | goto out; |
| 2311 | } |
| 2312 | |
| 2313 | if (padded) { |
| 2314 | /* |
| 2315 | * Request was unaligned to request_alignment and therefore |
| 2316 | * padded. We are going to do read-modify-write, and must |
| 2317 | * serialize the request to prevent interactions of the |
| 2318 | * widened region with other transactions. |
| 2319 | */ |
| 2320 | assert(!(flags & BDRV_REQ_NO_WAIT)); |
| 2321 | bdrv_make_request_serialising(&req, align); |
| 2322 | bdrv_padding_rmw_read(child, &req, &pad, false); |
| 2323 | } |
| 2324 | |
| 2325 | ret = bdrv_aligned_pwritev(child, &req, offset, bytes, align, |
| 2326 | qiov, qiov_offset, flags); |
| 2327 | |
| 2328 | bdrv_padding_finalize(&pad); |
| 2329 | |
| 2330 | out: |
| 2331 | tracked_request_end(&req); |
| 2332 | bdrv_dec_in_flight(bs); |
| 2333 | |
| 2334 | return ret; |
| 2335 | } |
| 2336 | |
| 2337 | int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset, |
| 2338 | int64_t bytes, BdrvRequestFlags flags) |
| 2339 | { |
| 2340 | IO_CODE(); |
| 2341 | trace_bdrv_co_pwrite_zeroes(child->bs, offset, bytes, flags); |
| 2342 | assert_bdrv_graph_readable(); |
| 2343 | |
| 2344 | return bdrv_co_pwritev(child, offset, bytes, NULL, |
| 2345 | BDRV_REQ_ZERO_WRITE | flags); |
| 2346 | } |
| 2347 | |
| 2348 | /* |
| 2349 | * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not. |
| 2350 | */ |
| 2351 | int bdrv_flush_all(void) |
| 2352 | { |
| 2353 | BdrvNextIterator it; |
| 2354 | BlockDriverState *bs = NULL; |
| 2355 | int result = 0; |
| 2356 | |
| 2357 | GLOBAL_STATE_CODE(); |
| 2358 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 2359 | |
| 2360 | /* |
| 2361 | * bdrv queue is managed by record/replay, |
| 2362 | * creating new flush request for stopping |
| 2363 | * the VM may break the determinism |
| 2364 | */ |
| 2365 | if (replay_events_enabled()) { |
| 2366 | return result; |
| 2367 | } |
| 2368 | |
| 2369 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
| 2370 | int ret = bdrv_flush(bs); |
| 2371 | if (ret < 0 && !result) { |
| 2372 | result = ret; |
| 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | return result; |
| 2377 | } |
| 2378 | |
| 2379 | /* |
| 2380 | * Returns the allocation status of the specified sectors. |
| 2381 | * Drivers not implementing the functionality are assumed to not support |
| 2382 | * backing files, hence all their sectors are reported as allocated. |
| 2383 | * |
| 2384 | * 'mode' serves as a hint as to which results are favored; see the |
| 2385 | * BDRV_WANT_* macros for details. |
| 2386 | * |
| 2387 | * If 'offset' is beyond the end of the disk image the return value is |
| 2388 | * BDRV_BLOCK_EOF and 'pnum' is set to 0. |
| 2389 | * |
| 2390 | * 'bytes' is the max value 'pnum' should be set to. If bytes goes |
| 2391 | * beyond the end of the disk image it will be clamped; if 'pnum' is set to |
| 2392 | * the end of the image, then the returned value will include BDRV_BLOCK_EOF. |
| 2393 | * |
| 2394 | * 'pnum' is set to the number of bytes (including and immediately |
| 2395 | * following the specified offset) that are easily known to be in the |
| 2396 | * same allocated/unallocated state. Note that a second call starting |
| 2397 | * at the original offset plus returned pnum may have the same status. |
| 2398 | * The returned value is non-zero on success except at end-of-file. |
| 2399 | * |
| 2400 | * Returns negative errno on failure. Otherwise, if the |
| 2401 | * BDRV_BLOCK_OFFSET_VALID bit is set, 'map' and 'file' (if non-NULL) are |
| 2402 | * set to the host mapping and BDS corresponding to the guest offset. |
| 2403 | */ |
| 2404 | static int coroutine_fn GRAPH_RDLOCK |
| 2405 | bdrv_co_do_block_status(BlockDriverState *bs, unsigned int mode, |
| 2406 | int64_t offset, int64_t bytes, |
| 2407 | int64_t *pnum, int64_t *map, BlockDriverState **file) |
| 2408 | { |
| 2409 | int64_t total_size; |
| 2410 | int64_t n; /* bytes */ |
| 2411 | int ret; |
| 2412 | int64_t local_map = 0; |
| 2413 | BlockDriverState *local_file = NULL; |
| 2414 | int64_t aligned_offset, aligned_bytes; |
| 2415 | uint32_t align; |
| 2416 | bool has_filtered_child; |
| 2417 | |
| 2418 | assert(pnum); |
| 2419 | assert_bdrv_graph_readable(); |
| 2420 | *pnum = 0; |
| 2421 | total_size = bdrv_co_getlength(bs); |
| 2422 | if (total_size < 0) { |
| 2423 | ret = total_size; |
| 2424 | goto early_out; |
| 2425 | } |
| 2426 | |
| 2427 | if (offset >= total_size) { |
| 2428 | ret = BDRV_BLOCK_EOF; |
| 2429 | goto early_out; |
| 2430 | } |
| 2431 | if (!bytes) { |
| 2432 | ret = 0; |
| 2433 | goto early_out; |
| 2434 | } |
| 2435 | |
| 2436 | n = total_size - offset; |
| 2437 | if (n < bytes) { |
| 2438 | bytes = n; |
| 2439 | } |
| 2440 | |
| 2441 | /* Must be non-NULL or bdrv_co_getlength() would have failed */ |
| 2442 | assert(bs->drv); |
| 2443 | has_filtered_child = bdrv_filter_child(bs); |
| 2444 | if (!bs->drv->bdrv_co_block_status && !has_filtered_child) { |
| 2445 | *pnum = bytes; |
| 2446 | ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED; |
| 2447 | if (offset + bytes == total_size) { |
| 2448 | ret |= BDRV_BLOCK_EOF; |
| 2449 | } |
| 2450 | if (bs->drv->protocol_name) { |
| 2451 | ret |= BDRV_BLOCK_OFFSET_VALID; |
| 2452 | local_map = offset; |
| 2453 | local_file = bs; |
| 2454 | } |
| 2455 | goto early_out; |
| 2456 | } |
| 2457 | |
| 2458 | bdrv_inc_in_flight(bs); |
| 2459 | |
| 2460 | /* Round out to request_alignment boundaries */ |
| 2461 | align = bs->bl.request_alignment; |
| 2462 | aligned_offset = QEMU_ALIGN_DOWN(offset, align); |
| 2463 | aligned_bytes = ROUND_UP(offset + bytes, align) - aligned_offset; |
| 2464 | |
| 2465 | if (bs->drv->bdrv_co_block_status) { |
| 2466 | /* |
| 2467 | * Use the block-status cache only for protocol nodes: Format |
| 2468 | * drivers are generally quick to inquire the status, but protocol |
| 2469 | * drivers often need to get information from outside of qemu, so |
| 2470 | * we do not have control over the actual implementation. There |
| 2471 | * have been cases where inquiring the status took an unreasonably |
| 2472 | * long time, and we can do nothing in qemu to fix it. |
| 2473 | * This is especially problematic for images with large data areas, |
| 2474 | * because finding the few holes in them and giving them special |
| 2475 | * treatment does not gain much performance. Therefore, we try to |
| 2476 | * cache the last-identified data region. |
| 2477 | * |
| 2478 | * Second, limiting ourselves to protocol nodes allows us to assume |
| 2479 | * the block status for data regions to be DATA | OFFSET_VALID, and |
| 2480 | * that the host offset is the same as the guest offset. |
| 2481 | * |
| 2482 | * Note that it is possible that external writers zero parts of |
| 2483 | * the cached regions without the cache being invalidated, and so |
| 2484 | * we may report zeroes as data. This is not catastrophic, |
| 2485 | * however, because reporting zeroes as data is fine. |
| 2486 | */ |
| 2487 | if (QLIST_EMPTY(&bs->children) && |
| 2488 | bdrv_bsc_is_data(bs, aligned_offset, pnum)) |
| 2489 | { |
| 2490 | ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; |
| 2491 | local_file = bs; |
| 2492 | local_map = aligned_offset; |
| 2493 | } else { |
| 2494 | ret = bs->drv->bdrv_co_block_status(bs, mode, aligned_offset, |
| 2495 | aligned_bytes, pnum, &local_map, |
| 2496 | &local_file); |
| 2497 | |
| 2498 | /* |
| 2499 | * Note that checking QLIST_EMPTY(&bs->children) is also done when |
| 2500 | * the cache is queried above. Technically, we do not need to check |
| 2501 | * it here; the worst that can happen is that we fill the cache for |
| 2502 | * non-protocol nodes, and then it is never used. However, filling |
| 2503 | * the cache requires an RCU update, so double check here to avoid |
| 2504 | * such an update if possible. |
| 2505 | * |
| 2506 | * Check mode, because we only want to update the cache when we |
| 2507 | * have accurate information about what is zero and what is data. |
| 2508 | */ |
| 2509 | if (mode == BDRV_WANT_PRECISE && |
| 2510 | ret == (BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID) && |
| 2511 | QLIST_EMPTY(&bs->children)) |
| 2512 | { |
| 2513 | /* |
| 2514 | * When a protocol driver reports BLOCK_OFFSET_VALID, the |
| 2515 | * returned local_map value must be the same as the offset we |
| 2516 | * have passed (aligned_offset), and local_bs must be the node |
| 2517 | * itself. |
| 2518 | * Assert this, because we follow this rule when reading from |
| 2519 | * the cache (see the `local_file = bs` and |
| 2520 | * `local_map = aligned_offset` assignments above), and the |
| 2521 | * result the cache delivers must be the same as the driver |
| 2522 | * would deliver. |
| 2523 | */ |
| 2524 | assert(local_file == bs); |
| 2525 | assert(local_map == aligned_offset); |
| 2526 | bdrv_bsc_fill(bs, aligned_offset, *pnum); |
| 2527 | } |
| 2528 | } |
| 2529 | } else { |
| 2530 | /* Default code for filters */ |
| 2531 | |
| 2532 | local_file = bdrv_filter_bs(bs); |
| 2533 | assert(local_file); |
| 2534 | |
| 2535 | *pnum = aligned_bytes; |
| 2536 | local_map = aligned_offset; |
| 2537 | ret = BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID; |
| 2538 | } |
| 2539 | if (ret < 0) { |
| 2540 | *pnum = 0; |
| 2541 | goto out; |
| 2542 | } |
| 2543 | |
| 2544 | /* |
| 2545 | * The driver's result must be a non-zero multiple of request_alignment. |
| 2546 | * Clamp pnum and adjust map to original request. |
| 2547 | */ |
| 2548 | assert(*pnum && QEMU_IS_ALIGNED(*pnum, align) && |
| 2549 | align > offset - aligned_offset); |
| 2550 | if (ret & BDRV_BLOCK_RECURSE) { |
| 2551 | assert(ret & BDRV_BLOCK_DATA); |
| 2552 | assert(ret & BDRV_BLOCK_OFFSET_VALID); |
| 2553 | assert(!(ret & BDRV_BLOCK_ZERO)); |
| 2554 | } |
| 2555 | |
| 2556 | *pnum -= offset - aligned_offset; |
| 2557 | if (*pnum > bytes) { |
| 2558 | *pnum = bytes; |
| 2559 | } |
| 2560 | if (ret & BDRV_BLOCK_OFFSET_VALID) { |
| 2561 | local_map += offset - aligned_offset; |
| 2562 | } |
| 2563 | |
| 2564 | if (ret & BDRV_BLOCK_RAW) { |
| 2565 | assert(ret & BDRV_BLOCK_OFFSET_VALID && local_file); |
| 2566 | ret = bdrv_co_do_block_status(local_file, mode, local_map, |
| 2567 | *pnum, pnum, &local_map, &local_file); |
| 2568 | goto out; |
| 2569 | } |
| 2570 | |
| 2571 | if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) { |
| 2572 | ret |= BDRV_BLOCK_ALLOCATED; |
| 2573 | } else if (bs->drv->supports_backing) { |
| 2574 | BlockDriverState *cow_bs = bdrv_cow_bs(bs); |
| 2575 | |
| 2576 | if (!cow_bs) { |
| 2577 | ret |= BDRV_BLOCK_ZERO; |
| 2578 | } else if (mode == BDRV_WANT_PRECISE) { |
| 2579 | int64_t size2 = bdrv_co_getlength(cow_bs); |
| 2580 | |
| 2581 | if (size2 >= 0 && offset >= size2) { |
| 2582 | ret |= BDRV_BLOCK_ZERO; |
| 2583 | } |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | if (mode == BDRV_WANT_PRECISE && ret & BDRV_BLOCK_RECURSE && |
| 2588 | local_file && local_file != bs && |
| 2589 | (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && |
| 2590 | (ret & BDRV_BLOCK_OFFSET_VALID)) { |
| 2591 | int64_t file_pnum; |
| 2592 | int ret2; |
| 2593 | |
| 2594 | ret2 = bdrv_co_do_block_status(local_file, mode, local_map, |
| 2595 | *pnum, &file_pnum, NULL, NULL); |
| 2596 | if (ret2 >= 0) { |
| 2597 | /* Ignore errors. This is just providing extra information, it |
| 2598 | * is useful but not necessary. |
| 2599 | */ |
| 2600 | if (ret2 & BDRV_BLOCK_EOF && |
| 2601 | (!file_pnum || ret2 & BDRV_BLOCK_ZERO)) { |
| 2602 | /* |
| 2603 | * It is valid for the format block driver to read |
| 2604 | * beyond the end of the underlying file's current |
| 2605 | * size; such areas read as zero. |
| 2606 | */ |
| 2607 | ret |= BDRV_BLOCK_ZERO; |
| 2608 | } else { |
| 2609 | /* Limit request to the range reported by the protocol driver */ |
| 2610 | *pnum = file_pnum; |
| 2611 | ret |= (ret2 & BDRV_BLOCK_ZERO); |
| 2612 | } |
| 2613 | } |
| 2614 | |
| 2615 | /* |
| 2616 | * Now that the recursive search was done, clear the flag. Otherwise, |
| 2617 | * with more complicated block graphs like snapshot-access -> |
| 2618 | * copy-before-write -> qcow2, where the return value will be propagated |
| 2619 | * further up to a parent bdrv_co_do_block_status() call, both the |
| 2620 | * BDRV_BLOCK_RECURSE and BDRV_BLOCK_ZERO flags would be set, which is |
| 2621 | * not allowed. |
| 2622 | */ |
| 2623 | ret &= ~BDRV_BLOCK_RECURSE; |
| 2624 | } |
| 2625 | |
| 2626 | out: |
| 2627 | bdrv_dec_in_flight(bs); |
| 2628 | if (ret >= 0 && offset + *pnum == total_size) { |
| 2629 | ret |= BDRV_BLOCK_EOF; |
| 2630 | } |
| 2631 | early_out: |
| 2632 | if (file) { |
| 2633 | *file = local_file; |
| 2634 | } |
| 2635 | if (map) { |
| 2636 | *map = local_map; |
| 2637 | } |
| 2638 | return ret; |
| 2639 | } |
| 2640 | |
| 2641 | int coroutine_fn |
| 2642 | bdrv_co_common_block_status_above(BlockDriverState *bs, |
| 2643 | BlockDriverState *base, |
| 2644 | bool include_base, |
| 2645 | unsigned int mode, |
| 2646 | int64_t offset, |
| 2647 | int64_t bytes, |
| 2648 | int64_t *pnum, |
| 2649 | int64_t *map, |
| 2650 | BlockDriverState **file, |
| 2651 | int *depth) |
| 2652 | { |
| 2653 | int ret; |
| 2654 | BlockDriverState *p; |
| 2655 | int64_t eof = 0; |
| 2656 | int dummy; |
| 2657 | IO_CODE(); |
| 2658 | |
| 2659 | assert(!include_base || base); /* Can't include NULL base */ |
| 2660 | assert_bdrv_graph_readable(); |
| 2661 | |
| 2662 | if (!depth) { |
| 2663 | depth = &dummy; |
| 2664 | } |
| 2665 | *depth = 0; |
| 2666 | |
| 2667 | if (!include_base && bs == base) { |
| 2668 | *pnum = bytes; |
| 2669 | return 0; |
| 2670 | } |
| 2671 | |
| 2672 | ret = bdrv_co_do_block_status(bs, mode, offset, bytes, pnum, |
| 2673 | map, file); |
| 2674 | ++*depth; |
| 2675 | if (ret < 0 || *pnum == 0 || ret & BDRV_BLOCK_ALLOCATED || bs == base) { |
| 2676 | return ret; |
| 2677 | } |
| 2678 | |
| 2679 | if (ret & BDRV_BLOCK_EOF) { |
| 2680 | eof = offset + *pnum; |
| 2681 | } |
| 2682 | |
| 2683 | assert(*pnum <= bytes); |
| 2684 | bytes = *pnum; |
| 2685 | |
| 2686 | for (p = bdrv_filter_or_cow_bs(bs); include_base || p != base; |
| 2687 | p = bdrv_filter_or_cow_bs(p)) |
| 2688 | { |
| 2689 | ret = bdrv_co_do_block_status(p, mode, offset, bytes, pnum, |
| 2690 | map, file); |
| 2691 | ++*depth; |
| 2692 | if (ret < 0) { |
| 2693 | return ret; |
| 2694 | } |
| 2695 | if (*pnum == 0) { |
| 2696 | /* |
| 2697 | * The top layer deferred to this layer, and because this layer is |
| 2698 | * short, any zeroes that we synthesize beyond EOF behave as if they |
| 2699 | * were allocated at this layer. |
| 2700 | * |
| 2701 | * We don't include BDRV_BLOCK_EOF into ret, as upper layer may be |
| 2702 | * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see |
| 2703 | * below. |
| 2704 | */ |
| 2705 | assert(ret & BDRV_BLOCK_EOF); |
| 2706 | *pnum = bytes; |
| 2707 | if (file) { |
| 2708 | *file = p; |
| 2709 | } |
| 2710 | ret = BDRV_BLOCK_ZERO | BDRV_BLOCK_ALLOCATED; |
| 2711 | break; |
| 2712 | } |
| 2713 | if (ret & BDRV_BLOCK_ALLOCATED) { |
| 2714 | /* |
| 2715 | * We've found the node and the status, we must break. |
| 2716 | * |
| 2717 | * Drop BDRV_BLOCK_EOF, as it's not for upper layer, which may be |
| 2718 | * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see |
| 2719 | * below. |
| 2720 | */ |
| 2721 | ret &= ~BDRV_BLOCK_EOF; |
| 2722 | break; |
| 2723 | } |
| 2724 | |
| 2725 | if (p == base) { |
| 2726 | assert(include_base); |
| 2727 | break; |
| 2728 | } |
| 2729 | |
| 2730 | /* |
| 2731 | * OK, [offset, offset + *pnum) region is unallocated on this layer, |
| 2732 | * let's continue the diving. |
| 2733 | */ |
| 2734 | assert(*pnum <= bytes); |
| 2735 | bytes = *pnum; |
| 2736 | } |
| 2737 | |
| 2738 | if (offset + *pnum == eof) { |
| 2739 | ret |= BDRV_BLOCK_EOF; |
| 2740 | } |
| 2741 | |
| 2742 | return ret; |
| 2743 | } |
| 2744 | |
| 2745 | int coroutine_fn bdrv_co_block_status_above(BlockDriverState *bs, |
| 2746 | BlockDriverState *base, |
| 2747 | int64_t offset, int64_t bytes, |
| 2748 | int64_t *pnum, int64_t *map, |
| 2749 | BlockDriverState **file) |
| 2750 | { |
| 2751 | IO_CODE(); |
| 2752 | return bdrv_co_common_block_status_above(bs, base, false, |
| 2753 | BDRV_WANT_PRECISE, offset, |
| 2754 | bytes, pnum, map, file, NULL); |
| 2755 | } |
| 2756 | |
| 2757 | int coroutine_fn bdrv_co_block_status(BlockDriverState *bs, int64_t offset, |
| 2758 | int64_t bytes, int64_t *pnum, |
| 2759 | int64_t *map, BlockDriverState **file) |
| 2760 | { |
| 2761 | IO_CODE(); |
| 2762 | return bdrv_co_block_status_above(bs, bdrv_filter_or_cow_bs(bs), |
| 2763 | offset, bytes, pnum, map, file); |
| 2764 | } |
| 2765 | |
| 2766 | /* |
| 2767 | * Check @bs (and its backing chain) to see if the range defined |
| 2768 | * by @offset and @bytes is known to read as zeroes. |
| 2769 | * Return 1 if that is the case, 0 otherwise and -errno on error. |
| 2770 | * This test is meant to be fast rather than accurate so returning 0 |
| 2771 | * does not guarantee non-zero data; but a return of 1 is reliable. |
| 2772 | */ |
| 2773 | int coroutine_fn bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset, |
| 2774 | int64_t bytes) |
| 2775 | { |
| 2776 | int ret; |
| 2777 | int64_t pnum; |
| 2778 | IO_CODE(); |
| 2779 | |
| 2780 | while (bytes) { |
| 2781 | ret = bdrv_co_common_block_status_above(bs, NULL, false, |
| 2782 | BDRV_WANT_ZERO, offset, bytes, |
| 2783 | &pnum, NULL, NULL, NULL); |
| 2784 | |
| 2785 | if (ret < 0) { |
| 2786 | return ret; |
| 2787 | } |
| 2788 | if (!(ret & BDRV_BLOCK_ZERO)) { |
| 2789 | return 0; |
| 2790 | } |
| 2791 | offset += pnum; |
| 2792 | bytes -= pnum; |
| 2793 | } |
| 2794 | |
| 2795 | return 1; |
| 2796 | } |
| 2797 | |
| 2798 | /* |
| 2799 | * Check @bs (and its backing chain) to see if the entire image is known |
| 2800 | * to read as zeroes. |
| 2801 | * Return 1 if that is the case, 0 otherwise and -errno on error. |
| 2802 | * This test is meant to be fast rather than accurate so returning 0 |
| 2803 | * does not guarantee non-zero data; however, a return of 1 is reliable, |
| 2804 | * and this function can report 1 in more cases than bdrv_co_is_zero_fast. |
| 2805 | */ |
| 2806 | int coroutine_fn bdrv_co_is_all_zeroes(BlockDriverState *bs) |
| 2807 | { |
| 2808 | int ret; |
| 2809 | int64_t pnum, bytes; |
| 2810 | char *buf; |
| 2811 | QEMUIOVector local_qiov; |
| 2812 | IO_CODE(); |
| 2813 | |
| 2814 | bytes = bdrv_co_getlength(bs); |
| 2815 | if (bytes < 0) { |
| 2816 | return bytes; |
| 2817 | } |
| 2818 | |
| 2819 | /* First probe - see if the entire image reads as zero */ |
| 2820 | ret = bdrv_co_common_block_status_above(bs, NULL, false, BDRV_WANT_ZERO, |
| 2821 | 0, bytes, &pnum, NULL, NULL, |
| 2822 | NULL); |
| 2823 | if (ret < 0) { |
| 2824 | return ret; |
| 2825 | } |
| 2826 | if (ret & BDRV_BLOCK_ZERO) { |
| 2827 | return bdrv_co_is_zero_fast(bs, pnum, bytes - pnum); |
| 2828 | } |
| 2829 | |
| 2830 | /* |
| 2831 | * Because of the way 'blockdev-create' works, raw files tend to |
| 2832 | * be created with a non-sparse region at the front to make |
| 2833 | * alignment probing easier. If the block starts with only a |
| 2834 | * small allocated region, it is still worth the effort to see if |
| 2835 | * the rest of the image is still sparse, coupled with manually |
| 2836 | * reading the first region to see if it reads zero after all. |
| 2837 | */ |
| 2838 | if (pnum > MAX_ZERO_CHECK_BUFFER) { |
| 2839 | return 0; |
| 2840 | } |
| 2841 | ret = bdrv_co_is_zero_fast(bs, pnum, bytes - pnum); |
| 2842 | if (ret <= 0) { |
| 2843 | return ret; |
| 2844 | } |
| 2845 | /* Only the head of the image is unknown, and it's small. Read it. */ |
| 2846 | buf = qemu_blockalign(bs, pnum); |
| 2847 | qemu_iovec_init_buf(&local_qiov, buf, pnum); |
| 2848 | ret = bdrv_driver_preadv(bs, 0, pnum, &local_qiov, 0, 0); |
| 2849 | if (ret >= 0) { |
| 2850 | ret = buffer_is_zero(buf, pnum); |
| 2851 | } |
| 2852 | qemu_vfree(buf); |
| 2853 | return ret; |
| 2854 | } |
| 2855 | |
| 2856 | int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t offset, |
| 2857 | int64_t bytes, int64_t *pnum) |
| 2858 | { |
| 2859 | int ret; |
| 2860 | int64_t dummy; |
| 2861 | IO_CODE(); |
| 2862 | |
| 2863 | ret = bdrv_co_common_block_status_above(bs, bs, true, BDRV_WANT_ALLOCATED, |
| 2864 | offset, bytes, pnum ? pnum : &dummy, |
| 2865 | NULL, NULL, NULL); |
| 2866 | if (ret < 0) { |
| 2867 | return ret; |
| 2868 | } |
| 2869 | return !!(ret & BDRV_BLOCK_ALLOCATED); |
| 2870 | } |
| 2871 | |
| 2872 | /* |
| 2873 | * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] |
| 2874 | * |
| 2875 | * Return a positive depth if (a prefix of) the given range is allocated |
| 2876 | * in any image between BASE and TOP (BASE is only included if include_base |
| 2877 | * is set). Depth 1 is TOP, 2 is the first backing layer, and so forth. |
| 2878 | * BASE can be NULL to check if the given offset is allocated in any |
| 2879 | * image of the chain. Return 0 otherwise, or negative errno on |
| 2880 | * failure. |
| 2881 | * |
| 2882 | * 'pnum' is set to the number of bytes (including and immediately |
| 2883 | * following the specified offset) that are known to be in the same |
| 2884 | * allocated/unallocated state. Note that a subsequent call starting |
| 2885 | * at 'offset + *pnum' may return the same allocation status (in other |
| 2886 | * words, the result is not necessarily the maximum possible range); |
| 2887 | * but 'pnum' will only be 0 when end of file is reached. |
| 2888 | */ |
| 2889 | int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *bs, |
| 2890 | BlockDriverState *base, |
| 2891 | bool include_base, int64_t offset, |
| 2892 | int64_t bytes, int64_t *pnum) |
| 2893 | { |
| 2894 | int depth; |
| 2895 | int ret; |
| 2896 | IO_CODE(); |
| 2897 | |
| 2898 | ret = bdrv_co_common_block_status_above(bs, base, include_base, |
| 2899 | BDRV_WANT_ALLOCATED, |
| 2900 | offset, bytes, pnum, NULL, NULL, |
| 2901 | &depth); |
| 2902 | if (ret < 0) { |
| 2903 | return ret; |
| 2904 | } |
| 2905 | |
| 2906 | if (ret & BDRV_BLOCK_ALLOCATED) { |
| 2907 | return depth; |
| 2908 | } |
| 2909 | return 0; |
| 2910 | } |
| 2911 | |
| 2912 | int coroutine_fn |
| 2913 | bdrv_co_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) |
| 2914 | { |
| 2915 | BlockDriver *drv = bs->drv; |
| 2916 | BlockDriverState *child_bs = bdrv_primary_bs(bs); |
| 2917 | int ret; |
| 2918 | IO_CODE(); |
| 2919 | assert_bdrv_graph_readable(); |
| 2920 | |
| 2921 | ret = bdrv_check_qiov_request(pos, qiov->size, qiov, 0, NULL); |
| 2922 | if (ret < 0) { |
| 2923 | return ret; |
| 2924 | } |
| 2925 | |
| 2926 | if (!drv) { |
| 2927 | return -ENOMEDIUM; |
| 2928 | } |
| 2929 | |
| 2930 | bdrv_inc_in_flight(bs); |
| 2931 | |
| 2932 | if (drv->bdrv_co_load_vmstate) { |
| 2933 | ret = drv->bdrv_co_load_vmstate(bs, qiov, pos); |
| 2934 | } else if (child_bs) { |
| 2935 | ret = bdrv_co_readv_vmstate(child_bs, qiov, pos); |
| 2936 | } else { |
| 2937 | ret = -ENOTSUP; |
| 2938 | } |
| 2939 | |
| 2940 | bdrv_dec_in_flight(bs); |
| 2941 | |
| 2942 | return ret; |
| 2943 | } |
| 2944 | |
| 2945 | int coroutine_fn |
| 2946 | bdrv_co_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) |
| 2947 | { |
| 2948 | BlockDriver *drv = bs->drv; |
| 2949 | BlockDriverState *child_bs = bdrv_primary_bs(bs); |
| 2950 | int ret; |
| 2951 | IO_CODE(); |
| 2952 | assert_bdrv_graph_readable(); |
| 2953 | |
| 2954 | ret = bdrv_check_qiov_request(pos, qiov->size, qiov, 0, NULL); |
| 2955 | if (ret < 0) { |
| 2956 | return ret; |
| 2957 | } |
| 2958 | |
| 2959 | if (!drv) { |
| 2960 | return -ENOMEDIUM; |
| 2961 | } |
| 2962 | |
| 2963 | bdrv_inc_in_flight(bs); |
| 2964 | |
| 2965 | if (drv->bdrv_co_save_vmstate) { |
| 2966 | ret = drv->bdrv_co_save_vmstate(bs, qiov, pos); |
| 2967 | } else if (child_bs) { |
| 2968 | ret = bdrv_co_writev_vmstate(child_bs, qiov, pos); |
| 2969 | } else { |
| 2970 | ret = -ENOTSUP; |
| 2971 | } |
| 2972 | |
| 2973 | bdrv_dec_in_flight(bs); |
| 2974 | |
| 2975 | return ret; |
| 2976 | } |
| 2977 | |
| 2978 | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
| 2979 | int64_t pos, int size) |
| 2980 | { |
| 2981 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size); |
| 2982 | int ret = bdrv_writev_vmstate(bs, &qiov, pos); |
| 2983 | IO_CODE(); |
| 2984 | |
| 2985 | return ret < 0 ? ret : size; |
| 2986 | } |
| 2987 | |
| 2988 | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
| 2989 | int64_t pos, int size) |
| 2990 | { |
| 2991 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size); |
| 2992 | int ret = bdrv_readv_vmstate(bs, &qiov, pos); |
| 2993 | IO_CODE(); |
| 2994 | |
| 2995 | return ret < 0 ? ret : size; |
| 2996 | } |
| 2997 | |
| 2998 | /**************************************************************/ |
| 2999 | /* async I/Os */ |
| 3000 | |
| 3001 | /** |
| 3002 | * Synchronously cancels an acb. Must be called with the BQL held and the acb |
| 3003 | * must be processed with the BQL held too (IOThreads are not allowed). |
| 3004 | * |
| 3005 | * Use bdrv_aio_cancel_async() instead when possible. |
| 3006 | */ |
| 3007 | void bdrv_aio_cancel(BlockAIOCB *acb) |
| 3008 | { |
| 3009 | GLOBAL_STATE_CODE(); |
| 3010 | qemu_aio_ref(acb); |
| 3011 | bdrv_aio_cancel_async(acb); |
| 3012 | AIO_WAIT_WHILE_UNLOCKED(NULL, acb->refcnt > 1); |
| 3013 | qemu_aio_unref(acb); |
| 3014 | } |
| 3015 | |
| 3016 | /* Async version of aio cancel. The caller is not blocked if the acb implements |
| 3017 | * cancel_async, otherwise we do nothing and let the request normally complete. |
| 3018 | * In either case the completion callback must be called. */ |
| 3019 | void bdrv_aio_cancel_async(BlockAIOCB *acb) |
| 3020 | { |
| 3021 | IO_CODE(); |
| 3022 | if (acb->aiocb_info->cancel_async) { |
| 3023 | acb->aiocb_info->cancel_async(acb); |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | /**************************************************************/ |
| 3028 | /* Coroutine block device emulation */ |
| 3029 | |
| 3030 | int coroutine_fn bdrv_co_flush(BlockDriverState *bs) |
| 3031 | { |
| 3032 | BdrvChild *primary_child = bdrv_primary_child(bs); |
| 3033 | BdrvChild *child; |
| 3034 | int current_gen; |
| 3035 | int ret = 0; |
| 3036 | IO_CODE(); |
| 3037 | |
| 3038 | assert_bdrv_graph_readable(); |
| 3039 | bdrv_inc_in_flight(bs); |
| 3040 | |
| 3041 | if (!bdrv_co_is_inserted(bs) || bdrv_is_read_only(bs) || |
| 3042 | bdrv_is_sg(bs)) { |
| 3043 | goto early_exit; |
| 3044 | } |
| 3045 | |
| 3046 | qemu_mutex_lock(&bs->reqs_lock); |
| 3047 | current_gen = qatomic_read(&bs->write_gen); |
| 3048 | |
| 3049 | /* Wait until any previous flushes are completed */ |
| 3050 | while (bs->active_flush_req) { |
| 3051 | qemu_co_queue_wait(&bs->flush_queue, &bs->reqs_lock); |
| 3052 | } |
| 3053 | |
| 3054 | /* Flushes reach this point in nondecreasing current_gen order. */ |
| 3055 | bs->active_flush_req = true; |
| 3056 | qemu_mutex_unlock(&bs->reqs_lock); |
| 3057 | |
| 3058 | /* Write back all layers by calling one driver function */ |
| 3059 | if (bs->drv->bdrv_co_flush) { |
| 3060 | ret = bs->drv->bdrv_co_flush(bs); |
| 3061 | goto out; |
| 3062 | } |
| 3063 | |
| 3064 | /* Write back cached data to the OS even with cache=unsafe */ |
| 3065 | BLKDBG_CO_EVENT(primary_child, BLKDBG_FLUSH_TO_OS); |
| 3066 | if (bs->drv->bdrv_co_flush_to_os) { |
| 3067 | ret = bs->drv->bdrv_co_flush_to_os(bs); |
| 3068 | if (ret < 0) { |
| 3069 | goto out; |
| 3070 | } |
| 3071 | } |
| 3072 | |
| 3073 | /* But don't actually force it to the disk with cache=unsafe */ |
| 3074 | if (bs->open_flags & BDRV_O_NO_FLUSH) { |
| 3075 | goto flush_children; |
| 3076 | } |
| 3077 | |
| 3078 | /* Check if we really need to flush anything */ |
| 3079 | if (bs->flushed_gen == current_gen) { |
| 3080 | goto flush_children; |
| 3081 | } |
| 3082 | |
| 3083 | BLKDBG_CO_EVENT(primary_child, BLKDBG_FLUSH_TO_DISK); |
| 3084 | if (!bs->drv) { |
| 3085 | /* bs->drv->bdrv_co_flush() might have ejected the BDS |
| 3086 | * (even in case of apparent success) */ |
| 3087 | ret = -ENOMEDIUM; |
| 3088 | goto out; |
| 3089 | } |
| 3090 | if (bs->drv->bdrv_co_flush_to_disk) { |
| 3091 | ret = bs->drv->bdrv_co_flush_to_disk(bs); |
| 3092 | } else if (bs->drv->bdrv_aio_flush) { |
| 3093 | BlockAIOCB *acb; |
| 3094 | CoroutineIOCompletion co = { |
| 3095 | .coroutine = qemu_coroutine_self(), |
| 3096 | }; |
| 3097 | |
| 3098 | acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); |
| 3099 | if (acb == NULL) { |
| 3100 | ret = -EIO; |
| 3101 | } else { |
| 3102 | qemu_coroutine_yield(); |
| 3103 | ret = co.ret; |
| 3104 | } |
| 3105 | } else { |
| 3106 | /* |
| 3107 | * Some block drivers always operate in either writethrough or unsafe |
| 3108 | * mode and don't support bdrv_flush therefore. Usually qemu doesn't |
| 3109 | * know how the server works (because the behaviour is hardcoded or |
| 3110 | * depends on server-side configuration), so we can't ensure that |
| 3111 | * everything is safe on disk. Returning an error doesn't work because |
| 3112 | * that would break guests even if the server operates in writethrough |
| 3113 | * mode. |
| 3114 | * |
| 3115 | * Let's hope the user knows what he's doing. |
| 3116 | */ |
| 3117 | ret = 0; |
| 3118 | } |
| 3119 | |
| 3120 | if (ret < 0) { |
| 3121 | goto out; |
| 3122 | } |
| 3123 | |
| 3124 | /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH |
| 3125 | * in the case of cache=unsafe, so there are no useless flushes. |
| 3126 | */ |
| 3127 | flush_children: |
| 3128 | ret = 0; |
| 3129 | QLIST_FOREACH(child, &bs->children, next) { |
| 3130 | if (child->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) { |
| 3131 | int this_child_ret = bdrv_co_flush(child->bs); |
| 3132 | if (!ret) { |
| 3133 | ret = this_child_ret; |
| 3134 | } |
| 3135 | } |
| 3136 | } |
| 3137 | |
| 3138 | out: |
| 3139 | /* Notify any pending flushes that we have completed */ |
| 3140 | if (ret == 0) { |
| 3141 | bs->flushed_gen = current_gen; |
| 3142 | } |
| 3143 | |
| 3144 | qemu_mutex_lock(&bs->reqs_lock); |
| 3145 | bs->active_flush_req = false; |
| 3146 | /* Return value is ignored - it's ok if wait queue is empty */ |
| 3147 | qemu_co_queue_next(&bs->flush_queue); |
| 3148 | qemu_mutex_unlock(&bs->reqs_lock); |
| 3149 | |
| 3150 | early_exit: |
| 3151 | bdrv_dec_in_flight(bs); |
| 3152 | return ret; |
| 3153 | } |
| 3154 | |
| 3155 | int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset, |
| 3156 | int64_t bytes) |
| 3157 | { |
| 3158 | BdrvTrackedRequest req; |
| 3159 | int ret; |
| 3160 | int64_t max_pdiscard; |
| 3161 | int head, tail, align; |
| 3162 | BlockDriverState *bs = child->bs; |
| 3163 | IO_CODE(); |
| 3164 | assert_bdrv_graph_readable(); |
| 3165 | |
| 3166 | if (!bs || !bs->drv || !bdrv_co_is_inserted(bs)) { |
| 3167 | return -ENOMEDIUM; |
| 3168 | } |
| 3169 | |
| 3170 | if (bdrv_has_readonly_bitmaps(bs)) { |
| 3171 | return -EPERM; |
| 3172 | } |
| 3173 | |
| 3174 | ret = bdrv_check_request(offset, bytes, NULL); |
| 3175 | if (ret < 0) { |
| 3176 | return ret; |
| 3177 | } |
| 3178 | |
| 3179 | /* Do nothing if disabled. */ |
| 3180 | if (!(bs->open_flags & BDRV_O_UNMAP)) { |
| 3181 | return 0; |
| 3182 | } |
| 3183 | |
| 3184 | if (!bs->drv->bdrv_co_pdiscard) { |
| 3185 | return 0; |
| 3186 | } |
| 3187 | |
| 3188 | /* Invalidate the cached block-status data range if this discard overlaps */ |
| 3189 | bdrv_bsc_invalidate_range(bs, offset, bytes); |
| 3190 | |
| 3191 | /* |
| 3192 | * Discard is advisory, but some devices track and coalesce |
| 3193 | * unaligned requests, so we must pass everything down rather than |
| 3194 | * round here. Still, most devices reject unaligned requests with |
| 3195 | * -EINVAL or -ENOTSUP, so we must fragment the request accordingly. |
| 3196 | */ |
| 3197 | align = MAX(bs->bl.pdiscard_alignment, bs->bl.request_alignment); |
| 3198 | assert(align % bs->bl.request_alignment == 0); |
| 3199 | head = offset % align; |
| 3200 | tail = (offset + bytes) % align; |
| 3201 | |
| 3202 | bdrv_inc_in_flight(bs); |
| 3203 | tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_DISCARD); |
| 3204 | |
| 3205 | ret = bdrv_co_write_req_prepare(child, offset, bytes, &req, 0); |
| 3206 | if (ret < 0) { |
| 3207 | goto out; |
| 3208 | } |
| 3209 | |
| 3210 | max_pdiscard = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_pdiscard, INT64_MAX), |
| 3211 | align); |
| 3212 | assert(max_pdiscard >= bs->bl.request_alignment); |
| 3213 | |
| 3214 | while (bytes > 0) { |
| 3215 | int64_t num = bytes; |
| 3216 | |
| 3217 | if (head) { |
| 3218 | /* Make small requests to get to alignment boundaries. */ |
| 3219 | num = MIN(bytes, align - head); |
| 3220 | if (!QEMU_IS_ALIGNED(num, bs->bl.request_alignment)) { |
| 3221 | num %= bs->bl.request_alignment; |
| 3222 | } |
| 3223 | head = (head + num) % align; |
| 3224 | assert(num < max_pdiscard); |
| 3225 | } else if (tail) { |
| 3226 | if (num > align) { |
| 3227 | /* Shorten the request to the last aligned cluster. */ |
| 3228 | num -= tail; |
| 3229 | } else if (!QEMU_IS_ALIGNED(tail, bs->bl.request_alignment) && |
| 3230 | tail > bs->bl.request_alignment) { |
| 3231 | tail %= bs->bl.request_alignment; |
| 3232 | num -= tail; |
| 3233 | } |
| 3234 | } |
| 3235 | /* limit request size */ |
| 3236 | if (num > max_pdiscard) { |
| 3237 | num = max_pdiscard; |
| 3238 | } |
| 3239 | |
| 3240 | if (!bs->drv) { |
| 3241 | ret = -ENOMEDIUM; |
| 3242 | goto out; |
| 3243 | } |
| 3244 | |
| 3245 | ret = bs->drv->bdrv_co_pdiscard(bs, offset, num); |
| 3246 | if (ret && ret != -ENOTSUP) { |
| 3247 | if (ret == -EINVAL && (offset % align != 0 || num % align != 0)) { |
| 3248 | /* Silently skip rejected unaligned head/tail requests */ |
| 3249 | } else { |
| 3250 | goto out; /* bail out */ |
| 3251 | } |
| 3252 | } |
| 3253 | |
| 3254 | offset += num; |
| 3255 | bytes -= num; |
| 3256 | } |
| 3257 | ret = 0; |
| 3258 | out: |
| 3259 | bdrv_co_write_req_finish(child, req.offset, req.bytes, &req, ret); |
| 3260 | tracked_request_end(&req); |
| 3261 | bdrv_dec_in_flight(bs); |
| 3262 | return ret; |
| 3263 | } |
| 3264 | |
| 3265 | int coroutine_fn bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf) |
| 3266 | { |
| 3267 | BlockDriver *drv = bs->drv; |
| 3268 | CoroutineIOCompletion co = { |
| 3269 | .coroutine = qemu_coroutine_self(), |
| 3270 | }; |
| 3271 | BlockAIOCB *acb; |
| 3272 | IO_CODE(); |
| 3273 | assert_bdrv_graph_readable(); |
| 3274 | |
| 3275 | bdrv_inc_in_flight(bs); |
| 3276 | if (!drv || (!drv->bdrv_aio_ioctl && !drv->bdrv_co_ioctl)) { |
| 3277 | co.ret = -ENOTSUP; |
| 3278 | goto out; |
| 3279 | } |
| 3280 | |
| 3281 | if (drv->bdrv_co_ioctl) { |
| 3282 | co.ret = drv->bdrv_co_ioctl(bs, req, buf); |
| 3283 | } else { |
| 3284 | acb = drv->bdrv_aio_ioctl(bs, req, buf, bdrv_co_io_em_complete, &co); |
| 3285 | if (!acb) { |
| 3286 | co.ret = -ENOTSUP; |
| 3287 | goto out; |
| 3288 | } |
| 3289 | qemu_coroutine_yield(); |
| 3290 | } |
| 3291 | out: |
| 3292 | bdrv_dec_in_flight(bs); |
| 3293 | return co.ret; |
| 3294 | } |
| 3295 | |
| 3296 | int coroutine_fn bdrv_co_zone_report(BlockDriverState *bs, int64_t offset, |
| 3297 | unsigned int *nr_zones, |
| 3298 | BlockZoneDescriptor *zones) |
| 3299 | { |
| 3300 | BlockDriver *drv = bs->drv; |
| 3301 | CoroutineIOCompletion co = { |
| 3302 | .coroutine = qemu_coroutine_self(), |
| 3303 | }; |
| 3304 | IO_CODE(); |
| 3305 | |
| 3306 | bdrv_inc_in_flight(bs); |
| 3307 | if (!drv || !drv->bdrv_co_zone_report || bs->bl.zoned == BLK_Z_NONE) { |
| 3308 | co.ret = -ENOTSUP; |
| 3309 | goto out; |
| 3310 | } |
| 3311 | co.ret = drv->bdrv_co_zone_report(bs, offset, nr_zones, zones); |
| 3312 | out: |
| 3313 | bdrv_dec_in_flight(bs); |
| 3314 | return co.ret; |
| 3315 | } |
| 3316 | |
| 3317 | int coroutine_fn bdrv_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op, |
| 3318 | int64_t offset, int64_t len) |
| 3319 | { |
| 3320 | BlockDriver *drv = bs->drv; |
| 3321 | CoroutineIOCompletion co = { |
| 3322 | .coroutine = qemu_coroutine_self(), |
| 3323 | }; |
| 3324 | IO_CODE(); |
| 3325 | |
| 3326 | bdrv_inc_in_flight(bs); |
| 3327 | if (!drv || !drv->bdrv_co_zone_mgmt || bs->bl.zoned == BLK_Z_NONE) { |
| 3328 | co.ret = -ENOTSUP; |
| 3329 | goto out; |
| 3330 | } |
| 3331 | co.ret = drv->bdrv_co_zone_mgmt(bs, op, offset, len); |
| 3332 | out: |
| 3333 | bdrv_dec_in_flight(bs); |
| 3334 | return co.ret; |
| 3335 | } |
| 3336 | |
| 3337 | int coroutine_fn bdrv_co_zone_append(BlockDriverState *bs, int64_t *offset, |
| 3338 | QEMUIOVector *qiov, |
| 3339 | BdrvRequestFlags flags) |
| 3340 | { |
| 3341 | int ret; |
| 3342 | BlockDriver *drv = bs->drv; |
| 3343 | CoroutineIOCompletion co = { |
| 3344 | .coroutine = qemu_coroutine_self(), |
| 3345 | }; |
| 3346 | IO_CODE(); |
| 3347 | |
| 3348 | ret = bdrv_check_qiov_request(*offset, qiov->size, qiov, 0, NULL); |
| 3349 | if (ret < 0) { |
| 3350 | return ret; |
| 3351 | } |
| 3352 | |
| 3353 | bdrv_inc_in_flight(bs); |
| 3354 | if (!drv || !drv->bdrv_co_zone_append || bs->bl.zoned == BLK_Z_NONE) { |
| 3355 | co.ret = -ENOTSUP; |
| 3356 | goto out; |
| 3357 | } |
| 3358 | co.ret = drv->bdrv_co_zone_append(bs, offset, qiov, flags); |
| 3359 | out: |
| 3360 | bdrv_dec_in_flight(bs); |
| 3361 | return co.ret; |
| 3362 | } |
| 3363 | |
| 3364 | void *qemu_blockalign(BlockDriverState *bs, size_t size) |
| 3365 | { |
| 3366 | IO_CODE(); |
| 3367 | return qemu_memalign(bdrv_opt_mem_align(bs), size); |
| 3368 | } |
| 3369 | |
| 3370 | void *qemu_blockalign0(BlockDriverState *bs, size_t size) |
| 3371 | { |
| 3372 | IO_CODE(); |
| 3373 | return memset(qemu_blockalign(bs, size), 0, size); |
| 3374 | } |
| 3375 | |
| 3376 | void *qemu_try_blockalign(BlockDriverState *bs, size_t size) |
| 3377 | { |
| 3378 | size_t align = bdrv_opt_mem_align(bs); |
| 3379 | IO_CODE(); |
| 3380 | |
| 3381 | /* Ensure that NULL is never returned on success */ |
| 3382 | assert(align > 0); |
| 3383 | if (size == 0) { |
| 3384 | size = align; |
| 3385 | } |
| 3386 | |
| 3387 | return qemu_try_memalign(align, size); |
| 3388 | } |
| 3389 | |
| 3390 | void *qemu_try_blockalign0(BlockDriverState *bs, size_t size) |
| 3391 | { |
| 3392 | void *mem = qemu_try_blockalign(bs, size); |
| 3393 | IO_CODE(); |
| 3394 | |
| 3395 | if (mem) { |
| 3396 | memset(mem, 0, size); |
| 3397 | } |
| 3398 | |
| 3399 | return mem; |
| 3400 | } |
| 3401 | |
| 3402 | /* Helper that undoes bdrv_register_buf() when it fails partway through */ |
| 3403 | static void GRAPH_RDLOCK |
| 3404 | bdrv_register_buf_rollback(BlockDriverState *bs, void *host, size_t size, |
| 3405 | BdrvChild *final_child) |
| 3406 | { |
| 3407 | BdrvChild *child; |
| 3408 | |
| 3409 | GLOBAL_STATE_CODE(); |
| 3410 | assert_bdrv_graph_readable(); |
| 3411 | |
| 3412 | QLIST_FOREACH(child, &bs->children, next) { |
| 3413 | if (child == final_child) { |
| 3414 | break; |
| 3415 | } |
| 3416 | |
| 3417 | bdrv_unregister_buf(child->bs, host, size); |
| 3418 | } |
| 3419 | |
| 3420 | if (bs->drv && bs->drv->bdrv_unregister_buf) { |
| 3421 | bs->drv->bdrv_unregister_buf(bs, host, size); |
| 3422 | } |
| 3423 | } |
| 3424 | |
| 3425 | bool bdrv_register_buf(BlockDriverState *bs, void *host, size_t size, |
| 3426 | Error **errp) |
| 3427 | { |
| 3428 | BdrvChild *child; |
| 3429 | |
| 3430 | GLOBAL_STATE_CODE(); |
| 3431 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 3432 | |
| 3433 | if (bs->drv && bs->drv->bdrv_register_buf) { |
| 3434 | if (!bs->drv->bdrv_register_buf(bs, host, size, errp)) { |
| 3435 | return false; |
| 3436 | } |
| 3437 | } |
| 3438 | QLIST_FOREACH(child, &bs->children, next) { |
| 3439 | if (!bdrv_register_buf(child->bs, host, size, errp)) { |
| 3440 | bdrv_register_buf_rollback(bs, host, size, child); |
| 3441 | return false; |
| 3442 | } |
| 3443 | } |
| 3444 | return true; |
| 3445 | } |
| 3446 | |
| 3447 | void bdrv_unregister_buf(BlockDriverState *bs, void *host, size_t size) |
| 3448 | { |
| 3449 | BdrvChild *child; |
| 3450 | |
| 3451 | GLOBAL_STATE_CODE(); |
| 3452 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 3453 | |
| 3454 | if (bs->drv && bs->drv->bdrv_unregister_buf) { |
| 3455 | bs->drv->bdrv_unregister_buf(bs, host, size); |
| 3456 | } |
| 3457 | QLIST_FOREACH(child, &bs->children, next) { |
| 3458 | bdrv_unregister_buf(child->bs, host, size); |
| 3459 | } |
| 3460 | } |
| 3461 | |
| 3462 | static int coroutine_fn GRAPH_RDLOCK bdrv_co_copy_range_internal( |
| 3463 | BdrvChild *src, int64_t src_offset, BdrvChild *dst, |
| 3464 | int64_t dst_offset, int64_t bytes, |
| 3465 | BdrvRequestFlags read_flags, BdrvRequestFlags write_flags, |
| 3466 | bool recurse_src) |
| 3467 | { |
| 3468 | BdrvTrackedRequest req; |
| 3469 | int ret; |
| 3470 | assert_bdrv_graph_readable(); |
| 3471 | |
| 3472 | /* TODO We can support BDRV_REQ_NO_FALLBACK here */ |
| 3473 | assert(!(read_flags & BDRV_REQ_NO_FALLBACK)); |
| 3474 | assert(!(write_flags & BDRV_REQ_NO_FALLBACK)); |
| 3475 | assert(!(read_flags & BDRV_REQ_NO_WAIT)); |
| 3476 | assert(!(write_flags & BDRV_REQ_NO_WAIT)); |
| 3477 | |
| 3478 | if (!dst || !dst->bs || !bdrv_co_is_inserted(dst->bs)) { |
| 3479 | return -ENOMEDIUM; |
| 3480 | } |
| 3481 | ret = bdrv_check_request32(dst_offset, bytes, NULL, 0); |
| 3482 | if (ret) { |
| 3483 | return ret; |
| 3484 | } |
| 3485 | if (write_flags & BDRV_REQ_ZERO_WRITE) { |
| 3486 | return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, write_flags); |
| 3487 | } |
| 3488 | |
| 3489 | if (!src || !src->bs || !bdrv_co_is_inserted(src->bs)) { |
| 3490 | return -ENOMEDIUM; |
| 3491 | } |
| 3492 | ret = bdrv_check_request32(src_offset, bytes, NULL, 0); |
| 3493 | if (ret) { |
| 3494 | return ret; |
| 3495 | } |
| 3496 | |
| 3497 | if (!src->bs->drv->bdrv_co_copy_range_from |
| 3498 | || !dst->bs->drv->bdrv_co_copy_range_to |
| 3499 | || src->bs->encrypted || dst->bs->encrypted) { |
| 3500 | return -ENOTSUP; |
| 3501 | } |
| 3502 | |
| 3503 | if (recurse_src) { |
| 3504 | bdrv_inc_in_flight(src->bs); |
| 3505 | tracked_request_begin(&req, src->bs, src_offset, bytes, |
| 3506 | BDRV_TRACKED_READ); |
| 3507 | |
| 3508 | /* BDRV_REQ_SERIALISING is only for write operation */ |
| 3509 | assert(!(read_flags & BDRV_REQ_SERIALISING)); |
| 3510 | bdrv_wait_serialising_requests(&req); |
| 3511 | |
| 3512 | ret = src->bs->drv->bdrv_co_copy_range_from(src->bs, |
| 3513 | src, src_offset, |
| 3514 | dst, dst_offset, |
| 3515 | bytes, |
| 3516 | read_flags, write_flags); |
| 3517 | |
| 3518 | tracked_request_end(&req); |
| 3519 | bdrv_dec_in_flight(src->bs); |
| 3520 | } else { |
| 3521 | bdrv_inc_in_flight(dst->bs); |
| 3522 | tracked_request_begin(&req, dst->bs, dst_offset, bytes, |
| 3523 | BDRV_TRACKED_WRITE); |
| 3524 | ret = bdrv_co_write_req_prepare(dst, dst_offset, bytes, &req, |
| 3525 | write_flags); |
| 3526 | if (!ret) { |
| 3527 | ret = dst->bs->drv->bdrv_co_copy_range_to(dst->bs, |
| 3528 | src, src_offset, |
| 3529 | dst, dst_offset, |
| 3530 | bytes, |
| 3531 | read_flags, write_flags); |
| 3532 | } |
| 3533 | bdrv_co_write_req_finish(dst, dst_offset, bytes, &req, ret); |
| 3534 | tracked_request_end(&req); |
| 3535 | bdrv_dec_in_flight(dst->bs); |
| 3536 | } |
| 3537 | |
| 3538 | return ret; |
| 3539 | } |
| 3540 | |
| 3541 | /* Copy range from @src to @dst. |
| 3542 | * |
| 3543 | * See the comment of bdrv_co_copy_range for the parameter and return value |
| 3544 | * semantics. */ |
| 3545 | int coroutine_fn bdrv_co_copy_range_from(BdrvChild *src, int64_t src_offset, |
| 3546 | BdrvChild *dst, int64_t dst_offset, |
| 3547 | int64_t bytes, |
| 3548 | BdrvRequestFlags read_flags, |
| 3549 | BdrvRequestFlags write_flags) |
| 3550 | { |
| 3551 | IO_CODE(); |
| 3552 | assert_bdrv_graph_readable(); |
| 3553 | trace_bdrv_co_copy_range_from(src, src_offset, dst, dst_offset, bytes, |
| 3554 | read_flags, write_flags); |
| 3555 | return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset, |
| 3556 | bytes, read_flags, write_flags, true); |
| 3557 | } |
| 3558 | |
| 3559 | /* Copy range from @src to @dst. |
| 3560 | * |
| 3561 | * See the comment of bdrv_co_copy_range for the parameter and return value |
| 3562 | * semantics. */ |
| 3563 | int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, int64_t src_offset, |
| 3564 | BdrvChild *dst, int64_t dst_offset, |
| 3565 | int64_t bytes, |
| 3566 | BdrvRequestFlags read_flags, |
| 3567 | BdrvRequestFlags write_flags) |
| 3568 | { |
| 3569 | IO_CODE(); |
| 3570 | assert_bdrv_graph_readable(); |
| 3571 | trace_bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes, |
| 3572 | read_flags, write_flags); |
| 3573 | return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset, |
| 3574 | bytes, read_flags, write_flags, false); |
| 3575 | } |
| 3576 | |
| 3577 | int coroutine_fn bdrv_co_copy_range(BdrvChild *src, int64_t src_offset, |
| 3578 | BdrvChild *dst, int64_t dst_offset, |
| 3579 | int64_t bytes, BdrvRequestFlags read_flags, |
| 3580 | BdrvRequestFlags write_flags) |
| 3581 | { |
| 3582 | IO_CODE(); |
| 3583 | assert_bdrv_graph_readable(); |
| 3584 | |
| 3585 | return bdrv_co_copy_range_from(src, src_offset, |
| 3586 | dst, dst_offset, |
| 3587 | bytes, read_flags, write_flags); |
| 3588 | } |
| 3589 | |
| 3590 | void coroutine_fn bdrv_co_parent_cb_resize(BlockDriverState *bs) |
| 3591 | { |
| 3592 | BdrvChild *c; |
| 3593 | |
| 3594 | IO_CODE(); |
| 3595 | assert_bdrv_graph_readable(); |
| 3596 | |
| 3597 | QLIST_FOREACH(c, &bs->parents, next_parent) { |
| 3598 | if (c->klass->resize) { |
| 3599 | c->klass->resize(c); |
| 3600 | } |
| 3601 | } |
| 3602 | } |
| 3603 | |
| 3604 | /** |
| 3605 | * Truncate file to 'offset' bytes (needed only for file protocols) |
| 3606 | * |
| 3607 | * If 'exact' is true, the file must be resized to exactly the given |
| 3608 | * 'offset'. Otherwise, it is sufficient for the node to be at least |
| 3609 | * 'offset' bytes in length. |
| 3610 | */ |
| 3611 | int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact, |
| 3612 | PreallocMode prealloc, BdrvRequestFlags flags, |
| 3613 | Error **errp) |
| 3614 | { |
| 3615 | BlockDriverState *bs = child->bs; |
| 3616 | BdrvChild *filtered, *backing; |
| 3617 | BlockDriver *drv = bs->drv; |
| 3618 | BdrvTrackedRequest req; |
| 3619 | int64_t old_size, new_bytes; |
| 3620 | int ret; |
| 3621 | IO_CODE(); |
| 3622 | assert_bdrv_graph_readable(); |
| 3623 | |
| 3624 | /* if bs->drv == NULL, bs is closed, so there's nothing to do here */ |
| 3625 | if (!drv) { |
| 3626 | error_setg(errp, "No medium inserted"); |
| 3627 | return -ENOMEDIUM; |
| 3628 | } |
| 3629 | if (offset < 0) { |
| 3630 | error_setg(errp, "Image size cannot be negative"); |
| 3631 | return -EINVAL; |
| 3632 | } |
| 3633 | |
| 3634 | ret = bdrv_check_request(offset, 0, errp); |
| 3635 | if (ret < 0) { |
| 3636 | return ret; |
| 3637 | } |
| 3638 | |
| 3639 | old_size = bdrv_co_getlength(bs); |
| 3640 | if (old_size < 0) { |
| 3641 | error_setg_errno(errp, -old_size, "Failed to get old image size"); |
| 3642 | return old_size; |
| 3643 | } |
| 3644 | |
| 3645 | if (bdrv_is_read_only(bs)) { |
| 3646 | error_setg(errp, "Image is read-only"); |
| 3647 | return -EACCES; |
| 3648 | } |
| 3649 | |
| 3650 | if (offset > old_size) { |
| 3651 | new_bytes = offset - old_size; |
| 3652 | } else { |
| 3653 | new_bytes = 0; |
| 3654 | } |
| 3655 | |
| 3656 | bdrv_inc_in_flight(bs); |
| 3657 | tracked_request_begin(&req, bs, offset - new_bytes, new_bytes, |
| 3658 | BDRV_TRACKED_TRUNCATE); |
| 3659 | |
| 3660 | /* If we are growing the image and potentially using preallocation for the |
| 3661 | * new area, we need to make sure that no write requests are made to it |
| 3662 | * concurrently or they might be overwritten by preallocation. */ |
| 3663 | if (new_bytes) { |
| 3664 | bdrv_make_request_serialising(&req, 1); |
| 3665 | } |
| 3666 | ret = bdrv_co_write_req_prepare(child, offset - new_bytes, new_bytes, &req, |
| 3667 | 0); |
| 3668 | if (ret < 0) { |
| 3669 | error_setg_errno(errp, -ret, |
| 3670 | "Failed to prepare request for truncation"); |
| 3671 | goto out; |
| 3672 | } |
| 3673 | |
| 3674 | filtered = bdrv_filter_child(bs); |
| 3675 | backing = bdrv_cow_child(bs); |
| 3676 | |
| 3677 | /* |
| 3678 | * If the image has a backing file that is large enough that it would |
| 3679 | * provide data for the new area, we cannot leave it unallocated because |
| 3680 | * then the backing file content would become visible. Instead, zero-fill |
| 3681 | * the new area. |
| 3682 | * |
| 3683 | * Note that if the image has a backing file, but was opened without the |
| 3684 | * backing file, taking care of keeping things consistent with that backing |
| 3685 | * file is the user's responsibility. |
| 3686 | */ |
| 3687 | if (new_bytes && backing) { |
| 3688 | int64_t backing_len; |
| 3689 | |
| 3690 | backing_len = bdrv_co_getlength(backing->bs); |
| 3691 | if (backing_len < 0) { |
| 3692 | ret = backing_len; |
| 3693 | error_setg_errno(errp, -ret, "Could not get backing file size"); |
| 3694 | goto out; |
| 3695 | } |
| 3696 | |
| 3697 | if (backing_len > old_size) { |
| 3698 | flags |= BDRV_REQ_ZERO_WRITE; |
| 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | if (drv->bdrv_co_truncate) { |
| 3703 | if (flags & ~bs->supported_truncate_flags) { |
| 3704 | error_setg(errp, "Block driver does not support requested flags"); |
| 3705 | ret = -ENOTSUP; |
| 3706 | goto out; |
| 3707 | } |
| 3708 | ret = drv->bdrv_co_truncate(bs, offset, exact, prealloc, flags, errp); |
| 3709 | } else if (filtered) { |
| 3710 | ret = bdrv_co_truncate(filtered, offset, exact, prealloc, flags, errp); |
| 3711 | } else { |
| 3712 | error_setg(errp, "Image format driver does not support resize"); |
| 3713 | ret = -ENOTSUP; |
| 3714 | goto out; |
| 3715 | } |
| 3716 | if (ret < 0) { |
| 3717 | goto out; |
| 3718 | } |
| 3719 | |
| 3720 | ret = bdrv_co_refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); |
| 3721 | if (ret < 0) { |
| 3722 | error_setg_errno(errp, -ret, "Could not refresh total sector count"); |
| 3723 | } else { |
| 3724 | offset = bs->total_sectors * BDRV_SECTOR_SIZE; |
| 3725 | } |
| 3726 | /* |
| 3727 | * It's possible that truncation succeeded but bdrv_refresh_total_sectors |
| 3728 | * failed, but the latter doesn't affect how we should finish the request. |
| 3729 | * Pass 0 as the last parameter so that dirty bitmaps etc. are handled. |
| 3730 | */ |
| 3731 | bdrv_co_write_req_finish(child, offset - new_bytes, new_bytes, &req, 0); |
| 3732 | |
| 3733 | out: |
| 3734 | tracked_request_end(&req); |
| 3735 | bdrv_dec_in_flight(bs); |
| 3736 | |
| 3737 | return ret; |
| 3738 | } |
| 3739 | |
| 3740 | void bdrv_cancel_in_flight(BlockDriverState *bs) |
| 3741 | { |
| 3742 | GLOBAL_STATE_CODE(); |
| 3743 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 3744 | |
| 3745 | if (!bs || !bs->drv) { |
| 3746 | return; |
| 3747 | } |
| 3748 | |
| 3749 | if (bs->drv->bdrv_cancel_in_flight) { |
| 3750 | bs->drv->bdrv_cancel_in_flight(bs); |
| 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | int coroutine_fn |
| 3755 | bdrv_co_preadv_snapshot(BdrvChild *child, int64_t offset, int64_t bytes, |
| 3756 | QEMUIOVector *qiov, size_t qiov_offset) |
| 3757 | { |
| 3758 | BlockDriverState *bs = child->bs; |
| 3759 | BlockDriver *drv = bs->drv; |
| 3760 | int ret; |
| 3761 | IO_CODE(); |
| 3762 | assert_bdrv_graph_readable(); |
| 3763 | |
| 3764 | if (!drv) { |
| 3765 | return -ENOMEDIUM; |
| 3766 | } |
| 3767 | |
| 3768 | if (!drv->bdrv_co_preadv_snapshot) { |
| 3769 | return -ENOTSUP; |
| 3770 | } |
| 3771 | |
| 3772 | bdrv_inc_in_flight(bs); |
| 3773 | ret = drv->bdrv_co_preadv_snapshot(bs, offset, bytes, qiov, qiov_offset); |
| 3774 | bdrv_dec_in_flight(bs); |
| 3775 | |
| 3776 | return ret; |
| 3777 | } |
| 3778 | |
| 3779 | int coroutine_fn |
| 3780 | bdrv_co_snapshot_block_status(BlockDriverState *bs, unsigned int mode, |
| 3781 | int64_t offset, int64_t bytes, |
| 3782 | int64_t *pnum, int64_t *map, |
| 3783 | BlockDriverState **file) |
| 3784 | { |
| 3785 | BlockDriver *drv = bs->drv; |
| 3786 | int ret; |
| 3787 | IO_CODE(); |
| 3788 | assert_bdrv_graph_readable(); |
| 3789 | |
| 3790 | if (!drv) { |
| 3791 | return -ENOMEDIUM; |
| 3792 | } |
| 3793 | |
| 3794 | if (!drv->bdrv_co_snapshot_block_status) { |
| 3795 | return -ENOTSUP; |
| 3796 | } |
| 3797 | |
| 3798 | bdrv_inc_in_flight(bs); |
| 3799 | ret = drv->bdrv_co_snapshot_block_status(bs, mode, offset, bytes, |
| 3800 | pnum, map, file); |
| 3801 | bdrv_dec_in_flight(bs); |
| 3802 | |
| 3803 | return ret; |
| 3804 | } |
| 3805 | |
| 3806 | int coroutine_fn |
| 3807 | bdrv_co_pdiscard_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes) |
| 3808 | { |
| 3809 | BlockDriver *drv = bs->drv; |
| 3810 | int ret; |
| 3811 | IO_CODE(); |
| 3812 | assert_bdrv_graph_readable(); |
| 3813 | |
| 3814 | if (!drv) { |
| 3815 | return -ENOMEDIUM; |
| 3816 | } |
| 3817 | |
| 3818 | if (!drv->bdrv_co_pdiscard_snapshot) { |
| 3819 | return -ENOTSUP; |
| 3820 | } |
| 3821 | |
| 3822 | bdrv_inc_in_flight(bs); |
| 3823 | ret = drv->bdrv_co_pdiscard_snapshot(bs, offset, bytes); |
| 3824 | bdrv_dec_in_flight(bs); |
| 3825 | |
| 3826 | return ret; |
| 3827 | } |