blob: 56fe88de811344046eb87b43784c5ebf8042aebd [file] [log] [blame]
Francesco Romanie2462112015-01-12 14:11:13 +01001/*
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 Maydell80c71a22016-01-18 18:01:42 +000013#include "qemu/osdep.h"
Markus Armbrustere2c1c342022-12-21 14:35:49 +010014#include "block/block-io.h"
Francesco Romanie2462112015-01-12 14:11:13 +010015#include "block/block_int.h"
Francesco Romanie2462112015-01-12 14:11:13 +010016#include "block/write-threshold.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010017#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010018#include "qapi/qapi-commands-block-core.h"
19#include "qapi/qapi-events-block-core.h"
Francesco Romanie2462112015-01-12 14:11:13 +010020
Francesco Romanie2462112015-01-12 14:11:13 +010021uint64_t bdrv_write_threshold_get(const BlockDriverState *bs)
22{
23 return bs->write_threshold_offset;
24}
25
Francesco Romanie2462112015-01-12 14:11:13 +010026void bdrv_write_threshold_set(BlockDriverState *bs, uint64_t threshold_bytes)
27{
Vladimir Sementsov-Ogievskiy94783302021-05-06 12:06:14 +030028 bs->write_threshold_offset = threshold_bytes;
Francesco Romanie2462112015-01-12 14:11:13 +010029}
30
31void qmp_block_set_write_threshold(const char *node_name,
32 uint64_t threshold_bytes,
33 Error **errp)
34{
35 BlockDriverState *bs;
Francesco Romanie2462112015-01-12 14:11:13 +010036
37 bs = bdrv_find_node(node_name);
38 if (!bs) {
Markus Armbruster6ec46ad2015-03-13 18:51:38 +010039 error_setg(errp, "Device '%s' not found", node_name);
Francesco Romanie2462112015-01-12 14:11:13 +010040 return;
41 }
42
Francesco Romanie2462112015-01-12 14:11:13 +010043 bdrv_write_threshold_set(bs, threshold_bytes);
Francesco Romanie2462112015-01-12 14:11:13 +010044}
Vladimir Sementsov-Ogievskiy94783302021-05-06 12:06:14 +030045
46void 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}