Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator block write threshold notification |
| 3 | * |
| 4 | * Copyright Red Hat, Inc. 2014 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Francesco Romani <fromani@redhat.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. |
| 10 | * See the COPYING.LIB file in the top-level directory. |
| 11 | */ |
| 12 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 14 | #include "block/block_int.h" |
Daniel P. Berrange | 10817bf | 2015-09-01 14:48:02 +0100 | [diff] [blame] | 15 | #include "qemu/coroutine.h" |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 16 | #include "block/write-threshold.h" |
| 17 | #include "qemu/notify.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 18 | #include "qapi/error.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 19 | #include "qapi/qapi-commands-block-core.h" |
| 20 | #include "qapi/qapi-events-block-core.h" |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 21 | |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 22 | uint64_t bdrv_write_threshold_get(const BlockDriverState *bs) |
| 23 | { |
| 24 | return bs->write_threshold_offset; |
| 25 | } |
| 26 | |
| 27 | bool bdrv_write_threshold_is_set(const BlockDriverState *bs) |
| 28 | { |
| 29 | return bs->write_threshold_offset > 0; |
| 30 | } |
| 31 | |
| 32 | static void write_threshold_disable(BlockDriverState *bs) |
| 33 | { |
| 34 | if (bdrv_write_threshold_is_set(bs)) { |
| 35 | notifier_with_return_remove(&bs->write_threshold_notifier); |
| 36 | bs->write_threshold_offset = 0; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | uint64_t bdrv_write_threshold_exceeded(const BlockDriverState *bs, |
| 41 | const BdrvTrackedRequest *req) |
| 42 | { |
| 43 | if (bdrv_write_threshold_is_set(bs)) { |
| 44 | if (req->offset > bs->write_threshold_offset) { |
| 45 | return (req->offset - bs->write_threshold_offset) + req->bytes; |
| 46 | } |
| 47 | if ((req->offset + req->bytes) > bs->write_threshold_offset) { |
| 48 | return (req->offset + req->bytes) - bs->write_threshold_offset; |
| 49 | } |
| 50 | } |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int coroutine_fn before_write_notify(NotifierWithReturn *notifier, |
| 55 | void *opaque) |
| 56 | { |
| 57 | BdrvTrackedRequest *req = opaque; |
| 58 | BlockDriverState *bs = req->bs; |
| 59 | uint64_t amount = 0; |
| 60 | |
| 61 | amount = bdrv_write_threshold_exceeded(bs, req); |
| 62 | if (amount > 0) { |
| 63 | qapi_event_send_block_write_threshold( |
| 64 | bs->node_name, |
| 65 | amount, |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 66 | bs->write_threshold_offset); |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 67 | |
| 68 | /* autodisable to avoid flooding the monitor */ |
| 69 | write_threshold_disable(bs); |
| 70 | } |
| 71 | |
| 72 | return 0; /* should always let other notifiers run */ |
| 73 | } |
| 74 | |
| 75 | static void write_threshold_register_notifier(BlockDriverState *bs) |
| 76 | { |
| 77 | bs->write_threshold_notifier.notify = before_write_notify; |
Paolo Bonzini | 818bbc8 | 2016-10-04 10:49:43 +0200 | [diff] [blame] | 78 | bdrv_add_before_write_notifier(bs, &bs->write_threshold_notifier); |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void write_threshold_update(BlockDriverState *bs, |
| 82 | int64_t threshold_bytes) |
| 83 | { |
| 84 | bs->write_threshold_offset = threshold_bytes; |
| 85 | } |
| 86 | |
| 87 | void bdrv_write_threshold_set(BlockDriverState *bs, uint64_t threshold_bytes) |
| 88 | { |
| 89 | if (bdrv_write_threshold_is_set(bs)) { |
| 90 | if (threshold_bytes > 0) { |
| 91 | write_threshold_update(bs, threshold_bytes); |
| 92 | } else { |
| 93 | write_threshold_disable(bs); |
| 94 | } |
| 95 | } else { |
| 96 | if (threshold_bytes > 0) { |
| 97 | /* avoid multiple registration */ |
| 98 | write_threshold_register_notifier(bs); |
| 99 | write_threshold_update(bs, threshold_bytes); |
| 100 | } |
| 101 | /* discard bogus disable request */ |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void qmp_block_set_write_threshold(const char *node_name, |
| 106 | uint64_t threshold_bytes, |
| 107 | Error **errp) |
| 108 | { |
| 109 | BlockDriverState *bs; |
| 110 | AioContext *aio_context; |
| 111 | |
| 112 | bs = bdrv_find_node(node_name); |
| 113 | if (!bs) { |
Markus Armbruster | 6ec46ad | 2015-03-13 18:51:38 +0100 | [diff] [blame] | 114 | error_setg(errp, "Device '%s' not found", node_name); |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
| 118 | aio_context = bdrv_get_aio_context(bs); |
| 119 | aio_context_acquire(aio_context); |
| 120 | |
| 121 | bdrv_write_threshold_set(bs, threshold_bytes); |
| 122 | |
| 123 | aio_context_release(aio_context); |
| 124 | } |