xref: /openbmc/qemu/block/write-threshold.c (revision b91a0fa7)
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 
13 #include "qemu/osdep.h"
14 #include "block/block_int.h"
15 #include "block/write-threshold.h"
16 #include "qapi/error.h"
17 #include "qapi/qapi-commands-block-core.h"
18 #include "qapi/qapi-events-block-core.h"
19 
20 uint64_t bdrv_write_threshold_get(const BlockDriverState *bs)
21 {
22     return bs->write_threshold_offset;
23 }
24 
25 void bdrv_write_threshold_set(BlockDriverState *bs, uint64_t threshold_bytes)
26 {
27     bs->write_threshold_offset = threshold_bytes;
28 }
29 
30 void qmp_block_set_write_threshold(const char *node_name,
31                                    uint64_t threshold_bytes,
32                                    Error **errp)
33 {
34     BlockDriverState *bs;
35     AioContext *aio_context;
36 
37     bs = bdrv_find_node(node_name);
38     if (!bs) {
39         error_setg(errp, "Device '%s' not found", node_name);
40         return;
41     }
42 
43     aio_context = bdrv_get_aio_context(bs);
44     aio_context_acquire(aio_context);
45 
46     bdrv_write_threshold_set(bs, threshold_bytes);
47 
48     aio_context_release(aio_context);
49 }
50 
51 void bdrv_write_threshold_check_write(BlockDriverState *bs, int64_t offset,
52                                       int64_t bytes)
53 {
54     int64_t end = offset + bytes;
55     uint64_t wtr = bs->write_threshold_offset;
56 
57     if (wtr > 0 && end > wtr) {
58         qapi_event_send_block_write_threshold(bs->node_name, end - wtr, wtr);
59 
60         /* autodisable to avoid flooding the monitor */
61         bdrv_write_threshold_set(bs, 0);
62     }
63 }
64