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" |
Markus Armbruster | e2c1c34 | 2022-12-21 14:35:49 +0100 | [diff] [blame] | 14 | #include "block/block-io.h" |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 15 | #include "block/block_int.h" |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 16 | #include "block/write-threshold.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 17 | #include "qapi/error.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 18 | #include "qapi/qapi-commands-block-core.h" |
| 19 | #include "qapi/qapi-events-block-core.h" |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 20 | |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 21 | uint64_t bdrv_write_threshold_get(const BlockDriverState *bs) |
| 22 | { |
| 23 | return bs->write_threshold_offset; |
| 24 | } |
| 25 | |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 26 | void bdrv_write_threshold_set(BlockDriverState *bs, uint64_t threshold_bytes) |
| 27 | { |
Vladimir Sementsov-Ogievskiy | 9478330 | 2021-05-06 12:06:14 +0300 | [diff] [blame] | 28 | bs->write_threshold_offset = threshold_bytes; |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void qmp_block_set_write_threshold(const char *node_name, |
| 32 | uint64_t threshold_bytes, |
| 33 | Error **errp) |
| 34 | { |
| 35 | BlockDriverState *bs; |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 36 | |
| 37 | bs = bdrv_find_node(node_name); |
| 38 | if (!bs) { |
Markus Armbruster | 6ec46ad | 2015-03-13 18:51:38 +0100 | [diff] [blame] | 39 | error_setg(errp, "Device '%s' not found", node_name); |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 43 | bdrv_write_threshold_set(bs, threshold_bytes); |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 44 | } |
Vladimir Sementsov-Ogievskiy | 9478330 | 2021-05-06 12:06:14 +0300 | [diff] [blame] | 45 | |
| 46 | void bdrv_write_threshold_check_write(BlockDriverState *bs, int64_t offset, |
| 47 | int64_t bytes) |
| 48 | { |
| 49 | int64_t end = offset + bytes; |
| 50 | uint64_t wtr = bs->write_threshold_offset; |
| 51 | |
| 52 | if (wtr > 0 && end > wtr) { |
| 53 | qapi_event_send_block_write_threshold(bs->node_name, end - wtr, wtr); |
| 54 | |
| 55 | /* autodisable to avoid flooding the monitor */ |
| 56 | bdrv_write_threshold_set(bs, 0); |
| 57 | } |
| 58 | } |